1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.core.*; |
---|
4 | import com.framsticks.gui.console.Console; |
---|
5 | import com.framsticks.gui.console.TrackConsole; |
---|
6 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
7 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
8 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
9 | import com.framsticks.remote.RemoteTree; |
---|
10 | import com.framsticks.util.FramsticksException; |
---|
11 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
12 | import com.framsticks.util.dispatching.Dispatcher; |
---|
13 | import com.framsticks.util.dispatching.Dispatching; |
---|
14 | import com.framsticks.util.dispatching.ExceptionResultHandler; |
---|
15 | import com.framsticks.util.dispatching.Future; |
---|
16 | import com.framsticks.util.dispatching.Joinable; |
---|
17 | import com.framsticks.util.dispatching.JoinableCollection; |
---|
18 | import com.framsticks.util.dispatching.JoinableParent; |
---|
19 | import com.framsticks.util.dispatching.JoinableState; |
---|
20 | |
---|
21 | import javax.swing.*; |
---|
22 | |
---|
23 | import org.apache.log4j.Logger; |
---|
24 | |
---|
25 | import java.awt.Dimension; |
---|
26 | import java.awt.Toolkit; |
---|
27 | import java.awt.datatransfer.StringSelection; |
---|
28 | import java.awt.event.ActionEvent; |
---|
29 | import java.util.ArrayList; |
---|
30 | import java.util.LinkedList; |
---|
31 | import java.util.List; |
---|
32 | import com.framsticks.util.dispatching.RunAt; |
---|
33 | |
---|
34 | /** |
---|
35 | * @author Piotr Sniegowski |
---|
36 | */ |
---|
37 | @FramsClassAnnotation |
---|
38 | public class Browser extends AbstractJoinable implements Dispatcher<Browser>, JoinableParent, ExceptionResultHandler { |
---|
39 | |
---|
40 | private static final Logger log = Logger.getLogger(Browser.class); |
---|
41 | |
---|
42 | protected final JoinableCollection<Frame> frames = new JoinableCollection<Frame>().setObservableName("frames"); |
---|
43 | protected final JoinableCollection<Tree> trees = new JoinableCollection<Tree>().setObservableName("trees"); |
---|
44 | protected final JoinableCollection<Console> consoles = new JoinableCollection<Console>().setObservableName("consoles"); |
---|
45 | |
---|
46 | protected final List<PopupMenuEntryProvider> popupMenuEntryProviders = new LinkedList<>(); |
---|
47 | // protected final SwingDispatcher |
---|
48 | |
---|
49 | protected final MainFrame mainFrame; |
---|
50 | protected final List<PanelProvider> panelProviders = new ArrayList<PanelProvider>(); |
---|
51 | protected Dimension defaultFrameDimension; |
---|
52 | |
---|
53 | String name; |
---|
54 | |
---|
55 | public void addFrame(Frame frame) { |
---|
56 | frames.add(frame); |
---|
57 | } |
---|
58 | |
---|
59 | public Browser() { |
---|
60 | setName("browser"); |
---|
61 | JPopupMenu.setDefaultLightWeightPopupEnabled(false); |
---|
62 | addPanelProvider(new StandardPanelProvider()); |
---|
63 | |
---|
64 | mainFrame = new MainFrame(Browser.this); |
---|
65 | |
---|
66 | // mainFrame.getStatusBar().setExceptionHandler(ThrowExceptionHandler.getInstance()); |
---|
67 | |
---|
68 | addFrame(mainFrame); |
---|
69 | |
---|
70 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
71 | @Override |
---|
72 | public void provide(JPopupMenu menu, Path path) { |
---|
73 | menu.add(new JMenuItem(path.getFullTextual())); |
---|
74 | menu.addSeparator(); |
---|
75 | } |
---|
76 | }); |
---|
77 | |
---|
78 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
79 | @SuppressWarnings("serial") |
---|
80 | @Override |
---|
81 | public void provide(JPopupMenu menu, final Path path) { |
---|
82 | menu.add(new AbstractAction("Copy path to clipboard") { |
---|
83 | @Override |
---|
84 | public void actionPerformed(ActionEvent e) { |
---|
85 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(path.getFullTextual()), null); |
---|
86 | } |
---|
87 | }); |
---|
88 | } |
---|
89 | }); |
---|
90 | |
---|
91 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
92 | @SuppressWarnings("serial") |
---|
93 | @Override |
---|
94 | public void provide(JPopupMenu menu, final Path path) { |
---|
95 | if (!(path.getTree() instanceof RemoteTree)) { |
---|
96 | return; |
---|
97 | } |
---|
98 | final RemoteTree remoteTree = (RemoteTree) path.getTree(); |
---|
99 | menu.add(new AbstractAction("Open tracking console") { |
---|
100 | @Override |
---|
101 | public void actionPerformed(ActionEvent e) { |
---|
102 | consoles.add(new TrackConsole().setConnection(remoteTree.getConnection())); |
---|
103 | } |
---|
104 | }); |
---|
105 | } |
---|
106 | }); |
---|
107 | // addNodeActionToTreePopupMenu("", new NodeAction() ) |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | @AutoAppendAnnotation |
---|
112 | public void addPanelProvider(PanelProvider panelProvider) { |
---|
113 | log.debug("added panel provider of type: " + panelProvider.getClass().getCanonicalName()); |
---|
114 | panelProviders.add(panelProvider); |
---|
115 | } |
---|
116 | |
---|
117 | @AutoAppendAnnotation |
---|
118 | public void addPopupMenuEntryProvider(PopupMenuEntryProvider popupMenuEntryProvider) { |
---|
119 | popupMenuEntryProviders.add(popupMenuEntryProvider); |
---|
120 | } |
---|
121 | |
---|
122 | @AutoAppendAnnotation |
---|
123 | public void addTree(Tree tree) { |
---|
124 | log.info("adding tree: " + tree); |
---|
125 | tree.setDispatcher(new SwingDispatcher<Tree>()); |
---|
126 | trees.add(tree); |
---|
127 | } |
---|
128 | |
---|
129 | public void autoResolvePath(final String path, final Future<Path> future) { |
---|
130 | // final Tree i = trees.get("localhost"); |
---|
131 | // i.dispatch(new RunAt<Tree>(future) { |
---|
132 | // @Override |
---|
133 | // protected void runAt() { |
---|
134 | // TreeOperations.tryGet(i, path, new FutureHandler<Path>(future) { |
---|
135 | // @Override |
---|
136 | // protected void result(final Path p) { |
---|
137 | // future.pass(p); |
---|
138 | // mainFrame.dispatch(new RunAt<Frame>(future) { |
---|
139 | // @Override |
---|
140 | // protected void runAt() { |
---|
141 | // mainFrame.goTo(p); |
---|
142 | // } |
---|
143 | // }); |
---|
144 | // } |
---|
145 | // }); |
---|
146 | // } |
---|
147 | // }); |
---|
148 | } |
---|
149 | |
---|
150 | public void clear() { |
---|
151 | assert isActive(); |
---|
152 | for (Frame f : frames) { |
---|
153 | f.clear(); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | public void createTreeNodeForChild(final Path path) { |
---|
158 | assert !isActive(); |
---|
159 | //assert tree.isActive(); |
---|
160 | |
---|
161 | /* |
---|
162 | final TreeNode parentTreeNode = (TreeNode) child.getParent().getUserObject(); |
---|
163 | if (parentTreeNode == null) { |
---|
164 | Dispatching.invokeDispatch(this, manager, new Runnable() { |
---|
165 | @Override |
---|
166 | public void run() { |
---|
167 | createTreeNodeForChild(child); |
---|
168 | } |
---|
169 | }); |
---|
170 | return; |
---|
171 | } |
---|
172 | log.debug(child.getClass().getSimpleName() + " created: " + child); |
---|
173 | |
---|
174 | |
---|
175 | invokeLater(new Runnable() { |
---|
176 | @Override |
---|
177 | public void run() { |
---|
178 | parentTreeNode.getOrCreateChildTreeNodeFor(child); |
---|
179 | } |
---|
180 | }); |
---|
181 | */ |
---|
182 | } |
---|
183 | |
---|
184 | @Override |
---|
185 | protected void joinableStart() { |
---|
186 | Dispatching.use(frames, this); |
---|
187 | Dispatching.use(trees, this); |
---|
188 | Dispatching.use(consoles, this); |
---|
189 | |
---|
190 | dispatch(new RunAt<Browser>(this) { |
---|
191 | @Override |
---|
192 | protected void runAt() { |
---|
193 | |
---|
194 | for (final Tree i : trees) { |
---|
195 | i.dispatch(new RunAt<Tree>(this) { |
---|
196 | @Override |
---|
197 | protected void runAt() { |
---|
198 | final Path p = Path.to(i, "/"); |
---|
199 | dispatch(new RunAt<Browser>(this) { |
---|
200 | @Override |
---|
201 | protected void runAt() { |
---|
202 | mainFrame.addRootPath(p); |
---|
203 | } |
---|
204 | }); |
---|
205 | } |
---|
206 | }); |
---|
207 | } |
---|
208 | } |
---|
209 | }); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * @return the tree |
---|
214 | */ |
---|
215 | public JoinableCollection<Tree> getTrees() { |
---|
216 | return trees; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * @return the mainFrame |
---|
221 | */ |
---|
222 | public MainFrame getMainFrame() { |
---|
223 | return mainFrame; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * @return the name |
---|
228 | */ |
---|
229 | @ParamAnnotation |
---|
230 | public String getName() { |
---|
231 | return name; |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * @param name the name to set |
---|
236 | */ |
---|
237 | @ParamAnnotation |
---|
238 | public void setName(String name) { |
---|
239 | this.name = name; |
---|
240 | } |
---|
241 | |
---|
242 | @Override |
---|
243 | public boolean isActive() { |
---|
244 | return SwingDispatcher.getInstance().isActive(); |
---|
245 | } |
---|
246 | |
---|
247 | @Override |
---|
248 | public void dispatch(RunAt<? extends Browser> runnable) { |
---|
249 | SwingDispatcher.getInstance().dispatch(runnable); |
---|
250 | } |
---|
251 | |
---|
252 | @Override |
---|
253 | protected void joinableJoin() throws InterruptedException { |
---|
254 | Dispatching.join(frames); |
---|
255 | Dispatching.join(trees); |
---|
256 | Dispatching.join(consoles); |
---|
257 | // super.join(); |
---|
258 | } |
---|
259 | |
---|
260 | @Override |
---|
261 | protected void joinableInterrupt() { |
---|
262 | Dispatching.drop(consoles, this); |
---|
263 | Dispatching.drop(frames, this); |
---|
264 | Dispatching.drop(trees, this); |
---|
265 | } |
---|
266 | |
---|
267 | @Override |
---|
268 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
269 | if (joinable == frames) { |
---|
270 | proceedToState(state); |
---|
271 | } |
---|
272 | |
---|
273 | } |
---|
274 | |
---|
275 | @Override |
---|
276 | protected void joinableFinish() { |
---|
277 | |
---|
278 | } |
---|
279 | |
---|
280 | @Override |
---|
281 | public String toString() { |
---|
282 | return getName(); |
---|
283 | } |
---|
284 | |
---|
285 | @Override |
---|
286 | public void handle(FramsticksException exception) { |
---|
287 | mainFrame.handle(exception); |
---|
288 | } |
---|
289 | |
---|
290 | // final protected Map<EventParam, Subscription<?>> userSubscriptions = new HashMap<>(); |
---|
291 | // public boolean hasSubscribed(EventParam param) { |
---|
292 | // assert frame.isActive(); |
---|
293 | // return userSubscriptions.containsKey(param); |
---|
294 | // } |
---|
295 | |
---|
296 | // public void unsubscribe(EventParam eventParam) { |
---|
297 | // assert frame.isActive(); |
---|
298 | // if (!hasSubscribed(eventParam)) { |
---|
299 | // log.error("could not unsubscribe from " + eventParam); |
---|
300 | // return; |
---|
301 | // } |
---|
302 | // userSubscriptions.get(eventParam).unsubscribe(new LoggingStateCallback(log, "unsubscribed " + eventParam)); |
---|
303 | // userSubscriptions.remove(eventParam); |
---|
304 | // } |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | } |
---|