View Javadoc

1   package org.catacomb.druid.build;
2   
3   import org.catacomb.druid.gui.base.DruAssemblyPanel;
4   import org.catacomb.druid.market.HookupBoard;
5   import org.catacomb.interlish.report.PrintProgressReport;
6   import org.catacomb.interlish.structure.*;
7   import org.catacomb.report.E;
8   
9   
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  
14  
15  public class DruidTargetStore implements TargetStore {
16  
17  
18      HashMap<String, Object> hashMap;
19  
20      HashMap<String, Object> shortHM;
21  
22      private HookupBoard hookupBoard;
23  
24      InfoReceiver infoReceiver;
25  
26  
27      public DruidTargetStore() {
28          hashMap = new HashMap<String, Object>();
29          shortHM = new HashMap<String, Object>();
30      }
31  
32  
33  
34      HashMap<String, Object> getHashMap() {
35          return hashMap;
36      }
37  
38  
39      public HashMap<String, Object> getIdentifiedComponentMap() {
40          return hashMap;
41      }
42  
43  
44  
45      public SaverLoader getModel() {
46          // TODO needed?
47          return null;
48      }
49  
50  
51  
52  
53      public void addComponent(String id, Object cpt) {
54          if (hashMap.containsKey(id)) {
55              E.warning("duplicate target ids: " + id + " " + hashMap.get(id) + "  and " + cpt);
56          }
57          hashMap.put(id, cpt);
58  
59          String ss = id.substring(id.lastIndexOf(".")  + 1, id.length());
60          if (shortHM.containsKey(ss)) {
61              E.warning("duplicates in shortHM " + ss + " must use full path");
62          } else {
63              shortHM.put(ss, cpt);
64          }
65      }
66  
67  
68  
69      private Object quietGet(String id) {
70          Object ret = null;
71  
72          if (id.indexOf(".") < 0) {
73              ret = shortHM.get(id);
74  
75  
76          } else if (id.startsWith("*")) {
77              ret = wildcardGet(id);
78          } else {
79              if (hashMap.containsKey(id)) {
80                  ret = hashMap.get(id);
81              }
82          }
83          return ret;
84      }
85  
86  
87      public Object get(String id) {
88          Object ret = quietGet(id);
89  
90          if (ret == null) {
91              //    E.error("target store - no such item " + id);
92  
93          }
94          return ret;
95      }
96  
97  
98      public void printAvailable() {
99          if (hashMap == null || hashMap.size() == 0) {
100             E.info("there are no entries in the target store");
101         } else {
102             for (Map.Entry<String, Object> ment : hashMap.entrySet()) {
103                 E.info("known item : " + ment.getKey() + " " + ment.getValue());
104             }
105         }
106     }
107 
108 
109     public boolean has(String id) {
110         Object obj = quietGet(id);
111         return (obj != null);
112     }
113 
114 
115 
116     // make this smarter!!!!!!! EFF
117     private Object wildcardGet(String s) {
118         Object ret = null;
119 
120         if (s.startsWith("*.")) {
121             // bit sloppy - ignore the dot, maybe should still check for duplicate matches;
122             ret = quietGet(s.substring(2, s.length()));
123         }
124 
125         if (ret == null) {
126             String sm = s.substring(1, s.length());
127 
128             String fm = null;
129             for (String key : hashMap.keySet()) {
130                 if (key.endsWith(sm)) {
131                     if (fm == null) {
132                         fm = key;
133                         ret = hashMap.get(key);
134                     } else {
135                         E.error("multiple items match wildcard " + s);
136                         E.info("first=" + fm);
137                         E.info("also: " + key);
138                     }
139                 }
140             }
141         }
142         return ret;
143     }
144 
145 
146 
147 
148     public void setStringValue(String id, String value) {
149         Object obj = get(id);
150         if (obj instanceof StringValueSettable) {
151             ((StringValueSettable)obj).setStringValue(value);
152         } else {
153             E.error("cant set " + value + " in " + id);
154         }
155     }
156 
157     public void setObjectValue(String id, Object val) {
158         Object obj = get(id);
159         if (obj  instanceof ObjectValueSettable) {
160             ((ObjectValueSettable)obj).setObjectValue(val);
161 
162         } else {
163             E.error("cant set " + val + " in " + id);
164         }
165 
166     }
167 
168 
169 
170     public ProgressReport getProgressReport(String id) {
171         ProgressReport ret = null;
172         Object obj = get(id);
173         if (obj != null && obj instanceof ProgressReport) {
174             ret = (ProgressReport)obj;
175 
176         } else {
177             E.error("cant get progress report " + id + " (got " + obj + ")");
178             ret = new PrintProgressReport();
179         }
180         return ret;
181     }
182 
183 
184 
185 
186     public PopulableMenu getSubMenu(String id) {
187         Object obj = get(id);
188         PopulableMenu ret = null;
189 
190         if (obj instanceof PopulableMenu) {
191             ret = (PopulableMenu)obj;
192 
193         } else {
194             E.error("cant get subMenu " + id + " (got " + obj + ")");
195         }
196         return ret;
197     }
198 
199 
200     public Button getButton(String id) {
201         Object obj = get(id);
202         Button ret = null;
203 
204         if (obj instanceof Button) {
205             ret = (Button)obj;
206 
207         } else {
208             E.error("cant get button " + id + " (got " + obj + ")");
209         }
210         return ret;
211     }
212 
213 
214 
215     public AssemblyEditor getAssemblyEditor() {
216         DruAssemblyPanel ret = null;
217 
218         for (Object obj : hashMap.values()) {
219             if (obj instanceof DruAssemblyPanel) {
220                 ret = (DruAssemblyPanel)obj;
221                 break;
222             }
223         }
224 
225         if (ret == null) {
226             E.error("no assembly panel in target store");
227         }
228         return ret;
229     }
230 
231 
232 
233     public void setHookupBoard(HookupBoard hb) {
234         hookupBoard = hb;
235 
236         hb.addVisible("TargetStore", this, "access");
237     }
238 
239 
240     public Marketplace getMarketplace() {
241         return hookupBoard;
242     }
243 
244 
245 
246     public HookupBoard getHoookupBoard() {
247         return hookupBoard;
248     }
249 
250 
251 
252     public void clear() {
253         hashMap.clear();
254     }
255 
256 
257 
258     public Dialog getDialog(String s) {
259         Object obj = get(s);
260 
261         Dialog ret = null;
262         if (obj instanceof Dialog) {
263             ret = (Dialog)obj;
264         } else {
265             E.error("wrong type " + s);
266         }
267 
268         return ret;
269     }
270 
271 
272 
273     public TextField getTextField(String s) {
274         Object obj = get(s);
275 
276         TextField ret = null;
277         if (obj instanceof TextField) {
278             ret = (TextField)obj;
279         } else {
280             E.error("wrong type " + s);
281         }
282 
283         return ret;
284     }
285 
286 
287 
288     public TextArea getTextArea(String s) {
289         Object obj = get(s);
290 
291         TextArea ret = null;
292         if (obj instanceof TextArea) {
293             ret = (TextArea)obj;
294         } else {
295             E.error("wrong type " + s);
296         }
297 
298         return ret;
299     }
300 
301 
302 
303     public Choice getChoice(String s) {
304         Object obj = get(s);
305 
306         Choice ret = null;
307         if (obj instanceof Choice) {
308             ret = (Choice)obj;
309         } else {
310             E.error("wrong type " + s);
311         }
312 
313         return ret;
314     }
315 
316 
317 
318     public Toggle getToggle(String s) {
319         Object obj = get(s);
320 
321         Toggle ret = null;
322         if (obj instanceof Toggle) {
323             ret = (Toggle)obj;
324         } else {
325             E.error("wrong type " + s);
326         }
327 
328         return ret;
329     }
330 
331 
332     public void setInfoReceiver(InfoReceiver ir) {
333         infoReceiver = ir;
334     }
335 
336     public InfoReceiver getInfoReceiver() {
337         return infoReceiver;
338     }
339 
340 
341 
342     public StringValueEditor getStringValueEditor(String sid) {
343         Object obj = get(sid);
344         StringValueEditor ret = null;
345         if (obj instanceof StringValueEditor) {
346             ret = (StringValueEditor)obj;
347         } else {
348             E.error("wrong type item " + sid + " - " + obj);
349         }
350 
351         return ret;
352     }
353 
354     public IntegerValueEditor getIntegerValueEditor(String sid) {
355         Object obj = get(sid);
356         IntegerValueEditor ret = null;
357         if (obj instanceof IntegerValueEditor) {
358             ret = (IntegerValueEditor)obj;
359         } else {
360             E.error("wrong type item " + sid + " - " + obj);
361         }
362 
363         return ret;
364     }
365 
366 
367 
368     public DoubleValueEditor getDoubleValueEditor(String sid) {
369         Object obj = get(sid);
370         DoubleValueEditor ret = null;
371         if (obj instanceof DoubleValueEditor) {
372             ret = (DoubleValueEditor)obj;
373         } else {
374             E.error("wrong type item " + sid + " - " + obj);
375         }
376 
377         return ret;
378     }
379 
380     public ColorValueEditor getColorValueEditor(String sid) {
381         Object obj = get(sid);
382         ColorValueEditor ret = null;
383         if (obj instanceof ColorValueEditor) {
384             ret = (ColorValueEditor)obj;
385         } else {
386             E.error("wrong type item " + sid + " - " + obj);
387         }
388 
389         return ret;
390     }
391 
392 
393 }