KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.EventObject JavaDoc;
62
63 import javax.swing.DefaultCellEditor JavaDoc;
64 import javax.swing.JComboBox JavaDoc;
65 import javax.swing.JPanel JavaDoc;
66 import javax.swing.JTable JavaDoc;
67 import javax.swing.JToolBar JavaDoc;
68 import javax.swing.event.ChangeEvent JavaDoc;
69 import javax.swing.event.ListSelectionEvent JavaDoc;
70 import javax.swing.event.ListSelectionListener JavaDoc;
71 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
72 import javax.swing.table.TableColumn JavaDoc;
73
74 import org.objectstyle.cayenne.map.ObjAttribute;
75 import org.objectstyle.cayenne.map.ObjEntity;
76 import org.objectstyle.cayenne.map.event.AttributeEvent;
77 import org.objectstyle.cayenne.map.event.EntityEvent;
78 import org.objectstyle.cayenne.map.event.ObjAttributeListener;
79 import org.objectstyle.cayenne.map.event.ObjEntityListener;
80 import org.objectstyle.cayenne.modeler.Application;
81 import org.objectstyle.cayenne.modeler.ProjectController;
82 import org.objectstyle.cayenne.modeler.action.CreateAttributeAction;
83 import org.objectstyle.cayenne.modeler.action.ObjEntitySyncAction;
84 import org.objectstyle.cayenne.modeler.action.RemoveAttributeAction;
85 import org.objectstyle.cayenne.modeler.event.AttributeDisplayEvent;
86 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent;
87 import org.objectstyle.cayenne.modeler.event.ObjEntityDisplayListener;
88 import org.objectstyle.cayenne.modeler.util.CayenneTable;
89 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
90 import org.objectstyle.cayenne.modeler.util.ModelerUtil;
91 import org.objectstyle.cayenne.modeler.util.PanelFactory;
92 import org.objectstyle.cayenne.modeler.util.UIUtil;
93
94 /**
95  * Detail view of the ObjEntity attributes.
96  *
97  * @author Michael Misha Shengaout
98  * @author Andrei Adamchik
99  */

100 public class ObjEntityAttributeTab extends JPanel JavaDoc implements ObjEntityDisplayListener,
101         ObjEntityListener, ObjAttributeListener, ExistingSelectionProcessor {
102
103     protected ProjectController mediator;
104     protected CayenneTable table;
105
106     public ObjEntityAttributeTab(ProjectController mediator) {
107         this.mediator = mediator;
108
109         init();
110         initController();
111     }
112
113     private void init() {
114         this.setLayout(new BorderLayout JavaDoc());
115         
116         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
117         Application app = Application.getInstance();
118         toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton());
119         toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton());
120         toolBar.addSeparator();
121         toolBar.add(app.getAction(RemoveAttributeAction.getActionName()).buildButton());
122         add(toolBar, BorderLayout.NORTH);
123
124         table = new CayenneTable();
125         table.setDefaultRenderer(String JavaDoc.class, new CellRenderer JavaDoc());
126
127         add(PanelFactory.createTablePanel(table, null) , BorderLayout.CENTER);
128     }
129
130     private void initController() {
131         mediator.addObjEntityDisplayListener(this);
132         mediator.addObjEntityListener(this);
133         mediator.addObjAttributeListener(this);
134
135         table.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
136
137             public void valueChanged(ListSelectionEvent JavaDoc e) {
138                 processExistingSelection(e);
139             }
140         });
141     }
142
143     /**
144      * Selects a specified attribute.
145      */

