KickJava   Java API By Example, From Geeks To Geeks.

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


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;
57
58 import java.awt.Component JavaDoc;
59 import java.awt.event.WindowAdapter JavaDoc;
60 import java.awt.event.WindowEvent JavaDoc;
61 import java.io.File JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.Vector JavaDoc;
64
65 import javax.swing.JFrame JavaDoc;
66
67 import org.objectstyle.cayenne.access.DataDomain;
68 import org.objectstyle.cayenne.modeler.action.ExitAction;
69 import org.objectstyle.cayenne.modeler.action.OpenProjectAction;
70 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog;
71 import org.objectstyle.cayenne.modeler.editor.EditorView;
72 import org.objectstyle.cayenne.modeler.pref.ComponentGeometry;
73 import org.objectstyle.cayenne.modeler.pref.FSPath;
74 import org.objectstyle.cayenne.modeler.util.CayenneController;
75 import org.objectstyle.cayenne.modeler.util.RecentFileMenu;
76 import org.objectstyle.cayenne.pref.Domain;
77 import org.objectstyle.cayenne.project.Project;
78 import org.objectstyle.cayenne.project.validator.Validator;
79
80 /**
81  * Controller of the main application frame.
82  *
83  * @author Andrei Adamchik
84  */

85 public class CayenneModelerController extends CayenneController {
86
87     protected ProjectController projectController;
88     protected ActionController actionController;
89
90     protected CayenneModelerFrame frame;
91     protected File JavaDoc initialProject;
92
93     public CayenneModelerController(Application application, File JavaDoc initialProject) {
94         super(application);
95
96         this.initialProject = initialProject;
97         this.frame = new CayenneModelerFrame(this);
98
99         projectController = new ProjectController(this);
100         actionController = new ActionController(application);
101     }
102
103     public Component JavaDoc getView() {
104         return frame;
105     }
106
107     public ProjectController getProjectController() {
108         return projectController;
109     }
110
111
112
113     public FSPath getLastEOModelDirectory() {
114         // find start directory in preferences
115

116         FSPath path = (FSPath) getViewDomain()
117                 .getDetail("lastEOMDir", FSPath.class, true);
118
119         if (path.getPath() == null) {
120             path.setPath(getLastDirectory().getPath());
121         }
122
123         return path;
124     }
125
126     protected void initBindings() {
127         frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
128
129         frame.addWindowListener(new WindowAdapter JavaDoc() {
130
131             public void windowClosing(WindowEvent JavaDoc e) {
132                 ((ExitAction) getApplication().getAction(ExitAction.getActionName()))
133                         .exit();
134             }
135         });
136
137         Domain prefDomain = application.getPreferenceDomain().getSubdomain(
138                 frame.getClass());
139         ComponentGeometry geometry = ComponentGeometry.getPreference(prefDomain);
140         geometry.bind(frame, 650, 550, 30);
141     }
142
143     public void dataDomainSelectedAction(DataDomain domain) {
144         actionController.domainSelected(domain);
145     }
146
147     public void startupAction() {
148         initBindings();
149         frame.setVisible(true);
150
151         // open project
152
if (initialProject != null) {
153             OpenProjectAction openAction = (OpenProjectAction) getApplication()
154                     .getAction(OpenProjectAction.getActionName());
155             openAction.openProject(initialProject);
156         }
157     }
158
159     public void projectModifiedAction() {
160         String JavaDoc title = (projectController.getProject().isLocationUndefined())
161                 ? "[New]"
162                 : projectController.getProject().getMainFile().getAbsolutePath();
163
164         frame.setTitle("* - " + ModelerConstants.TITLE + " - " + title);
165     }
166
167     public void projectSavedAction() {
168         projectController.setDirty(false);
169         updateStatus("Project saved...");
170         frame.setTitle(ModelerConstants.TITLE
171                 + " - "
172                 + projectController.getProject().getMainFile().getAbsolutePath());
173     }
174
175     /**
176      * Action method invoked on project closing.
177      */

178     public void projectClosedAction() {
179         // --- update view
180
RecentFileMenu recentFileMenu = frame.getRecentFileMenu();
181         recentFileMenu.rebuildFromPreferences();
182         recentFileMenu.setEnabled(recentFileMenu.getMenuComponentCount() > 0);
183
184         frame.setView(null);
185
186         // repaint is needed, since sometimes there is a
187
// trace from menu left on the screen
188
frame.repaint();
189         frame.setTitle(ModelerConstants.TITLE);
190
191         projectController.setProject(null);
192
193         projectController.reset();
194         actionController.projectClosed();
195
196         updateStatus("Project Closed...");
197     }
198
199     /**
200      * Handles project opening control. Updates main frame, then delegates control to
201      * child controllers.
202      */

203     public void projectOpenedAction(Project project) {
204
205         projectController.setProject(project);
206
207         frame.setView(new EditorView(projectController));
208
209         projectController.projectOpened();
210         actionController.projectOpened();
211
212         // do status update AFTER the project is actually opened...
213
if (project.isLocationUndefined()) {
214             updateStatus("New project created...");
215             frame.setTitle(ModelerConstants.TITLE + "- [New]");
216         }
217         else {
218             updateStatus("Project opened...");
219             frame.setTitle(ModelerConstants.TITLE
220                     + " - "
221                     + project.getMainFile().getAbsolutePath());
222         }
223
224         // update preferences
225
if (!project.isLocationUndefined()) {
226             getLastDirectory().setDirectory(project.getProjectDirectory());
227         }
228
229         // --- check for load errors
230
if (project.getLoadStatus().hasFailures()) {
231             // mark project as unsaved
232
project.setModified(true);
233             projectController.setDirty(true);
234
235             // show warning dialog
236
ValidatorDialog.showDialog(frame, new Validator(
237                     project,
238                     project.getLoadStatus()));
239         }
240
241     }
242
243     /** Adds path to the list of last opened projects in preferences. */
244     public void addToLastProjListAction(String JavaDoc path) {
245         ModelerPreferences pref = ModelerPreferences.getPreferences();
246         Vector JavaDoc arr = pref.getVector(ModelerPreferences.LAST_PROJ_FILES);
247         // Add proj path to the preferences
248
// Prevent duplicate entries.
249
if (arr.contains(path)) {
250             arr.remove(path);
251         }
252
253         arr.insertElementAt(path, 0);
254         while (arr.size() > 4) {
255             arr.remove(arr.size() - 1);
256         }
257
258         pref.remove(ModelerPreferences.LAST_PROJ_FILES);
259         Iterator JavaDoc iter = arr.iterator();
260         while (iter.hasNext()) {
261             pref.addProperty(ModelerPreferences.LAST_PROJ_FILES, iter.next());
262         }
263     }
264
265     /**
266      * Returns the child action controller.
267      */

268     public ActionController getActionController() {
269         return actionController;
270     }
271
272     /**
273      * Performs status bar update with a message. Message will dissappear in 6 seconds.
274      */

275     public void updateStatus(String JavaDoc message) {
276         frame.getStatus().setText(message);
277
278         // start message cleanup thread that would remove the message after X seconds
279
if (message != null && message.trim().length() > 0) {
280             Thread JavaDoc cleanup = new ExpireThread(message, 6);
281             cleanup.start();
282         }
283     }
284
285     class ExpireThread extends Thread JavaDoc {
286
287         protected int seconds;
288         protected String JavaDoc message;
289
290         public ExpireThread(String JavaDoc message, int seconds) {
291             this.seconds = seconds;
292             this.message = message;
293         }
294
295         public void run() {
296             try {
297                 sleep(seconds * 1000);
298             }
299             catch (InterruptedException JavaDoc e) {
300                 // ignore exception
301
}
302
303             if (message.equals(frame.getStatus().getText())) {
304                 updateStatus(null);
305             }
306         }
307     }
308 }
Popular Tags