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.io.IOException;
19  
20  import org.apache.commons.jelly.JellyTagException;
21  import org.apache.commons.jelly.MissingAttributeException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.eclipse.jface.preference.FieldEditor;
27  import org.eclipse.jface.preference.FieldEditorPreferencePage;
28  import org.eclipse.jface.preference.IPreferenceStore;
29  import org.eclipse.jface.preference.PreferenceDialog;
30  import org.eclipse.jface.preference.PreferenceNode;
31  import org.eclipse.jface.preference.PreferenceStore;
32  import org.eclipse.swt.widgets.Composite;
33  
34  /***
35   * This Tag creates a JFace PreferencePage
36   *
37   * Provides a concrete preference store implementation based on an internal java.util.Properties object
38   *
39   * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
40   */
41  public class PreferencePageTag extends TagSupport {
42  
43      /***
44       * Implementation of a FieldEditorPreferencePage
45       * method createFieldEditors is called on Dialog.open()
46       */
47      public class PreferencePageImpl extends FieldEditorPreferencePage {
48          private PreferenceStore preferenceStore;
49  
50          public PreferencePageImpl(String title) {
51              super(title, FieldEditorPreferencePage.GRID);
52              try {
53                  preferenceStore = new PreferenceStore(filename);
54                  preferenceStore.load();
55                  setPreferenceStore(preferenceStore);
56              } catch (IOException e) {
57                  log.error(e);
58              }
59          }
60  
61          public void addField(FieldEditor editor) {
62              super.addField(editor);
63          }
64  
65          protected void createFieldEditors() {
66              try {
67                  invokeBody(output);
68              } catch (JellyTagException e) {
69                  log.error(e);
70              }
71          }
72  
73          public Composite getFieldEditorParent() {
74              return super.getFieldEditorParent();
75          }
76  
77          public IPreferenceStore getPreferenceStore() {
78              return preferenceStore;
79          }
80      }
81  
82      /*** The Log to which logging calls will be made. */
83      private static final Log log = LogFactory.getLog(PreferencePageTag.class);
84  
85      /*** Filename of the store */
86      private String filename;
87  
88      /*** Jelly XMLOutput */
89      private XMLOutput output;
90  
91      /*** Current PreferencePageImpl */
92      private PreferencePageImpl page;
93  
94      /*** Title of both PreferenceNode and PreferencePage */
95      private String title;
96  
97      /*
98       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
99       */
100     public void doTag(XMLOutput output) throws JellyTagException {
101         // check location
102         PreferenceDialogTag dialogTag =
103             (PreferenceDialogTag) findAncestorWithClass(PreferenceDialogTag.class);
104         if (dialogTag == null) {
105             throw new JellyTagException("This tag must be nested within a <preferenceDialog>");
106         }
107 
108         // check for missing attributes
109         if (filename == null) {
110             throw new MissingAttributeException("filename");
111         }
112         if (title == null) {
113             throw new MissingAttributeException("title");
114         }
115 
116         // build new PreferenceNode with same title as the PreferencePage
117         PreferenceDialog dialog = dialogTag.getPreferenceDialog();
118         PreferenceNode node = new PreferenceNode(title);
119 
120         // build new PreferencePage
121         page = new PreferencePageImpl(title);
122 
123         // add node to PreferenceManager
124         node.setPage(page);
125         dialog.getPreferenceManager().addToRoot(node);
126 
127         // used by PreferencePageImpl
128         this.output = output;
129     }
130 
131     /***
132      * Get the PreferencePageImpl
133      * @return PreferencePageImpl
134      */
135     public PreferencePageImpl getPreferencePageImpl() {
136         return page;
137     }
138 
139     /***
140      * Sets the filename.
141      * @param filename The filename to set
142      */
143     public void setFilename(String filename) {
144         this.filename = filename;
145     }
146 
147     /***
148      * Sets the title.
149      * @param title The title to set
150      */
151     public void setTitle(String title) {
152         this.title = title;
153     }
154 
155 }