KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > editor > ObjEntityRelationshipTab


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 package org.objectstyle.cayenne.modeler.editor;
57
58 import java.awt.BorderLayout JavaDoc;
59 import java.awt.Color JavaDoc;
60 import java.awt.Component JavaDoc;
61 import java.awt.event.ActionEvent JavaDoc;
62 import java.awt.event.ActionListener JavaDoc;
63 import java.util.Collection JavaDoc;
64 import java.util.EventObject JavaDoc;
65
66 import javax.swing.DefaultCellEditor JavaDoc;
67 import javax.swing.DefaultComboBoxModel JavaDoc;
68 import javax.swing.JButton JavaDoc;
69 import javax.swing.JComboBox JavaDoc;
70 import javax.swing.JPanel JavaDoc;
71 import javax.swing.JTable JavaDoc;
72 import javax.swing.JToolBar JavaDoc;
73 import javax.swing.event.ChangeEvent JavaDoc;
74 import javax.swing.event.ListSelectionEvent JavaDoc;
75 import javax.swing.event.ListSelectionListener JavaDoc;
76 import javax.swing.event.TableModelEvent JavaDoc;
77 import javax.swing.event.TableModelListener JavaDoc;
78 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
79 import javax.swing.table.TableColumn JavaDoc;
80
81 import org.apache.log4j.Logger;
82 import org.objectstyle.cayenne.map.DataMap;
83 import org.objectstyle.cayenne.map.DeleteRule;
84 import org.objectstyle.cayenne.map.Entity;
85 import org.objectstyle.cayenne.map.MapObject;
86 import org.objectstyle.cayenne.map.ObjEntity;
87 import org.objectstyle.cayenne.map.ObjRelationship;
88 import org.objectstyle.cayenne.map.event.EntityEvent;
89 import org.objectstyle.cayenne.map.event.ObjEntityListener;
90 import org.objectstyle.cayenne.map.event.ObjRelationshipListener;
91 import org.objectstyle.cayenne.map.event.RelationshipEvent;
92 import org.objectstyle.cayenne.modeler.Application;
93 import org.objectstyle.cayenne.modeler.ProjectController;
94 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction;
95 import org.objectstyle.cayenne.modeler.action.ObjEntitySyncAction;
96 import org.objectstyle.cayenne.modeler.action.RemoveRelationshipAction;
97 import org.objectstyle.cayenne.modeler.dialog.objentity.ObjRelationshipInfoController;
98 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent;
99 import org.objectstyle.cayenne.modeler.event.ObjEntityDisplayListener;
100 import org.objectstyle.cayenne.modeler.event.RelationshipDisplayEvent;
101 import org.objectstyle.cayenne.modeler.util.CayenneTable;
102 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
103 import org.objectstyle.cayenne.modeler.util.CellRenderers;
104 import org.objectstyle.cayenne.modeler.util.ModelerUtil;
105 import org.objectstyle.cayenne.modeler.util.PanelFactory;
106 import org.objectstyle.cayenne.modeler.util.UIUtil;
107
108 /**
109  * Displays ObjRelationships for the edited ObjEntity.
110  *
111  * @author Michael Misha Shengaout
112  * @author Andrei Adamchik
113  */

