KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.EventObject JavaDoc;
63
64 import javax.swing.ComboBoxModel JavaDoc;
65 import javax.swing.DefaultCellEditor JavaDoc;
66 import javax.swing.DefaultComboBoxModel JavaDoc;
67 import javax.swing.JButton JavaDoc;
68 import javax.swing.JComboBox JavaDoc;
69 import javax.swing.JPanel JavaDoc;
70 import javax.swing.JTable JavaDoc;
71 import javax.swing.JToolBar JavaDoc;
72 import javax.swing.event.ChangeEvent JavaDoc;
73 import javax.swing.event.ListSelectionEvent JavaDoc;
74 import javax.swing.event.ListSelectionListener JavaDoc;
75 import javax.swing.event.TableModelEvent JavaDoc;
76 import javax.swing.event.TableModelListener JavaDoc;
77 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
78 import javax.swing.table.TableColumn JavaDoc;
79
80 import org.objectstyle.cayenne.map.DataMap;
81 import org.objectstyle.cayenne.map.DbEntity;
82 import org.objectstyle.cayenne.map.DbRelationship;
83 import org.objectstyle.cayenne.map.Entity;
84 import org.objectstyle.cayenne.map.MapObject;
85 import org.objectstyle.cayenne.map.event.DbEntityListener;
86 import org.objectstyle.cayenne.map.event.DbRelationshipListener;
87 import org.objectstyle.cayenne.map.event.EntityEvent;
88 import org.objectstyle.cayenne.map.event.RelationshipEvent;
89 import org.objectstyle.cayenne.modeler.Application;
90 import org.objectstyle.cayenne.modeler.ProjectController;
91 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction;
92 import org.objectstyle.cayenne.modeler.action.DbEntitySyncAction;
93 import org.objectstyle.cayenne.modeler.action.RemoveRelationshipAction;
94 import org.objectstyle.cayenne.modeler.dialog.ResolveDbRelationshipDialog;
95 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener;
96 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent;
97 import org.objectstyle.cayenne.modeler.event.RelationshipDisplayEvent;
98 import org.objectstyle.cayenne.modeler.util.CayenneTable;
99 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
100 import org.objectstyle.cayenne.modeler.util.CellRenderers;
101 import org.objectstyle.cayenne.modeler.util.ModelerUtil;
102 import org.objectstyle.cayenne.modeler.util.PanelFactory;
103 import org.objectstyle.cayenne.modeler.util.UIUtil;
104
105 /**
106  * Displays DbRelationships for the current DbEntity.
107  *
108  * @author Michael Misha Shengaout
109  * @author Andrei Adamchik
110  */

111 public class DbEntityRelationshipTab extends JPanel JavaDoc implements ActionListener JavaDoc,
112         DbEntityDisplayListener, DbEntityListener, DbRelationshipListener,
113         ExistingSelectionProcessor, ListSelectionListener JavaDoc, TableModelListener JavaDoc {
114
115     protected ProjectController mediator;
116     protected CayenneTable table;
117     protected JButton JavaDoc resolve;
118
119     public DbEntityRelationshipTab(ProjectController mediator) {
120         super();
121
122         this.mediator = mediator;
123         this.mediator.addDbEntityDisplayListener(this);
124         this.mediator.addDbEntityListener(this);
125         this.mediator.addDbRelationshipListener(this);
126
127         init();
128         resolve.addActionListener(this);
129     }
130
131     protected void init() {
132         this.setLayout(new BorderLayout JavaDoc());
133         
134         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
135         Application app = Application.getInstance();
136         toolBar.add(app.getAction(CreateRelationshipAction.getActionName()).buildButton());
137         toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton());
138         
139         toolBar.addSeparator();
140
141         resolve = new JButton JavaDoc();
142         resolve.setIcon(ModelerUtil.buildIcon("icon-info.gif"));
143         resolve.setToolTipText("Database Mapping");
144         toolBar.add(resolve);
145
146         toolBar.addSeparator();
147
148         toolBar.add(app.getAction(RemoveRelationshipAction.getActionName()).buildButton());
149         add(toolBar, BorderLayout.NORTH);
150
151         table = new CayenneTable();
152         table.setDefaultRenderer(DbEntity.class, new EntityRenderer());
153
154         add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER);
155     }
156
157     public void actionPerformed(ActionEvent JavaDoc e) {
158         Object JavaDoc src = e.getSource();
159         if (src == resolve) {
160             resolveRelationship();
161         }
162     }
163
164     public void valueChanged(ListSelectionEvent JavaDoc e) {
165         processExistingSelection(e);
166     }
167
168     public void tableChanged(TableModelEvent JavaDoc e) {
169         DbRelationship rel = null;
170         if (table.getSelectedRow() >= 0) {
171             DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
172             rel = model.getRelationship(table.getSelectedRow());
173             resolve.setEnabled(rel.getTargetEntity() != null);
174         }
175     }
176
177     /**
178      * Selects a specified relationship in the relationships table.
179      */

