KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > dvmodeler > FieldEditorHelper


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.dataview.dvmodeler;
58
59 import java.util.*;
60 import java.awt.event.*;
61 import javax.swing.*;
62 import javax.swing.event.*;
63
64 /**
65  *
66  * @author Nataliya Kholodna
67  * @version 1.0
68  */

69
70 class FieldEditorHelper {
71   private JTextField fieldNameField;
72   private JComboBox dataTypeCombo;
73   private JComboBox calcTypeCombo;
74   private JComboBox objAttributeCombo;
75   private JTextField defaultValueField;
76   private JTextField captionField;
77   private JCheckBox editableCheckBox;
78   private JCheckBox visibleCheckBox;
79   private JTextField displayClassField;
80   private JTextField displayPatternField;
81   private JTextField editClassField;
82   private JTextField editPatternField;
83   private JSpinner preferredIndexField;
84   private JComboBox lookupViewCombo;
85   private JComboBox lookupFieldCombo;
86   private JComboBox objRelationshipCombo;
87
88   private DataMapTreeModel dataMapTreeModel;
89   private DataViewTreeModel dataViewTreeModel;
90   private FieldsTableModel fieldsTableModel;
91
92   private ObjEntityViewField objEntityViewField;
93
94   FieldEditorHelper(FieldEditor fieldEditor){
95     // this.fieldEditor = fieldEditor;
96

97     fieldNameField = fieldEditor.getFieldNameField();
98     fieldNameField.getDocument().addDocumentListener(
99       new FieldNameChangeListener());
100
101     dataTypeCombo = fieldEditor.getDataTypeCombo();
102     dataTypeCombo.addActionListener(new ActionListener(){
103       public void actionPerformed(ActionEvent e){
104         dataTypeComboAction(e);
105       }
106     });
107     calcTypeCombo = fieldEditor.getCalcTypeCombo();
108     calcTypeCombo.addActionListener(new ActionListener(){
109       public void actionPerformed(ActionEvent e){
110         calcTypeComboAction(e);
111       }
112     });
113     lookupViewCombo = fieldEditor.getLookupViewCombo();
114     lookupViewCombo.addActionListener(new ActionListener(){
115       public void actionPerformed(ActionEvent e){
116         lookupViewComboAction(e);
117       }
118     });
119     lookupFieldCombo = fieldEditor.getLookupFieldCombo();
120     lookupFieldCombo.addActionListener(new ActionListener(){
121       public void actionPerformed(ActionEvent e){
122         lookupFieldComboAction(e);
123       }
124     });
125     objRelationshipCombo = fieldEditor.getObjRelationshipCombo();
126     objRelationshipCombo.addActionListener(new ActionListener(){
127       public void actionPerformed(ActionEvent e){
128         objRelationshipComboAction(e);
129       }
130     });
131
132     objAttributeCombo = fieldEditor.getObjAttributeCombo();
133     objAttributeCombo.addActionListener(new ActionListener(){
134       public void actionPerformed(ActionEvent e){
135         objAttributeComboAction(e);
136       }
137     });
138     defaultValueField = fieldEditor.getDefaultValueField();
139     defaultValueField.getDocument().addDocumentListener(
140       new DefaultValueChangeListener());
141
142     captionField = fieldEditor.getCaptionField();
143     captionField.getDocument().addDocumentListener(
144       new CaptionChangeListener());
145
146     editableCheckBox = fieldEditor.getEditableCheckBox();
147     editableCheckBox.addActionListener(new ActionListener(){
148       public void actionPerformed(ActionEvent e){
149         editableCheckBoxAction(e);
150       }
151     });
152     visibleCheckBox = fieldEditor.getVisibleCheckBox();
153     visibleCheckBox.addActionListener(new ActionListener(){
154       public void actionPerformed(ActionEvent e){
155         visibleCheckBoxAction(e);
156       }
157     });
158     displayClassField = fieldEditor.getDisplayClassField();
159     displayClassField.getDocument().addDocumentListener(
160       new DisplayClassChangeListener());
161
162     displayPatternField = fieldEditor.getDisplayPatternField();
163     displayPatternField.getDocument().addDocumentListener(
164       new DisplayPatternChangeListener());
165
166     editClassField = fieldEditor.getEditClassField();
167     editClassField.getDocument().addDocumentListener(
168       new EditClassChangeListener());
169
170     editPatternField = fieldEditor.getEditPatternField();
171     editPatternField.getDocument().addDocumentListener(
172       new EditPatternChangeListener());
173
174
175     preferredIndexField = fieldEditor.getPreferredIndexField();
176     preferredIndexField.addChangeListener(new ChangeListener(){
177       public void stateChanged(ChangeEvent e){
178         preferredIndexFieldChanged(e);
179       }
180     });
181   }
182
183   public void setObjEntityViewField(ObjEntityViewField field){
184     objEntityViewField = field;
185   }
186
187   private class FieldNameChangeListener extends BasicDocumentListener{
188     public void documentUpdated(String JavaDoc text) {
189       fieldNameChangeAction(text);
190     }
191   }
192   private void fieldNameChangeAction(String JavaDoc fieldName){
193     if (!objEntityViewField.getName().equals(fieldName.trim())){
194       objEntityViewField.setName(fieldName.trim());
195     }
196   }
197
198   private void dataTypeComboAction(ActionEvent e){
199     String JavaDoc comboDataType = (String JavaDoc)dataTypeCombo.getSelectedItem();
200     String JavaDoc dataType = objEntityViewField.getDataType();
201     if ( dataType != comboDataType){
202       objEntityViewField.setDataType(comboDataType);
203     }
204   }
205
206   private void calcTypeComboAction(ActionEvent e){
207     String JavaDoc comboCalcType = (String JavaDoc)calcTypeCombo.getSelectedItem();
208     ObjEntity objEntity = objEntityViewField.getObjEntityView().getObjEntity();
209
210     if (comboCalcType.equals("No Calculation")){
211       objEntityViewField.setCalcType("nocalc");
212
213       objRelationshipCombo.setEnabled(false);
214       lookupViewCombo.setEnabled(false);
215       lookupFieldCombo.setEnabled(false);
216
217
218       ObjAttribute nullAttribute = null;
219       java.util.List JavaDoc attributes = new ArrayList();
220       attributes.add(nullAttribute);
221       if (objEntity != null){
222         attributes.addAll(objEntity.getObjAttributes());
223       }
224
225       DefaultComboBoxModel attributesDefaultModel = new DefaultComboBoxModel(attributes.toArray());
226       objAttributeCombo.setModel(attributesDefaultModel);
227       objAttributeCombo.setEnabled(true);
228       if (objEntity != null){
229         if (objEntityViewField.getObjAttribute() != null){
230           objAttributeCombo.setSelectedItem(objEntityViewField.getObjAttribute());
231         }else {
232           objAttributeCombo.setSelectedIndex(0);
233         }
234       } else {
235         objAttributeCombo.setSelectedIndex(0);
236       }
237
238     }
239     if (comboCalcType.equals("Lookup")){
240       objEntityViewField.setCalcType("lookup");
241
242       objAttributeCombo.setEnabled(false);
243       objRelationshipCombo.setEnabled(true);
244       lookupViewCombo.setEnabled(true);
245       lookupFieldCombo.setEnabled(true);
246
247       ObjRelationship nullRelationship = null;
248       java.util.List JavaDoc relationships = new ArrayList();
249       relationships.add(nullRelationship);
250       if (objEntity != null){
251         relationships.addAll(objEntity.getDataMap().getObjRelationshipsBySourceToOne(objEntity));
252       }
253
254       DefaultComboBoxModel relationshipsDefaultModel = new DefaultComboBoxModel(relationships.toArray());
255       objRelationshipCombo.setModel(relationshipsDefaultModel);
256
257       ObjRelationship fieldRelationship = objEntityViewField.getObjRelationship();
258       if(fieldRelationship != null){
259         boolean flagSetSelectedItem = false;
260         for (Iterator itr = relationships.iterator();itr.hasNext();){
261           ObjRelationship relationship = (ObjRelationship)itr.next();
262           if ((relationship != null) && (fieldRelationship.getName().equals(relationship.getName()))){
263             objRelationshipCombo.setSelectedItem(fieldRelationship);
264             flagSetSelectedItem = true;
265             break;
266           }
267         }
268         if (!flagSetSelectedItem){
269           objRelationshipCombo.setSelectedIndex(0);
270         }
271       }else{
272         objRelationshipCombo.setSelectedIndex(0);
273       }
274     }
275   }
276
277   private void lookupViewComboAction(ActionEvent e){
278     ObjEntityView fieldLookupView = objEntityViewField.getLookup().getLookupObjEntityView();
279     ObjEntityView selectedLookupView = (ObjEntityView)lookupViewCombo.getSelectedItem();
280
281     if (selectedLookupView != fieldLookupView){
282       objEntityViewField.getLookup().setLookupObjEntityView(selectedLookupView);
283
284       dataViewTreeModel.fieldChanged(objEntityViewField);
285       dataMapTreeModel.fieldChanged(objEntityViewField);
286       fieldsTableModel.fireTableCellUpdated(objEntityViewField.
287          getObjEntityView().getIndexOfObjEntityViewField(objEntityViewField), 4);
288     }
289
290     if (selectedLookupView != null){
291       ObjEntityViewField nullField = null;
292       java.util.List JavaDoc lookupFields = new ArrayList();
293       lookupFields.add(nullField);
294       lookupFields.addAll(selectedLookupView.getObjEntityViewFields());
295
296       DefaultComboBoxModel lookupFieldsDefaultModel = new DefaultComboBoxModel(lookupFields.toArray());
297       lookupFieldCombo.setModel(lookupFieldsDefaultModel);
298       ObjEntityViewField fieldLookupField = objEntityViewField.getLookup().getLookupField();
299       if(fieldLookupField != null){
300         boolean flagSetSelectedItem = false;
301         for (Iterator itr = lookupFields.iterator();itr.hasNext();){
302           ObjEntityViewField field = (ObjEntityViewField)itr.next();
303           if ((field != null) && (fieldLookupField.getName().equals(field.getName()))){
304             lookupFieldCombo.setSelectedItem(fieldLookupField);
305             flagSetSelectedItem = true;
306             break;
307           }
308         }
309         if (!flagSetSelectedItem){
310           lookupFieldCombo.setSelectedIndex(0);
311         }
312       }else{
313         lookupFieldCombo.setSelectedIndex(0);
314       }
315     }else{
316       ObjEntityViewField nullField = null;
317       ObjEntityViewField[] fields = new ObjEntityViewField[]{nullField};
318
319       DefaultComboBoxModel fieldsModel = new DefaultComboBoxModel(fields);
320       lookupFieldCombo.setModel(fieldsModel);
321       lookupFieldCombo.setSelectedIndex(0);
322
323     }
324   }
325
326   private void lookupFieldComboAction(ActionEvent e){
327     ObjEntityViewField fieldLookupField = objEntityViewField.getLookup().getLookupField();
328     ObjEntityViewField selectedLookupField = (ObjEntityViewField)lookupFieldCombo.getSelectedItem();
329
330     if (selectedLookupField != fieldLookupField){
331       objEntityViewField.getLookup().setLookupField(selectedLookupField);
332
333       dataViewTreeModel.fieldChanged(objEntityViewField);
334       dataMapTreeModel.fieldChanged(objEntityViewField);
335       fieldsTableModel.fireTableCellUpdated(objEntityViewField.getObjEntityView().
336         getIndexOfObjEntityViewField(objEntityViewField), 4);
337     }
338   }
339
340   private void objRelationshipComboAction(ActionEvent e){
341     ObjRelationship selectedObjRelationship = (ObjRelationship)objRelationshipCombo.getSelectedItem();
342     ObjRelationship fieldObjRelationship = objEntityViewField.getObjRelationship();
343
344     if (selectedObjRelationship != fieldObjRelationship){
345       objEntityViewField.setObjRelationship(selectedObjRelationship);
346     }
347
348     if (selectedObjRelationship != null){
349       ObjEntity targetObjEntity = selectedObjRelationship.getTargetObjEntity();
350
351       ObjEntityView nullView = null;
352       java.util.List JavaDoc lookupViews = new ArrayList();
353       lookupViews.add(nullView);
354       lookupViews.addAll(targetObjEntity.getObjEntityViews());
355
356       DefaultComboBoxModel lookupViewModel = new DefaultComboBoxModel(lookupViews.toArray());
357       lookupViewCombo.setModel(lookupViewModel);
358       ObjEntityView fieldLookupView = objEntityViewField.getLookup().getLookupObjEntityView();
359       if(fieldLookupView != null){
360         boolean flagSetSelectedItem = false;
361         for (Iterator itr = lookupViews.iterator();itr.hasNext();){
362           ObjEntityView view = (ObjEntityView)itr.next();
363           if (fieldLookupView == view){
364             lookupViewCombo.setSelectedItem(fieldLookupView);
365             flagSetSelectedItem = true;
366             break;
367           }
368         }
369         if (!flagSetSelectedItem){
370           lookupViewCombo.setSelectedIndex(0);
371         }
372       }else{
373         lookupViewCombo.setSelectedIndex(0);
374       }
375     }else{
376       ObjEntityView nullView = null;
377       ObjEntityView[] views = new ObjEntityView[]{nullView};
378       DefaultComboBoxModel viewsModel = new DefaultComboBoxModel(views);
379       lookupViewCombo.setModel(viewsModel);
380       lookupViewCombo.setSelectedIndex(0);
381     }
382   }
383
384
385   private void objAttributeComboAction(ActionEvent e){
386
387     ObjAttribute selectedObjAttribute = (ObjAttribute)objAttributeCombo.getSelectedItem();
388     ObjAttribute fieldObjAttribute = objEntityViewField.getObjAttribute();
389     if (selectedObjAttribute != fieldObjAttribute){
390       objEntityViewField.setObjAttribute(selectedObjAttribute);
391     }
392   }
393
394   private class DefaultValueChangeListener extends BasicDocumentListener{
395     public void documentUpdated(String JavaDoc text) {
396       defaultValueChangeAction(text);
397     }
398   }
399
400   private void defaultValueChangeAction(String JavaDoc defaultValue){
401     String JavaDoc fieldDefaultValue = objEntityViewField.getDefaultValue();
402     if ( !defaultValue.trim().equals(fieldDefaultValue)){
403       objEntityViewField.setDefaultValue(defaultValue.trim());
404     }
405   }
406
407   private class CaptionChangeListener extends BasicDocumentListener{
408     public void documentUpdated(String JavaDoc text) {
409       captionChangeAction(text);
410     }
411   }
412
413   private void captionChangeAction(String JavaDoc caption){
414     String JavaDoc fieldCaption = objEntityViewField.getCaption();
415     if ( !caption.trim().equals(fieldCaption)){
416       objEntityViewField.setCaption(caption.trim());
417     }
418   }
419
420   private void editableCheckBoxAction(ActionEvent e){
421     boolean editable = editableCheckBox.isSelected();
422     if ( editable != objEntityViewField.getEditable()){
423       objEntityViewField.setEditable(editable);
424     }
425   }
426
427   private void visibleCheckBoxAction(ActionEvent e){
428     boolean visible = visibleCheckBox.isSelected();
429     if ( visible != objEntityViewField.getVisible()){
430       objEntityViewField.setVisible(visible);
431     }
432   }
433
434   private class DisplayClassChangeListener extends BasicDocumentListener{
435     public void documentUpdated(String JavaDoc text) {
436       displayClassChangeAction(text);
437     }
438   }
439
440   private void displayClassChangeAction(String JavaDoc displayClass){
441     String JavaDoc fieldDisplayClass = objEntityViewField.getDisplayFormat().getClassName();
442     if ( !displayClass.trim().equals(fieldDisplayClass)){
443       objEntityViewField.getDisplayFormat().setClassName(displayClass.trim());
444     }
445   }
446
447   private class DisplayPatternChangeListener extends BasicDocumentListener{
448     public void documentUpdated(String JavaDoc text) {
449       displayPatternChangeAction(text);
450     }
451   }
452
453   private void displayPatternChangeAction(String JavaDoc displayPattern){
454     String JavaDoc fieldDisplayPattern = objEntityViewField.getDisplayFormat().getPattern();
455     if ( !displayPattern.trim().equals(fieldDisplayPattern)){
456       objEntityViewField.getDisplayFormat().setPattern(displayPattern.trim());
457     }
458   }
459
460    private class EditClassChangeListener extends BasicDocumentListener{
461     public void documentUpdated(String JavaDoc text) {
462       editClassChangeAction(text);
463     }
464   }
465
466   private void editClassChangeAction(String JavaDoc editClass){
467     String JavaDoc fieldEditClass = objEntityViewField.getEditFormat().getClassName();
468     if ( !editClass.trim().equals(fieldEditClass)){
469       objEntityViewField.getEditFormat().setClassName(editClass.trim());
470     }
471   }
472
473   private class EditPatternChangeListener extends BasicDocumentListener{
474     public void documentUpdated(String JavaDoc text) {
475       editPatternChangeAction(text);
476     }
477   }
478
479   private void editPatternChangeAction(String JavaDoc editPattern){
480     String JavaDoc fieldEditPattern = objEntityViewField.getEditFormat().getPattern();
481     if ( !editPattern.trim().equals(fieldEditPattern)){
482       objEntityViewField.getEditFormat().setPattern(editPattern.trim());
483     }
484   }
485
486   private void preferredIndexFieldChanged(ChangeEvent e){
487     Integer JavaDoc indx = (Integer JavaDoc)(preferredIndexField.getValue());
488     int index = indx.intValue();
489     if ( index != objEntityViewField.getPrefIndex()){
490       objEntityViewField.setPrefIndex(index);
491     }
492   }
493
494   public void setModels(DataMapTreeModel mapModel,
495                         DataViewTreeModel viewModel,
496                         FieldsTableModel tableModel){
497     dataMapTreeModel = mapModel;
498     dataViewTreeModel = viewModel;
499     fieldsTableModel = tableModel;
500   }
501 }
502
Popular Tags