View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
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          // get new instance of FieldEditor
50          PreferencePageTag.PreferencePageImpl page = tag.getPreferencePageImpl();
51          getAttributes().put("parentComposite", page.getFieldEditorParent());
52  
53          // add fieldEditor to PreferencePage
54          Object fieldEditor = newInstance(getDefaultClass(), getAttributes(), output);
55          if (fieldEditor instanceof FieldEditor) {
56              page.addField((FieldEditor) fieldEditor);
57          }
58  
59      }
60  
61      /*
62       * @see org.apache.commons.jelly.tags.core.UseBeanTag#newInstance(java.lang.Class, java.util.Map, org.apache.commons.jelly.XMLOutput)
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          // let's try to call a constructor
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 }