summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/common/ConfigSet.py
blob: 7851fea4555625e1e679b01ac0d6e5f83ee4a807 (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
import traceback
from ConfigNode import *
from Configuration import Configuration

class ConfigSet(ConfigNode):
    '''
    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 = False

    def add(self, name, o):
        self.childrenMap[name] = o
        if isinstance(name, int):
            i = name
            self.childrenList.insert(i, o)
        else:
            try:
                i = o.cp_Index
                oldSize = self.getSize()
                if oldSize <= i:
                    newSize = i - oldSize
                    self.childrenList += [None] * newSize
                    self.noNulls = True
                else:
                    self.noNulls = False
                self.childrenList.insert(i, o);
                if oldSize > i:
                    oldSize = i
            except Exception:
                self.childrenList.append(o)

    def writeConfiguration(self, configView, param):
        names = self.childrenMap.keys()
        if isinstance(self.childClass, ConfigNode):
            #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 = configView.getByName(i)
                    child.writeConfiguration(childView, param)
                except Exception:
                    traceback.print_exc()
        else:
            raise AttributeError (
            "Unable to write primitive sets to configuration (not implemented)")

    def readConfiguration(self, configurationView, param):
        names = configurationView.ElementNames
        if isinstance(self.childClass, ConfigNode):
            if names:
                for i in names:
                    try:
                        child = type(self.childClass)()
                        child.readConfiguration(
                            configurationView.getByName(i), param)
                        self.add(i, child)
                    except Exception, ex:
                         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

        else:
            for i in names:
                try:
                    child = configurationView.getByName(i)
                    self.add(i, child)
                except Exception, ex:
                    traceback.print_exc()

    def remove(self, obj):
        key = getKey(obj)
        self.childrenMap.remove(key)
        i = self.childrenList.indexOf(obj)
        self.childrenList.remove(obj)
        fireListDataListenerIntervalRemoved(i, i)

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

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

    def createDOM(self, parent):
        items = items()
        i = 0
        while i < items.length:
            item = items[i]
            if item.instanceof.XMLProvider:
                item.createDOM(parent)

            i += 1
        return parent

    def getKey(self, object):
        i = self.childrenMap.entrySet().iterator()
        while i.hasNext():
            me = i.next()
            if me.getValue() == object:
                return me.getKey()

        return None

    def getKey(self, i):
        c = 0
        while i > -1:
            if getElementAt(c) != None:
                i -= 1

            c += 1
        if c == 0:
            return None
        else:
            return getKey(getElementAt(c - 1))

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

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

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

    def keys(self):
        return self.childrenMap.keySet()

    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 = Vector.Vector_unknown(names.length)
        member = None
        index = 0
        i = 0
        while i < names.length:
            if not names[i].equals(memberName):
                member = Configuration.getConfigurationNode(names[i], confView)
                index = Configuration.getInt(indexPropertyName, member)
                while index >= v.size():
                    v.add(None)
                v.setElementAt(member, index)
            '''
            Now I reindex them
            '''

            i += 1
        index = 1
        i = 0
        while i < v.size():
            member = v.get(i)
            if member != None:
                Configuration.set((index + 1), indexPropertyName, member)

            i += 1

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