KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
60 import java.awt.event.ActionListener JavaDoc;
61 import java.util.EventObject JavaDoc;
62
63 import javax.swing.DefaultCellEditor JavaDoc;
64 import javax.swing.JButton JavaDoc;
65 import javax.swing.JComboBox JavaDoc;
66 import javax.swing.JPanel 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.TableColumn JavaDoc;
72
73 import org.objectstyle.cayenne.dba.TypesMapping;
74 import org.objectstyle.cayenne.map.DbAttribute;
75 import org.objectstyle.cayenne.map.DbEntity;
76 import org.objectstyle.cayenne.map.DerivedDbAttribute;
77 import org.objectstyle.cayenne.map.DerivedDbEntity;
78 import org.objectstyle.cayenne.map.event.AttributeEvent;
79 import org.objectstyle.cayenne.map.event.DbAttributeListener;
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.DbEntitySyncAction;
84 import org.objectstyle.cayenne.modeler.action.RemoveAttributeAction;
85 import org.objectstyle.cayenne.modeler.dialog.EditDerivedParamsDialog;
86 import org.objectstyle.cayenne.modeler.event.AttributeDisplayEvent;
87 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener;
88 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent;
89 import org.objectstyle.cayenne.modeler.util.CayenneTable;
90 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
91 import org.objectstyle.cayenne.modeler.util.ModelerUtil;
92 import org.objectstyle.cayenne.modeler.util.PanelFactory;
93 import org.objectstyle.cayenne.modeler.util.UIUtil;
94
95 /**
96  * Detail view of the DbEntity attributes.
97  *
98  * @author Michael Misha Shengaout
99  * @author Andrei Adamchik
100  */

101 public class DbEntityAttributeTab
102     extends JPanel JavaDoc
103     implements
104         DbEntityDisplayListener,
105         ListSelectionListener JavaDoc,
106         DbAttributeListener,
107         ExistingSelectionProcessor,
108         ActionListener JavaDoc {
109
110     protected ProjectController mediator;
111     protected CayenneTable table;
112     protected JButton JavaDoc editParams;
113
114     public DbEntityAttributeTab(ProjectController temp_mediator) {
115         super();
116         mediator = temp_mediator;
117         mediator.addDbEntityDisplayListener(this);
118         mediator.addDbAttributeListener(this);
119
120         // Create and layout components
121
init();
122
123         editParams.addActionListener(this);
124     }
125
126     private void init() {
127         this.setLayout(new BorderLayout JavaDoc());
128         
129         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
130         Application app = Application.getInstance();
131         toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton());
132         toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton());
133         
134         toolBar.addSeparator();
135
136         /*
137          * Not sure why this will not show up???
138          */

139         editParams = new JButton JavaDoc();
140         editParams.setIcon(ModelerUtil.buildIcon("icon-info.gif"));
141         editParams.setToolTipText("Edit Parameters");
142         toolBar.add(editParams);
143
144         toolBar.addSeparator();
145         
146         toolBar.add(app.getAction(RemoveAttributeAction.getActionName()).buildButton());
147         
148         add(toolBar, BorderLayout.NORTH);
149
150         // Create table with two columns and no rows.
151
table = new CayenneTable();
152         add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER);
153     }
154
155     public void actionPerformed(ActionEvent JavaDoc e) {
156         if (e.getSource() == editParams) {
157             int row = table.getSelectedRow();
158             if (row >= 0) {
159                 DbAttribute attr =
160                     ((DbAttributeTableModel) table.getModel()).getAttribute(row);
161
162                 EditDerivedParamsDialog dialog =
163                     new EditDerivedParamsDialog((DerivedDbAttribute) attr);
164                 dialog.setVisible(true);
165                 dialog.dispose();
166             }
167         }
168     }
169
170     public void valueChanged(ListSelectionEvent JavaDoc e) {
171         processExistingSelection(e);
172     }
173
174     /**
175       * Selects a specified attribute.
176       */

177     public void selectAttribute(DbAttribute attr) {
178         if (attr == null) {
179             Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(false);
180             return;
181         }
182         // enable the remove button
183
Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(true);
184
185         DbAttributeTableModel model = (DbAttributeTableModel) table.getModel();
186         java.util.List JavaDoc attrs = model.getObjectList();
187         int attrPos = attrs.indexOf(attr);
188         if (attrPos >= 0) {
189             table.select(attrPos);
190         }
191     }
192         
193     public void processExistingSelection(EventObject JavaDoc e) {
194         if (e instanceof ChangeEvent JavaDoc) {
195             table.clearSelection();
196         }
197         DbAttribute att = null;
198         if (table.getSelectedRow() >= 0) {
199             DbAttributeTableModel model = (DbAttributeTableModel) table.getModel();
200             att = model.getAttribute(table.getSelectedRow());
201             editParams.setEnabled(att instanceof DerivedDbAttribute);
202
203             // scroll table
204
UIUtil.scrollToSelectedRow(table);
205         }
206
207         mediator.fireDbAttributeDisplayEvent(
208             new AttributeDisplayEvent(
209                 this,
210                 att,
211                 mediator.getCurrentDbEntity(),
212                 mediator.getCurrentDataMap(),
213                 mediator.getCurrentDataDomain()));
214     }
215
216     public void dbAttributeChanged(AttributeEvent e) {
217         table.select(e.getAttribute());
218     }
219
220     public void dbAttributeAdded(AttributeEvent e) {
221         rebuildTable((DbEntity) e.getEntity());
222         table.select(e.getAttribute());
223     }
224
225     public void dbAttributeRemoved(AttributeEvent e) {
226         DbAttributeTableModel model = (DbAttributeTableModel) table.getModel();
227         int ind = model.getObjectList().indexOf(e.getAttribute());
228         model.removeRow(e.getAttribute());
229         table.select(ind);
230     }
231
232     public void currentDbEntityChanged(EntityDisplayEvent e) {
233         DbEntity entity = (DbEntity) e.getEntity();
234         if (entity != null && e.isEntityChanged()) {
235             rebuildTable(entity);
236         }
237
238         // if an entity was selected on a tree,
239
// unselect currently selected row
240
if (e.isUnselectAttributes()) {
241             table.clearSelection();
242         }
243     }
244
245     protected void rebuildTable(DbEntity ent) {
246         editParams.setVisible(ent instanceof DerivedDbEntity);
247         editParams.setEnabled(false);
248
249         DbAttributeTableModel model =
250             (ent instanceof DerivedDbEntity)
251                 ? new DerivedDbAttributeTableModel(ent, mediator, this)
252                 : new DbAttributeTableModel(ent, mediator, this);
253         table.setModel(model);
254         table.setRowHeight(25);
255         table.setRowMargin(3);
256         TableColumn JavaDoc col = table.getColumnModel().getColumn(model.nameColumnInd());
257         col.setMinWidth(150);
258
259         col = table.getColumnModel().getColumn(model.typeColumnInd());
260         col.setMinWidth(90);
261
262         String JavaDoc[] types = TypesMapping.getDatabaseTypes();
263         JComboBox JavaDoc comboBox = CayenneWidgetFactory.createComboBox(types, true);
264         comboBox.setEditable(true);
265         col.setCellEditor(new DefaultCellEditor JavaDoc(comboBox));
266
267         table.getSelectionModel().addListSelectionListener(this);
268     }
269 }
270
Popular Tags