1 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 40 public class GenericOptions extends GenericFeature implements Cloneable , Options { 41 42 46 public static String [] OPTION_ELEMENTS = {"option","options-group"}; 47 48 public static Object [][] XML_COLLECTION_ELEMENTS = {{"all",OPTION_ELEMENTS}}; 49 50 protected GenericApplicationMemberAbstract parentMember; 51 52 53 57 public OptionEntry getOptionEntry(String id) { 58 try { 59 OptionEntry entry = (OptionEntry)get(id); 60 if (entry!=null) { 61 return entry; 62 } 63 } catch (ClassCastException cce) { 64 } 66 throw new IllegalOptionError(id); 67 } 68 69 public OptionsGroup getOptionsGroup(String id) { 70 return (OptionsGroup)get(id); 71 } 72 73 public String getString(String 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 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 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 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 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 getObject(String 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 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 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 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 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 o=it.next(); 195 Class t=o.getClass(); 196 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()); h.put(options.getId(),options); 206 } 207 } 208 } 209 } 210 h.putAll(Toolbox.filterMapOnType(super.getAll(),GenericOptions.class)); return h; 212 } 213 214 public Map getAll() { 215 Map h=new de.gulden.util.OrderedHashMap(); 217 h.putAll(getGroups()); 218 h.putAll(getEntries()); 219 for (Iterator it = h.values().iterator(); it.hasNext(); ) { 221 Object 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 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 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(); String 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 clone() { 266 try { 267 return super.clone(); 268 } catch (CloneNotSupportedException cnse) { 269 return null; 270 } 271 } 272 273 public Map getEntries() { 274 return Toolbox.filterMapOnType(super.getAll(),GenericOptionEntry.class); 275 } 276 277 public String findTitle() { 278 String title=de.gulden.framework.amoda.generic.metadata.GenericMetadata.findTitle(this); 279 if ((title==null)||(title.startsWith(Toolbox.capitalize(GenericApplicationMemberAbstract.AUTO_ID_PREFIX)))) { 280 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 setParentMember((GenericApplicationMemberAbstract)parent); 297 } 298 299 public void set(String id, Object 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 try { 307 Properties props=new Properties(); 308 java.io.FileInputStream f=new java.io.FileInputStream (file); 309 props.load(f); 310 f.close(); 311 for (Enumeration e=props.keys();e.hasMoreElements();) { 312 String id=(String )e.nextElement(); 313 String value=props.getProperty(id); 314 try { 315 GenericOptionEntry o = findOptionEntryByShortcut(id); if (o == null) { 317 o = (GenericOptionEntry)getOptionEntry(id); } 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 ioe) { 325 getApplication().log("properties file '"+file.getAbsolutePath()+"' not found - using defaults"); 326 } 327 } 328 329 public void saveToProperties(File file) { 330 try { 332 Properties props=new Properties(); 333 java.io.FileOutputStream f=new java.io.FileOutputStream (file); 334 for (Iterator it=this.getAll(GenericOptionEntry.class,true).values().iterator();it.hasNext();) { 335 GenericOptionEntry entry=(GenericOptionEntry)it.next(); 336 String id=entry.getQualifiedId(); 337 de.gulden.framework.amoda.model.data.Value value=entry.getValue(STATE_CURRENT); 338 if (value!=null) { 339 String 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 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 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 getQualifiedId() { 366 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 { 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 getClassType(String 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 getClassInstance(String 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 shortcut) { 404 Map all = getAll(); 405 for (Iterator it = all.values().iterator(); it.hasNext(); ) { 406 GenericFeature o = (GenericFeature)it.next(); 407 String 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 } | Popular Tags |