KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.generic.option.GenericOptionEntry
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.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 JavaDoc;
36 import java.awt.event.ItemListener JavaDoc;
37 import java.lang.*;
38 import java.util.*;
39 import javax.swing.*;
40 import javax.swing.event.*;
41 import javax.swing.event.DocumentListener JavaDoc;
42 import org.w3c.dom.*;
43
44 /**
45  * Class GenericOptionEntry.
46  *
47  * @author Jens Gulden
48  * @version snapshot-beautyj-1.1
49  */

50 public class GenericOptionEntry extends GenericFeature implements ItemListener JavaDoc, ActionListener JavaDoc, OptionEntry, XMLSerializableActive, DocumentListener JavaDoc, ChangeListener {
51
52     // ------------------------------------------------------------------------
53
// --- fields ---
54
// ------------------------------------------------------------------------
55

56     public String JavaDoc type;
57
58     public boolean directory = false;
59
60     protected Condition validityCondition;
61
62     protected Value[] value;
63
64
65     // ------------------------------------------------------------------------
66
// --- constructor ---
67
// ------------------------------------------------------------------------
68

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     // ------------------------------------------------------------------------
79
// --- methods ---
80
// ------------------------------------------------------------------------
81

82     public Value getValue() {
83         // get most current value which is set, or null if no value is set
84
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); // default semantics for validity is "non-null"
103
}
104     }
105
106     public void setType(String JavaDoc _type) {
107         // this allow SmartXMLSerializer to set type name from type attribue
108
//type = _type; - string var stays null!
109
Class JavaDoc clazz=null;
110         try {
111             clazz=Toolbox.getPrimitiveTypeWrapperClass(_type);
112         } catch (ClassNotFoundException JavaDoc cnfe) { // not a primitive type
113
}
114         if (clazz==null) {
115             try {
116                 clazz=Class.forName(_type);
117             } catch (ClassNotFoundException JavaDoc cnfe) {
118                 // allow mising "java.lang.", try again with it:
119
try {
120                     clazz=Class.forName("java.lang."+_type);
121                 } catch (ClassNotFoundException JavaDoc cnfe2) {
122                     throw new NoClassDefFoundError JavaDoc("unknown option type "+cnfe.getMessage());
123                 }
124             }
125         }
126         setType(clazz);
127     }
128
129     public Class JavaDoc getType() {
130         Class JavaDoc type = ((GenericValue)getValue(0)).getType();
131         if (type != null) {
132             return type;
133         } else {
134             return String JavaDoc.class; // default type is String
135
}
136     }
137
138     public void setType(Class JavaDoc 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 JavaDoc {
163         value[idx] = _value;
164     }
165
166     public Value getValue(int idx) throws ArrayIndexOutOfBoundsException JavaDoc {
167         return value[idx];
168     }
169
170     public Element xmlSerialize(Document doc, XMLSerializer serializer) {
171         // your code here
172
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); // inherit original deserializaton behaviour
177
xmlDeserializeValueEntry(element,STATE_INIT,"init-value");
178         xmlDeserializeValueEntry(element,STATE_DEFAULT,"default-value");
179         xmlDeserializeValueEntry(element,STATE_SAVED,"save-value");
180         // current value may be set directly in tag body
181
String JavaDoc 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()); // type might have been set by parsing if not explicitly initialized before
188
}
189         }
190         // but an explicit <value>..</value> also set current value
191
xmlDeserializeValueEntry(element,STATE_CURRENT,"value");
192         xmlDeserializeValueEntry(element,STATE_EDIT,"edit-value");
193     }
194
195     public String JavaDoc toString() {
196         return findTitle();
197     }
198
199     public void createGUIEditorComponent(JComponent parent, GUIApplicationEnvironment env) {
200         Class JavaDoc type=getType();
201         if (type==null) {
202             type=String JavaDoc.class; // if unknown (empty value), treat as string
203
}
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 JavaDoc.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 JavaDoc.class) {
221             component=new JPanel();
222             component.setPreferredSize(new java.awt.Dimension JavaDoc(80,20));
223             component.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
224             component.setBackground(this.getValue().getColor());
225         } else { // String or File
226
if (Number JavaDoc.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 JavaDoc.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 JavaDoc 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 JavaDoc)component).getDocument().addDocumentListener(this);
244             Value v=getValue();
245             if (v!=null) {
246                 ((javax.swing.text.JTextComponent JavaDoc)component).setText(v.getString());
247             }
248             if (component instanceof JTextArea) {
249                 component = new JScrollPane(component);
250             }
251         }
252         if ((type!=java.io.File JavaDoc.class)&&(type!=java.awt.Color JavaDoc.class)) {
253             gbc.gridwidth=GridBagConstraints.REMAINDER;
254         }
255         if ((type!=java.awt.Color JavaDoc.class)&&(!Number JavaDoc.class.isAssignableFrom(type))) {
256             gbc.fill=GridBagConstraints.HORIZONTAL;
257         }
258         if (type!=java.awt.Color JavaDoc.class) {
259             gbc.weightx=1.0;
260         }
261         parent.add(component,gbc);
262         if ((type==java.io.File JavaDoc.class)||(type==java.awt.Color JavaDoc.class)) {
263             String JavaDoc buttonLabel;
264             if (type==java.io.File JavaDoc.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 JavaDoc.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         // option type is Boolean
286
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 JavaDoc.class) {
291             // This is called when the user hits a "Pick..." button close to a file-field
292
//getApplication().message("****FILE REQUESTER****"); //*******************************
293
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);//this.isDirectory());
297
GenericOptionEntry o=new GenericOptionEntry();
298             o.setName("file");
299             o.setType(java.io.File JavaDoc.class);
300             //o.setValue(STATE_CURRENT,this.getValue()),
301
((GenericOptions)fd.getOptions()).add(o);
302             de.gulden.framework.amoda.model.data.Value v=this.getValue();
303             if (v!=null) {
304                 java.io.File JavaDoc file=v.getFile();
305                 // pre-set current value as initial file
306
((GenericValue)o.getValue(STATE_CURRENT)).set(file);
307             }
308             fd.perform();
309             java.io.File JavaDoc file=fd.getOptions().getFile("file");
310             // find text field close to button (HACKING!)
311
if (file!=null) {
312                 java.awt.Container JavaDoc parent=((javax.swing.JButton JavaDoc)e.getSource()).getParent();
313                 javax.swing.JTextField JavaDoc textfield=(javax.swing.JTextField JavaDoc)Toolbox.findChildComponentCloseTo(parent,javax.swing.JTextField JavaDoc.class, (JComponent)e.getSource());
314                 textfield.setText(file.getAbsolutePath()); // ! this will set this options' value through the document listener !
315
}
316         } else if (this.getType()==java.awt.Color JavaDoc.class) {
317             java.awt.Color JavaDoc color=javax.swing.JColorChooser.showDialog(((de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment)getApplication().getEnvironment()).getFrame(),"Choose Color",this.getValue().getColor());
318             // find color panel close to button (HACKING!)
319
if (color!=null) {
320                 ((GenericValue)getValue(STATE_CURRENT)).set(color);
321                 java.awt.Container JavaDoc parent=((javax.swing.JButton JavaDoc)e.getSource()).getParent();
322                 javax.swing.JPanel JavaDoc label=(javax.swing.JPanel JavaDoc)Toolbox.findChildComponent(parent,javax.swing.JPanel JavaDoc.class);
323                 label.setBackground(color); // ! this will set this options' value through the document listener !
324
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 JavaDoc 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 JavaDoc getQualifiedId() {
358         // overwrites GenericApplicationMemberAbstract.getQualifiedId
359
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 { // top-most option or option-group, parent feature names are not included
363
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             // OptionEntries are also considered "system" if they are member of a group that is "system"
376
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 JavaDoc tagname) {
385         String JavaDoc s=XMLToolbox.getChildText(element,tagname);
386         if (s!=null) {
387             GenericValue v=(GenericValue)getValue(state);
388             v.parseString(s);
389             setType(v.getType()); // type might have been set by parsing if not explicitly initialized before
390
}
391     }
392
393     private void guiTextValueChanged(DocumentEvent e) {
394         String JavaDoc s=Toolbox.getDocumentText(e.getDocument());
395         try {
396             ((GenericValue)getValue(STATE_EDIT)).parseString(s);
397         } catch (de.gulden.framework.amoda.model.data.IllegalTypeError ite) { // couldn't parse this type - this is another kind of input-verification here
398
/* can't reset value - write lock
399             // reset current value
400             Value v=getValue();
401             String valStr;
402             if (v!=null) {
403                 valStr=v.getString();
404                 if (valStr==null) {
405                     valStr="";
406                 }
407             } else {
408                 valStr="";
409             }
410             Toolbox.setDocumentText(e.getDocument(),valStr);
411             */

412         }
413     }
414
415 } // end GenericOptionEntry
416
Popular Tags