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