KickJava   Java API By Example, From Geeks To Geeks.

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


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.Configuration;
29 import org.objectweb.fractal.gui.model.Component;
30 import org.objectweb.fractal.gui.repository.api.Repository;
31 import org.objectweb.fractal.gui.repository.api.Storage;
32 import org.objectweb.fractal.gui.graph.model.GraphModel;
33 import org.objectweb.fractal.gui.UserData;
34 import org.objectweb.fractal.swing.AbstractAction;
35
36 import org.objectweb.fractal.gui.model.Interface;
37
38 import java.net.URL JavaDoc;
39 import java.awt.event.ActionEvent JavaDoc;
40 import java.io.File JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 import javax.swing.ImageIcon JavaDoc;
45 import javax.swing.KeyStroke JavaDoc;
46 import javax.swing.JOptionPane JavaDoc;
47
48 /**
49  * An action to save a configuration in a repository.
50  */

51
52 public class SaveAction extends AbstractAction implements
53   BindingController
54 {
55
56   static String JavaDoc LS = new String JavaDoc (System.getProperty("line.separator"));
57   private static int OK = 0;
58   private static int WARNING = 1;
59   private static int ERROR = 2;
60   private HashMap JavaDoc hmt = new HashMap JavaDoc ();
61
62   /**
63    * A mandatory client interface bound to a {@link Configuration configuration}
64    * model. This is the configuration that is saved by this action.
65    */

66
67   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
68
69   /**
70    * An optional server interface bound to a {@link GraphModel graph} model.
71    * This is the configuration graphical information that are saved by this
72    * action.
73    */

74
75   public final static String JavaDoc GRAPH_BINDING = "graph";
76
77   /**
78    * A mandatory client interface bound to a {@link Repository repository}. This
79    * repository is used to store the configurations in the storage.
80    */

81
82   public final static String JavaDoc REPOSITORY_BINDING = "repository";
83
84   /**
85    * A mandatory client interface bound to a {@link Storage storage}. This is
86    * the storage into which the saved configurations are stored.
87    */

88
89   public final static String JavaDoc STORAGE_BINDING = "storage";
90
91   /**
92    * An optional client interface bound to a {@link UserData userdata}. This is
93    * the storage into/from which some personal data peculiar to each user are
94    * written/read.
95    */

96
97   public final static String JavaDoc USER_DATA_BINDING = "user-data";
98
99   /**
100    * The configuration client interface.
101    */

102
103   private Configuration configuration;
104
105   /**
106    * The graph client interface.
107    */

108
109   private GraphModel graph;
110
111   /**
112    * The repository client interface.
113    */

114
115   private Repository repository;
116
117   /**
118    * The storage client interface.
119    */

120
121   private Storage storage;
122
123   /**
124    * The user data client interface.
125    */

126
127   private UserData userData;
128
129   /**
130    * Constructs a new {@link SaveAction} component.
131    */

132
133   public SaveAction () {
134     putValue(NAME, "Save");
135     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));
136     putValue(SHORT_DESCRIPTION, "Save");
137     URL JavaDoc url = getClass().getResource(
138       "/org/objectweb/fractal/gui/resources/filesave.gif");
139     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
140   }
141
142   // -------------------------------------------------------------------------
143
// Implementation of the BindingController interface
144
// -------------------------------------------------------------------------
145

