KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > CayenneModelerFrame


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;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.FlowLayout JavaDoc;
61 import java.awt.Font JavaDoc;
62
63 import javax.swing.Box JavaDoc;
64 import javax.swing.JFrame JavaDoc;
65 import javax.swing.JLabel JavaDoc;
66 import javax.swing.JMenu JavaDoc;
67 import javax.swing.JMenuBar JavaDoc;
68 import javax.swing.JPanel JavaDoc;
69 import javax.swing.JToolBar JavaDoc;
70
71 import org.objectstyle.cayenne.map.DerivedDbEntity;
72 import org.objectstyle.cayenne.modeler.action.AboutAction;
73 import org.objectstyle.cayenne.modeler.action.ConfigurePreferencesAction;
74 import org.objectstyle.cayenne.modeler.action.CreateAttributeAction;
75 import org.objectstyle.cayenne.modeler.action.CreateDataMapAction;
76 import org.objectstyle.cayenne.modeler.action.CreateDbEntityAction;
77 import org.objectstyle.cayenne.modeler.action.CreateDerivedDbEntityAction;
78 import org.objectstyle.cayenne.modeler.action.CreateDomainAction;
79 import org.objectstyle.cayenne.modeler.action.CreateNodeAction;
80 import org.objectstyle.cayenne.modeler.action.CreateObjEntityAction;
81 import org.objectstyle.cayenne.modeler.action.CreateProcedureAction;
82 import org.objectstyle.cayenne.modeler.action.CreateProcedureParameterAction;
83 import org.objectstyle.cayenne.modeler.action.CreateQueryAction;
84 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction;
85 import org.objectstyle.cayenne.modeler.action.DbEntitySyncAction;
86 import org.objectstyle.cayenne.modeler.action.DerivedEntitySyncAction;
87 import org.objectstyle.cayenne.modeler.action.ExitAction;
88 import org.objectstyle.cayenne.modeler.action.GenerateClassesAction;
89 import org.objectstyle.cayenne.modeler.action.GenerateDBAction;
90 import org.objectstyle.cayenne.modeler.action.ImportDataMapAction;
91 import org.objectstyle.cayenne.modeler.action.ImportDBAction;
92 import org.objectstyle.cayenne.modeler.action.ImportEOModelAction;
93 import org.objectstyle.cayenne.modeler.action.NavigateBackwardAction;
94 import org.objectstyle.cayenne.modeler.action.NavigateForwardAction;
95 import org.objectstyle.cayenne.modeler.action.NewProjectAction;
96 import org.objectstyle.cayenne.modeler.action.ObjEntitySyncAction;
97 import org.objectstyle.cayenne.modeler.action.OpenProjectAction;
98 import org.objectstyle.cayenne.modeler.action.ProjectAction;
99 import org.objectstyle.cayenne.modeler.action.RemoveAction;
100 import org.objectstyle.cayenne.modeler.action.RevertAction;
101 import org.objectstyle.cayenne.modeler.action.SaveAction;
102 import org.objectstyle.cayenne.modeler.action.SaveAsAction;
103 import org.objectstyle.cayenne.modeler.action.ValidateAction;
104 import org.objectstyle.cayenne.modeler.editor.EditorView;
105 import org.objectstyle.cayenne.modeler.event.AttributeDisplayEvent;
106 import org.objectstyle.cayenne.modeler.event.DataMapDisplayEvent;
107 import org.objectstyle.cayenne.modeler.event.DataMapDisplayListener;
108 import org.objectstyle.cayenne.modeler.event.DataNodeDisplayEvent;
109 import org.objectstyle.cayenne.modeler.event.DataNodeDisplayListener;
110 import org.objectstyle.cayenne.modeler.event.DbAttributeDisplayListener;
111 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener;
112 import org.objectstyle.cayenne.modeler.event.DbRelationshipDisplayListener;
113 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent;
114 import org.objectstyle.cayenne.modeler.event.ObjAttributeDisplayListener;
115 import org.objectstyle.cayenne.modeler.event.ObjEntityDisplayListener;
116 import org.objectstyle.cayenne.modeler.event.ObjRelationshipDisplayListener;
117 import org.objectstyle.cayenne.modeler.event.ProcedureDisplayEvent;
118 import org.objectstyle.cayenne.modeler.event.ProcedureDisplayListener;
119 import org.objectstyle.cayenne.modeler.event.ProcedureParameterDisplayEvent;
120 import org.objectstyle.cayenne.modeler.event.ProcedureParameterDisplayListener;
121 import org.objectstyle.cayenne.modeler.event.QueryDisplayEvent;
122 import org.objectstyle.cayenne.modeler.event.QueryDisplayListener;
123 import org.objectstyle.cayenne.modeler.event.RelationshipDisplayEvent;
124 import org.objectstyle.cayenne.modeler.util.CayenneAction;
125 import org.objectstyle.cayenne.modeler.util.RecentFileMenu;
126
127 /**
128  * Main frame of CayenneModeler. Responsibilities include coordination of
129  * enabling/disabling of menu and toolbar.
130  */

