1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.introspection;
21
22 import java.awt.Color;
23 import java.awt.Font;
24
25 import javax.swing.DefaultCellEditor;
26 import javax.swing.DefaultComboBoxModel;
27 import javax.swing.JComboBox;
28 import javax.swing.JComponent;
29 import javax.swing.JTextField;
30 import javax.swing.border.Border;
31 import javax.swing.border.EmptyBorder;
32 import javax.swing.border.LineBorder;
33 import javax.swing.table.DefaultTableCellRenderer;
34 import javax.swing.table.TableCellEditor;
35 import javax.swing.table.TableCellRenderer;
36
37 import com.cosylab.gui.components.util.ColorHelper;
38 import com.cosylab.gui.property.editors.BooleanEditor;
39 import com.cosylab.gui.property.editors.ClassEditor;
40 import com.cosylab.gui.property.editors.ColorEditor;
41 import com.cosylab.gui.property.editors.DoubleEditor;
42 import com.cosylab.gui.property.editors.FloatEditor;
43 import com.cosylab.gui.property.editors.FontEditor;
44 import com.cosylab.gui.property.editors.IntegerEditor;
45 import com.cosylab.gui.property.editors.LongEditor;
46 import com.cosylab.gui.property.editors.NumberEditor;
47 import com.cosylab.gui.property.editors.PropertyEditor;
48 import com.cosylab.gui.property.editors.ShortEditor;
49 import com.cosylab.gui.property.editors.StringEditor;
50 import com.cosylab.introspection.ClassIntrospector;
51 import com.cosylab.introspection.DataFormatter;
52
53
54
55
56
57 public class EditorsHelper {
58 public static final Border EMPTY_BORDER=new EmptyBorder(1,1,1,1);
59 public static final Border FOCUS_BORDER=new LineBorder(ColorHelper.getFocus(),1);
60
61
62
63
64
65 public static PropertyEditor getPropertyEditorForClass(Class theClass){
66 if (theClass.equals(String.class)) {
67 return new StringEditor();
68 }
69 if ((theClass.equals(Boolean.class)) || (theClass.equals(Boolean.TYPE))) {
70 BooleanEditor be=new BooleanEditor();
71 be.setFocusPainted(false);
72 be.setOpaque(true);
73 be.setBorderPainted(true);
74 return be;
75 }
76 NumberEditor nf=null;
77 if ((theClass.equals(Double.class)) || (theClass.equals(Double.TYPE))) {
78 nf= new DoubleEditor();
79 }
80 if ((theClass.equals(Float.class)) || (theClass.equals(Float.TYPE))) {
81 nf= new FloatEditor();
82 }
83 if ((theClass.equals(Short.class)) || (theClass.equals(Short.TYPE))) {
84 nf= new ShortEditor();
85 }
86 if ((theClass.equals(Integer.class)) || (theClass.equals(Integer.TYPE))) {
87 nf= new IntegerEditor();
88 }
89 if ((theClass.equals(Long.class)) || (theClass.equals(Long.TYPE))) {
90 nf= new LongEditor();
91 }
92 if (nf!=null) {
93 nf.setBorder(null);
94 return nf;
95 }
96 if (theClass.equals(Class.class)) {
97 ClassEditor ce=new ClassEditor();
98 ce.setApplyMode(ClassEditor.APPLY_NEVER);
99 return ce;
100 }
101 if (theClass.equals(Color.class)) {
102 return new ColorEditor();
103 }
104 if (theClass.equals(Font.class)) {
105 return new FontEditor();
106 }
107 return null;
108 }
109
110
111
112
113
114 public static TableCellEditor getTableCellEditorForClass(Class theClass){
115 return getTableCellEditorForClass(theClass, 0);
116 }
117
118
119
120
121 public static TableCellEditor getTableCellEditorForClass(Class theClass, int clickCountToStart){
122 TableCellEditor ret=null;
123 if (theClass==null) {
124 DefaultCellEditor dce= new DefaultCellEditor(new JTextField());
125 dce.setClickCountToStart(clickCountToStart);
126 ret=dce;
127 }
128 if (ClassIntrospector.isEnum(theClass)) {
129 JComboBox b=new JComboBox();
130 b.setModel(new DefaultComboBoxModel(DataFormatter.toStringArray(ClassIntrospector.getEnumFields(theClass), false, false)));
131 DefaultCellEditor dce= new DefaultCellEditor(b);
132 dce.setClickCountToStart(clickCountToStart);
133 ret=dce;
134 } else {
135 PropertyEditor editor=getPropertyEditorForClass(theClass);
136 if (editor!=null) {
137 ret=new PropertyCellEditorRenderer(editor,clickCountToStart);
138 if (editor instanceof JComponent) {
139 ((JComponent)editor).setBorder(FOCUS_BORDER);
140 }
141 }
142 }
143 if (ret instanceof JComponent) {
144 ((JComponent)ret).setBorder(FOCUS_BORDER);
145 }
146 return ret;
147
148
149 }
150
151
152
153
154 public static TableCellRenderer getTableCellRendererForClass(Class theClass){
155 TableCellRenderer ret=null;
156
157
158
159
160
161
162 PropertyEditor editor = getPropertyEditorForClass(theClass);
163 if (editor!=null) {
164 ret=new PropertyCellEditorRenderer(editor);
165 if (editor instanceof JComponent) {
166 ((JComponent)editor).setBorder(EMPTY_BORDER);
167 }
168 }
169 else {
170 ret=new DefaultTableCellRenderer();
171 }
172 if (ret instanceof JComponent) {
173 ((JComponent)ret).setBorder(EMPTY_BORDER);
174 }
175 return ret;
176
177
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191 public static Object convert(Object value, Class type) {
192 if (type.isAssignableFrom(value.getClass())) {
193 return value;
194 }
195 if (value instanceof Number) {
196 if (type==double.class || type==Double.class) {
197 return new Double(((Number)value).doubleValue());
198 }
199 if (type==float.class || type==Float.class) {
200 return new Float(((Number)value).floatValue());
201 }
202 if (type==int.class || type==Integer.class) {
203 return new Integer(((Number)value).intValue());
204 }
205 if (type==long.class || type==Long.class) {
206 return new Long(((Number)value).longValue());
207 }
208 if (type==short.class || type==Short.class) {
209 return new Short(((Number)value).shortValue());
210 }
211 }
212 if (value instanceof Boolean) {
213 if (type==boolean.class || type==Boolean.class) {
214 return value;
215 }
216 }
217 if (value instanceof String) {
218 if (type==String.class) {
219 return value;
220 }
221 if (type==double.class || type==Double.class) {
222 return new Double((String)value);
223 }
224 if (type==float.class || type==Float.class) {
225 return new Float((String)value);
226 }
227 if (type==int.class || type==Integer.class) {
228 return new Integer((String)value);
229 }
230 if (type==short.class || type==Short.class) {
231 return new Short((String)value);
232 }
233 if (type==long.class || type==Long.class) {
234 return new Long((String)value);
235 }
236 if (type==boolean.class || type==Boolean.class) {
237 return new Boolean((String)value);
238 }
239 if (type==Number.class) {
240 return new Double((String)value);
241 }
242 if (type.isEnum()) {
243 Object[] enums = type.getEnumConstants();
244 for (Object obj : enums) {
245 if (((Enum)obj).name().equalsIgnoreCase((String)value)) {
246 return obj;
247 }
248 }
249 }
250 }
251
252 throw new UnsupportedOperationException("Unable to convert Object of type '" + value.getClass() + "' to specified type '" + type +"'.");
253 }
254
255 }