34
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.preference.PreferenceNode;
|
|
14
|
|
15 import dwtx.jface.preference.IPreferenceNode;
|
|
16 import dwtx.jface.preference.IPreferencePage;
|
|
17
|
|
18
|
|
19 import tango.util.collection.ArraySeq;
|
|
20 // import java.util.List;
|
|
21
|
|
22 import dwt.graphics.Image;
|
|
23 import dwtx.core.runtime.Assert;
|
|
24 import dwtx.jface.resource.ImageDescriptor;
|
|
25
|
|
26 import dwt.dwthelper.utils;
|
|
27
|
|
28 /**
|
|
29 * A concrete implementation of a node in a preference dialog tree. This class
|
|
30 * also supports lazy creation of the node's preference page.
|
|
31 */
|
|
32 public class PreferenceNode : IPreferenceNode {
|
|
33 /**
|
|
34 * Preference page, or <code>null</code> if not yet loaded.
|
|
35 */
|
|
36 private IPreferencePage page;
|
|
37
|
|
38 /**
|
|
39 * The list of subnodes (immediate children) of this node (element type:
|
|
40 * <code>IPreferenceNode</code>).
|
|
41 */
|
|
42 private ArraySeq!(Object) subNodes;
|
|
43
|
|
44 /**
|
|
45 * Name of a class that implements <code>IPreferencePage</code>, or
|
|
46 * <code>null</code> if none.
|
|
47 */
|
|
48 private String classname;
|
|
49
|
|
50 /**
|
|
51 * The id of this node.
|
|
52 */
|
|
53 private String id;
|
|
54
|
|
55 /**
|
|
56 * Text label for this node. Note that this field is only used prior to the
|
|
57 * creation of the preference page.
|
|
58 */
|
|
59 private String label;
|
|
60
|
|
61 /**
|
|
62 * Image descriptor for this node, or <code>null</code> if none.
|
|
63 */
|
|
64 private ImageDescriptor imageDescriptor;
|
|
65
|
|
66 /**
|
|
67 * Cached image, or <code>null</code> if none.
|
|
68 */
|
|
69 private Image image;
|
|
70
|
|
71 /**
|
|
72 * Creates a new preference node with the given id. The new node has no
|
|
73 * subnodes.
|
|
74 *
|
|
75 * @param id
|
|
76 * the node id
|
|
77 */
|
|
78 public this(String id) {
|
|
79 Assert.isNotNull(id);
|
|
80 this.id = id;
|
|
81 }
|
|
82
|
|
83 /**
|
|
84 * Creates a preference node with the given id, label, and image, and
|
|
85 * lazily-loaded preference page. The preference node assumes (sole)
|
|
86 * responsibility for disposing of the image; this will happen when the node
|
|
87 * is disposed.
|
|
88 *
|
|
89 * @param id
|
|
90 * the node id
|
|
91 * @param label
|
|
92 * the label used to display the node in the preference dialog's
|
|
93 * tree
|
|
94 * @param image
|
|
95 * the image displayed left of the label in the preference
|
|
96 * dialog's tree, or <code>null</code> if none
|
|
97 * @param className
|
|
98 * the class name of the preference page; this class must
|
|
99 * implement <code>IPreferencePage</code>
|
|
100 */
|
|
101 public this(String id, String label, ImageDescriptor image,
|
|
102 String className) {
|
|
103 this(id);
|
|
104 this.imageDescriptor = image;
|
|
105 Assert.isNotNull(label);
|
|
106 this.label = label;
|
|
107 this.classname = className;
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Creates a preference node with the given id and preference page. The
|
|
112 * title of the preference page is used for the node label. The node will
|
|
113 * not have an image.
|
|
114 *
|
|
115 * @param id
|
|
116 * the node id
|
|
117 * @param preferencePage
|
|
118 * the preference page
|
|
119 */
|
|
120 public this(String id, IPreferencePage preferencePage) {
|
|
121 this(id);
|
|
122 Assert.isNotNull(cast(Object)preferencePage);
|
|
123 page = preferencePage;
|
|
124 }
|
|
125
|
|
126 /*
|
|
127 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
128 */
|
|
129 public void add(IPreferenceNode node) {
|
|
130 if (subNodes is null) {
|
|
131 subNodes = new ArraySeq!(Object);
|
|
132 }
|
|
133 subNodes.append(cast(Object)node);
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Creates a new instance of the given class <code>className</code>.
|
|
138 *
|
|
139 * @param className
|
|
140 * @return new Object or <code>null</code> in case of failures.
|
|
141 */
|
|
142 private Object createObject(String className) {
|
|
143 Assert.isNotNull(className);
|
|
144 // try {
|
|
145 ClassInfo cl = ClassInfo.find(className);
|
|
146 if (cl !is null) {
|
|
147 return cl.create();
|
|
148 }
|
|
149 // } catch (ClassNotFoundException e) {
|
|
150 // return null;
|
|
151 // } catch (InstantiationException e) {
|
|
152 // return null;
|
|
153 // } catch (IllegalAccessException e) {
|
|
154 // return null;
|
|
155 // } catch (NoSuchMethodError e) {
|
|
156 // return null;
|
|
157 // }
|
|
158 return null;
|
|
159 }
|
|
160
|
|
161 /*
|
|
162 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
163 */
|
|
164 public void createPage() {
|
|
165 page = cast(IPreferencePage) createObject(classname);
|
|
166 if (getLabelImage() !is null) {
|
|
167 page.setImageDescriptor(imageDescriptor);
|
|
168 }
|
|
169 page.setTitle(label);
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
174 */
|
|
175 public void disposeResources() {
|
|
176 if (image !is null) {
|
|
177 image.dispose();
|
|
178 image = null;
|
|
179 }
|
|
180 if (page !is null) {
|
|
181 page.dispose();
|
|
182 page = null;
|
|
183 }
|
|
184 }
|
|
185
|
|
186 /*
|
|
187 * (non-Javadoc) Method declared on IContributionNode.
|
|
188 */
|
|
189 public IPreferenceNode findSubNode(String id) {
|
|
190 Assert.isNotNull(id);
|
|
191 Assert.isTrue(id.length > 0);
|
|
192 if (subNodes is null) {
|
|
193 return null;
|
|
194 }
|
|
195 int size = subNodes.size();
|
|
196 for (int i = 0; i < size; i++) {
|
|
197 IPreferenceNode node = cast(IPreferenceNode) subNodes.get(i);
|
|
198 if (id.equals(node.getId())) {
|
|
199 return node;
|
|
200 }
|
|
201 }
|
|
202 return null;
|
|
203 }
|
|
204
|
|
205 /*
|
|
206 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
207 */
|
|
208 public String getId() {
|
|
209 return this.id;
|
|
210 }
|
|
211
|
|
212 /**
|
|
213 * Returns the image descriptor for this node.
|
|
214 *
|
|
215 * @return the image descriptor
|
|
216 */
|
|
217 protected ImageDescriptor getImageDescriptor() {
|
|
218 return imageDescriptor;
|
|
219 }
|
|
220
|
|
221 /*
|
|
222 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
223 */
|
|
224 public Image getLabelImage() {
|
|
225 if (image is null && imageDescriptor !is null) {
|
|
226 image = imageDescriptor.createImage();
|
|
227 }
|
|
228 return image;
|
|
229 }
|
|
230
|
|
231 /*
|
|
232 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
233 */
|
|
234 public String getLabelText() {
|
|
235 if (page !is null) {
|
|
236 return page.getTitle();
|
|
237 }
|
|
238 return label;
|
|
239 }
|
|
240
|
|
241 /*
|
|
242 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
243 */
|
|
244 public IPreferencePage getPage() {
|
|
245 return page;
|
|
246 }
|
|
247
|
|
248 /*
|
|
249 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
250 */
|
|
251 public IPreferenceNode[] getSubNodes() {
|
|
252 if (subNodes is null) {
|
|
253 return new IPreferenceNode[0];
|
|
254 }
|
|
255 return arraycast!(IPreferenceNode)( subNodes
|
|
256 .toArray());
|
|
257 }
|
|
258
|
|
259 /*
|
|
260 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
261 */
|
|
262 public IPreferenceNode remove(String id) {
|
|
263 IPreferenceNode node = findSubNode(id);
|
|
264 if (node !is null) {
|
|
265 remove(node);
|
|
266 }
|
|
267 return node;
|
|
268 }
|
|
269
|
|
270 /*
|
|
271 * (non-Javadoc) Method declared on IPreferenceNode.
|
|
272 */
|
|
273 public bool remove(IPreferenceNode node) {
|
|
274 if (subNodes is null) {
|
|
275 return false;
|
|
276 }
|
|
277 bool res = subNodes.contains(cast(Object)node);
|
|
278 subNodes.remove(cast(Object)node);
|
|
279 return res;
|
|
280 }
|
|
281
|
|
282 /**
|
|
283 * Set the current page to be newPage.
|
|
284 *
|
|
285 * @param newPage
|
|
286 */
|
|
287 public void setPage(IPreferencePage newPage) {
|
|
288 page = newPage;
|
|
289 }
|
|
290 }
|