1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.communication.File; |
---|
4 | import com.framsticks.communication.queries.NeedFile; |
---|
5 | import com.framsticks.communication.queries.NeedFileAcceptor; |
---|
6 | import com.framsticks.core.*; |
---|
7 | import com.framsticks.gui.console.Console; |
---|
8 | import com.framsticks.gui.console.TrackConsole; |
---|
9 | import com.framsticks.gui.table.ColumnsConfig; |
---|
10 | import com.framsticks.gui.table.ListPanelProvider; |
---|
11 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
12 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
13 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
14 | import com.framsticks.parsers.FileSource; |
---|
15 | import com.framsticks.remote.RemoteTree; |
---|
16 | import com.framsticks.util.FramsticksException; |
---|
17 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
18 | import com.framsticks.util.dispatching.Dispatcher; |
---|
19 | import com.framsticks.util.dispatching.Dispatching; |
---|
20 | import com.framsticks.util.dispatching.ExceptionResultHandler; |
---|
21 | import com.framsticks.util.dispatching.Future; |
---|
22 | import com.framsticks.util.dispatching.Joinable; |
---|
23 | import com.framsticks.util.dispatching.JoinableCollection; |
---|
24 | import com.framsticks.util.dispatching.JoinableParent; |
---|
25 | import com.framsticks.util.dispatching.JoinableState; |
---|
26 | |
---|
27 | import javax.swing.*; |
---|
28 | import javax.swing.filechooser.FileNameExtensionFilter; |
---|
29 | |
---|
30 | import org.apache.logging.log4j.Logger; |
---|
31 | import org.apache.logging.log4j.LogManager; |
---|
32 | |
---|
33 | import java.awt.Dimension; |
---|
34 | import java.awt.Toolkit; |
---|
35 | import java.awt.datatransfer.StringSelection; |
---|
36 | import java.awt.event.ActionEvent; |
---|
37 | import java.awt.event.ActionListener; |
---|
38 | import java.awt.event.WindowAdapter; |
---|
39 | import java.awt.event.WindowEvent; |
---|
40 | import java.io.IOException; |
---|
41 | import java.util.ArrayList; |
---|
42 | import java.util.LinkedList; |
---|
43 | import java.util.List; |
---|
44 | import java.util.regex.Matcher; |
---|
45 | import java.util.regex.Pattern; |
---|
46 | |
---|
47 | import com.framsticks.util.dispatching.RunAt; |
---|
48 | import com.framsticks.util.lang.Strings; |
---|
49 | |
---|
50 | /** |
---|
51 | * @author Piotr Sniegowski |
---|
52 | */ |
---|
53 | @FramsClassAnnotation |
---|
54 | public class Browser extends AbstractJoinable implements Dispatcher<Browser>, JoinableParent, ExceptionResultHandler { |
---|
55 | |
---|
56 | private static final Logger log = LogManager.getLogger(Browser.class); |
---|
57 | |
---|
58 | protected final JoinableCollection<Frame> frames = new JoinableCollection<Frame>().setObservableName("frames"); |
---|
59 | protected final JoinableCollection<Tree> trees = new JoinableCollection<Tree>().setObservableName("trees"); |
---|
60 | protected final JoinableCollection<Console> consoles = new JoinableCollection<Console>().setObservableName("consoles"); |
---|
61 | |
---|
62 | protected final List<PopupMenuEntryProvider> popupMenuEntryProviders = new LinkedList<>(); |
---|
63 | // protected final SwingDispatcher |
---|
64 | |
---|
65 | protected final MainFrame mainFrame; |
---|
66 | protected final List<PanelProvider> panelProviders = new ArrayList<PanelProvider>(); |
---|
67 | protected Dimension defaultFrameDimension; |
---|
68 | |
---|
69 | String name; |
---|
70 | |
---|
71 | public void addFrame(Frame frame) { |
---|
72 | frames.add(frame); |
---|
73 | } |
---|
74 | |
---|
75 | protected final StandardPanelProvider standardPanelProvider; |
---|
76 | protected final ListPanelProvider listPanelProvider; |
---|
77 | |
---|
78 | public Browser() { |
---|
79 | setName("browser"); |
---|
80 | JPopupMenu.setDefaultLightWeightPopupEnabled(false); |
---|
81 | addPanelProvider(standardPanelProvider = new StandardPanelProvider()); |
---|
82 | addPanelProvider(listPanelProvider = new ListPanelProvider()); |
---|
83 | |
---|
84 | mainFrame = new MainFrame(Browser.this); |
---|
85 | |
---|
86 | // mainFrame.getStatusBar().setExceptionHandler(ThrowExceptionHandler.getInstance()); |
---|
87 | |
---|
88 | addFrame(mainFrame); |
---|
89 | |
---|
90 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
91 | @Override |
---|
92 | public void provide(JPopupMenu menu, Path path) { |
---|
93 | menu.add(new JMenuItem(path.getFullTextual())); |
---|
94 | menu.addSeparator(); |
---|
95 | } |
---|
96 | }); |
---|
97 | |
---|
98 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
99 | @SuppressWarnings("serial") |
---|
100 | @Override |
---|
101 | public void provide(JPopupMenu menu, final Path path) { |
---|
102 | menu.add(new AbstractAction("Copy path to clipboard") { |
---|
103 | @Override |
---|
104 | public void actionPerformed(ActionEvent e) { |
---|
105 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(path.getFullTextual()), null); |
---|
106 | } |
---|
107 | }); |
---|
108 | } |
---|
109 | }); |
---|
110 | |
---|
111 | addPopupMenuEntryProvider(new PopupMenuEntryProvider() { |
---|
112 | @SuppressWarnings("serial") |
---|
113 | @Override |
---|
114 | public void provide(JPopupMenu menu, final Path path) { |
---|
115 | if (!(path.getTree() instanceof RemoteTree)) { |
---|
116 | return; |
---|
117 | } |
---|
118 | final RemoteTree remoteTree = (RemoteTree) path.getTree(); |
---|
119 | menu.add(new AbstractAction("Open tracking console") { |
---|
120 | @Override |
---|
121 | public void actionPerformed(ActionEvent e) { |
---|
122 | consoles.add(new TrackConsole().setConnection(remoteTree.getConnection())); |
---|
123 | } |
---|
124 | }); |
---|
125 | } |
---|
126 | }); |
---|
127 | // addNodeActionToTreePopupMenu("", new NodeAction() ) |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | @AutoAppendAnnotation |
---|
132 | public void addPanelProvider(PanelProvider panelProvider) { |
---|
133 | log.debug("added panel provider of type: {}", panelProvider.getClass().getCanonicalName()); |
---|
134 | panelProviders.add(panelProvider); |
---|
135 | } |
---|
136 | |
---|
137 | @AutoAppendAnnotation |
---|
138 | public void addColumnsConfig(ColumnsConfig columnsConfig) { |
---|
139 | listPanelProvider.addColumnsConfig(columnsConfig); |
---|
140 | } |
---|
141 | |
---|
142 | @AutoAppendAnnotation |
---|
143 | public void addPopupMenuEntryProvider(PopupMenuEntryProvider popupMenuEntryProvider) { |
---|
144 | popupMenuEntryProviders.add(popupMenuEntryProvider); |
---|
145 | } |
---|
146 | |
---|
147 | protected static final Pattern extensionFilterPattern = Pattern.compile("\\*\\.(\\S+)"); |
---|
148 | |
---|
149 | @AutoAppendAnnotation |
---|
150 | public void addTree(final Tree tree) { |
---|
151 | log.debug("adding tree: {}", tree); |
---|
152 | tree.setDispatcher(new SwingDispatcher<Tree>()); |
---|
153 | tree.setExceptionHandler(this); |
---|
154 | trees.add(tree); |
---|
155 | |
---|
156 | final NeedFileAcceptor acceptor = new NeedFileAcceptor() { |
---|
157 | |
---|
158 | protected boolean done = false; |
---|
159 | |
---|
160 | @Override |
---|
161 | public boolean acceptNeed(final NeedFile needFile) { |
---|
162 | final JFileChooser chooser = new JFileChooser(); |
---|
163 | final JFrame frame = new JFrame(); |
---|
164 | |
---|
165 | frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
---|
166 | |
---|
167 | frame.addWindowListener(new WindowAdapter() { |
---|
168 | @Override |
---|
169 | public void windowClosing(WindowEvent e) { |
---|
170 | if (!done) { |
---|
171 | needFile.getFuture().handle(new FramsticksException().msg("user closed the window")); |
---|
172 | } |
---|
173 | frame.setVisible(false); |
---|
174 | frame.dispose(); |
---|
175 | } |
---|
176 | }); |
---|
177 | |
---|
178 | frame.setTitle(Strings.toStringEmptyProof(needFile.getDescription(), "Choose file")); |
---|
179 | chooser.setMultiSelectionEnabled(false); |
---|
180 | Matcher matcher = extensionFilterPattern.matcher(needFile.getSuggestedName()); |
---|
181 | if (matcher.matches()) { |
---|
182 | chooser.setFileFilter(new FileNameExtensionFilter(Strings.toStringEmptyProof(needFile.getDescription(), "file"), Strings.takeGroup(needFile.getSuggestedName(), matcher, 1).toString())); |
---|
183 | } |
---|
184 | |
---|
185 | frame.getContentPane().add(chooser); |
---|
186 | |
---|
187 | chooser.addActionListener(new ActionListener() { |
---|
188 | |
---|
189 | @Override |
---|
190 | public void actionPerformed(ActionEvent event) { |
---|
191 | if (event.getActionCommand().equals("CancelSelection")) { |
---|
192 | needFile.getFuture().handle(new FramsticksException().msg("user cancelled choose")); |
---|
193 | frame.setVisible(false); |
---|
194 | frame.dispose(); |
---|
195 | } |
---|
196 | if (event.getActionCommand().equals("ApproveSelection")) { |
---|
197 | File file = null; |
---|
198 | String filename = chooser.getSelectedFile().getAbsolutePath(); |
---|
199 | try { |
---|
200 | file = new File("", new FileSource(filename)); |
---|
201 | } catch (IOException e) { |
---|
202 | needFile.getFuture().handle(new FramsticksException().msg("failed to open choosed file").arg("filename", filename).cause(e)); |
---|
203 | } |
---|
204 | if (file != null) { |
---|
205 | done = true; |
---|
206 | needFile.getFuture().pass(file); |
---|
207 | } |
---|
208 | frame.setVisible(false); |
---|
209 | frame.dispose(); |
---|
210 | } |
---|
211 | } |
---|
212 | }); |
---|
213 | frame.setVisible(true); |
---|
214 | return true; |
---|
215 | } |
---|
216 | }; |
---|
217 | |
---|
218 | tree.dispatch(new RunAt<Tree>(this) { |
---|
219 | @Override |
---|
220 | protected void runAt() { |
---|
221 | log.debug("adding need file acceptor: {}", acceptor); |
---|
222 | tree.addNeedFileAcceptor(Integer.MAX_VALUE, acceptor); |
---|
223 | } |
---|
224 | }); |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | public void autoResolvePath(final String path, final Future<Path> future) { |
---|
229 | // final Tree i = trees.get("localhost"); |
---|
230 | // i.dispatch(new RunAt<Tree>(future) { |
---|
231 | // @Override |
---|
232 | // protected void runAt() { |
---|
233 | // TreeOperations.tryGet(i, path, new FutureHandler<Path>(future) { |
---|
234 | // @Override |
---|
235 | // protected void result(final Path p) { |
---|
236 | // future.pass(p); |
---|
237 | // mainFrame.dispatch(new RunAt<Frame>(future) { |
---|
238 | // @Override |
---|
239 | // protected void runAt() { |
---|
240 | // mainFrame.goTo(p); |
---|
241 | // } |
---|
242 | // }); |
---|
243 | // } |
---|
244 | // }); |
---|
245 | // } |
---|
246 | // }); |
---|
247 | } |
---|
248 | |
---|
249 | public void clear() { |
---|
250 | assert isActive(); |
---|
251 | for (Frame f : frames) { |
---|
252 | f.clear(); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | @Override |
---|
257 | protected void joinableStart() { |
---|
258 | Dispatching.use(frames, this); |
---|
259 | Dispatching.use(trees, this); |
---|
260 | Dispatching.use(consoles, this); |
---|
261 | |
---|
262 | dispatch(new RunAt<Browser>(this) { |
---|
263 | @Override |
---|
264 | protected void runAt() { |
---|
265 | |
---|
266 | for (final Tree i : trees) { |
---|
267 | i.dispatch(new RunAt<Tree>(this) { |
---|
268 | @Override |
---|
269 | protected void runAt() { |
---|
270 | final Path p = Path.to(i, "/"); |
---|
271 | log.debug("adding path: {}", p); |
---|
272 | dispatch(new RunAt<Browser>(this) { |
---|
273 | @Override |
---|
274 | protected void runAt() { |
---|
275 | mainFrame.addRootPath(p); |
---|
276 | } |
---|
277 | }); |
---|
278 | } |
---|
279 | }); |
---|
280 | } |
---|
281 | } |
---|
282 | }); |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * @return the tree |
---|
287 | */ |
---|
288 | public JoinableCollection<Tree> getTrees() { |
---|
289 | return trees; |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * @return the mainFrame |
---|
294 | */ |
---|
295 | public MainFrame getMainFrame() { |
---|
296 | return mainFrame; |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * @return the name |
---|
301 | */ |
---|
302 | @ParamAnnotation |
---|
303 | public String getName() { |
---|
304 | return name; |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * @param name the name to set |
---|
309 | */ |
---|
310 | @ParamAnnotation |
---|
311 | public void setName(String name) { |
---|
312 | this.name = name; |
---|
313 | } |
---|
314 | |
---|
315 | @Override |
---|
316 | public boolean isActive() { |
---|
317 | return SwingDispatcher.getInstance().isActive(); |
---|
318 | } |
---|
319 | |
---|
320 | @Override |
---|
321 | public void dispatch(RunAt<? extends Browser> runnable) { |
---|
322 | SwingDispatcher.getInstance().dispatch(runnable); |
---|
323 | } |
---|
324 | |
---|
325 | @Override |
---|
326 | protected void joinableJoin() throws InterruptedException { |
---|
327 | Dispatching.join(frames); |
---|
328 | Dispatching.join(trees); |
---|
329 | Dispatching.join(consoles); |
---|
330 | // super.join(); |
---|
331 | } |
---|
332 | |
---|
333 | @Override |
---|
334 | protected void joinableInterrupt() { |
---|
335 | Dispatching.drop(consoles, this); |
---|
336 | Dispatching.drop(frames, this); |
---|
337 | Dispatching.drop(trees, this); |
---|
338 | } |
---|
339 | |
---|
340 | @Override |
---|
341 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
342 | if (joinable == frames) { |
---|
343 | proceedToState(state); |
---|
344 | } |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | @Override |
---|
349 | protected void joinableFinish() { |
---|
350 | |
---|
351 | } |
---|
352 | |
---|
353 | @Override |
---|
354 | public String toString() { |
---|
355 | return getName(); |
---|
356 | } |
---|
357 | |
---|
358 | @Override |
---|
359 | public void handle(FramsticksException exception) { |
---|
360 | mainFrame.handle(exception); |
---|
361 | } |
---|
362 | |
---|
363 | } |
---|