author | David Powell <David.Powell@sun.com> |
Tue, 12 Aug 2008 17:52:52 -0700 | |
changeset 21 | 48868cfbefa3 |
parent 20 | 5423319ed064 |
child 30 | 0ad10e34fb31 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
5 |
* Common Development and Distribution License (the "License"). |
|
6 |
* You may not use this file except in compliance with the License. |
|
7 |
* |
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
||
22 |
/* |
|
23 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
|
24 |
* Use is subject to license terms. |
|
25 |
* |
|
26 |
* ident "@(#)Navigator.java 1.19 08/06/26 SMI" |
|
27 |
*/ |
|
28 |
||
29 |
package org.opensolaris.os.vp.panel; |
|
30 |
||
31 |
import java.util.*; |
|
32 |
import java.util.concurrent.*; |
|
33 |
import java.util.regex.Pattern; |
|
34 |
import org.opensolaris.os.vp.panel.Navigable.Util; |
|
35 |
import org.opensolaris.os.vp.util.*; |
|
36 |
||
37 |
public class Navigator { |
|
38 |
// |
|
39 |
// Static data |
|
40 |
// |
|
41 |
||
42 |
public static final String PATH_SEPARATOR = "/"; |
|
43 |
public static final String PARENT_ID = ".."; |
|
44 |
||
45 |
// Use a thread pool to autmatically handle uncaught exceptions and queued |
|
46 |
// requests |
|
47 |
private static final ThreadPoolExecutor navThreadPool; |
|
48 |
static { |
|
49 |
// Unbounded |
|
50 |
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); |
|
51 |
||
52 |
navThreadPool = new ThreadPoolExecutor( |
|
53 |
1, 1, 10, TimeUnit.MINUTES, queue); |
|
54 |
||
55 |
navThreadPool.allowCoreThreadTimeOut(true); |
|
56 |
} |
|
57 |
||
58 |
// |
|
59 |
// Instance data |
|
60 |
// |
|
61 |
||
62 |
private StackList<Control> stack = new StackList<Control>(); |
|
63 |
private List<Control> roStack = Collections.unmodifiableList(stack); |
|
64 |
||
65 |
private List<NavigationListener> listeners = |
|
66 |
new ArrayList<NavigationListener>(); |
|
67 |
||
68 |
private List<NavigationListener> roListeners = |
|
69 |
Collections.unmodifiableList(listeners); |
|
70 |
||
71 |
// |
|
72 |
// Constructors |
|
73 |
// |
|
74 |
||
75 |
/** |
|
76 |
* Constructs a {@code Navigator} with the given initial root. |
|
77 |
* |
|
78 |
* @param root |
|
79 |
* the intial root {@link Control} |
|
80 |
* |
|
81 |
* @param parameters |
|
82 |
* initialization parameters for the root {@link Control}, or |
|
83 |
* {@code null} if no parameters apply |
|
84 |
* |
|
85 |
* @exception ActionAbortedException |
|
86 |
* see {@link #goTo(Control,Navigable...)} |
|
87 |
* |
|
88 |
* @exception InvalidAddressException |
|
89 |
* see {@link #goTo(Control,Navigable...)} |
|
90 |
* |
|
91 |
* @exception MissingParameterException |
|
92 |
* see {@link #goTo(Control,Navigable...)} |
|
93 |
* |
|
94 |
* @exception InvalidParameterException |
|
95 |
* see {@link #goTo(Control,Navigable...)} |
|
96 |
* |
|
97 |
* @exception IllegalArgumentException |
|
98 |
* see {@link #goTo(Control,Navigable...)} |
|
99 |
*/ |
|
100 |
public Navigator(Control root, Map<String, String> parameters) |
|
101 |
throws ActionAbortedException, InvalidAddressException, |
|
102 |
MissingParameterException, InvalidParameterException { |
|
103 |
||
104 |
if (parameters == null) { |
|
105 |
parameters = Collections.emptyMap(); |
|
106 |
} else { |
|
107 |
parameters = Collections.unmodifiableMap(parameters); |
|
108 |
} |
|
109 |
||
110 |
root.start(this, parameters); |
|
111 |
stack.push(root); |
|
112 |
||
113 |
String forward = root.getForwardingPath(); |
|
114 |
if (forward != null) { |
|
115 |
SimpleNavigable[] path = toArray(forward); |
|
116 |
Control relativeTo = isAbsolute(forward) ? null : root; |
|
117 |
goTo(relativeTo, path); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
// |
|
122 |
// Navigator methods |
|
123 |
// |
|
124 |
||
125 |
/** |
|
126 |
* Adds a {@link NavigationListener} to be notified when navigation is |
|
127 |
* stopped and started. |
|
128 |
*/ |
|
129 |
public void addNavigationListener(NavigationListener l) { |
|
130 |
listeners.add(l); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Runs the given {@code Runnable} on this {@code Navigator}'s navigation |
|
135 |
* thread. This thread is intended to be used specifically for |
|
136 |
* navigation; the given {@code Runnable} should thus be limited to calling |
|
137 |
* {@link #goTo(Control,Navigable...)} and (briefly) handling any thrown |
|
138 |
* exceptions. |
|
139 |
* <p> |
|
140 |
* Notes: |
|
141 |
* <ol> |
|
142 |
* <li> |
|
143 |
* If this method is called when the navigation thread is busy, the |
|
144 |
* given {@code Runnable} will be queued and run when the thread is |
|
145 |
* available. |
|
146 |
* </li> |
|
147 |
* <li> |
|
148 |
* Navigation does not <i>have</i> to occur on this thread, of course. |
|
149 |
* It is provided here solely as a convenience for asynchronous |
|
150 |
* navigation. |
|
151 |
* </li> |
|
152 |
* </ol> |
|
153 |
* <p> |
|
154 |
* |
|
155 |
* @param r |
|
156 |
* the {@code Runnable} to navigate and handle any resulting |
|
157 |
* exceptions |
|
158 |
*/ |
|
19
9c1f74f86687
2837 Minor spelling errors and inconsistencies
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
159 |
public void asyncExec(Runnable r) { |
0 | 160 |
navThreadPool.execute(r); |
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Notifies all registered {@link NavigationListener}s that a navigation has |
|
165 |
* begun. |
|
166 |
*/ |
|
167 |
protected void fireNavigationStarted(NavigationEvent e) { |
|
168 |
// Iterate backwards to allow listener removal during iteration |
|
169 |
for (int i = listeners.size() - 1; i >= 0; i--) { |
|
170 |
listeners.get(i).navigationStarted(e); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Notifies all registered {@link NavigationListener}s that a navigation has |
|
176 |
* stopped. |
|
177 |
*/ |
|
178 |
protected void fireNavigationStopped(NavigationEvent e) { |
|
179 |
// Iterate backwards to allow listener removal during iteration |
|
180 |
for (int i = listeners.size() - 1; i >= 0; i--) { |
|
181 |
listeners.get(i).navigationStopped(e); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public List<NavigationListener> getNavigationListeners() { |
|
186 |
return roListeners; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Gets the current path. |
|
191 |
*/ |
|
192 |
public List<Control> getPath() { |
|
193 |
synchronized (stack) { |
|
194 |
return roStack; |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Gets the current path as a {@code String}. |
|
200 |
*/ |
|
201 |
public String getPathString() { |
|
202 |
synchronized (stack) { |
|
203 |
return getPathString(stack); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Navigates to the {@link Control} identified by the given path. |
|
209 |
* |
|
210 |
* @param relativeTo |
|
211 |
* a {@link Control} within the navigation stack to which |
|
212 |
* {@code path} is relative, or {@code null} if {@code path} is |
|
213 |
* absolute |
|
214 |
* |
|
215 |
* @param path |
|
216 |
* the path (relative to {@code relativeTo}), or unspecified to |
|
217 |
* navigate up the stack to {@code relativeTo} |
|
218 |
* |
|
219 |
* @return the previously current {@link Control} |
|
220 |
* |
|
221 |
* @exception ActionAbortedException |
|
222 |
* if the navigation is vetoed |
|
223 |
* |
|
224 |
* @exception InvalidAddressException |
|
225 |
* if {@code path} does not refer to a valid address |
|
226 |
* |
|
227 |
* @exception MissingParameterException |
|
228 |
* if some {@link Control} in the process of navigation could |
|
229 |
* not be {@link Control#start started} due to a missing |
|
230 |
* intialization parameter |
|
231 |
* |
|
232 |
* @exception InvalidParameterException |
|
233 |
* if some {@link Control} in the process of navigation could |
|
234 |
* not be {@link Control#start started} due to an invalid |
|
235 |
* intialization parameter |
|
236 |
* |
|
237 |
* @exception IllegalArgumentException |
|
238 |
* if {@code relativeTo} is not in the navigation stack |
|
239 |
*/ |
|
240 |
public Control goTo(Control relativeTo, Navigable... path) |
|
241 |
throws ActionAbortedException, InvalidAddressException, |
|
242 |
MissingParameterException, InvalidParameterException { |
|
243 |
||
244 |
synchronized (stack) { |
|
245 |
// Last absolute path |
|
246 |
StackList<Navigable> laPath = new StackList<Navigable>(); |
|
247 |
laPath.addAll(stack); |
|
248 |
||
249 |
while (true) { |
|
250 |
// Current absolute path |
|
251 |
StackList<Navigable> caPath = new StackList<Navigable>(); |
|
252 |
||
253 |
if (relativeTo != null) { |
|
254 |
caPath.addAll(laPath); |
|
255 |
try { |
|
256 |
// Remove path elements until relativeTo is at the top |
|
257 |
while (!((HasControl)caPath.peek()).getControl(). |
|
258 |
equals(relativeTo)) { |
|
259 |
caPath.pop(); |
|
260 |
} |
|
261 |
} catch (EmptyStackException e) { |
|
262 |
throw new IllegalArgumentException(String.format( |
|
263 |
"Control not in navigation path: %s (%s)", |
|
264 |
relativeTo.getClass().getName(), |
|
265 |
relativeTo.getId())); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
if (path != null && path.length != 0) { |
|
270 |
CollectionUtil.addAll(caPath, path); |
|
271 |
||
272 |
// Remove unnecessary ".." segments |
|
273 |
normalize(caPath); |
|
274 |
} |
|
275 |
||
276 |
List<Navigable> rPath = getRelativePath(laPath, caPath); |
|
277 |
if (rPath.isEmpty()) { |
|
278 |
break; |
|
279 |
} |
|
280 |
||
281 |
// Iterate through rPath, adding/removing Controls |
|
282 |
// to/from laPath as appropriate |
|
283 |
for (Navigable hIP : rPath) { |
|
284 |
if (hIP.getId().equals(PARENT_ID)) { |
|
285 |
switch (laPath.size()) { |
|
286 |
case 0: |
|
287 |
// Should be impossible |
|
288 |
throw new IllegalArgumentException( |
|
289 |
"navigation stack has no root"); |
|
290 |
||
291 |
case 1: |
|
292 |
// Tried to pop root off navigation stack |
|
293 |
throw new InvalidAddressException( |
|
294 |
getPathString(caPath)); |
|
295 |
} |
|
296 |
||
297 |
laPath.pop(); |
|
298 |
} else { |
|
299 |
String id = hIP.getId(); |
|
300 |
||
301 |
Object last = laPath.peek(); |
|
302 |
||
303 |
// laPath elements are Controls or PendingControls |
|
304 |
Control curControl = ((HasControl)last).getControl(); |
|
305 |
Control newControl = curControl.getChildControl(id); |
|
306 |
||
307 |
if (newControl == null) { |
|
308 |
throw new InvalidAddressException( |
|
309 |
String.format("%s%s%s", getPathString(laPath), |
|
310 |
PATH_SEPARATOR, id)); |
|
311 |
} |
|
312 |
||
313 |
PendingControl pair = |
|
314 |
new PendingControl(newControl, hIP.getParameters()); |
|
315 |
||
316 |
laPath.push(pair); |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
Control control = ((HasControl)laPath.peek()).getControl(); |
|
321 |
String forward = control.getForwardingPath(); |
|
322 |
if (forward == null) { |
|
323 |
break; |
|
324 |
} |
|
325 |
||
326 |
path = toArray(forward); |
|
327 |
relativeTo = isAbsolute(forward) ? null : control; |
|
328 |
} |
|
329 |
||
330 |
// The original Control, before navigation |
|
331 |
Control orig = peek(); |
|
332 |
||
333 |
// Mildly hackish |
|
334 |
List tmp = getRelativePath(stack, laPath); |
|
335 |
||
336 |
@SuppressWarnings({"unchecked"}) |
|
337 |
List<PendingControl> rPath = (List<PendingControl>)tmp; |
|
338 |
||
339 |
if (!rPath.isEmpty()) { |
|
340 |
NavigationEvent event = new NavigationEvent(this, |
|
341 |
Collections.unmodifiableList(rPath)); |
|
342 |
fireNavigationStarted(event); |
|
343 |
||
344 |
try { |
|
345 |
for (PendingControl pend : rPath) { |
|
346 |
Control curControl = peek(); |
|
347 |
||
348 |
if (pend.getId().equals(PARENT_ID)) { |
|
349 |
curControl.stop(); |
|
350 |
stack.pop(); |
|
351 |
peek().childStopped(curControl); |
|
352 |
} else { |
|
353 |
Control newControl = pend.getControl(); |
|
354 |
||
355 |
newControl.start(this, pend.getParameters()); |
|
356 |
stack.push(newControl); |
|
357 |
||
358 |
curControl.childStarted(newControl); |
|
359 |
} |
|
360 |
||
361 |
if (System.getProperty("vpanels.debug.navigator") != |
|
362 |
null) { |
|
363 |
System.out.printf("%s\n", getPathString()); |
|
364 |
} |
|
365 |
} |
|
366 |
} finally { |
|
367 |
fireNavigationStopped(event); |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
return orig; |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
/** |
|
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
376 |
* Asynchronously navigates to the {@link Control} identified by the |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
377 |
* given path. A wrapper around {@link #goTo(Control,Navigable...) goTo} |
21
48868cfbefa3
2853 Async navigation in serious need of factoring (fix broken javadoc link)
David Powell <David.Powell@sun.com>
parents:
20
diff
changeset
|
378 |
* that is run asynchronously using {@link #asyncExec(Runnable) asyncExec}. |
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
379 |
* |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
380 |
* @param relativeTo |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
381 |
* a {@link Control} within the navigation stack to which |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
382 |
* {@code path} is relative, or {@code null} if {@code path} is |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
383 |
* absolute |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
384 |
* |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
385 |
* @param path |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
386 |
* the path (relative to {@code relativeTo}), or unspecified to |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
387 |
* navigate up the stack to {@code relativeTo} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
388 |
*/ |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
389 |
public void goToAsync(final Control relativeTo, final Navigable... path) { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
390 |
asyncExec( |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
391 |
new Runnable() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
392 |
@Override |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
393 |
public void run() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
394 |
try { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
395 |
goTo(relativeTo, path); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
396 |
} catch (NavigationException ignore) { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
397 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
398 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
399 |
}); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
400 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
401 |
|
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
402 |
/** |
0 | 403 |
* Shortcut for: |
404 |
* <p> |
|
405 |
* <code> |
|
406 |
* {@link #goTo(Control,Navigable...) goTo}(relativeTo, |
|
407 |
* new {@link SimpleNavigable#SimpleNavigable(String,Map) |
|
408 |
* SimpleNavigable}(id, {@link Navigable.Util#toMap |
|
409 |
* Navigable.Util.toMap}(parameters))); |
|
410 |
* </code> |
|
411 |
* </p> |
|
412 |
*/ |
|
413 |
public Control goTo(Control relativeTo, String id, |
|
414 |
String... parameters) throws ActionAbortedException, |
|
415 |
InvalidAddressException, MissingParameterException, |
|
416 |
InvalidParameterException { |
|
417 |
||
418 |
return goTo(relativeTo, |
|
419 |
new SimpleNavigable(id, Navigable.Util.toMap(parameters))); |
|
420 |
} |
|
421 |
||
422 |
/** |
|
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
423 |
* A wrapper around {@link #goTo(Control,String,String...) goTo} |
21
48868cfbefa3
2853 Async navigation in serious need of factoring (fix broken javadoc link)
David Powell <David.Powell@sun.com>
parents:
20
diff
changeset
|
424 |
* that is run asynchronously using {@link #asyncExec(Runnable) asyncExec}. |
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
425 |
*/ |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
426 |
public void goToAsync(final Control relativeTo, final String id, |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
427 |
final String... parameters) { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
428 |
asyncExec( |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
429 |
new Runnable() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
430 |
@Override |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
431 |
public void run() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
432 |
try { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
433 |
goTo(relativeTo, id, parameters); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
434 |
} catch (NavigationException ignore) { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
435 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
436 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
437 |
}); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
438 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
439 |
|
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
440 |
/** |
0 | 441 |
* Returns the current {@link Control}. |
442 |
*/ |
|
443 |
public Control peek() { |
|
444 |
synchronized (stack) { |
|
445 |
return stack.peek(); |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Navigate to the parent of the current {@link Control}. |
|
451 |
* |
|
452 |
* @return the previously current {@link Control} |
|
453 |
* |
|
454 |
* @exception ActionAbortedException |
|
455 |
* if the navigation is vetoed |
|
456 |
* |
|
457 |
* @exception IllegalArgumentException |
|
458 |
* if the navigation stack consists only of the root element |
|
459 |
*/ |
|
460 |
public Control pop() throws ActionAbortedException { |
|
461 |
try { |
|
462 |
return goTo(peek(), PendingControl.PARENT); |
|
463 |
} catch (InvalidAddressException ignore) { |
|
464 |
// Should not be possible |
|
465 |
} catch (InvalidParameterException ignore) { |
|
466 |
// Should not be possible |
|
467 |
} |
|
468 |
return null; |
|
469 |
} |
|
470 |
||
471 |
/** |
|
472 |
* Removes a {@link NavigationListener} from notification of when navigation |
|
473 |
* is stopped and started. |
|
474 |
*/ |
|
475 |
public boolean removeNavigationListener(NavigationListener l) { |
|
476 |
return listeners.remove(l); |
|
477 |
} |
|
478 |
||
479 |
// |
|
480 |
// Private methods |
|
481 |
// |
|
482 |
||
483 |
/** |
|
484 |
* Gets the relative path between the given absolute paths. |
|
485 |
* |
|
486 |
* @param fromPath |
|
487 |
* an absolute source path |
|
488 |
* |
|
489 |
* @param toPath |
|
490 |
* an absolute destination path |
|
491 |
* |
|
492 |
* @return a relative path from the current path to the given path |
|
493 |
*/ |
|
494 |
private List<Navigable> getRelativePath( |
|
495 |
List<? extends Navigable> fromPath, |
|
496 |
List<? extends Navigable> toPath) { |
|
497 |
||
498 |
List<Navigable> rPath = new ArrayList<Navigable>(); |
|
499 |
||
500 |
// Determine branching index |
|
501 |
int branch = 0; |
|
502 |
while (branch < fromPath.size() && branch < toPath.size() && |
|
503 |
Navigable.Util.equals(fromPath.get(branch), toPath.get(branch))) { |
|
504 |
||
505 |
branch++; |
|
506 |
} |
|
507 |
||
508 |
for (int i = fromPath.size() - 1; i >= branch; i--) { |
|
509 |
rPath.add(PendingControl.PARENT); |
|
510 |
} |
|
511 |
||
512 |
for (int i = branch; i < toPath.size(); i++) { |
|
513 |
rPath.add(toPath.get(i)); |
|
514 |
} |
|
515 |
||
516 |
return rPath; |
|
517 |
} |
|
518 |
||
519 |
/** |
|
520 |
* Removes each ".." segment and preceding non-".." segment the given path. |
|
521 |
*/ |
|
522 |
private void normalize(List<? extends HasId> path) { |
|
523 |
for (int i = 1; i < path.size(); i++) { |
|
524 |
if (path.get(i).getId().equals(PARENT_ID) && |
|
525 |
!path.get(i - 1).getId().equals(PARENT_ID)) { |
|
526 |
path.remove(i--); |
|
527 |
path.remove(i--); |
|
528 |
} |
|
529 |
} |
|
530 |
} |
|
531 |
||
532 |
// |
|
533 |
// Static methods |
|
534 |
// |
|
535 |
||
536 |
/** |
|
537 |
* Gets the given path as a {@code String}. |
|
538 |
*/ |
|
539 |
public static String getPathString(Collection<? extends Navigable> path) { |
|
540 |
StringBuffer buffer = new StringBuffer(); |
|
541 |
||
542 |
for (Navigable hIP : path) { |
|
543 |
buffer.append(PATH_SEPARATOR).append(Control.encode( |
|
544 |
hIP.getId(), hIP.getParameters())); |
|
545 |
} |
|
546 |
||
547 |
return buffer.toString(); |
|
548 |
} |
|
549 |
||
550 |
/** |
|
551 |
* Determines whether the given path is absolute. If the given path starts |
|
552 |
* with PATH_SEPARATOR, it is considered absolute. |
|
553 |
*/ |
|
554 |
public static boolean isAbsolute(String path) { |
|
555 |
return path.startsWith(PATH_SEPARATOR); |
|
556 |
} |
|
557 |
||
558 |
public static SimpleNavigable[] toArray(String path) { |
|
559 |
path = path.replaceFirst( |
|
560 |
"^(" + Pattern.quote(PATH_SEPARATOR) + ")+", ""); |
|
561 |
||
562 |
String[] parts = path.split(PATH_SEPARATOR, 0); |
|
563 |
SimpleNavigable[] elements = new SimpleNavigable[parts.length]; |
|
564 |
||
565 |
for (int i = 0; i < parts.length; i++) { |
|
566 |
elements[i] = Control.decode(parts[i]); |
|
567 |
} |
|
568 |
||
569 |
return elements; |
|
570 |
} |
|
571 |
} |