View Javadoc

1   package org.catacomb.druid.build;
2   
3   
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import org.catacomb.druid.gui.base.DruActionRelay;
9   import org.catacomb.druid.gui.base.DruPanel;
10  import org.catacomb.druid.gui.base.PanelWrapper;
11  import org.catacomb.interlish.service.AppPersist;
12  import org.catacomb.interlish.service.ResourceAccess;
13  import org.catacomb.interlish.service.ResourceLoader;
14  import org.catacomb.interlish.structure.*;
15  import org.catacomb.interlish.util.JUtil;
16  import org.catacomb.report.E;
17  import org.catacomb.util.PathUtil;
18  
19  
20  
21  public class Druid implements Portal {
22  
23      String configPath;
24  
25  
26      Object rootComponent;
27      FrameShowable frameShowable;
28  
29      GUIStore guiStore;
30  
31      Controller baseController;
32      Context parentContext;
33  
34      Context context;
35  
36  
37      public Druid(String cp) {
38          configPath = cp;
39  
40      }
41  
42  
43      public Druid(String cp, Context ctx) {
44          this(cp);
45          parentContext = ctx;
46      }
47  
48  
49      public void whizzBang() {
50          buildGUI();
51          if (rootComponent instanceof ControllerSpecifier) {
52              selfActivate();
53          }
54          packShow();
55  
56          context.getMarketplace().logUnresolved();
57  
58      }
59  
60  
61      public void whizzNoBang() {
62          buildGUI();
63          if (rootComponent instanceof ControllerSpecifier) {
64              selfActivate();
65          }
66      }
67  
68  
69  
70  
71  
72      public void buildGUI() {
73          // parse the config into an abstract representation of the gui
74          ResourceLoader resourceLoader = ResourceAccess.getResourceLoader();
75  
76          if (configPath.startsWith("com.") || configPath.startsWith("org.")) {
77              // presumably an absolute path;
78          } else {
79              if (parentContext != null) {
80                  configPath = parentContext.getSourceLocation() + "." + configPath;
81  
82              } else {
83                  // requies resource loader to be able to find it - ie, the resource was
84                  //  there when the manifest was built;
85              }
86          }
87  
88          Object obj = resourceLoader.getResource(configPath, null);
89  
90          if (obj == null) {
91              E.error("druid cant find config " + configPath);
92          }
93  
94          // parent context null for main frame;
95          context = new Context(parentContext);
96          context.setSourceLocation(PathUtil.getPackage(configPath));
97  
98          if (obj instanceof Realizer) {
99              rootComponent = ((Realizer)obj).realize(context, new GUIPath());
100 
101 
102             if (rootComponent instanceof FrameShowable) {
103                 frameShowable = (FrameShowable)rootComponent;
104             }
105 
106         } else {
107             E.fatalError("config does not define an application: " + obj + " " + configPath);
108             AppPersist.forceExit();
109         }
110         // give access to this somehow TODO
111         // context.dumpIDs();
112 
113 
114 
115         guiStore = context.getGUIStore();
116     }
117 
118 
119 
120     public Object getRootComponent() {
121         return rootComponent;
122     }
123 
124 
125     public DruPanel getMainPanel() {
126         DruPanel ret = null;
127         if (rootComponent instanceof DruPanel) {
128             ret = (DruPanel)rootComponent;
129 
130         } else if (rootComponent instanceof PanelWrapper) {
131             ret = ((PanelWrapper)rootComponent).getPanel();
132 
133         } else {
134             E.error("cant get panel - root=" + rootComponent + " " + baseController);
135         }
136         return ret;
137     }
138 
139 
140     public Controller getController() {
141         return baseController;
142     }
143 
144 
145     public FrameShowable getFrameShowable() {
146         return frameShowable;
147     }
148 
149 
150     public void packShow() {
151         pack();
152         show();
153     }
154 
155 
156     public void pack() {
157         if (frameShowable != null) {
158             frameShowable.pack();
159         }
160     }
161 
162 
163     public void show() {
164         if (frameShowable != null) {
165             frameShowable.show();
166         }
167     }
168 
169 
170 
171     public void hide() {
172         if (frameShowable != null) {
173             frameShowable.hide();
174         }
175     }
176 
177 
178     public void show(Object focusObject) {
179         if (baseController != null && baseController instanceof Targetable) {
180             ((Targetable)baseController).setTarget(focusObject);
181 
182         } else {
183             E.error("cant show " + focusObject + " with " + baseController);
184         }
185         show();
186     }
187 
188 
189 
190     private void addController(ArrayList<Controller> controllers, Controller ctrl) {
191         if (ctrl != null) {
192             controllers.add(ctrl);
193 
194             if (baseController == null) {
195                 baseController = ctrl;
196             }
197         }
198         if (ctrl instanceof RootController) {
199             for (Controller coctrl : ((RootController)ctrl).getCoControllers()) {
200                 addController(controllers, coctrl);
201             }
202         }
203     }
204 
205 
206 
207     public void selfActivate() {
208         if (rootComponent instanceof ControllerSpecifier) {
209             ArrayList<Controller> controllers = new ArrayList<Controller>();
210 
211             ControllerSpecifier cspec = (ControllerSpecifier)rootComponent;
212 
213             String path = cspec.getControllerPath();
214             if (path != null && path.length() > 1) {
215                 if (path.indexOf(".") < 0) {
216                     path = PathUtil.getPackage(configPath) + "." + path;
217                 }
218 
219                 Controller ctrl = (Controller)(JUtil.newInstance(path));
220                 baseController = ctrl;
221                 addController(controllers, ctrl);
222             }
223 
224 
225             if (controllers.size() == 0) {
226                 E.warning("No controllers found in druid selfActivate");
227 
228 
229             } else if (controllers.size() == 1) {
230                 attachSingleController(controllers.get(0));
231 
232             } else {
233                 attachMultipleControllers(controllers);
234             }
235 
236         } else {
237             E.warning("druid cant self-activate " + rootComponent);
238         }
239 
240         // used to log unresolved here - done in app now? but others?
241 
242     }
243 
244 
245 
246     public void attachSingleController(Controller ctrl) {
247         if (ctrl instanceof RootController) {
248             E.error("cant attach root controller this way");
249         }
250         baseController = ctrl;
251 
252         if (ctrl instanceof InfoExporter) {
253             ((InfoExporter)ctrl).setInfoReceiver(guiStore.getInfoReceiver());
254         }
255 
256         if (guiStore == null) {
257             E.warning("no guiStore for controller? " + ctrl);
258 
259         } else {
260             AnnotationConnector anoc = new AnnotationConnector(guiStore.getTargetStore());
261 
262             anoc.annotationConnect(ctrl);
263 
264             if (ctrl instanceof Marketeer) {
265                 ((Marketeer)ctrl).setMarketplace(guiStore.getMarketplace());
266             }
267 
268             DruActionRelay relay = new DruActionRelay(ctrl);
269 
270             for (Object obj : guiStore.getComponents()) {
271                 if (obj instanceof ActionSource) {
272                     ((ActionSource)obj).setActionRelay(relay);
273                 }
274             }
275         }
276 
277 
278         ctrl.attached();
279     }
280 
281 
282 
283 
284     public void setID(String id) {
285         if (rootComponent instanceof IDable) {
286             ((IDable)rootComponent).setID(id);
287         } else {
288             E.error("cant set id on " + rootComponent);
289         }
290     }
291 
292 
293 
294     public void attachMultipleControllers(ArrayList<Controller> acc) {
295 
296         HashMap<String, ActionRelay> pathRelays = new HashMap<String, ActionRelay>();
297 
298         ActionRelay wcRelay = null;
299 
300 
301         AnnotationConnector anoc = new AnnotationConnector(guiStore.getTargetStore());
302         for (Controller ctrl : acc) {
303             anoc.annotationConnect(ctrl);
304 
305             if (ctrl instanceof Marketeer) {
306                 ((Marketeer)ctrl).setMarketplace(guiStore.getMarketplace());
307             }
308 
309             if (ctrl instanceof InfoExporter) {
310                 ((InfoExporter)ctrl).setInfoReceiver(guiStore.getInfoReceiver());
311             }
312 
313             ActionRelay acr = new DruActionRelay(ctrl);
314 
315             String ss = "*";
316             if (ctrl instanceof GUISourced) {
317                 ss = ((GUISourced)ctrl).getGUISources();
318             }
319             if (ss.trim().equals("*")) {
320                 if (wcRelay == null) {
321                     wcRelay = acr;
322                 } else {
323                     E.error("duplicate wildcard controllers " + ctrl);
324                 }
325 
326             } else {
327                 for (String pth : Scope.getFullPaths(ss)) {
328                     pathRelays.put(pth, acr);
329                 }
330             }
331         }
332 
333 
334 
335         HashMap<String, ArrayList<Object>> hmanon = guiStore.getAnonymousComponentMap();
336         HashMap<String, Object> hmid = guiStore.getIdentifiedComponentMap();
337 
338         for (Map.Entry<String, Object> me : hmid.entrySet()) {
339             Object val = me.getValue();
340 
341             if (val instanceof ActionSource) {
342                 ActionRelay ar = bestRelay(me.getKey(), pathRelays, wcRelay);
343                 ((ActionSource)val).setActionRelay(ar);
344             }
345         }
346 
347 
348 
349         for (Map.Entry<String, ArrayList<Object>> me : hmanon.entrySet()) {
350             ActionRelay ar = bestRelay(me.getKey(), pathRelays, wcRelay);
351 
352             for (Object val : me.getValue()) {
353                 if (val instanceof ActionSource) {
354                     ((ActionSource)val).setActionRelay(ar);
355                 }
356             }
357         }
358 
359         for (Controller ctrl : acc) {
360             ctrl.attached();
361         }
362     }
363 
364 
365 
366 
367     // REFAC could be improved;
368     private ActionRelay bestRelay(String path, HashMap<String, ActionRelay> map, ActionRelay wcRelay) {
369         ActionRelay ret = null;
370 
371         if (map.containsKey(path)) {
372             ret = map.get(path);
373             // E.info("got exact relay for " + path);
374         } else {
375             int ilm = 0;
376             for (String sp : map.keySet()) {
377                 if (path.startsWith(sp) && sp.length() > ilm) {
378                     ilm = sp.length();
379                     ret = map.get(sp);
380                     // E.info("considering relay " + sp + " for " + path);
381                 }
382             }
383         }
384 
385         if (ret == null && wcRelay != null) {
386             ret = wcRelay;
387             // E.info("falling back on wildcard for " + wcRelay);
388         }
389 
390         //   E.info("best relay for " + path + " is " + ret);
391 
392         return ret;
393     }
394 
395 
396     public void setModal(boolean b) {
397         if (rootComponent instanceof Dialog) {
398             ((Dialog)rootComponent).setModal(b);
399 
400         } else {
401             E.error("cant set modal on " + rootComponent.getClass().getName());
402         }
403     }
404 
405     public boolean isShowing() {
406         boolean ret = false;
407         if (rootComponent instanceof Dialog) {
408             ret = ((Dialog)rootComponent).isShowing();
409         } else {
410             E.missing();
411         }
412         return ret;
413     }
414 
415 
416     public int[] getXY() {
417         int[] ret = {100, 100};
418         if (frameShowable != null) {
419             ret = frameShowable.getLocation();
420         } else if (rootComponent instanceof Dialog) {
421             ret = ((Dialog)rootComponent).getLocation();
422         } else if (rootComponent instanceof PanelWrapper) {
423             ret = ((PanelWrapper)rootComponent).getPanel().getLocation();
424         } else {
425             E.warning("cant get position for " + rootComponent);
426         }
427         return ret;
428     }
429 
430 
431 }