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