KickJava   Java API By Example, From Geeks To Geeks.

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


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
60 import org.objectstyle.cayenne.access.DataDomain;
61 import org.objectstyle.cayenne.map.DataMap;
62 import org.objectstyle.cayenne.map.DbAttribute;
63 import org.objectstyle.cayenne.map.DbEntity;
64 import org.objectstyle.cayenne.map.DbRelationship;
65 import org.objectstyle.cayenne.map.ObjAttribute;
66 import org.objectstyle.cayenne.map.ObjEntity;
67 import org.objectstyle.cayenne.map.ObjRelationship;
68 import org.objectstyle.cayenne.modeler.action.CreateDataMapAction;
69 import org.objectstyle.cayenne.modeler.action.CreateDomainAction;
70 import org.objectstyle.cayenne.modeler.action.CreateNodeAction;
71 import org.objectstyle.cayenne.modeler.action.ImportDataMapAction;
72 import org.objectstyle.cayenne.modeler.action.ImportDBAction;
73 import org.objectstyle.cayenne.modeler.action.ImportEOModelAction;
74 import org.objectstyle.cayenne.modeler.action.ProjectAction;
75 import org.objectstyle.cayenne.modeler.action.RemoveAction;
76 import org.objectstyle.cayenne.modeler.action.RevertAction;
77 import org.objectstyle.cayenne.modeler.action.SaveAction;
78 import org.objectstyle.cayenne.modeler.action.SaveAsAction;
79 import org.objectstyle.cayenne.modeler.action.ValidateAction;
80 import org.objectstyle.cayenne.modeler.util.CayenneAction;
81 import org.objectstyle.cayenne.modeler.util.CayenneController;
82 import org.objectstyle.cayenne.project.ProjectPath;
83
84 /**
85  * @author Andrei Adamchik
86  */

87 public class ActionController extends CayenneController {
88
89     public ActionController(Application application) {
90         super(application);
91     }
92
93     public Component JavaDoc getView() {
94         throw new UnsupportedOperationException JavaDoc("ActionController is 'headless'");
95     }
96
97     public void domainSelected(DataDomain domain) {
98         enableDataDomainActions(domain);
99         updateRemoveAction(domain);
100     }
101
102     public void projectOpened() {
103         enableProjectActions();
104         updateRemoveAction(null);
105     }
106
107     public void projectClosed() {
108         disableAllActions();
109         getAction(ValidateAction.getActionName()).setEnabled(false);
110         getAction(ProjectAction.getActionName()).setEnabled(false);
111         getAction(SaveAction.getActionName()).setEnabled(false);
112         getAction(SaveAsAction.getActionName()).setEnabled(false);
113         getAction(RevertAction.getActionName()).setEnabled(false);
114         getAction(CreateDomainAction.getActionName()).setEnabled(false);
115
116         updateRemoveAction(null);
117     }
118
119     /**
120      * Updates the name of "Remove" action based on the current model state.
121      */

122     protected void updateRemoveAction(Object JavaDoc selected) {
123         String JavaDoc name = null;
124
125         if (selected == null) {
126             name = "Remove";
127         }
128         else if (selected instanceof DataDomain) {
129             name = "Remove DataDomain";
130         }
131         else if (selected instanceof DataMap) {
132             name = "Remove DataMap";
133         }
134         else if (selected instanceof DbEntity) {
135             name = "Remove DbEntity";
136         }
137         else if (selected instanceof ObjEntity) {
138             name = "Remove ObjEntity";
139         }
140         else if (selected instanceof DbAttribute) {
141             name = "Remove DbAttribute";
142         }
143         else if (selected instanceof ObjAttribute) {
144             name = "Remove ObjAttribute";
145         }
146         else if (selected instanceof DbRelationship) {
147             name = "Remove DbRelationship";
148         }
149         else if (selected instanceof ObjRelationship) {
150             name = "Remove ObjRelationship";
151         }
152         else {
153             name = "Remove";
154         }
155
156         getAction(RemoveAction.getActionName()).setName(name);
157     }
158
159     /**
160      * Returns an action object for the specified key.
161      */

162     private CayenneAction getAction(String JavaDoc key) {
163         return application.getAction(key);
164     }
165
166     /**
167      * Disables all controlled actions.
168      */

169     protected void disableAllActions() {
170         // disable everything we can
171
Object JavaDoc[] keys = application.getActionMap().allKeys();
172         int len = keys.length;
173         for (int i = 0; i < len; i++) {
174
175             // "save" button has its own rules
176
if (keys[i].equals(SaveAction.getActionName())
177                     || keys[i].equals(RevertAction.getActionName())) {
178                 continue;
179             }
180
181             application.getActionMap().get(keys[i]).setEnabled(false);
182         }
183     }
184
185     /**
186      * Configures actions to support an active project.
187      */

188     protected void enableProjectActions() {
189         disableAllActions();
190         getAction(CreateDomainAction.getActionName()).setEnabled(true);
191         getAction(ProjectAction.getActionName()).setEnabled(true);
192         getAction(ValidateAction.getActionName()).setEnabled(true);
193         getAction(SaveAsAction.getActionName()).setEnabled(true);
194     }
195
196     /**
197      * Updates actions "on/off" state for the selected project path.
198      */

199     protected void updateActions(ProjectPath objectPath) {
200         Object JavaDoc[] keys = application.getActionMap().allKeys();
201         int len = keys.length;
202         for (int i = 0; i < len; i++) {
203             CayenneAction action = getAction((String JavaDoc) keys[i]);
204             action.setEnabled(action.enableForPath(objectPath));
205         }
206     }
207
208     /**
209      * Configures actions to support an active DataDomain.
210      */

211     protected void enableDataDomainActions(DataDomain domain) {
212         enableProjectActions();
213
214         if (domain != null) {
215             getAction(ImportDataMapAction.getActionName()).setEnabled(true);
216             getAction(CreateDataMapAction.getActionName()).setEnabled(true);
217             getAction(RemoveAction.getActionName()).setEnabled(true);
218             getAction(CreateNodeAction.getActionName()).setEnabled(true);
219             getAction(ImportDBAction.getActionName()).setEnabled(true);
220             getAction(ImportEOModelAction.getActionName()).setEnabled(true);
221         }
222     }
223 }
Popular Tags