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

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

File size: 10.1 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.core.*;
7import com.framsticks.gui.console.Console;
8import com.framsticks.gui.console.TrackConsole;
9import com.framsticks.gui.table.ColumnsConfig;
10import com.framsticks.gui.table.ListPanelProvider;
11import com.framsticks.params.annotations.AutoAppendAnnotation;
12import com.framsticks.params.annotations.FramsClassAnnotation;
13import com.framsticks.params.annotations.ParamAnnotation;
14import com.framsticks.parsers.FileSource;
15import com.framsticks.remote.RemoteTree;
16import com.framsticks.util.FramsticksException;
17import com.framsticks.util.dispatching.AbstractJoinable;
18import com.framsticks.util.dispatching.Dispatcher;
19import com.framsticks.util.dispatching.Dispatching;
20import com.framsticks.util.dispatching.ExceptionResultHandler;
21import com.framsticks.util.dispatching.Future;
22import com.framsticks.util.dispatching.Joinable;
23import com.framsticks.util.dispatching.JoinableCollection;
24import com.framsticks.util.dispatching.JoinableParent;
25import com.framsticks.util.dispatching.JoinableState;
26
27import javax.swing.*;
28import javax.swing.filechooser.FileNameExtensionFilter;
29
30import org.apache.logging.log4j.Logger;
31import org.apache.logging.log4j.LogManager;
32
33import java.awt.Dimension;
34import java.awt.Toolkit;
35import java.awt.datatransfer.StringSelection;
36import java.awt.event.ActionEvent;
37import java.awt.event.ActionListener;
38import java.awt.event.WindowAdapter;
39import java.awt.event.WindowEvent;
40import java.io.IOException;
41import java.util.ArrayList;
42import java.util.LinkedList;
43import java.util.List;
44import java.util.regex.Matcher;
45import java.util.regex.Pattern;
46
47import com.framsticks.util.dispatching.RunAt;
48import com.framsticks.util.lang.Strings;
49
50/**
51 * @author Piotr Sniegowski
52 */
53@FramsClassAnnotation
54public 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}
Note: See TracBrowser for help on using the repository browser.