114 public class ObjEntityRelationshipTab extends JPanel JavaDoc implements ObjEntityDisplayListener,
115         ObjEntityListener, ObjRelationshipListener, ExistingSelectionProcessor {
116     
117     private static final Logger logObj = Logger.getLogger(ObjEntityRelationshipTab.class);
118
119     private static final Object JavaDoc[] deleteRules = new Object JavaDoc[] {
120             DeleteRule.deleteRuleName(DeleteRule.NO_ACTION),
121             DeleteRule.deleteRuleName(DeleteRule.NULLIFY),
122             DeleteRule.deleteRuleName(DeleteRule.CASCADE),
123             DeleteRule.deleteRuleName(DeleteRule.DENY),
124     };
125
126     ProjectController mediator;
127
128     CayenneTable table;
129     JButton JavaDoc resolve;
130
131     public ObjEntityRelationshipTab(ProjectController mediator) {
132         this.mediator = mediator;
133
134         init();
135         initController();
136     }
137
138     private void init() {
139         this.setLayout(new BorderLayout JavaDoc());
140         
141         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
142         Application app = Application.getInstance();
143         toolBar.add(app.getAction(CreateRelationshipAction.getActionName()).buildButton());
144         toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton());
145
146         toolBar.addSeparator();
147
148         resolve = new JButton JavaDoc();
149         resolve.setIcon(ModelerUtil.buildIcon("icon-info.gif"));
150         resolve.setToolTipText("Edit Relationship");
151         toolBar.add(resolve);
152
153         toolBar.addSeparator();
154         
155         toolBar.add(app.getAction(RemoveRelationshipAction.getActionName()).buildButton());
156         add(toolBar, BorderLayout.NORTH);
157
158         table = new CayenneTable();
159         table.setDefaultRenderer(String JavaDoc.class, new StringRenderer());
160         table.setDefaultRenderer(ObjEntity.class, new EntityRenderer());
161
162         add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER);
163     }
164
165     private void initController() {
166         mediator.addObjEntityDisplayListener(this);
167         mediator.addObjEntityListener(this);
168         mediator.addObjRelationshipListener(this);
169
170         resolve.addActionListener(new ActionListener JavaDoc() {
171
172             public void actionPerformed(ActionEvent JavaDoc e) {
173                 int row = table.getSelectedRow();
174                 if (row < 0) {
175                     return;
176                 }
177
178                 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table
179                         .getModel();
180                 new ObjRelationshipInfoController(mediator, model.getRelationship(row))
181                         .startup();
182
183                 // need to refresh selected row... do this by unselecting/selecting the
184
// row
185
table.getSelectionModel().clearSelection();
186                 table.select(row);
187             }
188         });
189
190         table.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
191
192             public void valueChanged(ListSelectionEvent JavaDoc e) {
193                 processExistingSelection(e);
194             }
195         });
196     }
197
198     /**
199      * Selects a specified relationship in the relationships table.
200      */

201     public void selectRelationship(ObjRelationship rel) {
202         if (rel == null) {
203             Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(false);
204             return;
205         }
206         // enable the remove button
207
Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(true);
208
209         ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel();
210         java.util.List JavaDoc rels = model.getObjectList();
211         int relPos = rels.indexOf(rel);
212         if (relPos >= 0) {
213             table.select(relPos);
214         }
215     }
216     
217     public void processExistingSelection(EventObject JavaDoc e) {
218         if (e instanceof ChangeEvent JavaDoc) {
219             table.clearSelection();
220         }
221         ObjRelationship rel = null;
222         if (table.getSelectedRow() >= 0) {
223             ObjRelationshipTableModel model = (ObjRelationshipTableModel) table
224                     .getModel();
225             rel = model.getRelationship(table.getSelectedRow());
226             if (rel.getTargetEntity() != null
227                     && ((ObjEntity) rel.getSourceEntity()).getDbEntity() != null
228                     && ((ObjEntity) rel.getTargetEntity()).getDbEntity() != null) {
229                 resolve.setEnabled(true);
230             }
231             else
232                 resolve.setEnabled(false);
233
234             // scroll table
235
UIUtil.scrollToSelectedRow(table);
236         }
237         else
238             resolve.setEnabled(false);
239         
240         RelationshipDisplayEvent ev = new RelationshipDisplayEvent(this, rel, mediator
241                 .getCurrentObjEntity(), mediator.getCurrentDataMap(), mediator
242                 .getCurrentDataDomain());
243
244         mediator.fireObjRelationshipDisplayEvent(ev);
245     }
246
247     /** Loads obj relationships into table. */
248     public void currentObjEntityChanged(EntityDisplayEvent e) {
249         if (e.getSource() == this) {
250             return;
251         }
252         
253         ObjEntity entity = (ObjEntity) e.getEntity();
254         // Important: process event even if this is the same entity,
255
// since the inheritance structure might have changed
256
if (entity != null) {
257             rebuildTable(entity);
258         }
259
260         // if an entity was selected on a tree,
261
// unselect currently selected row
262
if (e.isUnselectAttributes()) {
263             table.clearSelection();
264         }
265     }
266
267     /**
268      * Creates a list of ObjEntity names.
269      */

