KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > generic > option > GenericOptions


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.generic.option.GenericOptions
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.generic.option;
20
21 import de.gulden.framework.amoda.generic.behaviour.*;
22 import de.gulden.framework.amoda.generic.core.*;
23 import de.gulden.framework.amoda.generic.core.GenericFeature;
24 import de.gulden.framework.amoda.generic.data.*;
25 import de.gulden.framework.amoda.model.data.*;
26 import de.gulden.framework.amoda.model.option.*;
27 import de.gulden.framework.amoda.model.option.Options;
28 import de.gulden.util.Toolbox;
29 import java.awt.*;
30 import java.io.*;
31 import java.lang.*;
32 import java.util.*;
33
34 /**
35  * Class GenericOptions.
36  *
37  * @author Jens Gulden
38  * @version snapshot-beautyj-1.1
39  */

40 public class GenericOptions extends GenericFeature implements Cloneable JavaDoc, Options {
41
42     // ------------------------------------------------------------------------
43
// --- fields ---
44
// ------------------------------------------------------------------------
45

46     public static String JavaDoc[] OPTION_ELEMENTS = {"option","options-group"};
47
48     public static Object JavaDoc[][] XML_COLLECTION_ELEMENTS = {{"all",OPTION_ELEMENTS}};
49
50     protected GenericApplicationMemberAbstract parentMember;
51
52
53     // ------------------------------------------------------------------------
54
// --- methods ---
55
// ------------------------------------------------------------------------
56

57     public OptionEntry getOptionEntry(String JavaDoc id) {
58         try {
59             OptionEntry entry = (OptionEntry)get(id);
60             if (entry!=null) {
61                 return entry;
62             }
63         } catch (ClassCastException JavaDoc cce) {
64             // fallthrough
65
}
66         throw new IllegalOptionError(id);
67     }
68
69     public OptionsGroup getOptionsGroup(String JavaDoc id) {
70         return (OptionsGroup)get(id);
71     }
72
73     public String JavaDoc getString(String JavaDoc name) {
74         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
75         de.gulden.framework.amoda.model.data.Value v=e.getValue();
76         if (v!=null) {
77             return v.getString();
78         } else {
79             return null;
80         }
81     }
82
83     public int getInt(String JavaDoc name) {
84         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
85         de.gulden.framework.amoda.model.data.Value v=e.getValue();
86         if (v!=null) {
87             return v.getInt();
88         } else {
89             return -1;
90         }
91     }
92
93     public float getFloat(String JavaDoc name) {
94         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
95         de.gulden.framework.amoda.model.data.Value v=e.getValue();
96         if (v!=null) {
97             return v.getFloat();
98         } else {
99             return -1.0f;
100         }
101     }
102
103     public double getDouble(String JavaDoc name) {
104         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
105         de.gulden.framework.amoda.model.data.Value v=e.getValue();
106         if (v!=null) {
107             return v.getDouble();
108         } else {
109             return -1.0;
110         }
111     }
112
113     public boolean getBoolean(String JavaDoc name) {
114         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
115         de.gulden.framework.amoda.model.data.Value v=e.getValue();
116         if (v!=null) {
117             return v.getBoolean();
118         } else {
119             return false;
120         }
121     }
122
123     public Object JavaDoc getObject(String JavaDoc name) {
124         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
125         de.gulden.framework.amoda.model.data.Value v=e.getValue();
126         if (v!=null) {
127             return v.get();
128         } else {
129             return null;
130         }
131     }
132
133     public File getFile(String JavaDoc name) {
134         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
135         de.gulden.framework.amoda.model.data.Value v=e.getValue();
136         if (v!=null) {
137             return v.getFile();
138         } else {
139             return null;
140         }
141     }
142
143     public Color getColor(String JavaDoc name) {
144         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
145         de.gulden.framework.amoda.model.data.Value v=e.getValue();
146         if (v!=null) {
147             return v.getColor();
148         } else {
149             return null;
150         }
151     }
152
153     public void addAll(Options options) {
154         addAll(options,false);
155     }
156
157     public void addAll(Options options, boolean addInFront) {
158         for (Iterator it=options.getEntries().values().iterator();it.hasNext();) {
159             de.gulden.framework.amoda.model.option.Option option=(de.gulden.framework.amoda.model.option.Option)it.next();
160             add(option);
161         }
162         for (Iterator it=options.getGroups().values().iterator();it.hasNext();) {
163             de.gulden.framework.amoda.model.option.Option option=(de.gulden.framework.amoda.model.option.Option)it.next();
164             add(option);
165         }
166     }
167
168     public boolean isValid() {
169         // your code here
170
return true;
171     }
172
173     public CompositeGroup getParent() {
174         GenericApplicationMemberAbstract parentMember=getParentMember();
175         if (parentMember instanceof CompositeGroup) {
176             return (CompositeGroup)parentMember;
177         } else {
178             if (parentMember != null) {
179                 return parentMember.getParent();
180             } else {
181                 return null;
182             }
183         }
184     }
185
186     public Map getGroups() {
187         // this re-implements getGroups in order to simulate a recursive structure on options based on the structure of feature-groups
188
Map h=new de.gulden.util.OrderedHashMap();
189         GenericApplicationMemberAbstract parentMember=getParentMember();
190         if (parentMember instanceof de.gulden.framework.amoda.model.core.Feature) {
191             de.gulden.framework.amoda.model.core.Feature parentFeature=(de.gulden.framework.amoda.model.core.Feature)parentMember;
192             Map siblingMembers=parentFeature.getAll();
193             for (Iterator it=siblingMembers.values().iterator();it.hasNext();) {
194                 Object JavaDoc o=it.next();
195                 Class JavaDoc t=o.getClass();
196                 // allow only feature-groups and features, NOT subclasses like commands, conditions etc.
197
// (to commands etc, inner options are local and are invisible here)
198
if ((t==de.gulden.framework.amoda.generic.core.GenericFeature.class)
199                    ||(t==de.gulden.framework.amoda.generic.core.GenericFeatureGroup.class))
200                    {
201                     de.gulden.framework.amoda.generic.core.GenericFeature feature=(de.gulden.framework.amoda.generic.core.GenericFeature)o;
202                     GenericOptions options=(GenericOptions)feature.getOptions();
203                     if (options!=null) {
204                         options.setId(feature.getId()); // fake same id as feature-group, <options> don't have their own id anyway
205
h.put(options.getId(),options);
206                     }
207                 }
208             }
209         }
210         h.putAll(Toolbox.filterMapOnType(super.getAll(),GenericOptions.class)); // for directly added options-groups
211
return h;
212     }
213
214     public Map getAll() {
215         // needs to be re-implemented, too in order to incorporate groups
216
Map h=new de.gulden.util.OrderedHashMap();
217         h.putAll(getGroups());
218         h.putAll(getEntries());
219         // (make sure all members have a valid reference to a parent)
220
for (Iterator it = h.values().iterator(); it.hasNext(); ) {
221             Object JavaDoc o = it.next();
222             if (o instanceof CompositeMember) {
223                 CompositeMember m = (CompositeMember)o;
224                 if (m.getParent()==null) {
225                     m.setParent(this);
226                 }
227             }
228         }
229         return h;
230     }
231
232     public GenericOptions flattenHierarchy(int maxDepth, GenericCondition cond) {
233         // ************ UNTESTED ****************
234
if (countMaxDepth(cond)<=maxDepth) {
235             return this;
236         } else {
237             GenericOptions opt=new GenericOptions();
238             opt.setParent(this.getParent());
239             for (Iterator it=getAll().values().iterator();it.hasNext();) {
240                 Object JavaDoc o=it.next();
241                 if (cond.test(o)) {
242                     if (o instanceof GenericOptions) {
243                         GenericOptions gOpt=((GenericOptions)o).flattenHierarchy(maxDepth,cond);
244                         gOpt=(GenericOptions)gOpt.clone(); // allow changing title
245
String JavaDoc optTitle=opt.findTitle();
246                         ((de.gulden.framework.amoda.generic.metadata.GenericMetadata)gOpt.getMetadata()).set("title",optTitle+(optTitle.length()>0?" / ":"")+gOpt.findTitle());
247                         opt.add(gOpt);
248                     } else {
249                         opt.add((de.gulden.framework.amoda.model.data.CompositeMember)o);
250                     }
251                 }
252             }
253             return opt;
254         }
255     }
256
257     public GenericApplicationMemberAbstract getParentMember() {
258         return parentMember;
259     }
260
261     public void setParentMember(GenericApplicationMemberAbstract _parentMember) {
262         parentMember = _parentMember;
263     }
264
265     public Object JavaDoc clone() {
266         try {
267             return super.clone();
268         } catch (CloneNotSupportedException JavaDoc cnse) {
269             return null;
270         }
271     }
272
273     public Map getEntries() {
274         return Toolbox.filterMapOnType(super.getAll(),GenericOptionEntry.class);
275     }
276
277     public String JavaDoc findTitle() {
278         String JavaDoc title=de.gulden.framework.amoda.generic.metadata.GenericMetadata.findTitle(this);
279         if ((title==null)||(title.startsWith(Toolbox.capitalize(GenericApplicationMemberAbstract.AUTO_ID_PREFIX)))) {
280             // no actual title: is <options> inside a member
281
GenericApplicationMemberAbstract parent=getParentMember();
282             if (parent!=null) {
283                 return parent.findTitle();
284             } else {
285                 return "";
286             }
287         } else {
288             return title;
289         }
290     }
291
292     public void setParent(CompositeGroup parent) {
293         // overwrites super.setParent
294
//super.setParent(parent); *** NOETIG?
295
// if a Feature is parent, then it is also the parent member (but not the other way round):
296
setParentMember((GenericApplicationMemberAbstract)parent);
297     }
298
299     public void set(String JavaDoc id, Object JavaDoc value) {
300         GenericOptionEntry o=(GenericOptionEntry)getOptionEntry(id);
301         ((GenericValue)o.getValue(GenericOptionEntry.STATE_CURRENT)).set(value);
302     }
303
304     public void loadFromProperties(File file) {
305         // assumes that this is the main application options
306
try {
307             Properties props=new Properties();
308             java.io.FileInputStream JavaDoc f=new java.io.FileInputStream JavaDoc(file);
309             props.load(f);
310             f.close();
311             for (Enumeration e=props.keys();e.hasMoreElements();) {
312                 String JavaDoc id=(String JavaDoc)e.nextElement();
313                 String JavaDoc value=props.getProperty(id);
314                 try {
315                     GenericOptionEntry o = findOptionEntryByShortcut(id); // id might actually be shortcut-name, return null if not found
316
if (o == null) {
317                         o = (GenericOptionEntry)getOptionEntry(id); // throws error if not found
318
}
319                     ((de.gulden.framework.amoda.generic.data.GenericValue)o.getValue(STATE_SAVED)).parseString(value);
320                 } catch (IllegalOptionError ioe) {
321                     getApplication().log("Unknown option name in properties file: "+id+" (will be ignored)");
322                 }
323             }
324         } catch (java.io.IOException JavaDoc ioe) {
325             getApplication().log("properties file '"+file.getAbsolutePath()+"' not found - using defaults");
326         }
327     }
328
329     public void saveToProperties(File file) {
330         // assumes that this is the main application options
331
try {
332             Properties props=new Properties();
333             java.io.FileOutputStream JavaDoc f=new java.io.FileOutputStream JavaDoc(file);
334             for (Iterator it=this.getAll(GenericOptionEntry.class,true).values().iterator();it.hasNext();) {
335                 GenericOptionEntry entry=(GenericOptionEntry)it.next();
336                 String JavaDoc id=entry.getQualifiedId();
337                 de.gulden.framework.amoda.model.data.Value value=entry.getValue(STATE_CURRENT);
338                 if (value!=null) {
339                     String JavaDoc s=value.getString();
340                     if (s!=null) {
341                         props.put(id,s);
342                         entry.shiftValue(STATE_CURRENT,STATE_SAVED);
343                     }
344                 }
345             }
346             props.store(f,"AMODA application options");
347             f.close();
348         } catch (java.io.IOException JavaDoc ioe) {
349             getApplication().error("Cannot save properties file.",ioe);
350         }
351     }
352
353     public void saveGlobalProperties() {
354         boolean persistentOptions = getApplication().getOptions().getBoolean(GenericApplicationEnvironment.CONFIGURATION_PERSISTENT_OPTIONS);
355         if (persistentOptions) {
356             java.io.File JavaDoc file=getApplication().getOptions().getFile("properties");
357             if (file!=null) {
358                 saveToProperties(file);
359             } else {
360                 getApplication().log("configuration properties not saved - no filename configured");
361             }
362         }
363     }
364
365     public String JavaDoc getQualifiedId() {
366         // overwrites GenericApplicationMemberAbstract.getQualifiedId
367
de.gulden.framework.amoda.generic.core.GenericApplicationMemberAbstract parent=(de.gulden.framework.amoda.generic.core.GenericApplicationMemberAbstract)getParentMember();
368         if ((parent instanceof GenericOptions)&&(parent!=getApplication().getOptions())) {
369             return parent.getQualifiedId()+"."+this.getId();
370         } else { // top-most option or option-group, parent feature names are not included
371
return this.getId();
372         }
373     }
374
375     public void shiftAllValues(int from, int to) {
376         Map map=getAll(GenericOptionEntry.class,true);
377         for (Iterator it=map.values().iterator();it.hasNext();) {
378             GenericOptionEntry entry=(GenericOptionEntry)it.next();
379             entry.shiftValue(from, to);
380         }
381     }
382
383     public Class JavaDoc getClassType(String JavaDoc name) {
384         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
385         de.gulden.framework.amoda.model.data.Value v=e.getValue();
386         if (v!=null) {
387             return v.getClassType();
388         } else {
389             return null;
390         }
391     }
392
393     public Object JavaDoc getClassInstance(String JavaDoc name) {
394         de.gulden.framework.amoda.model.option.OptionEntry e=getOptionEntry(name);
395         de.gulden.framework.amoda.model.data.Value v=e.getValue();
396         if (v!=null) {
397             return v.getClassInstance();
398         } else {
399             return null;
400         }
401     }
402
403     public GenericOptionEntry findOptionEntryByShortcut(String JavaDoc shortcut) {
404         Map all = getAll();
405         for (Iterator it = all.values().iterator(); it.hasNext(); ) {
406             GenericFeature o = (GenericFeature)it.next();
407             String JavaDoc s = o.getShortcut();
408             if ( (o instanceof GenericOptionEntry) && (s != null) && (s.equals(shortcut)) ) {
409                 return (GenericOptionEntry)o;
410             }
411         }
412         return null;
413     }
414
415 } // end GenericOptions
416
Popular Tags