146     public void selectAttribute(ObjAttribute attr) {
147         if (attr == null) {
148             Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(false);
149             return;
150         }
151         // enable the remove button
152
Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(true);
153
154         ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel();
155         java.util.List JavaDoc attrs = model.getObjectList();
156         int attrPos = attrs.indexOf(attr);
157         if (attrPos >= 0) {
158             table.select(attrPos);
159         }
160     }
161     
162     public void processExistingSelection(EventObject JavaDoc e) {
163         if (e instanceof ChangeEvent JavaDoc) {
164             table.clearSelection();
165         }
166         ObjAttribute attribute = null;
167         if (table.getSelectedRow() >= 0) {
168             ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel();
169             attribute = model.getAttribute(table.getSelectedRow());
170
171             // scroll table
172
UIUtil.scrollToSelectedRow(table);
173         }
174
175         AttributeDisplayEvent ev = new AttributeDisplayEvent(this, attribute, mediator
176                 .getCurrentObjEntity(), mediator.getCurrentDataMap(), mediator
177                 .getCurrentDataDomain());
178
179         mediator.fireObjAttributeDisplayEvent(ev);
180     }
181
182     public void objAttributeChanged(AttributeEvent e) {
183         table.select(e.getAttribute());
184     }
185
186     public void objAttributeAdded(AttributeEvent e) {
187         rebuildTable((ObjEntity) e.getEntity());
188         table.select(e.getAttribute());
189     }
190
191     public void objAttributeRemoved(AttributeEvent e) {
192         ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel();
193         int ind = model.getObjectList().indexOf(e.getAttribute());
194         model.removeRow(e.getAttribute());
195         table.select(ind);
196     }
197
198     public void currentObjEntityChanged(EntityDisplayEvent e) {
199         if (e.getSource() == this) {
200             return;
201         }
202
203         ObjEntity entity = (ObjEntity) e.getEntity();
204
205         // Important: process event even if this is the same entity,
206
// since the inheritance structure might have changed
207
if (entity != null) {
208             rebuildTable(entity);
209         }
210
211         // if an entity was selected on a tree,
212
// unselect currently selected row
213
if (e.isUnselectAttributes()) {
214             table.clearSelection();
215         }
216     }
217
218     protected void rebuildTable(ObjEntity entity) {
219         ObjAttributeTableModel model = new ObjAttributeTableModel(entity, mediator, this);
220         table.setModel(model);
221         table.setRowHeight(25);
222         table.setRowMargin(3);
223         setUpTableStructure(model);
224     }
225
226     protected void setUpTableStructure(ObjAttributeTableModel model) {
227         TableColumn JavaDoc nameColumn = table.getColumnModel().getColumn(
228                 ObjAttributeTableModel.OBJ_ATTRIBUTE);
229         nameColumn.setMinWidth(150);
230
231         TableColumn JavaDoc typeColumn = table.getColumnModel().getColumn(
232                 ObjAttributeTableModel.OBJ_ATTRIBUTE_TYPE);
233         typeColumn.setMinWidth(150);
234
235         JComboBox JavaDoc javaTypesCombo = CayenneWidgetFactory.createComboBox(ModelerUtil
236                 .getRegisteredTypeNames(), false);
237         javaTypesCombo.setEditable(true);
238         typeColumn.setCellEditor(new DefaultCellEditor JavaDoc(javaTypesCombo));
239
240         TableColumn JavaDoc lockColumn = table.getColumnModel().getColumn(
241                 ObjAttributeTableModel.LOCKING);
242         lockColumn.setMinWidth(100);
243
244         TableColumn JavaDoc dbTypeColumn = table.getColumnModel().getColumn(
245                 ObjAttributeTableModel.DB_ATTRIBUTE_TYPE);
246         dbTypeColumn.setMinWidth(120);
247
248         TableColumn JavaDoc dbNameColumn = table.getColumnModel().getColumn(
249                 ObjAttributeTableModel.DB_ATTRIBUTE);
250         dbNameColumn.setMinWidth(150);
251
252         if (model.getEntity().getDbEntity() != null) {
253             JComboBox JavaDoc dbAttributesCombo = CayenneWidgetFactory.createComboBox(ModelerUtil
254                     .getDbAttributeNames(mediator, mediator
255                             .getCurrentObjEntity()
256                             .getDbEntity()), true);
257
258             dbAttributesCombo.setEditable(false);
259             dbNameColumn.setCellEditor(new DefaultCellEditor JavaDoc(dbAttributesCombo));
260         }
261     }
262
263     /**
264      * Refreshes attributes view for the updated entity
265      */

266     public void objEntityChanged(EntityEvent e) {
267         if (e.getSource() == this) {
268             return;
269         }
270
271         ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel();
272         if (model.getDbEntity() != ((ObjEntity) e.getEntity()).getDbEntity()) {
273             model.resetDbEntity();
274             setUpTableStructure(model);
275         }
276     }
277
278     public void objEntityAdded(EntityEvent e) {
279     }
280
281     public void objEntityRemoved(EntityEvent e) {
282     }
283
284     // custom renderer used for inherited attributes highlighting
285
final class CellRenderer extends DefaultTableCellRenderer JavaDoc {
286
287         public Component JavaDoc getTableCellRendererComponent(
288                 JTable JavaDoc table,
289                 Object JavaDoc value,
290                 boolean isSelected,
291                 boolean hasFocus,
292                 int row,
293                 int column) {
294
295             super.getTableCellRendererComponent(
296                     table,
297                     value,
298                     isSelected,
299                     hasFocus,
300                     row,
301                     column);
302
303             ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel();
304             ObjAttribute attribute = model.getAttribute(row);
305
306             if (attribute != null && attribute.getEntity() != model.getEntity()) {
307                 setForeground(Color.GRAY);
308             }
309             else {
310                 setForeground(isSelected && !hasFocus
311                         ? table.getSelectionForeground()
312                         : table.getForeground());
313             }
314
315             return this;
316         }
317     }
318 }
Popular Tags