summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/WebConfigSet.py
blob: 1c73761f851b296433f43582c458cb5e90bdc742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
import traceback
from ..common.ConfigGroup import ConfigGroup
from ..common.Configuration import Configuration
from ..common.XMLProvider import XMLProvider
from ..ui.event.EventListenerList import EventListenerList
from ..ui.event.ListDataEvent import ListDataEvent
from ..ui.event.ListDataListener import ListDataListener

class WebConfigSet(ConfigGroup):
    '''
    After reading the configuration set items,
    the ConfigSet checks this field.
    If it is true, it will remove any nulls from
    the vector.
    subclasses can change this field in the constructor
    to avoid this "deletion" of nulls.
    '''

    def __init__(self, childType):
        self.childClass = childType
        self.childrenMap = {}
        self.childrenList = []
        self.noNulls = True
        self.listenerList = None

    def add(self, name, o):
        oldO = None
        if (name in self.childrenMap):
            oldO = self.childrenMap[name]
        self.childrenMap[name] = o
        try:
            i = int(name)
            self.childrenList.insert(i, o)
            self.fireListDataListenerIntervalAdded(i, i);
        except ValueError:
            if (hasattr(o, "cp_Index")):
                i = o.cp_Index
                oldSize = self.getSize()
                while (self.getSize() <= i):
                    self.childrenList.append(None)
                self.childrenList[i] = o
                if oldSize > i:
                    oldSize = i
                self.fireListDataListenerIntervalAdded(oldSize, i);
            else:
                if (oldO is not None):
                    i = self.childrenList.index(oldO)
                    self.childrenList[i] = o
                else:
                    self.childrenList.append(o)
                    self.fireListDataListenerIntervalAdded(self.getSize() - 1, self.getSize() - 1);


    def writeConfiguration(self, configView, param):
        names = self.childrenMap.keys()
        #first I remove all the children from the configuration.
        children = configView.ElementNames
        if children:
            for i in children:
                try:
                    Configuration.removeNode(configView, i)
                except Exception:
                    traceback.print_exc()

        # and add them new.
        for i in names:
            try:
                child = self.getElement(i)
                childView = Configuration.addConfigNode(configView, i)
                child.writeConfiguration(childView, param)
            except Exception:
                traceback.print_exc()

    def readConfiguration(self, configurationView, param):
        names = configurationView.ElementNames
        if names:
            for i in names:
                try:
                    child = self.childClass()
                    child.setRoot(self.root)
                    child.readConfiguration(
                        configurationView.getByName(i), param)
                    self.add(i, child)
                except Exception:
                    traceback.print_exc()
        #remove any nulls from the list
        if self.noNulls:
            i = 0
            while i < len(self.childrenList):
                if self.childrenList[i] is None:
                    del self.childrenList[i]
                    i -= 1
                i += 1

    def remove1(self, obj):
        key = self.getKey(obj)
        del self.childrenMap[key]
        i = self.childrenList.index(obj)
        self.childrenList.remove(obj)
        self.fireListDataListenerIntervalRemoved(i, i)

    def remove(self, i):
        o = self.getElementAt(i)
        self.remove1(o)

    def clear(self):
        self.childrenMap.clear()
        del self.childrenList[:]

    def update(i):
        self.fireListDataListenerContentsChanged(i, i)

    def createDOM(self, parent):
        items = self.childrenList
        i = 0
        while i < len(items):
            item = items[i]
            if hasattr(item, "createDOM"):
                item.createDOM(parent)
            i += 1
        return parent

    def getKey(self, _object):
        for k,v in self.childrenMap.items():
            if v is _object:
                return k
        return None

    def getElementAt(self, i):
        return self.childrenList[i]

    def getElement(self, o):
        return self.childrenMap[o]

    def getSize(self):
        return len(self.childrenList)

    def getIndexOf(self, item):
        return self.childrenList.index(item)

    '''
    Set members might include a property
    which orders them.
    This method reindexes the given member to be
    the index number 0
    Do not forget to call commit() after calling this method.
    @param confView
    @param memebrName
    '''

    def reindexSet(self, confView, memberName, indexPropertyName):
        '''
        First I read all memebrs of the set,
        except the one that should be number 0
        to a vector, ordered by there index property
        '''
        names = Configuration.getChildrenNames(confView)
        v = []
        member = None
        index = 0
        i = 0
        while i < len(names):
            if not names[i] == memberName:
                member = Configuration.getNode(names[i], confView)
                index = Configuration.getInt(indexPropertyName, member)
                while index >= v.size():
                    v.append(None)
                v[index] = member
            '''
            Now I reindex them
            '''
            i += 1
        index = 1
        i = 0
        while i < len(v):
            member = v[i]
            if member != None:
                Configuration.set((index + 1), indexPropertyName, member)
            i += 1

    def sort(self, comparator):
        self.childrenList.sort(comparator)


    # Registers ListDataListener to receive events.
    # @param listener The listener to register.
    def addListDataListener(self, listener):
        if (self.listenerList is None):
            self.listenerList = EventListenerList()
        self.listenerList.add(listener)

    # Removes ListDataListener from the list of listeners.
    #  @param listener The listener to remove.
    def removeListDataListener(self, listener):
        self.listenerList.remove(listener)

    # Notifies all registered listeners about the event.
    #
    # @param event The event to be fired
    def fireListDataListenerIntervalAdded(self, i0, i1):
        event = ListDataEvent(self, ListDataEvent.INTERVAL_ADDED, i0, i1)
        if (self.listenerList is None):
            return
        for listener in self.listenerList.getListenerList():
            if isinstance(listener, ListDataListener):
                listener.intervalAdded(event)

    # Notifies all registered listeners about the event.
    #
    # @param event The event to be fired
    def fireListDataListenerIntervalRemoved(self, i0, i1):
        event = ListDataEvent(self, ListDataEvent.INTERVAL_REMOVED, i0, i1)
        if (self.listenerList is None):
            return
        for listener in self.listenerList.getListenerList():
            if isinstance(listener, ListDataListener):
                listener.intervalRemoved(event)

    # Notifies all registered listeners about the event.
    #
    # @param event The event to be fired
    def fireListDataListenerContentsChanged(self, i0, i1):
        event = ListDataEvent(self, ListDataEvent.CONTENTS_CHANGED, i0, i1)
        if (self.listenerList is None):
            return
        for listener in self.listenerList.getListenerList():
            if isinstance(listener, ListDataListener):
                listener.contentsChanged(event)