summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/ui/event/DataAwareFields.py
blob: b23cce2eef013881e77399ed0b2d99535ce6ca80 (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
import traceback
from DataAware import *
import uno
'''
This class is a factory for Value objects for different types of
memebers.
Other than some Value implementations classes this class contains static
type conversion methods and factory methods.

@see com.sun.star.wizards.ui.event.DataAware.Value
'''

class DataAwareFields(object):
    TRUE = "true"
    FALSE = "false"
    '''
    returns a Value Object which sets and gets values
    and converting them to other types, according to the "value" argument.

    @param owner
    @param fieldname
    @param value
    @return
    @throws NoSuchFieldException
    '''

    @classmethod
    def getFieldValueFor(self, owner, fieldname, value):
        try:
            f = getattr(owner, fieldname)
            if isinstance(f,bool):
                return self.__BooleanFieldValue(fieldname, value)
            elif isinstance(f,str):
                return self.__ConvertedStringValue(fieldname, value)
            elif isinstance(f,int):
                return self.__IntFieldValue(fieldname, value)
            else:
                return SimpleFieldValue(f)

        except AttributeError, ex:
            traceback.print_exc()
            return None

    '''__ConvertedStringValue
    an abstract implementation of DataAware.Value to access
    object memebers (fields) usign reflection.
    '''
    class __FieldValue(DataAware.Value):
        __metaclass__ = ABCMeta

        def __init__(self, field_):
            self.field = field_

        def isAssignable(self, type_):
            return self.field.getDeclaringClass().isAssignableFrom(type_)

    class __BooleanFieldValue(__FieldValue):

        def __init__(self, f, convertTo_):
            super(type(self),self).__init__(f)
            self.convertTo = convertTo_

        def get(self, target):
            try:
                b = getattr(target, self.field)

                if isinstance(self.convertTo,bool):
                    if b:
                        return True
                    else:
                        return False
                elif isinstance(self.convertTo,int):
                    return int(b)
                elif isinstance(self.convertTo,str):
                    return str(b)
                elif self.convertTo.type == uno.Any("short",0).type:
                    return uno.Any("short",b)
                else:
                    raise AttributeError("Cannot convert boolean value to given type (" + str(type(self.convertTo)) + ").");

            except Exception, ex:
                traceback.print_exc()
                return None

        def set(self, value, target):
            try:
                self.field.setBoolean(target, toBoolean(value))
            except Exception, ex:
                traceback.print_exc()

    class __IntFieldValue(__FieldValue):

        def __init__(self, f, convertTo_):
            super(type(self),self).__init__(f)
            self.convertTo = convertTo_

        def get(self, target):
            try:
                i = getattr(target, self.field)
                if isinstance(self.convertTo,bool):
                    if i:
                        return True
                    else:
                        return False
                elif isinstance(self.convertTo, int):
                    return int(i)
                elif isinstance(self.convertTo,str):
                    return str(i)
                elif self.convertTo.type == uno.Any("short",0).type:
                    return uno.Any("[]short",(i,))
                else:
                    raise AttributeError("Cannot convert int value to given type (" + str(type(self.convertTo)) + ").");

            except Exception, ex:
                traceback.print_exc()
                #traceback.print_exc__ConvertedStringValue()
                return None

        def set(self, value, target):
            try:
                self.field.setInt(target, toDouble(value))
            except Exception, ex:
                traceback.print_exc()

    class __ConvertedStringValue(__FieldValue):

        def __init__(self, f, convertTo_):
            super(type(self),self).__init__(f)
            self.convertTo = convertTo_

        def get(self, target):
            try:
                s = getattr(target, self.field)
                if isinstance(self.convertTo,bool):
                    if s != None and not s == "" and s == "true":
                        return True
                    else:
                        return False
                elif isinstance(self.convertTo,str):
                    if s == None or s == "":
                        pass
                    else:
                        return s
                else:
                    raise AttributeError("Cannot convert int value to given type (" \
                        + str(type(self.convertTo)) + ")." )

            except Exception, ex:
                traceback.print_exc()
                return None

        def set(self, value, target):
            try:
                string_aux = ""
                #if value is not None or not isinstance(value,uno.Any()):
                #    string_aux = str(value)

                self.field.set(target, string_aux)
            except Exception, ex:
                traceback.print_exc()