146
147   public String JavaDoc[] listFc () {
148     return new String JavaDoc[] {
149       CONFIGURATION_BINDING,
150       GRAPH_BINDING,
151       REPOSITORY_BINDING,
152       STORAGE_BINDING,
153       USER_DATA_BINDING
154     };
155   }
156
157   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
158     if (CONFIGURATION_BINDING.equals(clientItfName)) {
159       return configuration;
160     } else if (GRAPH_BINDING.equals(clientItfName)) {
161       return graph;
162     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
163       return repository;
164     } else if (STORAGE_BINDING.equals(clientItfName)) {
165       return storage;
166     } else if (USER_DATA_BINDING.equals(clientItfName)) {
167       return userData;
168     }
169     return null;
170   }
171
172   public void bindFc (
173     final String JavaDoc clientItfName,
174     final Object JavaDoc serverItf)
175   {
176     if (CONFIGURATION_BINDING.equals(clientItfName)) {
177       configuration = (Configuration)serverItf;
178     } else if (GRAPH_BINDING.equals(clientItfName)) {
179       graph = (GraphModel)serverItf;
180     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
181       repository = (Repository)serverItf;
182     } else if (STORAGE_BINDING.equals(clientItfName)) {
183       storage = (Storage)serverItf;
184     } else if (USER_DATA_BINDING.equals(clientItfName)) {
185       userData = (UserData)serverItf;
186     }
187   }
188
189   public void unbindFc (final String JavaDoc clientItfName) {
190     if (CONFIGURATION_BINDING.equals(clientItfName)) {
191       configuration = null;
192     } else if (GRAPH_BINDING.equals(clientItfName)) {
193       graph = null;
194     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
195       repository = null;
196     } else if (STORAGE_BINDING.equals(clientItfName)) {
197       storage = null;
198     } else if (USER_DATA_BINDING.equals(clientItfName)) {
199       userData = null;
200     }
201   }
202
203   // -------------------------------------------------------------------------
204
// Implementation of the ActionListener interface
205
// -------------------------------------------------------------------------
206

