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