270     private Object JavaDoc[] createObjEntityComboModel() {
271         DataMap map = mediator.getCurrentDataMap();
272         
273         // this actually happens per CAY-221... can't reproduce though
274
if(map == null) {
275             logObj.warn("createObjEntityComboModel:: Null DataMap.");
276             return new Object JavaDoc[0];
277         }
278         
279         if(map.getNamespace() == null) {
280             logObj.warn("createObjEntityComboModel:: Null DataMap namespace - " + map);
281             return new Object JavaDoc[0];
282         }
283         
284         Collection JavaDoc objEntities = map.getNamespace().getObjEntities();
285         return objEntities.toArray();
286     }
287
288     public void objEntityChanged(EntityEvent e) {
289     }
290
291     public void objEntityAdded(EntityEvent e) {
292         reloadEntityList(e);
293     }
294
295     public void objEntityRemoved(EntityEvent e) {
296         reloadEntityList(e);
297     }
298
299     public void objRelationshipChanged(RelationshipEvent e) {
300         table.select(e.getRelationship());
301     }
302
303     public void objRelationshipAdded(RelationshipEvent e) {
304         rebuildTable((ObjEntity) e.getEntity());
305         table.select(e.getRelationship());
306     }
307
308     public void objRelationshipRemoved(RelationshipEvent e) {
309         ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel();
310         int ind = model.getObjectList().indexOf(e.getRelationship());
311         model.removeRow(e.getRelationship());
312         table.select(ind);
313     }
314
315     /**
316      * Refresh the list of ObjEntity targets. Also refresh the table in case some
317      * ObjRelationships were deleted.
318      */

