comparison org.eclipse.jface/src/org/eclipse/jface/preference/PreferenceNode.d @ 12:bc29606a740c

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