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 * This Tag creates a JFace FieldEditor
31 *
32 * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
33 */
34 public class FieldEditorTag extends UseBeanTag {
35
36 public FieldEditorTag(Class arg0) {
37 super(arg0);
38 }
39
40
41
42
43 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
44 PreferencePageTag tag = (PreferencePageTag) findAncestorWithClass(PreferencePageTag.class);
45 if (tag == null) {
46 throw new JellyTagException("This tag must be nested inside a <preferencePage>");
47 }
48
49
50 PreferencePageTag.PreferencePageImpl page = tag.getPreferencePageImpl();
51 getAttributes().put("parentComposite", page.getFieldEditorParent());
52
53
54 Object fieldEditor = newInstance(getDefaultClass(), getAttributes(), output);
55 if (fieldEditor instanceof FieldEditor) {
56 page.addField((FieldEditor) fieldEditor);
57 }
58
59 }
60
61
62
63
64 protected Object newInstance(Class theClass, Map attributes, XMLOutput output)
65 throws JellyTagException {
66
67 if (theClass == null) {
68 throw new JellyTagException("No Class available to create the FieldEditor");
69 }
70
71 String name = (String) attributes.get("name");
72 if (name == null) {
73 throw new MissingAttributeException("name");
74 }
75
76 String labelText = (String) attributes.get("labelText");
77 if (labelText == null) {
78 throw new MissingAttributeException("labelText");
79 }
80
81 Composite parentComposite = (Composite) attributes.get("parentComposite");
82 if (parentComposite == null) {
83 throw new MissingAttributeException("parentComposite");
84 }
85
86
87 try {
88 Class[] types = { String.class, String.class, Composite.class };
89 Constructor constructor = theClass.getConstructor(types);
90 if (constructor != null) {
91 Object[] arguments = { name, labelText, parentComposite };
92 return constructor.newInstance(arguments);
93 }
94 return theClass.newInstance();
95
96 } catch (NoSuchMethodException e) {
97 throw new JellyTagException(e);
98 } catch (InstantiationException e) {
99 throw new JellyTagException(e);
100 } catch (IllegalAccessException e) {
101 throw new JellyTagException(e);
102 } catch (InvocationTargetException e) {
103 throw new JellyTagException(e);
104 }
105 }
106
107 }