KickJava   Java API By Example, From Geeks To Geeks.

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


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.util;
58
59 import java.awt.event.ActionEvent JavaDoc;
60
61 import javax.swing.AbstractAction JavaDoc;
62 import javax.swing.Action JavaDoc;
63 import javax.swing.Icon JavaDoc;
64 import javax.swing.JButton JavaDoc;
65 import javax.swing.JMenuItem JavaDoc;
66 import javax.swing.KeyStroke JavaDoc;
67
68 import org.objectstyle.cayenne.modeler.Application;
69 import org.objectstyle.cayenne.modeler.ProjectController;
70 import org.objectstyle.cayenne.modeler.dialog.ErrorDebugDialog;
71 import org.objectstyle.cayenne.project.Project;
72 import org.objectstyle.cayenne.project.ProjectPath;
73
74 /**
75  * Superclass of CayenneModeler actions that implements support for some common
76  * functionality, exception handling, etc.
77  *
78  * @author Andrei Adamchik
79  */

80 public abstract class CayenneAction extends AbstractAction JavaDoc {
81
82     protected boolean alwaysOn;
83     protected Application application;
84
85     /**
86      * Creates a named CayenneAction.
87      */

88     public CayenneAction(String JavaDoc name, Application application) {
89         super(name);
90         super.putValue(Action.DEFAULT, name);
91
92         this.application = application;
93
94         Icon JavaDoc icon = createIcon();
95         if (icon != null) {
96             super.putValue(Action.SMALL_ICON, icon);
97         }
98
99         KeyStroke JavaDoc accelerator = getAcceleratorKey();
100         if (accelerator != null) {
101             super.putValue(Action.ACCELERATOR_KEY, accelerator);
102         }
103
104         setEnabled(false);
105     }
106
107     public Application getApplication() {
108         return application;
109     }
110
111     protected Project getCurrentProject() {
112         return application
113                 .getFrameController()
114                 .getProjectController()
115                 .getProject();
116     }
117
118     /**
119      * Changes the name of this action, propagating the change to all widgets using this
120      * action.
121      */

122     public void setName(String JavaDoc newName) {
123         super.putValue(Action.NAME, newName);
124     }
125
126     /**
127      * Returns keyboard shortcut for this action. Default implementation returns
128      * <code>null</code>.
129      */

130     public KeyStroke JavaDoc getAcceleratorKey() {
131         return null;
132     }
133
134     /**
135      * Returns the name of the icon that should be used for buttons. Name will be reolved
136      * relative to <code>RESOURCE_PATH</code>. Default implementation returns
137      * <code>null</code>.
138      */

139     public String JavaDoc getIconName() {
140         return null;
141     }
142
143     /**
144      * Creates and returns an ImageIcon that can be used for buttons that rely on this
145      * action. Returns <code>null</code> if <code>getIconName</code> returns
146      * <code>null</code>.
147      */

148     public Icon JavaDoc createIcon() {
149         String JavaDoc name = getIconName();
150         return (name != null) ? ModelerUtil.buildIcon(name) : null;
151     }
152
153     /**
154      * Returns the key under which this action should be stored in the ActionMap.
155      */

156     public String JavaDoc getKey() {
157         return (String JavaDoc) super.getValue(Action.DEFAULT);
158     }
159
160     /**
161      * Subclasses must implement this method instead of <code>actionPerformed</code> to
162      * allow for exception handling.
163      */

164     public abstract void performAction(ActionEvent JavaDoc e);
165
166     /**
167      * Returns <code>true</code> if the action is enabled for the specified "project
168      * path" - a path on the project tree to a currently selected object. Default
169      * implementation simply returns <code>false</code>.
170      */

171     public boolean enableForPath(ProjectPath obj) {
172         return false;
173     }
174
175     /**
176      * Returns current project controller.
177      */

178     public ProjectController getProjectController() {
179         return application.getFrameController().getProjectController();
180     }
181
182     /**
183      * Internally calls <code>performAction</code>. Traps exceptions that ocurred
184      * during action processing.
185      */

186     public void actionPerformed(ActionEvent JavaDoc e) {
187         try {
188             performAction(e);
189         }
190         catch (Throwable JavaDoc th) {
191             ErrorDebugDialog.guiException(th);
192         }
193     }
194
195     /**
196      * Factory method that creates a menu item hooked up to this action.
197      */

198     public JMenuItem JavaDoc buildMenu() {
199         return new JMenuItem JavaDoc(this);
200     }
201
202     /**
203      * Factory method that creates a button hooked up to this action.
204      */

205     public JButton JavaDoc buildButton() {
206         return new CayenneToolbarButton(this);
207     }
208
209     /**
210      * Returns true if this action is always enabled.
211      *
212      * @return boolean
213      */

214     public boolean isAlwaysOn() {
215         return alwaysOn;
216     }
217
218     /**
219      * Sets the alwaysOn.
220      *
221      * @param alwaysOn The alwaysOn to set
222      */

223     public void setAlwaysOn(boolean alwaysOn) {
224         this.alwaysOn = alwaysOn;
225
226         if (alwaysOn) {
227             super.setEnabled(true);
228         }
229     }
230
231     /**
232      * Overrides super implementation to take into account "alwaysOn" flag.
233      */

234     public void setEnabled(boolean b) {
235         if (!isAlwaysOn()) {
236             super.setEnabled(b);
237         }
238     }
239
240     /**
241      * On changes in action text, will update toolbar tip instead.
242      */

243     final class CayenneToolbarButton extends JButton JavaDoc {
244
245         protected boolean showingText;
246
247         /**
248          * Constructor for CayenneMenuItem.
249          */

250         public CayenneToolbarButton(Action JavaDoc a) {
251             super();
252             setAction(a);
253         }
254
255         /**
256          * Returns the showingText.
257          */

258         public boolean isShowingText() {
259             return showingText;
260         }
261
262         /**
263          * Sets the showingText.
264          */

265         public void setShowingText(boolean showingText) {
266             this.showingText = showingText;
267         }
268
269         /**
270          * @see javax.swing.AbstractButton#getText()
271          */

272         public String JavaDoc getText() {
273             return (showingText) ? super.getText() : null;
274         }
275
276         /**
277          * @see javax.swing.AbstractButton#setText(String)
278          */

279         public void setText(String JavaDoc text) {
280             if (showingText) {
281                 super.setText(text);
282             }
283             else {
284                 super.setToolTipText(text);
285             }
286         }
287     }
288 }
Popular Tags