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