My favorites | Sign in
Project Logo
                
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
package org.javabuilders.swt;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.javabuilders.BuildException;
import org.javabuilders.Node;
import org.javabuilders.TypeDefinition;

/**
* SWT Builder utils
* @author Jacek Furmankiewicz
*
*/
public class SWTBuilderUtils {

/**
* Get the SWT.constant value from a string description
* @param style Style
* @return SWT constant
* @throws BuildException Thrown if unable to get value
*/
@SuppressWarnings("unchecked")
public static int getSWTStyle(Object data) throws BuildException {

int value = SWT.NONE;

List<String> styles;
if (data instanceof List) {
styles = (List<String>) data;
} else {
styles = new ArrayList<String>();
styles.add(String.valueOf(data));
}

for(String style : styles) {
int styleValue = SWT.NONE;
boolean found = false;

Field[] fields = SWT.class.getFields();

for(Field field : fields) {

int mod = field.getModifiers();
if (Modifier.isPublic(mod) && Modifier.isFinal(mod) &&
Modifier.isStatic(mod) && field.getType().equals(int.class)) {
String name = field.getName();
String camelCase = TypeDefinition.getShortEnumConstant(field.getName());

if (style.equals(name) || style.equals(camelCase)) { //value entered like the constant, e.g. EXIT_ON_CLOSE
try {
styleValue = field.getInt(null);
found = true;
break;
} catch (Exception e) {
throw new BuildException(e);
}
}
}
}

if (found) {
value = value | styleValue;
} else {
throw new BuildException("\"{0}\" does not correspond to a valid SWT constant",style);
}
}

return value;
}

/**
* Gets the SWT constant value from exact name, e.g. "SHIFT"
* @param constantName Exact constant name
* @return value
*/
public static int getSWTConstantFromExactName(String constantName)
throws BuildException {
int value = SWT.NONE;

try {
Field field = SWT.class.getField(constantName);
value = field.getInt(null);
} catch (Exception e) {
throw new BuildException("{0} is not a valid SWT int constant name",constantName);
}

return value;
}

/**
* Gets the first shell
* @param node Node
* @return
*/
public static Shell getShell(Node node) {
Shell shell = null;

if (node != null) {
Object main = node.getMainObject();
if (main instanceof Control) {
Control c = (Control) main;
shell = c.getShell();
} else {
//keep looking
return getShell(node.getParent());
}
}

return shell;
}

/**
* Splits a binding expresion pointing to a nested property into two separate components
* for easier parsisng, as a workaround for the limitations of JFace Databinding
* @param expression Expression
* @return 0 = path to object, 1 = property name on the object
*/
public static String[] getNestedBindingExpressionParts(String expression) {
String[] parts = new String[2];

int pos = expression.lastIndexOf(".");
if (pos > 0) {
parts[0] = expression.substring(0,pos);
parts[1] = expression.substring(pos + 1);
} else {
throw new BuildException("{0} is not a nested binding expression", expression);
}

return parts;
}


}
Show details Hide details

Change log

r784 by jacek99 on Dec 16, 2008   Diff
Swing: further fixes to Actions,
SWT: ability to pass a specific parent to
object for creation, further fixes
Go to: 
Project members, sign in to write a code review

Older revisions

r762 by jacek99 on Dec 01, 2008   Diff
Iniital import
All revisions of this file

File info

Size: 3562 bytes, 139 lines

File properties

svn:executable
*
Hosted by Google Code