KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > menu > control > NewAction


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.menu.control;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.model.Component;
29 import org.objectweb.fractal.gui.model.Configuration;
30 import org.objectweb.fractal.gui.model.Factory;
31 import org.objectweb.fractal.gui.selection.model.Selection;
32 import org.objectweb.fractal.gui.UserData;
33 import org.objectweb.fractal.swing.AbstractAction;
34
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.net.URL JavaDoc;
37
38 import javax.swing.ImageIcon JavaDoc;
39 import javax.swing.KeyStroke JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.Action JavaDoc;
42
43 /**
44  * An action to create new, empty configurations.
45  */

46
47 public class NewAction extends AbstractAction implements
48   BindingController
49 {
50
51   /**
52    * A mandatory client interface bound to a {@link Configuration configuration}
53    * model. This is the configuration that is reinitialized by this action.
54    */

55
56   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
57
58   /**
59    * A mandatory client interface bound to a {@link Selection selection} model.
60    * This is the selection that is reinitialized by this action.
61    */

62
63   public final static String JavaDoc SELECTION_BINDING = "selection";
64
65   /**
66    * A mandatory client interface bound to a {@link Factory factory}. This
67    * factory is used to create an empty initial component in newly created
68    * configurations.
69    */

70
71   public final static String JavaDoc FACTORY_BINDING = "configuration-factory";
72
73   /**
74    * An optional client interface bound to a {@link UserData userdata}. This is
75    * the storage into/from which some personal data peculiar to each user are
76    * written/read.
77    */

78
79   public final static String JavaDoc USER_DATA_BINDING = "user-data";
80
81   /**
82    * An optional client interface bound to a save {@link Action action}. This
83    * action is used to save the current configuration, before creating a new
84    * one.
85    */

86
87   public final static String JavaDoc SAVE_ACTION_BINDING = "save-action";
88
89   /**
90    * The configuration client interface.
91    */

92
93   private Configuration configuration;
94
95   /**
96    * The selection client interface.
97    */

98
99   private Selection selection;
100
101   /**
102    * The factory client interface.
103    */

104
105   private Factory factory;
106
107   /**
108    * The user data client interface.
109    */

110
111   private UserData userData;
112
113   /**
114    * The save action client interface.
115    */

116
117   private Action save;
118
119   /**
120    * Constructs a new {@link NewAction} component.
121    */

122
123   public NewAction () {
124     putValue(NAME, "New");
125     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control N"));
126     putValue(SHORT_DESCRIPTION, "New");
127     URL JavaDoc url = getClass().getResource(
128       "/org/objectweb/fractal/gui/resources/filenew.gif");
129     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
130   }
131
132   // -------------------------------------------------------------------------
133
// Implementation of the BindingController interface
134
// -------------------------------------------------------------------------
135

136   public String JavaDoc[] listFc () {
137     return new String JavaDoc[] {
138       CONFIGURATION_BINDING,
139       SELECTION_BINDING,
140       FACTORY_BINDING,
141       USER_DATA_BINDING,
142       SAVE_ACTION_BINDING
143     };
144   }
145
146   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
147     if (CONFIGURATION_BINDING.equals(clientItfName)) {
148       return configuration;
149     } else if (SELECTION_BINDING.equals(clientItfName)) {
150       return selection;
151     } else if (FACTORY_BINDING.equals(clientItfName)) {
152       return factory;
153     } else if (USER_DATA_BINDING.equals(clientItfName)) {
154       return userData;
155     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
156       return save;
157     }
158     return null;
159   }
160
161   public void bindFc (
162     final String JavaDoc clientItfName,
163     final Object JavaDoc serverItf)
164   {
165     if (CONFIGURATION_BINDING.equals(clientItfName)) {
166       configuration = (Configuration)serverItf;
167     } else if (SELECTION_BINDING.equals(clientItfName)) {
168       selection = (Selection)serverItf;
169     } else if (FACTORY_BINDING.equals(clientItfName)) {
170       factory = (Factory)serverItf;
171     } else if (USER_DATA_BINDING.equals(clientItfName)) {
172       userData = (UserData)serverItf;
173     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
174       save = (Action)serverItf;
175     }
176   }
177
178   public void unbindFc (final String JavaDoc clientItfName) {
179     if (CONFIGURATION_BINDING.equals(clientItfName)) {
180       configuration = null;
181     } else if (SELECTION_BINDING.equals(clientItfName)) {
182       selection = null;
183     } else if (FACTORY_BINDING.equals(clientItfName)) {
184       factory = null;
185     } else if (USER_DATA_BINDING.equals(clientItfName)) {
186       userData = null;
187     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
188       save = null;
189     }
190   }
191
192   // -------------------------------------------------------------------------
193
// Implementation of the ActionListener interface
194
// -------------------------------------------------------------------------
195

196   public void actionPerformed (final ActionEvent JavaDoc e) {
197     try {
198       if (configuration.getChangeCount() > 0) {
199         Object JavaDoc[] options = {"Yes", "No", "Cancel" };
200         int n = JOptionPane.showOptionDialog(
201           null,
202           "Do you want to save the current configuration " +
203           "before creating a new one ?",
204           "Warning",
205           JOptionPane.YES_NO_CANCEL_OPTION,
206           JOptionPane.QUESTION_MESSAGE,
207           null,
208           options,
209           options[0]);
210         if (n == 0) {
211           save.actionPerformed(e);
212         }
213         else if (n == 2) return;
214       }
215       if (userData != null) {
216         userData.setStringData(UserData.LAST_OPEN_FILE, null);
217         userData.setStringData(UserData.LAST_SAVE_FILE, null);
218         userData.setStringData(UserData.LAST_OPEN_CONF, null);
219         userData.save();
220       }
221     } catch (Exception JavaDoc ex) {
222     }
223     Component c = factory.createComponent();
224     configuration.setRootComponent(c);
225     selection.selectComponent(c);
226   }
227 }
228
Popular Tags