KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > PreferenceNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.swt.graphics.Image;
19
20 /**
21  * A concrete implementation of a node in a preference dialog tree. This class
22  * also supports lazy creation of the node's preference page.
23  */

24 public class PreferenceNode implements IPreferenceNode {
25     /**
26      * Preference page, or <code>null</code> if not yet loaded.
27      */

28     private IPreferencePage page;
29
30     /**
31      * The list of subnodes (immediate children) of this node (element type:
32      * <code>IPreferenceNode</code>).
33      */

34     private List JavaDoc subNodes;
35
36     /**
37      * Name of a class that implements <code>IPreferencePage</code>, or
38      * <code>null</code> if none.
39      */

40     private String JavaDoc classname;
41
42     /**
43      * The id of this node.
44      */

45     private String JavaDoc id;
46
47     /**
48      * Text label for this node. Note that this field is only used prior to the
49      * creation of the preference page.
50      */

51     private String JavaDoc label;
52
53     /**
54      * Image descriptor for this node, or <code>null</code> if none.
55      */

56     private ImageDescriptor imageDescriptor;
57
58     /**
59      * Cached image, or <code>null</code> if none.
60      */

61     private Image image;
62
63     /**
64      * Creates a new preference node with the given id. The new node has no
65      * subnodes.
66      *
67      * @param id
68      * the node id
69      */

70     public PreferenceNode(String JavaDoc id) {
71         Assert.isNotNull(id);
72         this.id = id;
73     }
74
75     /**
76      * Creates a preference node with the given id, label, and image, and
77      * lazily-loaded preference page. The preference node assumes (sole)
78      * responsibility for disposing of the image; this will happen when the node
79      * is disposed.
80      *
81      * @param id
82      * the node id
83      * @param label
84      * the label used to display the node in the preference dialog's
85      * tree
86      * @param image
87      * the image displayed left of the label in the preference
88      * dialog's tree, or <code>null</code> if none
89      * @param className
90      * the class name of the preference page; this class must
91      * implement <code>IPreferencePage</code>
92      */

93     public PreferenceNode(String JavaDoc id, String JavaDoc label, ImageDescriptor image,
94             String JavaDoc className) {
95         this(id);
96         this.imageDescriptor = image;
97         Assert.isNotNull(label);
98         this.label = label;
99         this.classname = className;
100     }
101
102     /**
103      * Creates a preference node with the given id and preference page. The
104      * title of the preference page is used for the node label. The node will
105      * not have an image.
106      *
107      * @param id
108      * the node id
109      * @param preferencePage
110      * the preference page
111      */

112     public PreferenceNode(String JavaDoc id, IPreferencePage preferencePage) {
113         this(id);
114         Assert.isNotNull(preferencePage);
115         page = preferencePage;
116     }
117
118     /*
119      * (non-Javadoc) Method declared on IPreferenceNode.
120      */

121     public void add(IPreferenceNode node) {
122         if (subNodes == null) {
123             subNodes = new ArrayList JavaDoc();
124         }
125         subNodes.add(node);
126     }
127
128     /**
129      * Creates a new instance of the given class <code>className</code>.
130      *
131      * @param className
132      * @return new Object or <code>null</code> in case of failures.
133      */

134     private Object JavaDoc createObject(String JavaDoc className) {
135         Assert.isNotNull(className);
136         try {
137             Class JavaDoc cl = Class.forName(className);
138             if (cl != null) {
139                 return cl.newInstance();
140             }
141         } catch (ClassNotFoundException JavaDoc e) {
142             return null;
143         } catch (InstantiationException JavaDoc e) {
144             return null;
145         } catch (IllegalAccessException JavaDoc e) {
146             return null;
147         } catch (NoSuchMethodError JavaDoc e) {
148             return null;
149         }
150         return null;
151     }
152
153     /*
154      * (non-Javadoc) Method declared on IPreferenceNode.
155      */

156     public void createPage() {
157         page = (IPreferencePage) createObject(classname);
158         if (getLabelImage() != null) {
159             page.setImageDescriptor(imageDescriptor);
160         }
161         page.setTitle(label);
162     }
163
164     /**
165      * (non-Javadoc) Method declared on IPreferenceNode.
166      */

167     public void disposeResources() {
168         if (image != null) {
169             image.dispose();
170             image = null;
171         }
172         if (page != null) {
173             page.dispose();
174             page = null;
175         }
176     }
177
178     /*
179      * (non-Javadoc) Method declared on IContributionNode.
180      */

181     public IPreferenceNode findSubNode(String JavaDoc id) {
182         Assert.isNotNull(id);
183         Assert.isTrue(id.length() > 0);
184         if (subNodes == null) {
185             return null;
186         }
187         int size = subNodes.size();
188         for (int i = 0; i < size; i++) {
189             IPreferenceNode node = (IPreferenceNode) subNodes.get(i);
190             if (id.equals(node.getId())) {
191                 return node;
192             }
193         }
194         return null;
195     }
196
197     /*
198      * (non-Javadoc) Method declared on IPreferenceNode.
199      */

200     public String JavaDoc getId() {
201         return this.id;
202     }
203
204     /**
205      * Returns the image descriptor for this node.
206      *
207      * @return the image descriptor
208      */

209     protected ImageDescriptor getImageDescriptor() {
210         return imageDescriptor;
211     }
212
213     /*
214      * (non-Javadoc) Method declared on IPreferenceNode.
215      */

216     public Image getLabelImage() {
217         if (image == null && imageDescriptor != null) {
218             image = imageDescriptor.createImage();
219         }
220         return image;
221     }
222
223     /*
224      * (non-Javadoc) Method declared on IPreferenceNode.
225      */

226     public String JavaDoc getLabelText() {
227         if (page != null) {
228             return page.getTitle();
229         }
230         return label;
231     }
232
233     /*
234      * (non-Javadoc) Method declared on IPreferenceNode.
235      */

236     public IPreferencePage getPage() {
237         return page;
238     }
239
240     /*
241      * (non-Javadoc) Method declared on IPreferenceNode.
242      */

243     public IPreferenceNode[] getSubNodes() {
244         if (subNodes == null) {
245             return new IPreferenceNode[0];
246         }
247         return (IPreferenceNode[]) subNodes
248                 .toArray(new IPreferenceNode[subNodes.size()]);
249     }
250
251     /*
252      * (non-Javadoc) Method declared on IPreferenceNode.
253      */

254     public IPreferenceNode remove(String JavaDoc id) {
255         IPreferenceNode node = findSubNode(id);
256         if (node != null) {
257             remove(node);
258         }
259         return node;
260     }
261
262     /*
263      * (non-Javadoc) Method declared on IPreferenceNode.
264      */

265     public boolean remove(IPreferenceNode node) {
266         if (subNodes == null) {
267             return false;
268         }
269         return subNodes.remove(node);
270     }
271
272     /**
273      * Set the current page to be newPage.
274      *
275      * @param newPage
276      */

277     public void setPage(IPreferencePage newPage) {
278         page = newPage;
279     }
280 }
281
Popular Tags