1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.commons.jelly.tags.jface.preference; |
17 |
|
|
18 |
|
import java.lang.reflect.Constructor; |
19 |
|
import java.lang.reflect.InvocationTargetException; |
20 |
|
import java.util.Map; |
21 |
|
|
22 |
|
import org.apache.commons.jelly.JellyTagException; |
23 |
|
import org.apache.commons.jelly.MissingAttributeException; |
24 |
|
import org.apache.commons.jelly.XMLOutput; |
25 |
|
import org.apache.commons.jelly.tags.core.UseBeanTag; |
26 |
|
import org.eclipse.jface.preference.FieldEditor; |
27 |
|
import org.eclipse.swt.widgets.Composite; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
0 |
public class FieldEditorTag extends UseBeanTag { |
35 |
|
|
36 |
|
public FieldEditorTag(Class arg0) { |
37 |
0 |
super(arg0); |
38 |
0 |
} |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
44 |
0 |
PreferencePageTag tag = (PreferencePageTag) findAncestorWithClass(PreferencePageTag.class); |
45 |
0 |
if (tag == null) { |
46 |
0 |
throw new JellyTagException("This tag must be nested inside a <preferencePage>"); |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
0 |
PreferencePageTag.PreferencePageImpl page = tag.getPreferencePageImpl(); |
51 |
0 |
getAttributes().put("parentComposite", page.getFieldEditorParent()); |
52 |
|
|
53 |
|
|
54 |
0 |
Object fieldEditor = newInstance(getDefaultClass(), getAttributes(), output); |
55 |
0 |
if (fieldEditor instanceof FieldEditor) { |
56 |
0 |
page.addField((FieldEditor) fieldEditor); |
57 |
|
} |
58 |
|
|
59 |
0 |
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
protected Object newInstance(Class theClass, Map attributes, XMLOutput output) |
65 |
|
throws JellyTagException { |
66 |
|
|
67 |
0 |
if (theClass == null) { |
68 |
0 |
throw new JellyTagException("No Class available to create the FieldEditor"); |
69 |
|
} |
70 |
|
|
71 |
0 |
String name = (String) attributes.get("name"); |
72 |
0 |
if (name == null) { |
73 |
0 |
throw new MissingAttributeException("name"); |
74 |
|
} |
75 |
|
|
76 |
0 |
String labelText = (String) attributes.get("labelText"); |
77 |
0 |
if (labelText == null) { |
78 |
0 |
throw new MissingAttributeException("labelText"); |
79 |
|
} |
80 |
|
|
81 |
0 |
Composite parentComposite = (Composite) attributes.get("parentComposite"); |
82 |
0 |
if (parentComposite == null) { |
83 |
0 |
throw new MissingAttributeException("parentComposite"); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
try { |
88 |
0 |
Class[] types = { String.class, String.class, Composite.class }; |
89 |
0 |
Constructor constructor = theClass.getConstructor(types); |
90 |
0 |
if (constructor != null) { |
91 |
0 |
Object[] arguments = { name, labelText, parentComposite }; |
92 |
0 |
return constructor.newInstance(arguments); |
93 |
|
} |
94 |
0 |
return theClass.newInstance(); |
95 |
|
|
96 |
|
} catch (NoSuchMethodException e) { |
97 |
0 |
throw new JellyTagException(e); |
98 |
|
} catch (InstantiationException e) { |
99 |
0 |
throw new JellyTagException(e); |
100 |
|
} catch (IllegalAccessException e) { |
101 |
0 |
throw new JellyTagException(e); |
102 |
|
} catch (InvocationTargetException e) { |
103 |
0 |
throw new JellyTagException(e); |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
|
} |