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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 5.0 KB
Line 
1package com.framsticks.gui;
2
3import com.framsticks.core.*;
4import com.framsticks.observers.Endpoint;
5import com.framsticks.observers.Observer;
6import com.framsticks.util.Logging;
7import com.framsticks.util.dispatching.Dispatcher;
8import com.framsticks.util.dispatching.Future;
9
10import org.apache.commons.configuration.Configuration;
11import org.apache.commons.lang.ArrayUtils;
12import org.apache.log4j.Logger;
13
14import javax.swing.*;
15
16import java.awt.Dimension;
17import java.util.ArrayList;
18import java.util.HashSet;
19import java.util.List;
20import java.util.Set;
21import com.framsticks.util.dispatching.RunAt;
22
23/**
24 * @author Piotr Sniegowski
25 */
26public class Browser extends Observer {
27
28        private static final Logger log = Logger.getLogger(Browser.class.getName());
29
30        protected final Set<Frame> frames = new HashSet<Frame>();
31        protected MainFrame mainFrame;
32        public List<PanelProvider> panelProviders = new ArrayList<PanelProvider>();
33        protected Dimension defaultFrameDimension;
34
35        public void addFrame(Frame frame) {
36                frames.add(frame);
37        }
38
39        public Browser() {
40                JPopupMenu.setDefaultLightWeightPopupEnabled(false);
41                addPanelProvider(new StandardPanelProvider());
42        }
43
44        @Override
45        public void configure(Configuration config) {
46                super.configure(config);
47
48                defaultFrameDimension = new Dimension(config.getInteger("size.width", 1000), config.getInteger("size.height", 500));
49
50                for (String name : config.getStringArray("panel_providers")) {
51                        try {
52                                Class<?> c = Class.forName(name);
53                                if (ArrayUtils.indexOf(c.getInterfaces(), PanelProvider.class) == -1) {
54                                        continue;
55                                }
56                                PanelProvider p = (PanelProvider)c.newInstance();
57                                addPanelProvider(p);
58                        } catch (Exception e) {
59                                log.error("failed to load PanelProvider " + name + ": " + e);
60                        }
61                }
62
63                // for (final String path : config.getStringArray("resolve_paths")) {
64                //      invokeLater()
65                //      autoResolvePath(path, new Future<Path>() {
66                //              @Override
67                //              public void result(Path p, Exception e) {
68                //                      Logging.log(log, "auto resolve path", path, e);
69                //              }
70                //      });
71                // }
72        }
73
74        public void addPanelProvider(PanelProvider panelProvider) {
75                log.debug("added panel provider of type: " + panelProvider.getClass().getCanonicalName());
76                panelProviders.add(panelProvider);
77        }
78
79        public void autoResolvePath(final String path, final Future<Path> future) {
80                final Instance i = endpoints.get("localhost").getInstance();
81                i.invokeLater(new RunAt<Instance>() {
82                        @Override
83                        public void run() {
84                                i.resolveAndFetch(path, new Future<Path>() {
85                                        @Override
86                                        public void result(final Path p, Exception e) {
87                                                Logging.log(log, "auto resolve path", path, e);
88                                                if (future != null) {
89                                                        future.result(p, e);
90                                                }
91                                                if (e == null) {
92                                                        mainFrame.invokeLater(new RunAt<Frame>() {
93                                                                @Override
94                                                                public void run() {
95                                                                        mainFrame.goTo(p);
96                                                                }
97                                                        });
98                                                }
99                                        }
100                                });
101                        }
102                });
103        }
104
105        public void clear() {
106                assert isActive();
107                for (Frame f : frames) {
108                        f.clear();
109                }
110        }
111
112        @Override
113        public void run() {
114                super.run();
115
116                assert isActive();
117
118                try {
119                        boolean found = false;
120                        // for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
121                        //      log.info("look and feel available: " + info.getName());
122                        //      if ("Nimbus".equals(info.getName())) {
123                        //              UIManager.setLookAndFeel(info.getClassName());
124                        //              found = true;
125                        //              break;
126                        //      }
127                        // }
128                        if (!found) {
129                                UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
130                        }
131                } catch (Exception ex) {
132                        log.warn("failed loading Look&Feel: ", ex);
133                }
134
135                javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
136
137                mainFrame = new MainFrame(Browser.this);
138                addFrame(mainFrame);
139
140                for (Frame f : frames) {
141                        f.configure();
142                }
143
144                for (final Endpoint e : getEndpoints().values()) {
145                        e.invokeLater(new RunAt<Instance>() {
146                                @Override
147                                public void run() {
148                                        final Path p = e.getInstance().getRootPath();
149                                        invokeLater(new RunAt<Browser>() {
150                                                @Override
151                                                public void run() {
152                                                        mainFrame.addRootPath((BrowserEndpoint) e, p);
153                                                }
154                                        });
155                                }
156                        });
157                }
158
159                for (Frame f : frames) {
160                        f.setVisible(true);
161                }
162
163                // autoResolvePath("/simulator/genepools/groups/0", null);
164                // autoResolvePath("/", null);
165        }
166
167        public void createTreeNodeForChild(final Path path) {
168                assert !isActive();
169                //assert instance.isActive();
170
171
172/*
173                final TreeNode parentTreeNode = (TreeNode) child.getParent().getUserObject();
174                if (parentTreeNode == null) {
175                        Dispatching.invokeDispatch(this, manager, new Runnable() {
176                                @Override
177                                public void run() {
178                                        createTreeNodeForChild(child);
179                                }
180                        });
181                        return;
182                }
183                log.debug(child.getClass().getSimpleName() + " created: " + child);
184
185
186                invokeLater(new Runnable() {
187                        @Override
188                        public void run() {
189                                parentTreeNode.getOrCreateChildTreeNodeFor(child);
190                        }
191                });
192*/
193        }
194
195
196        @Override
197        protected Endpoint createEndpoint() {
198                return new BrowserEndpoint();
199        }
200
201        @Override
202        public Dispatcher<Entity> createDefaultDispatcher() {
203                return SwingDispatcher.getInstance();
204        }
205
206        /**
207         * @return the mainFrame
208         */
209        public MainFrame getMainFrame() {
210                return mainFrame;
211        }
212
213}
Note: See TracBrowser for help on using the repository browser.