207   public void actionPerformed (final ActionEvent JavaDoc e) {
208     try {
209       File JavaDoc storage = null;
210       if (configuration.getStorage() != null) {
211         storage = new File JavaDoc(configuration.getStorage());
212         if (!storage.exists() || !storage.isDirectory()) {
213           storage = null;
214         }
215       }
216       if (storage == null) {
217         JOptionPane.showMessageDialog(
218           null,
219           "A storage directory must be selected before files can be saved",
220           "Error",
221           JOptionPane.ERROR_MESSAGE);
222       }
223       
224       // preverification
225
int ind = verify (configuration.getRootComponent());
226
227       if (ind == WARNING) {
228         Object JavaDoc[] options = { "Yes", "No" };
229         int n = JOptionPane.showOptionDialog (null,
230         "Configuration not completed\nDo you want to save it anyway ?", "Warning",
231         JOptionPane.YES_NO_CANCEL_OPTION,
232         JOptionPane.QUESTION_MESSAGE, null,
233         options,
234         options[0]);
235         if (n == 1) return;
236       }
237       else if (ind == ERROR) return;
238
239       // TODO no longer necessary, or must be changed to the selection of the storage directory.
240
/*
241       String pwd = null;
242       if (userData != null) {
243         pwd = userData.getStringData(UserData.LAST_SAVE_DIR);
244       }
245       if (pwd == null) {
246         pwd = System.getProperty("user.dir");
247       }
248       JFileChooser fileChooser = new JFileChooser();
249       fileChooser.setCurrentDirectory(new File(pwd));
250
251       String curfilename = null;
252       if (userData != null) {
253         curfilename = userData.getStringData(UserData.LAST_OPEN_FILE);
254       }
255       if (curfilename == null && userData != null) {
256         curfilename = userData.getStringData(UserData.LAST_SAVE_FILE);
257       }
258       if (curfilename != null) {
259         fileChooser.setSelectedFile (new File(curfilename));
260       }
261
262       fileChooser.addChoosableFileFilter(
263         new SimpleFileFilter("fgl", "Fractal GUI files"));
264       if (fileChooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
265         return;
266       }
267       File f = fileChooser.getSelectedFile();
268       if (userData != null) {
269         userData.setStringData(UserData.LAST_SAVE_FILE, f.getAbsolutePath());
270         userData.setStringData(UserData.LAST_SAVE_DIR, f.getParent());
271       }
272       */

273       
274       try {
275         this.storage.open(storage.getAbsolutePath());
276         Component c = configuration.getRootComponent();
277         String JavaDoc root = repository.storeComponent(c, graph, null);
278         configuration.setChangeCount(0);
279       } finally {
280         try {
281           this.storage.close();
282         } catch (Exception JavaDoc ex) {
283           JOptionPane.showMessageDialog (null,
284             ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
285         }
286       }
287     } catch (Exception JavaDoc ignored) {
288       ignored.printStackTrace();
289     }
290   }
291
292   // ----- verify : recursive verification of configuration
293

294   private int verify (Component c) {
295     List JavaDoc subComponents = c.getSubComponents();
296     int avert = 0;
297     HashMap JavaDoc hm = new HashMap JavaDoc ();
298     HashMap JavaDoc hmn = new HashMap JavaDoc ();
299
300     for (int i = 0; i < subComponents.size(); i++) {
301       Component subC = (Component)subComponents.get(i);
302       if (subC.getStatus() != Component.OK) avert = WARNING;
303
304       if (hmn.get(subC.getName()) != null) {
305         JOptionPane.showMessageDialog (null,
306         "Error in configuration : two components\nwith the same name ("+subC.getName()
307         +")\nin component '"+c.getName()+"'", "Error",
308         JOptionPane.ERROR_MESSAGE);
309         return ERROR;
310       }
311       else hmn.put(subC.getName(), "z");
312
313       String JavaDoc typ = subC.getType();
314       if ((typ != null) && (typ.length() > 0)) {
315         String JavaDoc cpsit = (String JavaDoc)hmt.get(typ);
316         if (cpsit != null) {
317           /* TODO bug with shared composite components
318           if (((subC.isComposite()) && (cpsit.equals("p")))
319            || ((!subC.isComposite()) && (cpsit.equals("c")))) {
320             JOptionPane.showMessageDialog (null,
321               "Error in configuration : the type '"+typ+"' is declared \nboth for "
322               +"a composite-template and primitive-template\n"
323               +"(see '"+subC.getName()+"')", "Error",
324               JOptionPane.ERROR_MESSAGE);
325             return ERROR;
326           }*/

327         }
328         else hmt.put(typ, (subC.isComposite() ? "c" : "p"));
329       }
330       hm.clear();
331
332       List JavaDoc itfc = subC.getClientInterfaces();
333       for (int j = 0; j < itfc.size(); ++j) {
334         Interface itf = (Interface)itfc.get(j);
335         if (itf.getName().length() < 1) continue; // specific case
336

337         if (itf.getStatus() != Interface.OK) avert = WARNING;
338         if (hm.get(itf.getName()) != null) {
339           JOptionPane.showMessageDialog (null,
340           " Error in configuration : two client\ninterfaces with the same name ("+itf.getName()
341           +")\n in component '"+subC.getName()+"'", "Error",
342           JOptionPane.ERROR_MESSAGE);
343           return ERROR;
344         }
345         else hm.put(itf.getName(), "z");
346
347       }
348       hm.clear();
349
350       List JavaDoc itfs = subC.getServerInterfaces();
351       for (int k = 0; k < itfs.size(); ++k) {
352         Interface itf = (Interface)itfs.get(k);
353         if (itf.getName().length() < 1) continue; // specific case
354

355         if (itf.getStatus() != Interface.OK) avert = WARNING;
356         if (hm.get(itf.getName()) != null) {
357           JOptionPane.showMessageDialog (null,
358           " Error in configuration : two server\ninterfaces with the same name ("+itf.getName()
359           +")\nin component '"+subC.getName()+"'", "Error",
360           JOptionPane.ERROR_MESSAGE);
361           return ERROR;
362         }
363         else hm.put(itf.getName(), "z");
364       }
365
366       int ind = verify (subC);
367       if (ind == ERROR) return ind;
368     }
369     hmn.clear();
370     return avert;
371   }
372 }
373
Popular Tags