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 | import com.framsticks.util.dispatching.RunAt; |
---|
22 | |
---|
23 | /** |
---|
24 | * @author Piotr Sniegowski |
---|
25 | */ |
---|
26 | public 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 | } |
---|