KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.CardLayout JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.io.File JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 import javax.swing.DefaultComboBoxModel JavaDoc;
66 import javax.swing.JComboBox JavaDoc;
67 import javax.swing.JPanel JavaDoc;
68 import javax.swing.JScrollPane JavaDoc;
69 import javax.swing.JTable JavaDoc;
70 import javax.swing.JTextField JavaDoc;
71 import javax.swing.ListSelectionModel JavaDoc;
72
73 import com.jgoodies.forms.builder.DefaultFormBuilder;
74 import com.jgoodies.forms.factories.Borders;
75 import com.jgoodies.forms.layout.FormLayout;
76
77 /**
78  * This class defines contents for part of DVModelerFrame frame.
79  *
80  * @author Nataliya Kholodna
81  * @version 1.0
82  */

83
84 public class CardPanel extends JPanel JavaDoc{
85   private FieldEditor fieldPanel;
86   private JTable JavaDoc attributesTable;
87   private JPanel JavaDoc objEntityViewPanel;
88
89   private JPanel JavaDoc dataMapPanel;
90   private JTextField JavaDoc dataMapNameField;
91   private JTextField JavaDoc dataMapFileField;
92
93   private JPanel JavaDoc dataViewPanel;
94   private DataView dataView;
95   private JTextField JavaDoc dataViewNameField;
96   private JTextField JavaDoc dataViewFileField;
97   private DataMapTreeModel dataMapTreeModel;
98
99   private JTextField JavaDoc dataViewField;
100   private JTextField JavaDoc viewNameField;
101   private ObjEntityView objEntityView;
102   private JComboBox JavaDoc objEntityCombo;
103
104   private JPanel JavaDoc emptyPanel;
105   private CardLayout JavaDoc cardLayout;
106
107
108   private DVObject selectedObject;
109
110   public CardPanel() {
111     super();
112     cardLayout = new CardLayout JavaDoc();
113     this.setLayout( cardLayout );
114
115     /*empty panel*/
116     emptyPanel = new JPanel JavaDoc();
117     add(emptyPanel, "emptyPanel");
118
119     /*panel for field editing */
120     fieldPanel = new FieldEditor();
121     add(fieldPanel, "objEntityViewFieldPanel");
122
123     /*table for ObjEntity's attributes*/
124     attributesTable = new JTable JavaDoc();
125     attributesTable.setModel(new AttributesTableModel());
126     attributesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
127     JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(attributesTable);
128     scrollPane.setBorder(Borders.EMPTY_BORDER);
129     add(scrollPane, "attributesTable");
130
131     /*building panel for objEntityView properties*/
132     FormLayout layout = new FormLayout(
133         "right:55dlu, 3dlu, 200dlu",
134         "");
135     DefaultFormBuilder builderView = new DefaultFormBuilder(layout);
136     builderView.setDefaultDialogBorder();
137
138     viewNameField = new JTextField JavaDoc();
139     viewNameField.getDocument().addDocumentListener(
140         new NameChangeListener());
141     builderView.append("Name:", viewNameField);
142
143     objEntityCombo = new JComboBox JavaDoc();
144     objEntityCombo.addActionListener(new ActionListener JavaDoc(){
145       public void actionPerformed(ActionEvent JavaDoc e){
146         objEntityComboAction(e);
147       }
148     });
149     builderView.append("ObjEntity:", objEntityCombo);
150
151     dataViewField = new JTextField JavaDoc();
152
153     dataViewField.setEditable(false);
154     dataViewField.setBorder(Borders.EMPTY_BORDER);
155     builderView.append("Data View:", dataViewField);
156
157     objEntityViewPanel = builderView.getPanel();
158     scrollPane = new JScrollPane JavaDoc(objEntityViewPanel);
159     scrollPane.setBorder(Borders.EMPTY_BORDER);
160     add(scrollPane, "objEntityViewPanel");
161
162     /*Building panel for dataMap properties*/
163     FormLayout layout1 = new FormLayout(
164         "right:55dlu, 3dlu, 200dlu",
165         "");
166
167     DefaultFormBuilder builderMap = new DefaultFormBuilder(layout1);
168     builderMap.setDefaultDialogBorder();
169
170     dataMapNameField = new JTextField JavaDoc();
171     dataMapNameField.setEditable(false);
172     dataMapNameField.setBorder(Borders.EMPTY_BORDER);
173     builderMap.append("Name:", dataMapNameField);
174
175     dataMapFileField = new JTextField JavaDoc();
176     dataMapFileField.setEditable(false);
177     dataMapFileField.setBorder(Borders.EMPTY_BORDER);
178     builderMap.append("Location:", dataMapFileField);
179
180     dataMapPanel = builderMap.getPanel();
181     scrollPane = new JScrollPane JavaDoc(dataMapPanel);
182     scrollPane.setBorder(Borders.EMPTY_BORDER);
183     add(scrollPane, "dataMapPanel");
184
185     /*Building panel for dataView properties*/
186     FormLayout layout2 = new FormLayout(
187         "right:55dlu, 3dlu, 200dlu",
188         "");
189
190     DefaultFormBuilder builderDataView = new DefaultFormBuilder(layout2);
191     builderDataView.setDefaultDialogBorder();
192
193     dataViewNameField = new JTextField JavaDoc();
194     dataViewNameField.getDocument().addDocumentListener(
195         new NameChangeListener());
196
197     builderDataView.append("Name:", dataViewNameField);
198
199     dataViewFileField = new JTextField JavaDoc();
200     dataViewFileField.setEditable(false);
201     dataViewFileField.setBorder(Borders.EMPTY_BORDER);
202     builderDataView.append("Location:", dataViewFileField);
203
204     dataViewPanel = builderDataView.getPanel();
205     scrollPane = new JScrollPane JavaDoc(dataViewPanel);
206     scrollPane.setBorder(Borders.EMPTY_BORDER);
207     add(scrollPane, "dataViewPanel");
208     /*setting default panel*/
209     cardLayout.show(this, "emptyPanel");
210   }
211
212   /*shows definning by selectedObject panel */
213   public void showPanel(DVObject selectedObject){
214     this.selectedObject = null;
215     if (selectedObject instanceof DataMap){
216       DataMap dataMap = (DataMap)selectedObject;
217       dataMapNameField.setText(dataMap.getName());
218       dataMapFileField.setText(dataMap.getFile().getName());
219       cardLayout.show(this, "dataMapPanel");
220     } else if (selectedObject instanceof DataView){
221       dataView = (DataView)selectedObject;
222       dataViewNameField.setText(dataView.getName());
223       dataViewFileField.setText(
224           (dataView.getFile() != null ?
225           dataView.getFile().getName() :
226           "New Data View"));
227
228       cardLayout.show(this, "dataViewPanel");
229     } else if (selectedObject instanceof ObjEntity){
230       ObjEntity objEntity = (ObjEntity)selectedObject;
231       AttributesTableModel attributesTableModel =
232           (AttributesTableModel)attributesTable.getModel();
233       attributesTableModel.setObjEntity(objEntity);
234       cardLayout.show(this, "attributesTable");
235     } else if (selectedObject instanceof ObjEntityView){
236       objEntityView = (ObjEntityView)selectedObject;
237       dataViewField.setText(objEntityView.getDataView().getName());
238       viewNameField.setText(objEntityView.getName());
239
240       ObjEntity objEntity = objEntityView.getObjEntity();
241       if (objEntity == null){
242         objEntityCombo.setSelectedIndex(0);
243       } else {
244         objEntityCombo.setSelectedItem(objEntity);
245       }
246
247       cardLayout.show(this, "objEntityViewPanel");
248     } else if (selectedObject instanceof ObjEntityViewField){
249       ObjEntityViewField objEntityViewField = (ObjEntityViewField)selectedObject;
250       fieldPanel.setFieldProperties(objEntityViewField);
251       cardLayout.show(this, "objEntityViewFieldPanel");
252     } else if (selectedObject == null ){
253       cardLayout.show(this, "emptyPanel");
254     }
255     this.selectedObject = selectedObject;
256   }
257
258   /*sets models which can be changed*/
259   public void setModels(DataMapTreeModel mapModel,
260                         DataViewTreeModel viewModel,
261                         FieldsTableModel tableModel){
262     dataMapTreeModel = mapModel;
263     fieldPanel.setModels(mapModel, viewModel, tableModel);
264   }
265
266   /*sets another CayenneProject*/
267   public void setProject(CayenneProject project) {
268     if (project == null) {
269       objEntityCombo.setModel(new DefaultComboBoxModel JavaDoc());
270       return;
271     }
272
273     ObjEntity[] projectEntities = project.getObjEntities();
274     ObjEntity[] entities = new ObjEntity[projectEntities.length + 1];
275     ObjEntity nullEntity = null;
276     entities[0] = nullEntity;
277
278     for (int j = 0; j < projectEntities.length; j++){
279       entities[j+1] = projectEntities[j];
280     }
281     DefaultComboBoxModel JavaDoc objEntitiesDefaultModel =
282         new DefaultComboBoxModel JavaDoc(entities);
283     objEntityCombo.setModel(objEntitiesDefaultModel);
284   }
285
286   private class NameChangeListener extends BasicDocumentListener {
287     public void documentUpdated(String JavaDoc text) {
288       if (selectedObject != null){
289         selectedObject.setName(text.trim());
290         if (selectedObject instanceof DataView){
291           File JavaDoc file = dataView.getFile();
292           File JavaDoc newFile = new File JavaDoc(file.getParentFile(), dataView.getName() + ".view.xml");
293           dataView.setFile(newFile);
294           dataViewFileField.setText(dataView.getFile().getName());
295         }
296       }
297     }
298   }
299
300   private void objEntityComboAction(ActionEvent JavaDoc e){
301     ObjEntity selectedObjEntity = (ObjEntity)objEntityCombo.getSelectedItem();
302     ObjEntity objEntity = objEntityView.getObjEntity();
303     if ((objEntity != selectedObjEntity)){
304       if ((objEntity != null) && (selectedObjEntity != null)){
305         java.util.List JavaDoc relationships = selectedObjEntity.getDataMap().getObjRelationshipsBySourceToOne(selectedObjEntity);
306         int oldIndex = objEntity.getIndexOfObjEntityView(objEntityView);
307         objEntityView.setObjEntity(selectedObjEntity);
308
309         /*refrashing dataMapTreeModel: replace ObjEntityView
310           to new ObjEntity*/

311         dataMapTreeModel.replaceObjEntityView(objEntity, oldIndex, selectedObjEntity, objEntityView);
312
313         /*changing fields properties, which depend on ObjEntity */
314         java.util.List JavaDoc fields = objEntityView.getObjEntityViewFields();
315         for (Iterator JavaDoc itr = fields.iterator();itr.hasNext();){
316           ObjEntityViewField field = (ObjEntityViewField)itr.next();
317           if(field.getCalcType().equals("nocalc")){
318             if (field.getObjAttribute() != null){
319               String JavaDoc fieldAttributeName = field.getObjAttribute().getName();
320               if (selectedObjEntity.getObjAttribute(fieldAttributeName) != null){
321                 field.setObjAttribute(selectedObjEntity.getObjAttribute(fieldAttributeName));
322               }else{
323                 field.setObjAttribute(null);
324               }
325             }
326           }
327           if(field.getCalcType().equals("lookup")){
328             ObjRelationship fieldRelationship = field.getObjRelationship();
329             if (fieldRelationship != null){
330               String JavaDoc relationshipName = fieldRelationship.getName();
331               ObjEntity targetObjEntity = fieldRelationship.getTargetObjEntity();
332               Lookup lookup = field.getLookup();
333               Lookup nullLookup = new Lookup(field);
334               nullLookup.setLookupField(null);
335               nullLookup.setLookupObjEntityView(null);
336
337               field.setObjRelationship(null);
338               field.setLookup(nullLookup);
339               for (Iterator JavaDoc j = relationships.iterator(); j.hasNext();){
340                 ObjRelationship relationship = (ObjRelationship)j.next();
341                 if ((relationship.getName().equals(relationshipName))&&
342                     (relationship.getTargetObjEntity() == targetObjEntity)){
343                   field.setObjRelationship(relationship);
344                   field.setLookup(lookup);
345                 }
346               }
347             }
348           }
349         }
350       } else if ((objEntity == null) && (selectedObjEntity != null)){
351         objEntityView.setObjEntity(selectedObjEntity);
352
353         /*refreshing dataMapTreeModel: adding objEntityView to objEntity*/
354         dataMapTreeModel.objEntityViewAdded(objEntityView);
355       } else if ((objEntity != null) && (selectedObjEntity == null)){
356         int index = objEntity.getIndexOfObjEntityView(objEntityView);
357         objEntityView.setObjEntity(null);
358
359         /*refreshing dataMapTreeModel: removing objEntityView from objEntity*/
360         dataMapTreeModel.objEntityViewRemoved(objEntity, objEntityView, index);
361
362         /*changing fields properties, which depend on ObjEntity */
363         java.util.List JavaDoc fields = objEntityView.getObjEntityViewFields();
364
365         for (Iterator JavaDoc itr = fields.iterator();itr.hasNext();){
366           ObjEntityViewField field = (ObjEntityViewField)itr.next();
367           if(field.getCalcType().equals("nocalc")){
368             field.setObjAttribute(null);
369           }
370           if(field.getCalcType().equals("lookup")){
371             Lookup nullLookup = new Lookup(field);
372             nullLookup.setLookupField(null);
373             nullLookup.setLookupObjEntityView(null);
374
375             field.setObjRelationship(null);
376             field.setLookup(nullLookup);
377
378           }
379         }
380       }
381     }
382   }
383 }
384
Popular Tags