source: java/main/src/main/java/com/framsticks/gui/Browser.java @ 105

Last change on this file since 105 was 105, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • import refactorization: move Tree, Path, etc.

from core to structure package

  • initial serialization implementation
  • improve PrimeExperiment? test
  • many organizational changes and convenience improvements

CHANGELOG:
Make registry in AbstractTree? final.

Move most classes from core to structure package.

Minor changes.

Switch names of Future and FutureHandler?.

Rename ExceptionResultHandler? to ExceptionHandler?.

Rename ExceptionHandler? to ExceptionDispatcherHandler?.

Fix bug in ParamCandidate? cache.

Add missing synchronization to the BufferedDispatcher?.

Develop @Serialized support.

Rework serialization further.

Add serialization/deserialization interface to ValueParam?.

Move getStorageType and isNumeric from Param down to params hierarchy.

Minor changes.

Improve param type induction.

Add TestSerializedClass? for testing new serialization.

Add info files gor GenePool? and Population.

Add standard.expt exemplary netfile.

Add type name field to PropertiesObject?.

Use PropertiesObject? for PropertiesAccess? instead of ordinary map.

Hide getFramsClass is several more places.

More unification accross FramsClass?, Access and Path.

Add ParamCollection?.

Simplify interface for getting params from FramsClass?, Access
or Path.

Make Access.call() interface variadic.

Add arguments(args) convenience wrapper around new Object[] {args}.

Upgrade to apache.commons.lang version 3.1

Minor improvement with Response constructors.

Develop proper result printing in ClientAtServer?.

Add experimentNetsave to PrimeExperiment?.

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