KickJava   Java API By Example, From Geeks To Geeks.

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


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.modeler.editor;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.Collection JavaDoc;
64 import java.util.EventObject JavaDoc;
65 import java.util.Iterator JavaDoc;
66
67 import javax.swing.DefaultComboBoxModel JavaDoc;
68 import javax.swing.InputVerifier JavaDoc;
69 import javax.swing.JButton JavaDoc;
70 import javax.swing.JCheckBox JavaDoc;
71 import javax.swing.JComboBox JavaDoc;
72 import javax.swing.JComponent JavaDoc;
73 import javax.swing.JLabel JavaDoc;
74 import javax.swing.JPanel JavaDoc;
75 import javax.swing.JTextField JavaDoc;
76 import javax.swing.JToolBar JavaDoc;
77
78 import org.objectstyle.cayenne.access.DataDomain;
79 import org.objectstyle.cayenne.dba.JdbcPkGenerator;
80 import org.objectstyle.cayenne.map.DataMap;
81 import org.objectstyle.cayenne.map.DbEntity;
82 import org.objectstyle.cayenne.map.DbKeyGenerator;
83 import org.objectstyle.cayenne.map.DerivedDbEntity;
84 import org.objectstyle.cayenne.map.event.EntityEvent;
85 import org.objectstyle.cayenne.modeler.Application;
86 import org.objectstyle.cayenne.modeler.ProjectController;
87 import org.objectstyle.cayenne.modeler.action.CreateAttributeAction;
88 import org.objectstyle.cayenne.modeler.action.CreateObjEntityAction;
89 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction;
90 import org.objectstyle.cayenne.modeler.action.DbEntitySyncAction;
91 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener;
92 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent;
93 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
94 import org.objectstyle.cayenne.modeler.util.CellRenderers;
95 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
96 import org.objectstyle.cayenne.modeler.util.TextAdapter;
97 import org.objectstyle.cayenne.util.Util;
98 import org.objectstyle.cayenne.validation.ValidationException;
99
100 import com.jgoodies.forms.builder.DefaultFormBuilder;
101 import com.jgoodies.forms.layout.FormLayout;
102
103 /**
104  * Detail view of the DbEntity properties.
105  *
106  * @author Michael Misha Shengaout
107  * @author Andrei Adamchik
108  */