180     public void selectRelationship(DbRelationship rel) {
181         if (rel == null) {
182             Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(false);
183             return;
184         }
185         // enable the remove button
186
Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(true);
187
188         DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
189         java.util.List JavaDoc rels = model.getObjectList();
190         int relPos = rels.indexOf(rel);
191         if (relPos >= 0) {
192             table.select(relPos);
193         }
194     }
195     
196     public void processExistingSelection(EventObject JavaDoc e) {
197         if (e instanceof ChangeEvent JavaDoc) {
198             table.clearSelection();
199         }
200         DbRelationship rel = null;
201         if (table.getSelectedRow() >= 0) {
202             DbRelationshipTableModel model;
203             model = (DbRelationshipTableModel) table.getModel();
204             rel = model.getRelationship(table.getSelectedRow());
205             resolve.setEnabled(rel.getTargetEntity() != null);
206
207             // scroll table
208
UIUtil.scrollToSelectedRow(table);
209         }
210         else
211             resolve.setEnabled(false);
212
213         RelationshipDisplayEvent ev = new RelationshipDisplayEvent(this, rel, mediator
214                 .getCurrentDbEntity(), mediator.getCurrentDataMap(), mediator
215                 .getCurrentDataDomain());
216
217         mediator.fireDbRelationshipDisplayEvent(ev);
218     }
219
220     private void resolveRelationship() {
221         int row = table.getSelectedRow();
222         if (row < 0) {
223             return;
224         }
225
226         // Get DbRelationship
227
DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
228         DbRelationship rel = model.getRelationship(row);
229         ResolveDbRelationshipDialog dialog = new ResolveDbRelationshipDialog(rel);
230         dialog.setVisible(true);
231         dialog.dispose();
232     }
233
234     /** Loads obj relationships into table. */
235     public void currentDbEntityChanged(EntityDisplayEvent e) {
236         DbEntity entity = (DbEntity) e.getEntity();
237         if (entity != null && e.isEntityChanged()) {
238             rebuildTable(entity);
239         }
240
241         // if an entity was selected on a tree,
242
// unselect currently selected row
243
if (e.isUnselectAttributes()) {
244             table.clearSelection();
245         }
246     }
247
248     protected void rebuildTable(DbEntity entity) {
249         DbRelationshipTableModel model = new DbRelationshipTableModel(
250                 entity,
251                 mediator,
252                 this);
253         model.addTableModelListener(this);
254         table.setModel(model);
255         table.setRowHeight(25);
256         table.setRowMargin(3);
257         TableColumn JavaDoc col = table.getColumnModel().getColumn(DbRelationshipTableModel.NAME);
258         col.setMinWidth(150);
259         col = table.getColumnModel().getColumn(DbRelationshipTableModel.TARGET);
260         col.setMinWidth(150);
261
262         JComboBox JavaDoc combo = CayenneWidgetFactory.createComboBox();
263         combo.setRenderer(CellRenderers.entityListRendererWithIcons(entity.getDataMap()));
264         combo.setModel(createComboModel());
265         combo.setEditable(false);
266         col.setCellEditor(new DefaultCellEditor JavaDoc(combo));
267         table.getSelectionModel().addListSelectionListener(this);
268     }
269
270     public void dbEntityChanged(EntityEvent e) {
271     }
272
273     public void dbEntityAdded(EntityEvent e) {
274         reloadEntityList(e);
275     }
276
277     public void dbEntityRemoved(EntityEvent e) {
278         reloadEntityList(e);
279     }
280
281     public void dbRelationshipChanged(RelationshipEvent e) {
282         if (e.getSource() != this) {
283             if (!(table.getModel() instanceof DbRelationshipTableModel)) {
284                 rebuildTable((DbEntity) e.getEntity());
285             }
286
287             table.select(e.getRelationship());
288             DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
289             model.fireTableDataChanged();
290         }
291     }
292
293     public void dbRelationshipAdded(RelationshipEvent e) {
294         rebuildTable((DbEntity) e.getEntity());
295         table.select(e.getRelationship());
296     }
297
298     public void dbRelationshipRemoved(RelationshipEvent e) {
299         DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
300         int ind = model.getObjectList().indexOf(e.getRelationship());
301         model.removeRelationship(e.getRelationship());
302         table.select(ind);
303     }
304
305     /**
306      * Refresh the list of db entities (targets). Also refresh the table in case some db
307      * relationships were deleted.
308      */

