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

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

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

File size: 5.9 KB
Line 
1package com.framsticks.gui;
2
3import com.framsticks.core.*;
4import com.framsticks.params.annotations.AutoAppendAnnotation;
5import com.framsticks.params.annotations.FramsClassAnnotation;
6import com.framsticks.params.annotations.ParamAnnotation;
7import com.framsticks.util.dispatching.AbstractJoinable;
8import com.framsticks.util.dispatching.Dispatcher;
9import com.framsticks.util.dispatching.Dispatching;
10import com.framsticks.util.dispatching.Future;
11import com.framsticks.util.dispatching.Joinable;
12import com.framsticks.util.dispatching.JoinableCollection;
13import com.framsticks.util.dispatching.JoinableParent;
14import com.framsticks.util.dispatching.JoinableState;
15
16import javax.swing.*;
17
18import org.apache.log4j.Logger;
19
20import java.awt.Dimension;
21import java.util.ArrayList;
22import java.util.List;
23import com.framsticks.util.dispatching.RunAt;
24
25/**
26 * @author Piotr Sniegowski
27 */
28@FramsClassAnnotation
29public class Browser extends AbstractJoinable implements Dispatcher<Browser>, JoinableParent {
30
31        private static final Logger log = Logger.getLogger(Browser.class.getName());
32
33        protected JoinableCollection<Frame> frames = new JoinableCollection<Frame>().setObservableName("frames");
34        protected JoinableCollection<Instance> instances = new JoinableCollection<Instance>().setObservableName("instances");
35
36        protected MainFrame mainFrame;
37        public List<PanelProvider> panelProviders = new ArrayList<PanelProvider>();
38        protected Dimension defaultFrameDimension;
39
40        String name;
41
42        public void addFrame(Frame frame) {
43                frames.add(frame);
44        }
45
46        public Browser() {
47                setName("browser");
48                JPopupMenu.setDefaultLightWeightPopupEnabled(false);
49                addPanelProvider(new StandardPanelProvider());
50
51                mainFrame = new MainFrame(Browser.this);
52                addFrame(mainFrame);
53        }
54
55        @AutoAppendAnnotation
56        public void addPanelProvider(PanelProvider panelProvider) {
57                log.debug("added panel provider of type: " + panelProvider.getClass().getCanonicalName());
58                panelProviders.add(panelProvider);
59        }
60
61        @AutoAppendAnnotation
62        public void addInstance(Instance instance) {
63                log.info("adding instance: " + instance);
64                instances.add(instance);
65        }
66
67        public void autoResolvePath(final String path, final Future<Path> future) {
68                final Instance i = instances.get("localhost");
69                i.dispatch(new RunAt<Instance>() {
70                        @Override
71                        public void run() {
72                                InstanceUtils.resolveAndFetch(i, path, new Future<Path>(future) {
73                                        @Override
74                                        protected void result(final Path p) {
75                                                future.pass(p);
76                                                mainFrame.dispatch(new RunAt<Frame>() {
77                                                        @Override
78                                                        public void run() {
79                                                                mainFrame.goTo(p);
80                                                        }
81                                                });
82                                        }
83                                });
84                        }
85                });
86        }
87
88        public void clear() {
89                assert isActive();
90                for (Frame f : frames) {
91                        f.clear();
92                }
93        }
94
95        protected void initializeGUI() {
96                assert isActive();
97                log.debug("executing first task");
98
99                try {
100                        boolean found = false;
101                        // for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
102                        //      log.info("look and feel available: " + info.getName());
103                        //      if ("Nimbus".equals(info.getName())) {
104                        //              UIManager.setLookAndFeel(info.getClassName());
105                        //              found = true;
106                        //              break;
107                        //      }
108                        // }
109                        if (!found) {
110                                UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
111                        }
112                } catch (Exception ex) {
113                        log.warn("failed loading Look&Feel: ", ex);
114                }
115
116                javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
117
118
119                for (Frame f : frames) {
120                        f.configure();
121                }
122
123                for (final Instance i : instances) {
124                        i.dispatch(new RunAt<Instance>() {
125                                @Override
126                                public void run() {
127                                        final Path p = Path.to(i, "/");
128                                        dispatch(new RunAt<Browser>() {
129                                                @Override
130                                                public void run() {
131                                                        mainFrame.addRootPath(p);
132                                                }
133                                        });
134                                }
135                        });
136                }
137
138                for (Frame f : frames) {
139                        f.getSwing().setVisible(true);
140                }
141
142                // autoResolvePath("/simulator/genepools/groups/0", null);
143                // autoResolvePath("/", null);
144        }
145
146        public void createTreeNodeForChild(final Path path) {
147                assert !isActive();
148                //assert instance.isActive();
149
150                /*
151                 final TreeNode parentTreeNode = (TreeNode) child.getParent().getUserObject();
152                 if (parentTreeNode == null) {
153                 Dispatching.invokeDispatch(this, manager, new Runnable() {
154                 @Override
155                 public void run() {
156                 createTreeNodeForChild(child);
157                 }
158                 });
159                 return;
160                 }
161                 log.debug(child.getClass().getSimpleName() + " created: " + child);
162
163
164                 invokeLater(new Runnable() {
165                 @Override
166                 public void run() {
167                 parentTreeNode.getOrCreateChildTreeNodeFor(child);
168                 }
169                 });
170                 */
171        }
172
173        @Override
174        protected void joinableStart() {
175                Dispatching.use(frames, this);
176                Dispatching.use(instances, this);
177
178                dispatch(new RunAt<Browser>() {
179                        @Override
180                        public void run() {
181                                initializeGUI();
182                        }
183                });
184        }
185
186        /**
187         * @return the instances
188         */
189        public JoinableCollection<Instance> getInstances() {
190                return instances;
191        }
192
193        /**
194         * @return the mainFrame
195         */
196        public MainFrame getMainFrame() {
197                return mainFrame;
198        }
199
200        /**
201         * @return the name
202         */
203        @ParamAnnotation
204        public String getName() {
205                return name;
206        }
207
208        /**
209         * @param name the name to set
210         */
211        @ParamAnnotation
212        public void setName(String name) {
213                this.name = name;
214        }
215
216        @Override
217        public boolean isActive() {
218                return SwingDispatcher.getInstance().isActive();
219        }
220
221        @Override
222        public void dispatch(RunAt<? extends Browser> runnable) {
223                SwingDispatcher.getInstance().dispatch(runnable);
224        }
225
226        @Override
227        protected void joinableJoin() throws InterruptedException {
228                Dispatching.join(frames);
229                Dispatching.join(instances);
230                // super.join();
231        }
232
233        @Override
234        protected void joinableInterrupt() {
235                Dispatching.drop(frames, this);
236                Dispatching.drop(instances, this);
237        }
238
239        @Override
240        public void childChangedState(Joinable joinable, JoinableState state) {
241                if (joinable == frames) {
242                        proceedToState(state);
243                }
244
245        }
246
247        @Override
248        protected void joinableFinish() {
249
250        }
251
252        @Override
253        public String toString() {
254                return getName();
255        }
256
257
258        // @Override
259        // public boolean isDone() {
260        //      return frames.isDone() && instances.isDone();
261        // }
262}
Note: See TracBrowser for help on using the repository browser.