109 public class DbEntityTab extends JPanel JavaDoc implements ExistingSelectionProcessor,
110         DbEntityDisplayListener, ActionListener JavaDoc {
111
112     protected ProjectController mediator;
113
114     protected TextAdapter name;
115     protected JTextField JavaDoc schema;
116     protected JComboBox JavaDoc parentEntities;
117     protected JButton JavaDoc parentLabel;
118     protected JLabel JavaDoc schemaLabel;
119
120     protected JCheckBox JavaDoc customPKGenerator;
121     protected JLabel JavaDoc customPKGeneratorLabel;
122     protected JLabel JavaDoc customPKGeneratorNote;
123     protected JLabel JavaDoc customPKGeneratorNameLabel;
124     protected JLabel JavaDoc customPKSizeLabel;
125     protected JTextField JavaDoc customPKName;
126     protected JTextField JavaDoc customPKSize;
127
128     public DbEntityTab(ProjectController mediator) {
129         super();
130         this.mediator = mediator;
131
132         initView();
133         initController();
134     }
135
136     private void initView() {
137         this.setLayout(new BorderLayout JavaDoc());
138         
139         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
140         Application app = Application.getInstance();
141         toolBar.add(app.getAction(CreateObjEntityAction.getActionName()).buildButton());
142         toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton());
143         toolBar.addSeparator();
144
145         toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton());
146         toolBar.add(app.getAction(CreateRelationshipAction.getActionName()).buildButton());
147         add(toolBar, BorderLayout.NORTH);
148
149         // create widgets
150
name = new TextAdapter(new JTextField JavaDoc()) {
151
152             protected void updateModel(String JavaDoc text) {
153                 setEntityName(text);
154             }
155         };
156         schemaLabel = CayenneWidgetFactory.createLabel("Schema:");
157         schema = CayenneWidgetFactory.createTextField();
158
159         parentLabel = CayenneWidgetFactory.createLabelButton("Parent DbEntity:");
160         parentLabel.setEnabled(false);
161
162         parentEntities = CayenneWidgetFactory.createComboBox();
163         parentEntities.setEditable(false);
164         parentEntities.setEnabled(false);
165
166         customPKGenerator = new JCheckBox JavaDoc();
167         customPKGeneratorLabel = CayenneWidgetFactory
168                 .createLabel("Customize primary key generation");
169         customPKGeneratorNote = CayenneWidgetFactory
170                 .createLabel("(currently ignored by all adapters except Oracle)");
171         customPKGeneratorNote.setFont(customPKGeneratorNote.getFont().deriveFont(10));
172         customPKGeneratorNameLabel = CayenneWidgetFactory
173                 .createLabel("Database object name: ");
174         customPKSizeLabel = CayenneWidgetFactory.createLabel("Cached PK Size: ");
175
176         customPKName = CayenneWidgetFactory.createTextField();
177         customPKSize = CayenneWidgetFactory.createTextField();
178
179         // assemble
180
FormLayout layout = new FormLayout(
181                 "right:max(50dlu;pref), 3dlu, fill:max(200dlu;pref)",
182                 "");
183         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
184         builder.setDefaultDialogBorder();
185
186         builder.appendSeparator("DbEntity Configuration");
187         builder.append("DbEntity name:", name.getComponent());
188         builder.append(schemaLabel, schema);
189         builder.append(parentLabel, parentEntities);
190
191         builder.appendSeparator("PK Generation");
192         builder.append(customPKGenerator, customPKGeneratorLabel);
193         builder.append("", customPKGeneratorNote);
194         builder.append(customPKGeneratorNameLabel, customPKName);
195         builder.append(customPKSizeLabel, customPKSize);
196
197         add(builder.getPanel(), BorderLayout.CENTER);
198     }
199
200     private void initController() {
201         mediator.addDbEntityDisplayListener(this);
202         InputVerifier JavaDoc inputCheck = new FieldVerifier();
203         schema.setInputVerifier(inputCheck);
204         customPKName.setInputVerifier(inputCheck);
205         customPKSize.setInputVerifier(inputCheck);
206
207         parentEntities.addActionListener(new ActionListener JavaDoc() {
208
209             public void actionPerformed(ActionEvent JavaDoc e) {
210                 DbEntity current = mediator.getCurrentDbEntity();
211
212                 if (current instanceof DerivedDbEntity) {
213                     DerivedDbEntity derived = (DerivedDbEntity) current;
214                     DbEntity parent = (DbEntity) parentEntities.getSelectedItem();
215
216                     if (parent != derived.getParentEntity()) {
217                         derived.setParentEntity(parent);
218                         derived.resetToParentView();
219                         ProjectUtil.cleanObjMappings(mediator.getCurrentDataMap());
220
221                         EntityEvent event = new EntityEvent(this, current);
222                         mediator.fireDbEntityEvent(event);
223                     }
224                 }
225             }
226         });
227
228         parentLabel.addActionListener(this);
229         customPKGenerator.addActionListener(this);
230     }
231     
232     public void processExistingSelection(EventObject JavaDoc e) {
233         EntityDisplayEvent ede = new EntityDisplayEvent(
234                 this,
235                 mediator.getCurrentDbEntity(),
236                 mediator.getCurrentDataMap(),
237                 mediator.getCurrentDataDomain());
238         mediator.fireDbEntityDisplayEvent(ede);
239     }
240
241     public void currentDbEntityChanged(EntityDisplayEvent e) {
242         DbEntity entity = (DbEntity) e.getEntity();
243         if (null == entity || !e.isEntityChanged()) {
244             return;
245         }
246
247         name.setText(entity.getName());
248         schema.setText(entity.getSchema());
249
250         if (entity instanceof DerivedDbEntity) {
251             updateState(true);
252
253             // build a list consisting of non-derived entities
254

255             DataMap map = mediator.getCurrentDataMap();
256             Collection JavaDoc allEntities = map.getNamespace().getDbEntities();
257             java.util.List JavaDoc entities = new ArrayList JavaDoc(allEntities.size());
258             Iterator JavaDoc it = allEntities.iterator();
259
260             while (it.hasNext()) {
261                 DbEntity parentEntity = (DbEntity) it.next();
262                 if (!(parentEntity instanceof DerivedDbEntity)) {
263                     entities.add(parentEntity);
264                 }
265             }
266
267             DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(entities.toArray());
268             model.setSelectedItem(((DerivedDbEntity) entity).getParentEntity());
269             parentEntities.setRenderer(CellRenderers.entityListRendererWithIcons(map));
270             parentEntities.setModel(model);
271         }
272         else {
273             updateState(false);
274             parentEntities.setSelectedIndex(-1);
275         }
276     }
277
278     /**
279      * Enables or disbales form fields depending on the type of entity shown.
280      */

