KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > CayenneController


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.util;
57
58 import java.awt.Component JavaDoc;
59 import java.awt.Dimension JavaDoc;
60 import java.awt.Point JavaDoc;
61 import java.awt.Window JavaDoc;
62 import java.awt.event.ActionEvent JavaDoc;
63 import java.awt.event.ActionListener JavaDoc;
64 import java.awt.event.KeyEvent JavaDoc;
65 import java.beans.PropertyChangeListener JavaDoc;
66 import java.beans.PropertyChangeSupport JavaDoc;
67
68 import javax.swing.JComponent JavaDoc;
69 import javax.swing.JDialog JavaDoc;
70 import javax.swing.JFrame JavaDoc;
71 import javax.swing.JOptionPane JavaDoc;
72 import javax.swing.KeyStroke JavaDoc;
73
74 import org.apache.log4j.Logger;
75 import org.objectstyle.cayenne.modeler.Application;
76 import org.objectstyle.cayenne.modeler.pref.FSPath;
77 import org.objectstyle.cayenne.pref.Domain;
78 import org.objectstyle.cayenne.swing.BoundComponent;
79 import org.objectstyle.cayenne.util.Util;
80
81 /**
82  * A superclass of CayenneModeler controllers.
83  *
84  * @author Andrei Adamchik
85  */

86 public abstract class CayenneController implements BoundComponent {
87
88     private static final Logger logObj = Logger.getLogger(CayenneController.class);
89
90     protected CayenneController parent;
91     protected Application application;
92     protected PropertyChangeSupport JavaDoc propertyChangeSupport;
93
94     public CayenneController(CayenneController parent) {
95         this.application = (parent != null) ? parent.getApplication() : null;
96         this.parent = parent;
97     }
98
99     public CayenneController(Application application) {
100         this.application = application;
101     }
102
103     public Application getApplication() {
104         return application;
105     }
106
107     public CayenneController getParent() {
108         return parent;
109     }
110
111     /**
112      * Returns the vie wassociated with this Controller.
113      */

114     public abstract Component JavaDoc getView();
115
116     /**
117      * Returns last file system directory visited by user for this component. If there is
118      * no such directory set up in the preferences, creates a new object, setting its path
119      * to the parent last directory or to the user HOME directory.
120      */

121     public FSPath getLastDirectory() {
122         // find start directory in preferences
123
FSPath path = (FSPath) getViewDomain().getDetail("lastDir", FSPath.class, true);
124
125         if (path.getPath() == null) {
126
127             String JavaDoc pathString = (getParent() != null) ? getParent()
128                     .getLastDirectory()
129                     .getPath() : System.getProperty("user.home");
130             path.setPath(pathString);
131         }
132
133         return path;
134     }
135
136     /**
137      * Returns preference domaing for this component view.
138      */

139     protected Domain getViewDomain() {
140         return getApplication().getPreferenceDomain().getSubdomain(getView().getClass());
141     }
142
143     /**
144      * Utility method to provide a visual indication an execution error. This
145      * implementation logs an error and pops up a dialog window with error message.
146      */

147     protected void reportError(String JavaDoc title, Throwable JavaDoc th) {
148         th = Util.unwindException(th);
149         logObj.info("Error in " + getClass().getName(), th);
150
151         JOptionPane.showMessageDialog(getView(),
152                 th.getMessage(),
153                 title,
154                 JOptionPane.ERROR_MESSAGE);
155     }
156
157     /**
158      * Centers view on parent window.
159      */

160     protected void centerView() {
161         Window JavaDoc parentWindow = parent.getWindow();
162
163         Dimension JavaDoc parentSize = parentWindow.getSize();
164         Dimension JavaDoc windowSize = getView().getSize();
165         Point JavaDoc parentLocation = new Point JavaDoc(0, 0);
166         if (parentWindow.isShowing()) {
167             parentLocation = parentWindow.getLocationOnScreen();
168         }
169
170         int x = parentLocation.x + parentSize.width / 2 - windowSize.width / 2;
171         int y = parentLocation.y + parentSize.height / 2 - windowSize.height / 2;
172
173         getView().setLocation(x, y);
174     }
175
176     /**
177      * If this view or a parent view is a JDialog, makes it closeable on ESC hit. Dialog
178      * "defaultCloseOperation" property is taken into account when processing ESC button
179      * click.
180      */

181     protected void makeCloseableOnEscape() {
182
183         Window JavaDoc window = getWindow();
184         if (!(window instanceof JDialog JavaDoc)) {
185             return;
186         }
187
188         final JDialog JavaDoc dialog = (JDialog JavaDoc) window;
189
190         KeyStroke JavaDoc escReleased = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
191         ActionListener JavaDoc closeAction = new ActionListener JavaDoc() {
192
193             public void actionPerformed(ActionEvent JavaDoc e) {
194                 if (dialog.isVisible()) {
195                     switch (dialog.getDefaultCloseOperation()) {
196                         case JDialog.HIDE_ON_CLOSE:
197                             dialog.setVisible(false);
198                             break;
199                         case JDialog.DISPOSE_ON_CLOSE:
200                             dialog.setVisible(false);
201                             dialog.dispose();
202                             break;
203                         case JDialog.DO_NOTHING_ON_CLOSE:
204                         default:
205                             break;
206                     }
207                 }
208             }
209         };
210         dialog.getRootPane().registerKeyboardAction(closeAction,
211                 escReleased,
212                 JComponent.WHEN_IN_FOCUSED_WINDOW);
213     }
214
215     /**
216      * Finds a Window in the view hierarchy.
217      */

218     public Window JavaDoc getWindow() {
219         Component JavaDoc view = getView();
220         while (view != null) {
221             if (view instanceof Window JavaDoc) {
222                 return (Window JavaDoc) view;
223             }
224
225             view = view.getParent();
226         }
227
228         return null;
229     }
230
231     /**
232      * Finds a JFrame in the view hierarchy.
233      */

234     public JFrame JavaDoc getFrame() {
235         Component JavaDoc view = getView();
236         while (view != null) {
237             if (view instanceof JFrame JavaDoc) {
238                 return (JFrame JavaDoc) view;
239             }
240
241             view = view.getParent();
242         }
243
244         return null;
245     }
246
247     /**
248      * Fires property change event. Exists for the benefit of subclasses.
249      */

250     protected void firePropertyChange(
251             String JavaDoc propertyName,
252             Object JavaDoc oldValue,
253             Object JavaDoc newValue) {
254         if (propertyChangeSupport != null) {
255             propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
256         }
257     }
258
259     /**
260      * Adds a listener for property change events.
261      */

262     public void addPropertyChangeListener(
263             String JavaDoc expression,
264             PropertyChangeListener JavaDoc listener) {
265
266         if (propertyChangeSupport == null) {
267             propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
268         }
269
270         propertyChangeSupport.addPropertyChangeListener(expression, listener);
271     }
272
273     /**
274      * Default implementation is a noop. Override to handle parent binding updates.
275      */

276     public void bindingUpdated(String JavaDoc expression, Object JavaDoc newValue) {
277         // do nothing...
278
}
279 }
Popular Tags