Mercurial > projects > dwt-addons
comparison dwtx/jface/preference/PreferenceManager.d @ 34:b3c8e32d406f
preference
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 05 Apr 2008 01:45:47 +0200 |
parents | |
children | 46a6e0e6ccd4 |
comparison
equal
deleted
inserted
replaced
33:f25582573129 | 34:b3c8e32d406f |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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.PreferenceManager; | |
14 | |
15 import dwtx.jface.preference.PreferenceNode; | |
16 import dwtx.jface.preference.IPreferenceNode; | |
17 | |
18 import tango.util.collection.ArraySeq; | |
19 import tango.util.collection.model.Seq; | |
20 // import java.util.List; | |
21 // import java.util.StringTokenizer; | |
22 | |
23 import dwtx.core.runtime.Assert; | |
24 | |
25 import dwt.dwthelper.utils; | |
26 import tango.text.Util; | |
27 | |
28 /** | |
29 * A preference manager maintains a hierarchy of preference nodes and | |
30 * associated preference pages. | |
31 */ | |
32 public class PreferenceManager { | |
33 /** | |
34 * Pre-order traversal means visit the root first, | |
35 * then the children. | |
36 */ | |
37 public static const int PRE_ORDER = 0; | |
38 | |
39 /** | |
40 * Post-order means visit the children, and then the root. | |
41 */ | |
42 public static const int POST_ORDER = 1; | |
43 | |
44 /** | |
45 * The root node. | |
46 * Note that the root node is a special internal node | |
47 * that is used to collect together all the nodes that | |
48 * have no parent; it is not given out to clients. | |
49 */ | |
50 PreferenceNode root; | |
51 | |
52 /** | |
53 * The path separator character. | |
54 */ | |
55 String separator; | |
56 | |
57 /** | |
58 * Creates a new preference manager. | |
59 */ | |
60 public this() { | |
61 this('.'); | |
62 } | |
63 | |
64 /** | |
65 * Creates a new preference manager with the given | |
66 * the path separator. | |
67 * | |
68 * @param separatorChar the separator character | |
69 */ | |
70 public this(char separatorChar) { | |
71 root = new PreferenceNode("");//$NON-NLS-1$ | |
72 separator = [ separatorChar ]; | |
73 } | |
74 | |
75 /** | |
76 * Adds the given preference node as a subnode of the | |
77 * node at the given path. | |
78 * | |
79 * @param path the path | |
80 * @param node the node to add | |
81 * @return <code>true</code> if the add was successful, | |
82 * and <code>false</code> if there is no contribution at | |
83 * the given path | |
84 */ | |
85 public bool addTo(String path, IPreferenceNode node) { | |
86 IPreferenceNode target = find(path); | |
87 if (target is null) { | |
88 return false; | |
89 } | |
90 target.add(node); | |
91 return true; | |
92 } | |
93 | |
94 /** | |
95 * Adds the given preference node as a subnode of the | |
96 * root. | |
97 * | |
98 * @param node the node to add, which must implement | |
99 * <code>IPreferenceNode</code> | |
100 */ | |
101 public void addToRoot(IPreferenceNode node) { | |
102 Assert.isNotNull(cast(Object)node); | |
103 root.add(node); | |
104 } | |
105 | |
106 /** | |
107 * Recursively enumerates all nodes at or below the given node | |
108 * and adds them to the given list in the given order. | |
109 * | |
110 * @param node the starting node | |
111 * @param sequence a read-write list of preference nodes | |
112 * (element type: <code>IPreferenceNode</code>) | |
113 * in the given order | |
114 * @param order the traversal order, one of | |
115 * <code>PRE_ORDER</code> and <code>POST_ORDER</code> | |
116 */ | |
117 protected void buildSequence(IPreferenceNode node, Seq!(Object) sequence, int order) { | |
118 if (order is PRE_ORDER) { | |
119 sequence.append(cast(Object)node); | |
120 } | |
121 IPreferenceNode[] subnodes = node.getSubNodes(); | |
122 for (int i = 0; i < subnodes.length; i++) { | |
123 buildSequence(subnodes[i], sequence, order); | |
124 } | |
125 if (order is POST_ORDER) { | |
126 sequence.append(cast(Object)node); | |
127 } | |
128 } | |
129 | |
130 /** | |
131 * Finds and returns the contribution node at the given path. | |
132 * | |
133 * @param path the path | |
134 * @return the node, or <code>null</code> if none | |
135 */ | |
136 public IPreferenceNode find(String path) { | |
137 return find(path,root); | |
138 } | |
139 | |
140 /** | |
141 * Finds and returns the preference node directly | |
142 * below the top at the given path. | |
143 * | |
144 * @param path the path | |
145 * @param top top at the given path | |
146 * @return the node, or <code>null</code> if none | |
147 * | |
148 * @since 3.1 | |
149 */ | |
150 protected IPreferenceNode find(String path,IPreferenceNode top){ | |
151 Assert.isNotNull(path); | |
152 auto tokens = tango.text.Util.delimit( path, separator ); | |
153 IPreferenceNode node = top; | |
154 foreach( id; tokens ){ | |
155 node = node.findSubNode(id); | |
156 if (node is null) { | |
157 return null; | |
158 } | |
159 } | |
160 if (node is top) { | |
161 return null; | |
162 } | |
163 return node; | |
164 } | |
165 | |
166 /** | |
167 * Returns all preference nodes managed by this | |
168 * manager. | |
169 * | |
170 * @param order the traversal order, one of | |
171 * <code>PRE_ORDER</code> and <code>POST_ORDER</code> | |
172 * @return a list of preference nodes | |
173 * (element type: <code>IPreferenceNode</code>) | |
174 * in the given order | |
175 */ | |
176 public Seq!(Object) getElements(int order) { | |
177 Assert.isTrue(order is PRE_ORDER || order is POST_ORDER, | |
178 "invalid traversal order");//$NON-NLS-1$ | |
179 auto sequence = new ArraySeq!(Object); | |
180 IPreferenceNode[] subnodes = getRoot().getSubNodes(); | |
181 for (int i = 0; i < subnodes.length; i++) { | |
182 buildSequence(subnodes[i], sequence, order); | |
183 } | |
184 return sequence; | |
185 } | |
186 | |
187 /** | |
188 * Returns the root node. | |
189 * Note that the root node is a special internal node | |
190 * that is used to collect together all the nodes that | |
191 * have no parent; it is not given out to clients. | |
192 * | |
193 * @return the root node | |
194 */ | |
195 protected IPreferenceNode getRoot() { | |
196 return root; | |
197 } | |
198 package IPreferenceNode getRoot_package() { | |
199 return getRoot(); | |
200 } | |
201 | |
202 /** | |
203 * Returns the root level nodes of this preference manager. | |
204 * | |
205 * @return an array containing the root nodes | |
206 * @since 3.2 | |
207 */ | |
208 public final IPreferenceNode[] getRootSubNodes() { | |
209 return getRoot().getSubNodes(); | |
210 } | |
211 | |
212 /** | |
213 * Removes the preference node at the given path. | |
214 * | |
215 * @param path | |
216 * the path | |
217 * @return the node that was removed, or <code>null</code> if there was no | |
218 * node at the given path | |
219 */ | |
220 public IPreferenceNode remove(String path) { | |
221 Assert.isNotNull(path); | |
222 int index = path.lastIndexOf(separator); | |
223 if (index is -1) { | |
224 return root.remove(path); | |
225 } | |
226 // Make sure that the last character in the string isn't the "." | |
227 Assert.isTrue(index < path.length - 1, "Path can not end with a dot");//$NON-NLS-1$ | |
228 String parentPath = path.substring(0, index); | |
229 String id = path.substring(index + 1); | |
230 IPreferenceNode parentNode = find(parentPath); | |
231 if (parentNode is null) { | |
232 return null; | |
233 } | |
234 return parentNode.remove(id); | |
235 } | |
236 | |
237 /** | |
238 * Removes the given prefreence node if it is managed by | |
239 * this contribution manager. | |
240 * | |
241 * @param node the node to remove | |
242 * @return <code>true</code> if the node was removed, | |
243 * and <code>false</code> otherwise | |
244 */ | |
245 public bool remove(IPreferenceNode node) { | |
246 Assert.isNotNull(cast(Object)node); | |
247 | |
248 return root.remove(node); | |
249 } | |
250 | |
251 /** | |
252 * Removes all contribution nodes known to this manager. | |
253 */ | |
254 public void removeAll() { | |
255 root = new PreferenceNode("");//$NON-NLS-1$ | |
256 } | |
257 } |