1 18 19 package de.gulden.framework.amoda.generic.option; 20 21 import de.gulden.framework.amoda.environment.gui.*; 22 import de.gulden.framework.amoda.generic.core.GenericFeature; 23 import de.gulden.framework.amoda.generic.data.*; 24 import de.gulden.framework.amoda.generic.data.GenericValue; 25 import de.gulden.framework.amoda.model.behaviour.*; 26 import de.gulden.framework.amoda.model.data.*; 27 import de.gulden.framework.amoda.model.option.OptionEntry; 28 import de.gulden.util.Toolbox; 29 import de.gulden.util.xml.*; 30 import de.gulden.util.xml.XMLToolbox; 31 import de.gulden.util.xml.serializer.*; 32 import de.gulden.util.xml.serializer.XMLSerializableActive; 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.ItemListener ; 37 import java.lang.*; 38 import java.util.*; 39 import javax.swing.*; 40 import javax.swing.event.*; 41 import javax.swing.event.DocumentListener ; 42 import org.w3c.dom.*; 43 44 50 public class GenericOptionEntry extends GenericFeature implements ItemListener , ActionListener , OptionEntry, XMLSerializableActive, DocumentListener , ChangeListener { 51 52 56 public String type; 57 58 public boolean directory = false; 59 60 protected Condition validityCondition; 61 62 protected Value[] value; 63 64 65 69 public GenericOptionEntry() { 70 super(); 71 value = new Value[STATE_EDIT+1]; 72 for (int i=0;i<value.length;i++) { 73 value[i]=new de.gulden.framework.amoda.generic.data.GenericValue(); 74 } 75 } 76 77 78 82 public Value getValue() { 83 for (int i=STATE_CURRENT;i>=0;i--) { 85 Value v=getValue(i); 86 if (v.get()!=null) { 87 return v; 88 } 89 } 90 return null; 91 } 92 93 public void setValue(Value value) { 94 setValue(STATE_CURRENT,value); 95 } 96 97 public boolean isValid() { 98 Condition c=getValidityCondition(); 99 if (c!=null) { 100 return c.test(); 101 } else { 102 return (getValue()!=null); } 104 } 105 106 public void setType(String _type) { 107 Class clazz=null; 110 try { 111 clazz=Toolbox.getPrimitiveTypeWrapperClass(_type); 112 } catch (ClassNotFoundException cnfe) { } 114 if (clazz==null) { 115 try { 116 clazz=Class.forName(_type); 117 } catch (ClassNotFoundException cnfe) { 118 try { 120 clazz=Class.forName("java.lang."+_type); 121 } catch (ClassNotFoundException cnfe2) { 122 throw new NoClassDefFoundError ("unknown option type "+cnfe.getMessage()); 123 } 124 } 125 } 126 setType(clazz); 127 } 128 129 public Class getType() { 130 Class type = ((GenericValue)getValue(0)).getType(); 131 if (type != null) { 132 return type; 133 } else { 134 return String .class; } 136 } 137 138 public void setType(Class type) { 139 if (type!=getType()) { 140 for (int i=0;i<value.length;i++) { 141 ((de.gulden.framework.amoda.generic.data.GenericValue)value[i]).setType(type); 142 } 143 } 144 } 145 146 public Condition getValidityCondition() { 147 return validityCondition; 148 } 149 150 public void setValidityCondition(Condition _validityCondition) { 151 validityCondition = _validityCondition; 152 } 153 154 public Value[] getValues() { 155 return value; 156 } 157 158 public void setValues(Value[] _value) { 159 value = _value; 160 } 161 162 public void setValue(int idx, Value _value) throws ArrayIndexOutOfBoundsException { 163 value[idx] = _value; 164 } 165 166 public Value getValue(int idx) throws ArrayIndexOutOfBoundsException { 167 return value[idx]; 168 } 169 170 public Element xmlSerialize(Document doc, XMLSerializer serializer) { 171 return null; 173 } 174 175 public void xmlDeserialize(Element element, XMLSerializer serializer) throws XMLException { 176 ((de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializer)serializer).xmlDeserialize(this,element,false); xmlDeserializeValueEntry(element,STATE_INIT,"init-value"); 178 xmlDeserializeValueEntry(element,STATE_DEFAULT,"default-value"); 179 xmlDeserializeValueEntry(element,STATE_SAVED,"save-value"); 180 String s=XMLToolbox.getText(element); 182 if (s!=null) { 183 s=s.trim(); 184 if (s.length()>0) { 185 GenericValue v=(GenericValue)getValue(STATE_CURRENT); 186 v.parseString(s); 187 setType(v.getType()); } 189 } 190 xmlDeserializeValueEntry(element,STATE_CURRENT,"value"); 192 xmlDeserializeValueEntry(element,STATE_EDIT,"edit-value"); 193 } 194 195 public String toString() { 196 return findTitle(); 197 } 198 199 public void createGUIEditorComponent(JComponent parent, GUIApplicationEnvironment env) { 200 Class type=getType(); 201 if (type==null) { 202 type=String .class; } 204 GridBagConstraints gbc=new GridBagConstraints(); 205 gbc.anchor=GridBagConstraints.WEST; 206 JLabel label=new JLabel(this.findTitle()); 207 label.setFont(env.getFont(de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment.FONT_LABEL)); 208 parent.add(label,gbc); 209 JComponent component; 210 if (type==Boolean .class) { 211 component=new JCheckBox(); 212 ((JCheckBox)component).addItemListener(this); 213 gbc.gridwidth=GridBagConstraints.REMAINDER; 214 gbc.fill=GridBagConstraints.HORIZONTAL; 215 gbc.weightx=1.0; 216 Value v=getValue(); 217 if (v!=null) { 218 ((JCheckBox)component).setSelected(v.getBoolean()); 219 } 220 } else if (type==java.awt.Color .class) { 221 component=new JPanel(); 222 component.setPreferredSize(new java.awt.Dimension (80,20)); 223 component.setBorder(new javax.swing.border.EtchedBorder ()); 224 component.setBackground(this.getValue().getColor()); 225 } else { if (Number .class.isAssignableFrom(type)) { 227 component=new JTextField(5); 228 ((JTextField)component).setInputVerifier(new de.gulden.util.swing.InputVerifierNumber(type)); 229 } else if (type==java.io.File .class) { 230 component=new JTextField(50); 231 ((JTextField)component).setInputVerifier(new de.gulden.util.swing.InputVerifierFile(isDirectory())); 232 } else { 233 Properties style = this.getStyleAttributes(); 234 String ml = style.getProperty("multiline"); 235 if (ml != null) { 236 component = new JTextArea(Integer.parseInt(ml), 50); 237 ((JTextArea)component).setLineWrap(true); 238 ((JTextArea)component).setWrapStyleWord(true); 239 } else { 240 component = new JTextField(50); 241 } 242 } 243 ((javax.swing.text.JTextComponent )component).getDocument().addDocumentListener(this); 244 Value v=getValue(); 245 if (v!=null) { 246 ((javax.swing.text.JTextComponent )component).setText(v.getString()); 247 } 248 if (component instanceof JTextArea) { 249 component = new JScrollPane(component); 250 } 251 } 252 if ((type!=java.io.File .class)&&(type!=java.awt.Color .class)) { 253 gbc.gridwidth=GridBagConstraints.REMAINDER; 254 } 255 if ((type!=java.awt.Color .class)&&(!Number .class.isAssignableFrom(type))) { 256 gbc.fill=GridBagConstraints.HORIZONTAL; 257 } 258 if (type!=java.awt.Color .class) { 259 gbc.weightx=1.0; 260 } 261 parent.add(component,gbc); 262 if ((type==java.io.File .class)||(type==java.awt.Color .class)) { 263 String buttonLabel; 264 if (type==java.io.File .class) { 265 buttonLabel="Pick..."; 266 } else { 267 buttonLabel="Choose..."; 268 } 269 JButton button=new JButton(buttonLabel); 270 button.setFont(env.getFont(de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment.FONT_BUTTON)); 271 button.addActionListener(this); 272 gbc.fill=GridBagConstraints.NONE; 273 gbc.gridwidth=GridBagConstraints.REMAINDER; 274 if (type!=java.awt.Color .class) { 275 gbc.weightx=1.0; 276 gbc.anchor=GridBagConstraints.WEST; 277 } else { 278 gbc.weightx=0.0; 279 } 280 parent.add(button,gbc); 281 } 282 } 283 284 public void itemStateChanged(ItemEvent e) { 285 getValue(STATE_CURRENT).set(Boolean.valueOf(e.getStateChange()==ItemEvent.SELECTED)); 287 } 288 289 public void actionPerformed(ActionEvent e) { 290 if (this.getType()==java.io.File .class) { 291 de.gulden.framework.amoda.generic.interaction.GenericDialog fd=new de.gulden.framework.amoda.generic.interaction.GenericDialog(); 294 fd.setParent(this.getParent()); 295 fd.setFileDialog(true); 296 fd.setDirectories(true); GenericOptionEntry o=new GenericOptionEntry(); 298 o.setName("file"); 299 o.setType(java.io.File .class); 300 ((GenericOptions)fd.getOptions()).add(o); 302 de.gulden.framework.amoda.model.data.Value v=this.getValue(); 303 if (v!=null) { 304 java.io.File file=v.getFile(); 305 ((GenericValue)o.getValue(STATE_CURRENT)).set(file); 307 } 308 fd.perform(); 309 java.io.File file=fd.getOptions().getFile("file"); 310 if (file!=null) { 312 java.awt.Container parent=((javax.swing.JButton )e.getSource()).getParent(); 313 javax.swing.JTextField textfield=(javax.swing.JTextField )Toolbox.findChildComponentCloseTo(parent,javax.swing.JTextField .class, (JComponent)e.getSource()); 314 textfield.setText(file.getAbsolutePath()); } 316 } else if (this.getType()==java.awt.Color .class) { 317 java.awt.Color color=javax.swing.JColorChooser.showDialog(((de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment)getApplication().getEnvironment()).getFrame(),"Choose Color",this.getValue().getColor()); 318 if (color!=null) { 320 ((GenericValue)getValue(STATE_CURRENT)).set(color); 321 java.awt.Container parent=((javax.swing.JButton )e.getSource()).getParent(); 322 javax.swing.JPanel label=(javax.swing.JPanel )Toolbox.findChildComponent(parent,javax.swing.JPanel .class); 323 label.setBackground(color); label.repaint(); 325 } 326 327 } 328 } 329 330 public void insertUpdate(DocumentEvent e) { 331 guiTextValueChanged(e); 332 } 333 334 public void removeUpdate(DocumentEvent e) { 335 guiTextValueChanged(e); 336 } 337 338 public void changedUpdate(DocumentEvent e) { 339 guiTextValueChanged(e); 340 } 341 342 public boolean isDirectory() { 343 return directory; 344 } 345 346 public void setDirectory(boolean _directory) { 347 directory = _directory; 348 } 349 350 public void shiftValue(int stateOld, int stateNew) { 351 Object value=getValue(stateOld).get(); 352 if (value!=null) { 353 ((de.gulden.framework.amoda.generic.data.GenericValue)getValue(stateNew)).set(value); 354 } 355 } 356 357 public String getQualifiedId() { 358 de.gulden.framework.amoda.generic.core.GenericApplicationMemberAbstract parent=(de.gulden.framework.amoda.generic.core.GenericApplicationMemberAbstract)getParent(); 360 if ((parent instanceof GenericOptions)&&(parent!=getApplication().getOptions())) { 361 return parent.getQualifiedId()+"."+this.getId(); 362 } else { return this.getId(); 364 } 365 } 366 367 public void stateChanged(ChangeEvent e) { 368 shiftValue(STATE_EDIT, STATE_CURRENT); 369 } 370 371 public boolean isSystem() { 372 if (super.isSystem()) { 373 return true; 374 } else { 375 de.gulden.framework.amoda.model.data.CompositeMember f = this.getParent(); 377 while ( (f != null) && (f instanceof GenericFeature) && (!((GenericFeature)f).isSystem()) ) { 378 f = f.getParent(); 379 } 380 return (f != null); 381 } 382 } 383 384 protected void xmlDeserializeValueEntry(Element element, int state, String tagname) { 385 String s=XMLToolbox.getChildText(element,tagname); 386 if (s!=null) { 387 GenericValue v=(GenericValue)getValue(state); 388 v.parseString(s); 389 setType(v.getType()); } 391 } 392 393 private void guiTextValueChanged(DocumentEvent e) { 394 String s=Toolbox.getDocumentText(e.getDocument()); 395 try { 396 ((GenericValue)getValue(STATE_EDIT)).parseString(s); 397 } catch (de.gulden.framework.amoda.model.data.IllegalTypeError ite) { 412 } 413 } 414 415 } | Popular Tags |