281     protected void updateState(boolean isDerivedEntity) {
282         schemaLabel.setEnabled(!isDerivedEntity);
283         schema.setEnabled(!isDerivedEntity);
284
285         parentLabel.setEnabled(isDerivedEntity);
286         parentEntities.setEnabled(isDerivedEntity);
287         parentLabel.setVisible(isDerivedEntity);
288         parentEntities.setVisible(isDerivedEntity);
289
290         DbEntity entity = mediator.getCurrentDbEntity();
291         updatePrimaryKeyGeneratorView(entity);
292     }
293
294     protected void updatePrimaryKeyGeneratorView(DbEntity entity) {
295         DbKeyGenerator generator = entity.getPrimaryKeyGenerator();
296         boolean isPKGeneratorCustomized = generator != null;
297
298         customPKGenerator.setSelected(isPKGeneratorCustomized);
299
300         customPKGeneratorNameLabel.setEnabled(isPKGeneratorCustomized);
301         customPKSizeLabel.setEnabled(isPKGeneratorCustomized);
302
303         customPKName.setEnabled(isPKGeneratorCustomized);
304         customPKSize.setEnabled(isPKGeneratorCustomized);
305
306         if (isPKGeneratorCustomized) {
307             customPKName.setText(generator.getGeneratorName());
308             customPKSize.setText(generator.getKeyCacheSize() != null ? generator
309                     .getKeyCacheSize()
310                     .toString() : "0");
311         }
312         else {
313             customPKName.setText("");
314             customPKSize.setText("");
315         }
316     }
317
318     public void actionPerformed(ActionEvent JavaDoc e) {
319         if (parentLabel == e.getSource()) {
320             DbEntity current = mediator.getCurrentDbEntity();
321
322             if (current instanceof DerivedDbEntity) {
323                 DbEntity parent = ((DerivedDbEntity) current).getParentEntity();
324                 if (parent != null) {
325                     DataDomain dom = mediator.getCurrentDataDomain();
326                     mediator.fireDbEntityDisplayEvent(new EntityDisplayEvent(
327                             this,
328                             parent,
329                             parent.getDataMap(),
330                             dom));
331                 }
332             }
333         }
334         else if (customPKGenerator == e.getSource()) {
335             DbEntity entity = mediator.getCurrentDbEntity();
336             if (entity == null) {
337                 return;
338             }
339
340             if (customPKGenerator.isSelected()) {
341                 if (entity.getPrimaryKeyGenerator() == null) {
342                     DbKeyGenerator generator = new DbKeyGenerator();
343                     generator.setGeneratorType(DbKeyGenerator.ORACLE_TYPE);
344                     generator.setKeyCacheSize(new Integer JavaDoc(
345                             JdbcPkGenerator.DEFAULT_PK_CACHE_SIZE));
346                     entity.setPrimaryKeyGenerator(generator);
347                 }
348
349                 updatePrimaryKeyGeneratorView(entity);
350             }
351             else {
352                 entity.setPrimaryKeyGenerator(null);
353                 updatePrimaryKeyGeneratorView(entity);
354             }
355         }
356     }
357
358     void setEntityName(String JavaDoc newName) {
359         if (newName != null && newName.trim().length() == 0) {
360             newName = null;
361         }
362
363         DbEntity entity = mediator.getCurrentDbEntity();
364
365         if (entity == null || Util.nullSafeEquals(newName, entity.getName())) {
366             return;
367         }
368
369         if (newName == null) {
370             throw new ValidationException("Entity name is required.");
371         }
372         else if (entity.getDataMap().getDbEntity(newName) == null) {
373             // completely new name, set new name for entity
374
EntityEvent e = new EntityEvent(this, entity, entity.getName());
375             entity.setName(newName);
376             //ProjectUtil.setDbEntityName(entity, newName);
377
mediator.fireDbEntityEvent(e);
378         }
379         else {
380             // there is an entity with the same name
381
throw new ValidationException("There is another entity with name '"
382                     + newName
383                     + "'.");
384         }
385     }
386
387     class FieldVerifier extends InputVerifier JavaDoc {
388
389         public boolean verify(JComponent JavaDoc input) {
390             if (input == schema) {
391                 return verifySchema();
392             }
393             else if (input == customPKSize) {
394                 return verifyCustomPKSize();
395             }
396             else if (input == customPKName) {
397                 return verifyCustomPKName();
398             }
399             else {
400                 return true;
401             }
402         }
403
404         protected boolean verifySchema() {
405             String JavaDoc text = schema.getText();
406             if (text != null && text.trim().length() == 0) {
407                 text = null;
408             }
409
410             DbEntity ent = mediator.getCurrentDbEntity();
411
412             if (ent != null && !Util.nullSafeEquals(ent.getSchema(), text)) {
413                 ent.setSchema(text);
414                 mediator.fireDbEntityEvent(new EntityEvent(this, ent));
415             }
416
417             return true;
418         }
419
420         protected boolean verifyCustomPKSize() {
421             String JavaDoc text = customPKSize.getText();
422             int cacheSize = 0;
423
424             if (text != null && text.trim().length() > 0) {
425                 try {
426                     cacheSize = Integer.parseInt(text);
427                 }
428                 catch (NumberFormatException JavaDoc nfex) {
429                 }
430             }
431
432             // erase any incorrect input
433
if (cacheSize == 0) {
434                 customPKSize.setText("0");
435             }
436
437             DbEntity entity = mediator.getCurrentDbEntity();
438             DbKeyGenerator generator = entity.getPrimaryKeyGenerator();
439
440             if (generator != null
441                     && (generator.getKeyCacheSize() == null || generator
442                             .getKeyCacheSize()
443                             .intValue() != cacheSize)) {
444                 generator.setKeyCacheSize(new Integer JavaDoc(cacheSize));
445                 mediator.fireDbEntityEvent(new EntityEvent(this, entity));
446             }
447
448             return true;
449         }
450
451         protected boolean verifyCustomPKName() {
452             String JavaDoc text = customPKName.getText();
453             if (text != null && text.trim().length() == 0) {
454                 text = null;
455             }
456
457             DbEntity entity = mediator.getCurrentDbEntity();
458             DbKeyGenerator generator = entity.getPrimaryKeyGenerator();
459
460             if (generator != null && (!Util.nullSafeEquals(text, generator.getName()))) {
461                 generator.setGeneratorName(text);
462                 mediator.fireDbEntityEvent(new EntityEvent(this, entity));
463             }
464             return true;
465         }
466     }
467 }
Popular Tags