131 public class CayenneModelerFrame extends JFrame JavaDoc implements DataNodeDisplayListener,
132         DataMapDisplayListener, ObjEntityDisplayListener, DbEntityDisplayListener,
133         ObjAttributeDisplayListener, DbAttributeDisplayListener,
134         ObjRelationshipDisplayListener, DbRelationshipDisplayListener,
135         QueryDisplayListener, ProcedureDisplayListener, ProcedureParameterDisplayListener {
136
137     protected EditorView view;
138     protected RecentFileMenu recentFileMenu;
139     protected CayenneModelerController controller;
140     protected JLabel JavaDoc status;
141
142     public CayenneModelerFrame(CayenneModelerController controller) {
143         super(ModelerConstants.TITLE);
144
145         this.controller = controller;
146
147         initMenus();
148         initToolbar();
149         initStatusBar();
150     }
151
152     /**
153      * Returns an action object associated with the key.
154      */

155     public CayenneAction getAction(String JavaDoc key) {
156         return controller.getApplication().getAction(key);
157     }
158
159     protected void initMenus() {
160         getContentPane().setLayout(new BorderLayout JavaDoc());
161
162         JMenu JavaDoc fileMenu = new JMenu JavaDoc("File");
163         JMenu JavaDoc projectMenu = new JMenu JavaDoc("Project");
164         JMenu JavaDoc toolMenu = new JMenu JavaDoc("Tools");
165         JMenu JavaDoc helpMenu = new JMenu JavaDoc("Help");
166
167         fileMenu.add(getAction(NewProjectAction.getActionName()).buildMenu());
168         fileMenu.add(getAction(OpenProjectAction.getActionName()).buildMenu());
169         fileMenu.add(getAction(ProjectAction.getActionName()).buildMenu());
170         fileMenu.add(getAction(ImportDataMapAction.getActionName()).buildMenu());
171         fileMenu.addSeparator();
172         fileMenu.add(getAction(SaveAction.getActionName()).buildMenu());
173         fileMenu.add(getAction(SaveAsAction.getActionName()).buildMenu());
174         fileMenu.add(getAction(RevertAction.getActionName()).buildMenu());
175         fileMenu.addSeparator();
176
177         recentFileMenu = new RecentFileMenu("Recent Files");
178         recentFileMenu.rebuildFromPreferences();
179         recentFileMenu.setEnabled(recentFileMenu.getMenuComponentCount() > 0);
180         fileMenu.add(recentFileMenu);
181
182         fileMenu.addSeparator();
183         fileMenu.add(getAction(ExitAction.getActionName()).buildMenu());
184
185         projectMenu.add(getAction(ValidateAction.getActionName()).buildMenu());
186         projectMenu.addSeparator();
187         projectMenu.add(getAction(CreateDomainAction.getActionName()).buildMenu());
188         projectMenu.add(getAction(CreateNodeAction.getActionName()).buildMenu());
189         projectMenu.add(getAction(CreateDataMapAction.getActionName()).buildMenu());
190
191         projectMenu.add(getAction(CreateObjEntityAction.getActionName()).buildMenu());
192         projectMenu.add(getAction(CreateDbEntityAction.getActionName()).buildMenu());
193         projectMenu.add(getAction(CreateDerivedDbEntityAction.getActionName())
194                 .buildMenu());
195         projectMenu.add(getAction(CreateProcedureAction.getActionName()).buildMenu());
196         projectMenu.add(getAction(CreateQueryAction.getActionName()).buildMenu());
197         projectMenu.addSeparator();
198         projectMenu.add(getAction(ObjEntitySyncAction.getActionName()).buildMenu());
199         projectMenu.add(getAction(DerivedEntitySyncAction.getActionName()).buildMenu());
200         projectMenu.addSeparator();
201         projectMenu.add(getAction(RemoveAction.getActionName()).buildMenu());
202
203         toolMenu.add(getAction(ImportDBAction.getActionName()).buildMenu());
204         toolMenu.add(getAction(ImportEOModelAction.getActionName()).buildMenu());
205         toolMenu.addSeparator();
206         toolMenu.add(getAction(GenerateClassesAction.getActionName()).buildMenu());
207         toolMenu.add(getAction(GenerateDBAction.getActionName()).buildMenu());
208         toolMenu.addSeparator();
209         toolMenu.add(getAction(ConfigurePreferencesAction.getActionName()).buildMenu());
210
211         helpMenu.add(getAction(AboutAction.getActionName()).buildMenu());
212
213         JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
214
215         menuBar.add(fileMenu);
216         menuBar.add(projectMenu);
217         menuBar.add(toolMenu);
218         menuBar.add(helpMenu);
219
220         setJMenuBar(menuBar);
221     }
222
223     protected void initStatusBar() {
224         status = new JLabel JavaDoc();
225         status.setFont(status.getFont().deriveFont(Font.PLAIN, 10));
226
227         JPanel JavaDoc statusBar = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.LEFT, 3, 1));
228         // add placeholder
229
statusBar.add(Box.createVerticalStrut(16));
230         statusBar.add(status);
231
232         getContentPane().add(statusBar, BorderLayout.SOUTH);
233     }
234
235     /** Initializes main toolbar. */
236     protected void initToolbar() {
237         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
238         toolBar.add(getAction(NewProjectAction.getActionName()).buildButton());
239         toolBar.add(getAction(OpenProjectAction.getActionName()).buildButton());
240         toolBar.add(getAction(SaveAction.getActionName()).buildButton());
241         toolBar.add(getAction(RemoveAction.getActionName()).buildButton());
242
243         toolBar.addSeparator();
244
245         toolBar.add(getAction(CreateDomainAction.getActionName()).buildButton());
246         toolBar.add(getAction(CreateNodeAction.getActionName()).buildButton());
247         toolBar.add(getAction(CreateDataMapAction.getActionName()).buildButton());
248
249         toolBar.addSeparator();
250
251         toolBar.add(getAction(CreateDbEntityAction.getActionName()).buildButton());
252         toolBar.add(getAction(CreateDerivedDbEntityAction.getActionName()).buildButton());
253         toolBar.add(getAction(CreateProcedureAction.getActionName()).buildButton());
254
255         toolBar.addSeparator();
256
257         toolBar.add(getAction(CreateObjEntityAction.getActionName()).buildButton());
258         toolBar.add(getAction(CreateQueryAction.getActionName()).buildButton());
259
260         toolBar.addSeparator();
261
262         toolBar.add(getAction(NavigateBackwardAction.getActionName()).buildButton());
263         toolBar.add(getAction(NavigateForwardAction.getActionName()).buildButton());
264
265         getContentPane().add(toolBar, BorderLayout.NORTH);
266     }
267
268     public void currentDataNodeChanged(DataNodeDisplayEvent e) {
269         enableDataNodeMenu();
270         getAction(RemoveAction.getActionName()).setName("Remove DataNode");
271     }
272
273     public void currentDataMapChanged(DataMapDisplayEvent e) {
274         enableDataMapMenu();
275         getAction(RemoveAction.getActionName()).setName("Remove DataMap");
276     }
277
278     public void currentObjEntityChanged(EntityDisplayEvent e) {
279         enableObjEntityMenu();
280         getAction(RemoveAction.getActionName()).setName("Remove ObjEntity");
281     }
282
283     public void currentDbEntityChanged(EntityDisplayEvent e) {
284         enableDbEntityMenu();
285         getAction(RemoveAction.getActionName()).setName("Remove DbEntity");
286     }
287
288     public void currentQueryChanged(QueryDisplayEvent e) {
289         enableDataMapMenu();
290
291         if (e.getQuery() != null) {
292             getAction(RemoveAction.getActionName()).setName("Remove Query");
293         }
294     }
295
296     public void currentProcedureChanged(ProcedureDisplayEvent e) {
297         enableProcedureMenu();
298
299         if (e.getProcedure() != null) {
300             getAction(RemoveAction.getActionName()).setName("Remove Stored Procedure");
301         }
302     }
303
304     public void currentDbAttributeChanged(AttributeDisplayEvent e) {
305         enableDbEntityMenu();
306         if (e.getAttribute() != null) {
307             getAction(RemoveAction.getActionName()).setName("Remove DbAttribute");
308         }
309     }
310
311     public void currentProcedureParameterChanged(ProcedureParameterDisplayEvent e) {
312         enableProcedureMenu();
313         if (e.getProcedureParameter() != null) {
314             getAction(RemoveAction.getActionName()).setName("Remove Parameter");
315         }
316     }
317
318     public void currentObjAttributeChanged(AttributeDisplayEvent e) {
319         enableObjEntityMenu();
320         if (e.getAttribute() != null) {
321             getAction(RemoveAction.getActionName()).setName("Remove ObjAttribute");
322         }
323     }
324
325     public void currentDbRelationshipChanged(RelationshipDisplayEvent e) {
326         enableDbEntityMenu();
327         if (e.getRelationship() != null) {
328             getAction(RemoveAction.getActionName()).setName("Remove DbRelationship");
329         }
330     }
331
332     public void currentObjRelationshipChanged(RelationshipDisplayEvent e) {
333         enableObjEntityMenu();
334         if (e.getRelationship() != null) {
335             getAction(RemoveAction.getActionName()).setName("Remove ObjRelationship");
336         }
337     }
338
339     private void enableDataMapMenu() {
340         if (controller.getProjectController().getCurrentDataNode() != null)
341             enableDataNodeMenu();
342         else {
343             // Andrus: Temp hack till moved to controller
344
controller.getActionController().domainSelected(
345                     controller.getProjectController().getCurrentDataDomain());
346         }
347
348         getAction(GenerateClassesAction.getActionName()).setEnabled(true);
349         getAction(CreateObjEntityAction.getActionName()).setEnabled(true);
350         getAction(CreateDbEntityAction.getActionName()).setEnabled(true);
351         getAction(CreateDerivedDbEntityAction.getActionName()).setEnabled(true);
352         getAction(CreateQueryAction.getActionName()).setEnabled(true);
353         getAction(CreateProcedureAction.getActionName()).setEnabled(true);
354         getAction(GenerateDBAction.getActionName()).setEnabled(true);
355
356         // reset
357
getAction(CreateAttributeAction.getActionName()).setName("Create Attribute");
358     }
359
360     private void enableObjEntityMenu() {
361         enableDataMapMenu();
362         getAction(ObjEntitySyncAction.getActionName()).setEnabled(true);
363         getAction(CreateAttributeAction.getActionName()).setEnabled(true);
364         getAction(CreateRelationshipAction.getActionName()).setEnabled(true);
365     }
366
367     private void enableDbEntityMenu() {
368         enableDataMapMenu();
369         getAction(CreateAttributeAction.getActionName()).setEnabled(true);
370         getAction(CreateRelationshipAction.getActionName()).setEnabled(true);
371         getAction(DbEntitySyncAction.getActionName()).setEnabled(true);
372         
373         if (controller.getProjectController().getCurrentDbEntity() instanceof DerivedDbEntity) {
374             getAction(DerivedEntitySyncAction.getActionName()).setEnabled(true);
375         }
376     }
377
378     private void enableProcedureMenu() {
379         enableDataMapMenu();
380         getAction(CreateProcedureParameterAction.getActionName()).setEnabled(true);
381     }
382
383     private void enableDataNodeMenu() {
384         // Andrus: Temp hack till moved to controller
385
controller.getActionController().domainSelected(
386                 controller.getProjectController().getCurrentDataDomain());
387     }
388
389     /**
390      * Returns the right side view panel.
391      *
392      * @return EditorView
393      */

394     public EditorView getView() {
395         return view;
396     }
397
398     public JLabel JavaDoc getStatus() {
399         return status;
400     }
401
402     /**
403      * Returns the recentFileMenu.
404      *
405      * @return RecentFileMenu
406      */

407     public RecentFileMenu getRecentFileMenu() {
408         return recentFileMenu;
409     }
410
411     /**
412      * Adds editor view to the frame.
413      */

414     public void setView(EditorView view) {
415         boolean change = false;
416
417         if (this.view != null) {
418             getContentPane().remove(this.view);
419             change = true;
420         }
421
422         this.view = view;
423
424         if (view != null) {
425             getContentPane().add(view, BorderLayout.CENTER);
426             change = true;
427         }
428
429         if (change) {
430             validate();
431         }
432     }
433 }
Popular Tags