author | Stephen Talley <stephen.talley@sun.com> |
Thu, 30 Oct 2008 13:52:45 -0400 | |
changeset 133 | e1112f707f32 |
parent 68 | 712f02aa3c15 |
child 139 | 2fbe46054558 |
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 |
||
40
d1412af1ae5b
3215 ActionAbortedException is used too broadly
Stephen Talley <stephen.talley@sun.com>
parents:
30
diff
changeset
|
27 |
package org.opensolaris.os.vp.panel.common.control; |
0 | 28 |
|
29 |
import java.util.*; |
|
30 |
import java.util.concurrent.*; |
|
31 |
import java.util.regex.Pattern; |
|
32 |
import org.opensolaris.os.vp.util.*; |
|
33 |
||
34 |
public class Navigator { |
|
35 |
// |
|
36 |
// Static data |
|
37 |
// |
|
38 |
||
39 |
public static final String PATH_SEPARATOR = "/"; |
|
40 |
public static final String PARENT_ID = ".."; |
|
41 |
||
59
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
42 |
// |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
43 |
// Instance data |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
44 |
// |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
45 |
|
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
46 |
private Thread dispatchThread; |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
47 |
|
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
48 |
// Use a thread pool with a single core thread to autmatically handle |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
49 |
// uncaught exceptions and queued requests |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
50 |
private ThreadPoolExecutor threadPool; |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
51 |
{ |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
52 |
ThreadFactory factory = |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
53 |
new NamedThreadFactory("Navigator-") { |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
54 |
@Override |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
55 |
public Thread newThread(Runnable r) { |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
56 |
dispatchThread = super.newThread(r); |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
57 |
return dispatchThread; |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
58 |
} |
25cc24ff4f57
3329 navigation thread pool should be non-static, per-Navigator
Stephen Talley <stephen.talley@sun.com>
parents:
54
diff
changeset
|
59 |
}; |
30
0ad10e34fb31
3025 Thread pools should be named for better diagnostics
Stephen Talley <stephen.talley@sun.com>
parents:
21
diff
changeset
|
60 |
|
0 | 61 |
// Unbounded |
62 |
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); |
|
63 |
||
30
0ad10e34fb31
3025 Thread pools should be named for better diagnostics
Stephen Talley <stephen.talley@sun.com>
parents:
21
diff
changeset
|
64 |
threadPool = new ThreadPoolExecutor( |
0ad10e34fb31
3025 Thread pools should be named for better diagnostics
Stephen Talley <stephen.talley@sun.com>
parents:
21
diff
changeset
|
65 |
1, 1, 10, TimeUnit.MINUTES, queue, factory); |
0 | 66 |
|
30
0ad10e34fb31
3025 Thread pools should be named for better diagnostics
Stephen Talley <stephen.talley@sun.com>
parents:
21
diff
changeset
|
67 |
threadPool.allowCoreThreadTimeOut(true); |
0 | 68 |
} |
69 |
||
70 |
private StackList<Control> stack = new StackList<Control>(); |
|
71 |
||
72 |
private List<NavigationListener> listeners = |
|
73 |
new ArrayList<NavigationListener>(); |
|
74 |
||
75 |
private List<NavigationListener> roListeners = |
|
76 |
Collections.unmodifiableList(listeners); |
|
77 |
||
78 |
// |
|
79 |
// Navigator methods |
|
80 |
// |
|
81 |
||
82 |
/** |
|
83 |
* Adds a {@link NavigationListener} to be notified when navigation is |
|
84 |
* stopped and started. |
|
85 |
*/ |
|
86 |
public void addNavigationListener(NavigationListener l) { |
|
87 |
listeners.add(l); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
91 |
* Runs the given {@code Runnable} asynchronously on this {@code |
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
92 |
* Navigator}'s navigation thread. This thread is intended to be used |
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
93 |
* specifically for navigation; the given {@code Runnable} should thus be |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
94 |
* limited to calling {@link #goTo(Control,Navigable...)} and (briefly) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
95 |
* handling any thrown exceptions. |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
96 |
* <p/> |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
97 |
* Note: If this method is called when the navigation thread is busy, the |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
98 |
* given {@code Runnable} will be queued and run when the thread is |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
99 |
* available. |
0 | 100 |
* |
101 |
* @param r |
|
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
102 |
* the {@code Runnable} to run and handle any resulting |
0 | 103 |
* exceptions |
104 |
*/ |
|
19
9c1f74f86687
2837 Minor spelling errors and inconsistencies
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
105 |
public void asyncExec(Runnable r) { |
30
0ad10e34fb31
3025 Thread pools should be named for better diagnostics
Stephen Talley <stephen.talley@sun.com>
parents:
21
diff
changeset
|
106 |
threadPool.execute(r); |
0 | 107 |
} |
108 |
||
109 |
/** |
|
68
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
110 |
* Runs the given {@link NavRunnable} on this {@code Navigator}'s navigation |
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
111 |
* thread and waits for it to complete. Any exceptions thrown by {@code r} |
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
112 |
* are thrown here. |
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
113 |
* <p/> |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
114 |
* See {@link #asyncExec}. |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
115 |
* |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
116 |
* @param r |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
117 |
* the {@link NavRunnable} to run |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
118 |
*/ |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
119 |
public void asyncExecAndWait(final NavRunnable r) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
120 |
throws NavigationAbortedException, InvalidAddressException, |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
121 |
MissingParameterException, InvalidParameterException { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
122 |
|
68
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
123 |
if (isDispatchThread()) { |
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
124 |
r.run(); |
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
125 |
return; |
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
126 |
} |
712f02aa3c15
3348 After selecting existing login from new window, unable to close window
Stephen Talley <stephen.talley@sun.com>
parents:
60
diff
changeset
|
127 |
|
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
128 |
final boolean[] done = {false}; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
129 |
final Throwable[] throwable = {null}; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
130 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
131 |
asyncExec( |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
132 |
new Runnable() { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
133 |
@Override |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
134 |
public void run() { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
135 |
try { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
136 |
r.run(); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
137 |
} catch (Throwable t) { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
138 |
throwable[0] = t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
139 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
140 |
synchronized (done) { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
141 |
done[0] = true; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
142 |
done.notify(); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
143 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
144 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
145 |
}); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
146 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
147 |
// Sleep until nav thread is done |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
148 |
synchronized (done) { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
149 |
while (!done[0]) { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
150 |
try { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
151 |
done.wait(); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
152 |
} catch (InterruptedException ignore) { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
153 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
154 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
155 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
156 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
157 |
Throwable t = throwable[0]; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
158 |
if (t != null) { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
159 |
if (t instanceof NavigationAbortedException) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
160 |
throw (NavigationAbortedException)t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
161 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
162 |
if (t instanceof InvalidAddressException) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
163 |
throw (InvalidAddressException)t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
164 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
165 |
if (t instanceof MissingParameterException) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
166 |
throw (MissingParameterException)t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
167 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
168 |
if (t instanceof InvalidParameterException) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
169 |
throw (InvalidParameterException)t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
170 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
171 |
if (t instanceof RuntimeException) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
172 |
throw (RuntimeException)t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
173 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
174 |
if (t instanceof Error) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
175 |
throw (Error)t; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
176 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
177 |
// All Throwables should be accounted for |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
178 |
assert false; |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
179 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
180 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
181 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
182 |
/** |
0 | 183 |
* Notifies all registered {@link NavigationListener}s that a navigation has |
184 |
* begun. |
|
185 |
*/ |
|
186 |
protected void fireNavigationStarted(NavigationEvent e) { |
|
187 |
// Iterate backwards to allow listener removal during iteration |
|
188 |
for (int i = listeners.size() - 1; i >= 0; i--) { |
|
189 |
listeners.get(i).navigationStarted(e); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
/** |
|
194 |
* Notifies all registered {@link NavigationListener}s that a navigation has |
|
195 |
* stopped. |
|
196 |
*/ |
|
197 |
protected void fireNavigationStopped(NavigationEvent e) { |
|
198 |
// Iterate backwards to allow listener removal during iteration |
|
199 |
for (int i = listeners.size() - 1; i >= 0; i--) { |
|
200 |
listeners.get(i).navigationStopped(e); |
|
201 |
} |
|
202 |
} |
|
203 |
||
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
204 |
/** |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
205 |
* Returns the current {@link Control}, or {@code null} if the root element |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
206 |
* of the navigation stack has not yet been set. |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
207 |
*/ |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
208 |
public Control getCurrentControl() { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
209 |
synchronized (stack) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
210 |
return StackUtil.getTop(stack); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
211 |
} |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
212 |
} |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
213 |
|
0 | 214 |
public List<NavigationListener> getNavigationListeners() { |
215 |
return roListeners; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
54
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
219 |
* Gets the current path. This method is not thread-safe; it is up to |
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
220 |
* callers to ensure the that navigation is not currently modifying this |
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
221 |
* path. |
0 | 222 |
*/ |
54
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
223 |
@SuppressWarnings({"unchecked"}) |
0 | 224 |
public List<Control> getPath() { |
54
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
225 |
return (List<Control>)stack.clone(); |
0 | 226 |
} |
227 |
||
228 |
/** |
|
54
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
229 |
* Gets the current path as a {@code String}. This method is not |
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
230 |
* thread-safe; it is up to callers to ensure the that navigation is not |
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
231 |
* currently modifying this path. |
0 | 232 |
*/ |
233 |
public String getPathString() { |
|
54
ffbdd1a03fca
3305 consumers of Navigator's getPath subject to ConcurrentModificationExceptions
Stephen Talley <stephen.talley@sun.com>
parents:
40
diff
changeset
|
234 |
return getPathString(stack); |
0 | 235 |
} |
236 |
||
237 |
/** |
|
238 |
* Navigates to the {@link Control} identified by the given path. |
|
239 |
* |
|
240 |
* @param relativeTo |
|
241 |
* a {@link Control} within the navigation stack to which |
|
242 |
* {@code path} is relative, or {@code null} if {@code path} is |
|
243 |
* absolute |
|
244 |
* |
|
245 |
* @param path |
|
246 |
* the path (relative to {@code relativeTo}), or unspecified to |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
247 |
* navigate up the stack to {@code relativeTo}; if the root of |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
248 |
* this {@code Navigator} has not yet been set, the first |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
249 |
* element of this path <strong>must</strong> be a {@link |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
250 |
* Control} |
0 | 251 |
* |
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
252 |
* @return the previously current {@link Control}, or {@code null} if |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
253 |
* the root {@link Control} of the navigation stack has not yet |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
254 |
* been set |
0 | 255 |
* |
40
d1412af1ae5b
3215 ActionAbortedException is used too broadly
Stephen Talley <stephen.talley@sun.com>
parents:
30
diff
changeset
|
256 |
* @exception NavigationAbortedException |
0 | 257 |
* if the navigation is vetoed |
258 |
* |
|
259 |
* @exception InvalidAddressException |
|
260 |
* if {@code path} does not refer to a valid address |
|
261 |
* |
|
262 |
* @exception MissingParameterException |
|
263 |
* if some {@link Control} in the process of navigation could |
|
264 |
* not be {@link Control#start started} due to a missing |
|
265 |
* intialization parameter |
|
266 |
* |
|
267 |
* @exception InvalidParameterException |
|
268 |
* if some {@link Control} in the process of navigation could |
|
269 |
* not be {@link Control#start started} due to an invalid |
|
270 |
* intialization parameter |
|
271 |
* |
|
272 |
* @exception IllegalArgumentException |
|
273 |
* if {@code relativeTo} is not in the navigation stack |
|
274 |
*/ |
|
275 |
public Control goTo(Control relativeTo, Navigable... path) |
|
40
d1412af1ae5b
3215 ActionAbortedException is used too broadly
Stephen Talley <stephen.talley@sun.com>
parents:
30
diff
changeset
|
276 |
throws NavigationAbortedException, InvalidAddressException, |
0 | 277 |
MissingParameterException, InvalidParameterException { |
278 |
||
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
279 |
assert isDispatchThread(); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
280 |
|
0 | 281 |
synchronized (stack) { |
282 |
// Last absolute path |
|
283 |
StackList<Navigable> laPath = new StackList<Navigable>(); |
|
284 |
laPath.addAll(stack); |
|
285 |
||
286 |
while (true) { |
|
287 |
// Current absolute path |
|
288 |
StackList<Navigable> caPath = new StackList<Navigable>(); |
|
289 |
||
290 |
if (relativeTo != null) { |
|
291 |
caPath.addAll(laPath); |
|
292 |
try { |
|
293 |
// Remove path elements until relativeTo is at the top |
|
294 |
while (!((HasControl)caPath.peek()).getControl(). |
|
295 |
equals(relativeTo)) { |
|
296 |
caPath.pop(); |
|
297 |
} |
|
298 |
} catch (EmptyStackException e) { |
|
299 |
throw new IllegalArgumentException(String.format( |
|
300 |
"Control not in navigation path: %s (%s)", |
|
301 |
relativeTo.getClass().getName(), |
|
302 |
relativeTo.getId())); |
|
303 |
} |
|
304 |
} |
|
305 |
||
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
306 |
if (path.length != 0) { |
0 | 307 |
CollectionUtil.addAll(caPath, path); |
308 |
||
309 |
// Remove unnecessary ".." segments |
|
310 |
normalize(caPath); |
|
311 |
} |
|
312 |
||
313 |
List<Navigable> rPath = getRelativePath(laPath, caPath); |
|
314 |
if (rPath.isEmpty()) { |
|
315 |
break; |
|
316 |
} |
|
317 |
||
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
318 |
// Iterate through rPath, adding/removing elements to/from |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
319 |
// laPath as appropriate |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
320 |
for (Navigable nav : rPath) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
321 |
if (nav.getId().equals(PARENT_ID)) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
322 |
if (laPath.isEmpty()) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
323 |
throw new IllegalArgumentException( |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
324 |
"navigation stack has no root"); |
0 | 325 |
} |
326 |
||
327 |
laPath.pop(); |
|
328 |
} else { |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
329 |
Control newControl; |
0 | 330 |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
331 |
// If the Navigation stack has no root... |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
332 |
if (laPath.isEmpty()) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
333 |
try { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
334 |
newControl = (Control)nav; |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
335 |
} catch (ClassCastException e) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
336 |
throw new IllegalArgumentException( |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
337 |
"root-level Navigable \"" + nav.getName() + |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
338 |
"\" must be a Control"); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
339 |
} |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
340 |
} else { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
341 |
String id = nav.getId(); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
342 |
Object last = laPath.peek(); |
0 | 343 |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
344 |
// laPath elements are Controls or PendingControls |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
345 |
Control curControl = |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
346 |
((HasControl)last).getControl(); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
347 |
newControl = curControl.getChildControl(id); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
348 |
|
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
349 |
if (newControl == null) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
350 |
throw new InvalidAddressException(String.format( |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
351 |
"%s%s%s", getPathString(laPath), |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
352 |
PATH_SEPARATOR, id)); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
353 |
} |
0 | 354 |
} |
355 |
||
356 |
PendingControl pair = |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
357 |
new PendingControl(newControl, nav.getParameters()); |
0 | 358 |
|
359 |
laPath.push(pair); |
|
360 |
} |
|
361 |
} |
|
362 |
||
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
363 |
try { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
364 |
Control control = ((HasControl)laPath.peek()).getControl(); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
365 |
String forward = control.getForwardingPath(); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
366 |
if (forward == null) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
367 |
break; |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
368 |
} |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
369 |
|
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
370 |
path = toArray(forward); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
371 |
relativeTo = isAbsolute(forward) ? null : control; |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
372 |
} catch (EmptyStackException ignore) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
373 |
// No root Control |
0 | 374 |
} |
375 |
} |
|
376 |
||
377 |
// The original Control, before navigation |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
378 |
Control orig = getCurrentControl(); |
0 | 379 |
|
380 |
@SuppressWarnings({"unchecked"}) |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
381 |
List<PendingControl> rPath = |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
382 |
(List<PendingControl>)(List)getRelativePath(stack, laPath); |
0 | 383 |
|
384 |
if (!rPath.isEmpty()) { |
|
385 |
NavigationEvent event = new NavigationEvent(this, |
|
386 |
Collections.unmodifiableList(rPath)); |
|
387 |
fireNavigationStarted(event); |
|
388 |
||
389 |
try { |
|
390 |
for (PendingControl pend : rPath) { |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
391 |
Control curControl = getCurrentControl(); |
0 | 392 |
|
393 |
if (pend.getId().equals(PARENT_ID)) { |
|
394 |
curControl.stop(); |
|
395 |
stack.pop(); |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
396 |
Control newCurControl = getCurrentControl(); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
397 |
if (newCurControl != null) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
398 |
newCurControl.childStopped(curControl); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
399 |
} |
0 | 400 |
} else { |
401 |
Control newControl = pend.getControl(); |
|
402 |
||
403 |
newControl.start(this, pend.getParameters()); |
|
404 |
stack.push(newControl); |
|
405 |
||
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
406 |
if (curControl != null) { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
407 |
curControl.childStarted(newControl); |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
408 |
} |
0 | 409 |
} |
410 |
||
411 |
if (System.getProperty("vpanels.debug.navigator") != |
|
412 |
null) { |
|
413 |
System.out.printf("%s\n", getPathString()); |
|
414 |
} |
|
415 |
} |
|
416 |
} finally { |
|
417 |
fireNavigationStopped(event); |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
return orig; |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
/** |
|
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
426 |
* Asynchronously navigates to the {@link Control} identified by the |
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
427 |
* given path. A wrapper around {@link #goTo(Control,Navigable...)} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
428 |
* that is run asynchronously using {@link #asyncExec}. |
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
429 |
*/ |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
430 |
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
|
431 |
asyncExec( |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
432 |
new Runnable() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
433 |
@Override |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
434 |
public void run() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
435 |
try { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
436 |
goTo(relativeTo, path); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
437 |
} catch (NavigationException ignore) { |
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 |
}); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
441 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
442 |
|
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
443 |
/** |
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
444 |
* Asynchronously navigates to the {@link Control} identified by the given |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
445 |
* path, returning only when navigation is complete. A wrapper around |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
446 |
* {@link #goTo(Control,Navigable...)} that is run asynchronously using |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
447 |
* {@link #asyncExecAndWait}. |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
448 |
*/ |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
449 |
public void goToAsyncAndWait( |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
450 |
final Control relativeTo, final Navigable... path) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
451 |
throws NavigationAbortedException, InvalidAddressException, |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
452 |
MissingParameterException, InvalidParameterException { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
453 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
454 |
asyncExecAndWait( |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
455 |
new NavRunnable() { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
456 |
@Override |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
457 |
public void run() |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
458 |
throws NavigationAbortedException, InvalidAddressException, |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
459 |
MissingParameterException, InvalidParameterException { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
460 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
461 |
goTo(relativeTo, path); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
462 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
463 |
}); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
464 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
465 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
466 |
/** |
0 | 467 |
* Shortcut for: |
468 |
* <p> |
|
469 |
* <code> |
|
470 |
* {@link #goTo(Control,Navigable...) goTo}(relativeTo, |
|
471 |
* new {@link SimpleNavigable#SimpleNavigable(String,Map) |
|
472 |
* SimpleNavigable}(id, {@link Navigable.Util#toMap |
|
473 |
* Navigable.Util.toMap}(parameters))); |
|
474 |
* </code> |
|
475 |
* </p> |
|
476 |
*/ |
|
477 |
public Control goTo(Control relativeTo, String id, |
|
40
d1412af1ae5b
3215 ActionAbortedException is used too broadly
Stephen Talley <stephen.talley@sun.com>
parents:
30
diff
changeset
|
478 |
String... parameters) throws NavigationAbortedException, |
0 | 479 |
InvalidAddressException, MissingParameterException, |
480 |
InvalidParameterException { |
|
481 |
||
482 |
return goTo(relativeTo, |
|
483 |
new SimpleNavigable(id, Navigable.Util.toMap(parameters))); |
|
484 |
} |
|
485 |
||
486 |
/** |
|
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
487 |
* A wrapper around {@link #goTo(Control,String,String...)} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
488 |
* that is run asynchronously using {@link #asyncExec}. |
20
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
489 |
*/ |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
490 |
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
|
491 |
final String... parameters) { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
492 |
asyncExec( |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
493 |
new Runnable() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
494 |
@Override |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
495 |
public void run() { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
496 |
try { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
497 |
goTo(relativeTo, id, parameters); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
498 |
} catch (NavigationException ignore) { |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
499 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
500 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
501 |
}); |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
502 |
} |
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
503 |
|
5423319ed064
2853 Async navigation in serious need of factoring
David Powell <David.Powell@sun.com>
parents:
19
diff
changeset
|
504 |
/** |
60
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
505 |
* A wrapper around {@link #goTo(Control,String,String...)} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
506 |
* that is run asynchronously using {@link #asyncExecAndWait}. |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
507 |
*/ |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
508 |
public void goToAsyncAndWait(final Control relativeTo, final String id, |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
509 |
final String... parameters) |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
510 |
throws NavigationAbortedException, InvalidAddressException, |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
511 |
MissingParameterException, InvalidParameterException { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
512 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
513 |
asyncExecAndWait( |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
514 |
new NavRunnable() { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
515 |
@Override |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
516 |
public void run() |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
517 |
throws NavigationAbortedException, InvalidAddressException, |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
518 |
MissingParameterException, InvalidParameterException { |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
519 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
520 |
goTo(relativeTo, id, parameters); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
521 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
522 |
}); |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
523 |
} |
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
524 |
|
9a179bed654e
3334 navigation should be run solely on navigation thread
Stephen Talley <stephen.talley@sun.com>
parents:
59
diff
changeset
|
525 |
/** |
0 | 526 |
* Navigate to the parent of the current {@link Control}. |
527 |
* |
|
528 |
* @return the previously current {@link Control} |
|
529 |
* |
|
40
d1412af1ae5b
3215 ActionAbortedException is used too broadly
Stephen Talley <stephen.talley@sun.com>
parents:
30
diff
changeset
|
530 |
* @exception NavigationAbortedException |
0 | 531 |
* if the navigation is vetoed |
532 |
* |
|
533 |
* @exception IllegalArgumentException |
|
534 |
* if the navigation stack consists only of the root element |
|
535 |
*/ |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
536 |
public Control goToParent() throws NavigationAbortedException { |
0 | 537 |
try { |
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
538 |
return goTo(getCurrentControl(), PendingControl.PARENT); |
0 | 539 |
} catch (InvalidAddressException ignore) { |
540 |
// Should not be possible |
|
541 |
} catch (InvalidParameterException ignore) { |
|
542 |
// Should not be possible |
|
543 |
} |
|
544 |
return null; |
|
545 |
} |
|
546 |
||
547 |
/** |
|
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
548 |
* Determines whether the current thread is the navigation dispatch thread. |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
549 |
* The navigation dispatch thread may change over time, but only one is |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
550 |
* alive at a time. |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
551 |
*/ |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
552 |
public boolean isDispatchThread() { |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
553 |
return Thread.currentThread() == dispatchThread; |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
554 |
} |
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
555 |
|
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
556 |
/** |
0 | 557 |
* Removes a {@link NavigationListener} from notification of when navigation |
558 |
* is stopped and started. |
|
559 |
*/ |
|
560 |
public boolean removeNavigationListener(NavigationListener l) { |
|
561 |
return listeners.remove(l); |
|
562 |
} |
|
563 |
||
564 |
// |
|
565 |
// Private methods |
|
566 |
// |
|
567 |
||
568 |
/** |
|
569 |
* Gets the relative path between the given absolute paths. |
|
570 |
* |
|
571 |
* @param fromPath |
|
572 |
* an absolute source path |
|
573 |
* |
|
574 |
* @param toPath |
|
575 |
* an absolute destination path |
|
576 |
* |
|
577 |
* @return a relative path from the current path to the given path |
|
578 |
*/ |
|
579 |
private List<Navigable> getRelativePath( |
|
580 |
List<? extends Navigable> fromPath, |
|
581 |
List<? extends Navigable> toPath) { |
|
582 |
||
583 |
List<Navigable> rPath = new ArrayList<Navigable>(); |
|
584 |
||
585 |
// Determine branching index |
|
586 |
int branch = 0; |
|
587 |
while (branch < fromPath.size() && branch < toPath.size() && |
|
588 |
Navigable.Util.equals(fromPath.get(branch), toPath.get(branch))) { |
|
589 |
||
590 |
branch++; |
|
591 |
} |
|
592 |
||
593 |
for (int i = fromPath.size() - 1; i >= branch; i--) { |
|
594 |
rPath.add(PendingControl.PARENT); |
|
595 |
} |
|
596 |
||
597 |
for (int i = branch; i < toPath.size(); i++) { |
|
598 |
rPath.add(toPath.get(i)); |
|
599 |
} |
|
600 |
||
601 |
return rPath; |
|
602 |
} |
|
603 |
||
604 |
/** |
|
605 |
* Removes each ".." segment and preceding non-".." segment the given path. |
|
606 |
*/ |
|
607 |
private void normalize(List<? extends HasId> path) { |
|
608 |
for (int i = 1; i < path.size(); i++) { |
|
609 |
if (path.get(i).getId().equals(PARENT_ID) && |
|
610 |
!path.get(i - 1).getId().equals(PARENT_ID)) { |
|
611 |
path.remove(i--); |
|
612 |
path.remove(i--); |
|
613 |
} |
|
614 |
} |
|
615 |
} |
|
616 |
||
617 |
// |
|
618 |
// Static methods |
|
619 |
// |
|
620 |
||
621 |
/** |
|
622 |
* Gets the given path as a {@code String}. |
|
623 |
*/ |
|
624 |
public static String getPathString(Collection<? extends Navigable> path) { |
|
625 |
StringBuffer buffer = new StringBuffer(); |
|
626 |
||
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
627 |
for (Navigable nav : path) { |
0 | 628 |
buffer.append(PATH_SEPARATOR).append(Control.encode( |
133
e1112f707f32
4258 navigator shouldn't navigate to root control in constructor
Stephen Talley <stephen.talley@sun.com>
parents:
68
diff
changeset
|
629 |
nav.getId(), nav.getParameters())); |
0 | 630 |
} |
631 |
||
632 |
return buffer.toString(); |
|
633 |
} |
|
634 |
||
635 |
/** |
|
636 |
* Determines whether the given path is absolute. If the given path starts |
|
637 |
* with PATH_SEPARATOR, it is considered absolute. |
|
638 |
*/ |
|
639 |
public static boolean isAbsolute(String path) { |
|
640 |
return path.startsWith(PATH_SEPARATOR); |
|
641 |
} |
|
642 |
||
643 |
public static SimpleNavigable[] toArray(String path) { |
|
644 |
path = path.replaceFirst( |
|
645 |
"^(" + Pattern.quote(PATH_SEPARATOR) + ")+", ""); |
|
646 |
||
647 |
String[] parts = path.split(PATH_SEPARATOR, 0); |
|
648 |
SimpleNavigable[] elements = new SimpleNavigable[parts.length]; |
|
649 |
||
650 |
for (int i = 0; i < parts.length; i++) { |
|
651 |
elements[i] = Control.decode(parts[i]); |
|
652 |
} |
|
653 |
||
654 |
return elements; |
|
655 |
} |
|
656 |
} |