309     private void reloadEntityList(EntityEvent e) {
310         if (e.getSource() == this)
311             return;
312         // If current model added/removed, do nothing.
313
if (mediator.getCurrentDbEntity() == e.getEntity())
314             return;
315         // If this is just loading new currentDbEntity, do nothing
316
if (mediator.getCurrentDbEntity() == null)
317             return;
318         TableColumn JavaDoc col = table.getColumnModel().getColumn(
319                 DbRelationshipTableModel.TARGET);
320         DefaultCellEditor JavaDoc editor = (DefaultCellEditor JavaDoc) col.getCellEditor();
321         JComboBox JavaDoc combo = (JComboBox JavaDoc) editor.getComponent();
322         combo.setModel(createComboModel());
323
324         DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
325         model.fireTableDataChanged();
326         table.getSelectionModel().addListSelectionListener(this);
327     }
328
329     /**
330      * Creates a list of DbEntities.
331      */

332     private ComboBoxModel JavaDoc createComboModel() {
333         DataMap map = mediator.getCurrentDataMap();
334         Object JavaDoc[] objects = map.getNamespace().getDbEntities().toArray();
335         return new DefaultComboBoxModel JavaDoc(objects);
336     }
337
338     class EntityRenderer extends DefaultTableCellRenderer JavaDoc {
339
340         public Component JavaDoc getTableCellRendererComponent(
341                 JTable JavaDoc table,
342                 Object JavaDoc value,
343                 boolean isSelected,
344                 boolean hasFocus,
345                 int row,
346                 int column) {
347
348             if (value instanceof MapObject) {
349                 MapObject mapObject = (MapObject) value;
350                 String JavaDoc label = mapObject.getName();
351
352                 if (mapObject instanceof Entity) {
353                     Entity entity = (Entity) mapObject;
354
355                     // for different namespace display its name
356
DataMap dataMap = entity.getDataMap();
357                     if (dataMap != null && dataMap != mediator.getCurrentDataMap()) {
358                         label += " (" + dataMap.getName() + ")";
359                     }
360                 }
361
362                 value = label;
363             }
364
365             super.getTableCellRendererComponent(
366                     table,
367                     value,
368                     isSelected,
369                     hasFocus,
370                     row,
371                     column);
372
373             return this;
374         }
375     }
376
377 }
Popular Tags