319     private void reloadEntityList(EntityEvent e) {
320         if (e.getSource() != this){
321             return;
322         }
323
324         // If current model added/removed, do nothing.
325
ObjEntity entity = mediator.getCurrentObjEntity();
326         if (entity == e.getEntity() || entity == null) {
327             return;
328         }
329
330         TableColumn JavaDoc col = table.getColumnModel().getColumn(
331                 ObjRelationshipTableModel.REL_TARGET);
332         DefaultCellEditor JavaDoc editor = (DefaultCellEditor JavaDoc) col.getCellEditor();
333
334         JComboBox JavaDoc combo = (JComboBox JavaDoc) editor.getComponent();
335         combo.setRenderer(CellRenderers.entityListRendererWithIcons(entity.getDataMap()));
336         combo.setModel(new DefaultComboBoxModel JavaDoc(createObjEntityComboModel()));
337
338         ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel();
339         model.fireTableDataChanged();
340     }
341
342     protected void rebuildTable(ObjEntity entity) {
343         final ObjRelationshipTableModel model = new ObjRelationshipTableModel(
344                 entity,
345                 mediator,
346                 this);
347
348         model.addTableModelListener(new TableModelListener JavaDoc() {
349
350             public void tableChanged(TableModelEvent JavaDoc e) {
351                 if (table.getSelectedRow() >= 0) {
352                     ObjRelationship rel = model.getRelationship(table.getSelectedRow());
353                     if (rel.getTargetEntity() != null
354                             && ((ObjEntity) rel.getSourceEntity()).getDbEntity() != null
355                             && ((ObjEntity) rel.getTargetEntity()).getDbEntity() != null) {
356                         resolve.setEnabled(true);
357                     }
358                     else
359                         resolve.setEnabled(false);
360                 }
361             }
362         });
363
364         table.setModel(model);
365         table.setRowHeight(25);
366         table.setRowMargin(3);
367
368         TableColumn JavaDoc lockColumn = table.getColumnModel().getColumn(
369                 ObjRelationshipTableModel.REL_LOCKING);
370         lockColumn.setMinWidth(100);
371
372         TableColumn JavaDoc col = table.getColumnModel().getColumn(
373                 ObjRelationshipTableModel.REL_NAME);
374         col.setMinWidth(150);
375
376         col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_TARGET);
377         col.setMinWidth(150);
378         JComboBox JavaDoc targetCombo = CayenneWidgetFactory.createComboBox(
379                 createObjEntityComboModel(),
380                 false);
381         targetCombo.setRenderer(CellRenderers.entityListRendererWithIcons(entity
382                 .getDataMap()));
383         targetCombo.setEditable(false);
384         targetCombo.setSelectedIndex(-1);
385         DefaultCellEditor JavaDoc editor = new DefaultCellEditor JavaDoc(targetCombo);
386         editor.setClickCountToStart(1);
387         col.setCellEditor(editor);
388
389         col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_CARDINALITY);
390         col.setMinWidth(150);
391
392         col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_DELETERULE);
393         col.setMinWidth(60);
394         JComboBox JavaDoc deleteRulesCombo = CayenneWidgetFactory.createComboBox(
395                 deleteRules,
396                 false);
397         deleteRulesCombo.setEditable(false);
398         deleteRulesCombo.setSelectedIndex(0); //Default to the first value
399
editor = new DefaultCellEditor JavaDoc(deleteRulesCombo);
400         editor.setClickCountToStart(1);
401         col.setCellEditor(editor);
402     }
403
404     class EntityRenderer extends StringRenderer {
405
406         public Component JavaDoc getTableCellRendererComponent(
407                 JTable JavaDoc table,
408                 Object JavaDoc value,
409                 boolean isSelected,
410                 boolean hasFocus,
411                 int row,
412                 int column) {
413
414             if (value instanceof MapObject) {
415                 MapObject mapObject = (MapObject) value;
416                 String JavaDoc label = mapObject.getName();
417
418                 if (mapObject instanceof Entity) {
419                     Entity entity = (Entity) mapObject;
420
421                     // for different namespace display its name
422
DataMap dataMap = entity.getDataMap();
423                     if (dataMap != null && dataMap != mediator.getCurrentDataMap()) {
424                         label += " (" + dataMap.getName() + ")";
425                     }
426                 }
427
428                 value = label;
429             }
430
431             return super.getTableCellRendererComponent(
432                     table,
433                     value,
434                     isSelected,
435                     hasFocus,
436                     row,
437                     column);
438         }
439     }
440
441     class StringRenderer extends DefaultTableCellRenderer JavaDoc {
442
443         public Component JavaDoc getTableCellRendererComponent(
444                 JTable JavaDoc table,
445                 Object JavaDoc value,
446                 boolean isSelected,
447                 boolean hasFocus,
448                 int row,
449                 int column) {
450
451             super.getTableCellRendererComponent(
452                     table,
453                     value,
454                     isSelected,
455                     hasFocus,
456                     row,
457                     column);
458
459             ObjRelationshipTableModel model = (ObjRelationshipTableModel) table
460                     .getModel();
461             ObjRelationship relationship = model.getRelationship(row);
462
463             if (relationship != null
464                     && relationship.getSourceEntity() != model.getEntity()) {
465                 setForeground(Color.GRAY);
466             }
467             else {
468                 setForeground(isSelected && !hasFocus
469                         ? table.getSelectionForeground()
470                         : table.getForeground());
471             }
472
473             return this;
474         }
475     }
476 }
Popular Tags