KickJava   Java API By Example, From Geeks To Geeks.

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


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.LifeCycleController;
27 import org.objectweb.fractal.api.control.BindingController;
28
29 import org.objectweb.fractal.gui.model.Configuration;
30 import org.objectweb.fractal.gui.model.Component;
31 import org.objectweb.fractal.gui.graph.model.GraphModel;
32 import org.objectweb.fractal.gui.repository.api.Repository;
33 import org.objectweb.fractal.gui.repository.api.Storage;
34 import org.objectweb.fractal.gui.selection.model.Selection;
35 import org.objectweb.fractal.gui.UserData;
36 import org.objectweb.fractal.swing.WaitGlassPane;
37 import org.objectweb.fractal.swing.AbstractAction;
38
39 import java.net.URL JavaDoc;
40 import java.awt.event.ActionEvent JavaDoc;
41 import java.io.File JavaDoc;
42
43 import javax.swing.ImageIcon JavaDoc;
44 import javax.swing.JFileChooser JavaDoc;
45 import javax.swing.KeyStroke JavaDoc;
46 import javax.swing.JComponent JavaDoc;
47 import javax.swing.JOptionPane JavaDoc;
48 import javax.swing.Action JavaDoc;
49 import javax.swing.JRootPane JavaDoc;
50 import javax.swing.JPopupMenu JavaDoc;
51 import javax.swing.JMenuItem JavaDoc;
52
53 /**
54  * An action to open a configuration stored in a repository.
55  */

56
57 public class OpenAction extends AbstractAction implements
58   BindingController, LifeCycleController
59 {
60
61   /**
62    * A mandatory client interface bound to a {@link Configuration configuration}
63    * model. This is the configuration into which the opened configurations are
64    * loaded.
65    */

66
67   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
68
69   /**
70    * An optional client interface bound to a {@link GraphModel graph} model.
71    * This is the model into which the opened configuration graphical
72    * information are loaded.
73    */

74
75   public final static String JavaDoc GRAPH_BINDING = "graph";
76
77   /**
78    * A mandatory client interface bound to a {@link Selection selection} model.
79    * This model is reinitialized after a configuration has been opened.
80    */

81
82   public final static String JavaDoc SELECTION_BINDING = "selection";
83
84   /**
85    * A mandatory client interface bound to a {@link Repository repository}. This
86    * repository is used to load the configurations stored in the storage.
87    */

88
89   public final static String JavaDoc REPOSITORY_BINDING = "repository";
90
91   /**
92    * A mandatory client interface bound to a {@link Storage storage}. This is
93    * the storage from which the opened configurations are read.
94    */

95
96   public final static String JavaDoc STORAGE_BINDING = "storage";
97
98   /**
99    * An optional client interface bound to a {@link UserData userdata}. This is
100    * the storage into/from which some personal data peculiar to each user are
101    * written/read.
102    */

103
104   public final static String JavaDoc USER_DATA_BINDING = "user-data";
105
106   /**
107    * An optional client interface bound to a save {@link Action action}. This
108    * action is used to save the current configuration, before opening a new
109    * one.
110    */

111
112   public final static String JavaDoc SAVE_ACTION_BINDING = "save-action";
113
114   /**
115    * The configuration client interface.
116    */

117
118   private Configuration configuration;
119
120   /**
121    * The graph client interface.
122    */

123
124   private GraphModel graph;
125
126   /**
127    * The selection client interface.
128    */

129
130   private Selection selection;
131
132   /**
133    * The repository client interface.
134    */

135
136   private Repository repository;
137
138   /**
139    * The storage client interface.
140    */

141
142   private Storage storage;
143
144   /**
145    * The user data client interface.
146    */

147
148   private UserData userData;
149
150   /**
151    * The save action client interface.
152    */

153
154   private Action save;
155
156   /**
157    * Constructs a new {@link OpenAction} component.
158    */

159
160   public OpenAction () {
161     putValue(NAME, "Open");
162     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control O"));
163     putValue(SHORT_DESCRIPTION, "Open");
164     URL JavaDoc url = getClass().getResource(
165       "/org/objectweb/fractal/gui/resources/fileopen.gif");
166     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
167   }
168
169   // -------------------------------------------------------------------------
170
// Implementation of the UserBindingController interface
171
// -------------------------------------------------------------------------
172

173   public String JavaDoc[] listFc () {
174     return new String JavaDoc[] {
175       CONFIGURATION_BINDING,
176       GRAPH_BINDING,
177       SELECTION_BINDING,
178       REPOSITORY_BINDING,
179       STORAGE_BINDING,
180       USER_DATA_BINDING,
181       SAVE_ACTION_BINDING
182     };
183   }
184
185   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
186     if (CONFIGURATION_BINDING.equals(clientItfName)) {
187       return configuration;
188     } else if (GRAPH_BINDING.equals(clientItfName)) {
189       return graph;
190     } else if (SELECTION_BINDING.equals(clientItfName)) {
191       return selection;
192     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
193       return repository;
194     } else if (STORAGE_BINDING.equals(clientItfName)) {
195       return storage;
196     } else if (USER_DATA_BINDING.equals(clientItfName)) {
197       return userData;
198     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
199       return save;
200     }
201     return null;
202   }
203
204   public void bindFc (
205     final String JavaDoc clientItfName,
206     final Object JavaDoc serverItf)
207   {
208     if (CONFIGURATION_BINDING.equals(clientItfName)) {
209       configuration = (Configuration)serverItf;
210     } else if (GRAPH_BINDING.equals(clientItfName)) {
211       graph = (GraphModel)serverItf;
212     } else if (SELECTION_BINDING.equals(clientItfName)) {
213       selection = (Selection)serverItf;
214     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
215       repository = (Repository)serverItf;
216     } else if (STORAGE_BINDING.equals(clientItfName)) {
217       storage = (Storage)serverItf;
218     } else if (USER_DATA_BINDING.equals(clientItfName)) {
219       userData = (UserData)serverItf;
220     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
221       save = (Action)serverItf;
222     }
223   }
224
225   public void unbindFc (final String JavaDoc clientItfName) {
226     if (CONFIGURATION_BINDING.equals(clientItfName)) {
227       configuration = null;
228     } else if (GRAPH_BINDING.equals(clientItfName)) {
229       graph = null;
230     } else if (SELECTION_BINDING.equals(clientItfName)) {
231       selection = null;
232     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
233       repository = null;
234     } else if (STORAGE_BINDING.equals(clientItfName)) {
235       storage = null;
236     } else if (USER_DATA_BINDING.equals(clientItfName)) {
237       userData = null;
238     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
239       save = null;
240     }
241   }
242
243   // -------------------------------------------------------------------------
244
// Implementation of the LifeCycleController interface
245
// -------------------------------------------------------------------------
246

247   public String JavaDoc getFcState () {
248     return null;
249   }
250
251   public void startFc () {
252     // TODO open last opened file
253
/*String fileName = null;
254     try {
255       fileName = userData.getStringData(UserData.LAST_OPEN_FILE);
256       // Pre-opening of the LAST_OPEN_FILE file stored in order to test its
257       // availability and to prevent any dysfunction with storage.
258       // (the file can be corrupted consequently to an external event).
259       FileInputStream fin = new FileInputStream (fileName);
260       fin.close();
261     } catch (Exception e) {
262       //userData.clean();
263       return;
264     }
265     if (fileName == null) {
266       return;
267     }
268     try {
269       storage.open(fileName, false);
270       try {
271         Component c = repository.loadComponent(
272           header.getAttribute("root", null), graph);
273         c.setName (header.getAttribute("rootName", "root"));
274         userData.setStringData(UserData.LAST_OPEN_CONF, c.getName());
275         userData.save();
276         configuration.setRootComponent(c);
277         selection.selectComponent(c);
278       } finally {
279         storage.close();
280       }
281     } catch (Exception ignored) {
282       try {
283         //userData.clean();
284       } catch (RuntimeException ex) {
285         //ex.printStackTrace();
286       }
287     }*/

288   }
289
290   public void stopFc () {
291   }
292
293   // -------------------------------------------------------------------------
294
// Implementation of the ActionListener interface
295
// -------------------------------------------------------------------------
296

297   public void actionPerformed (final ActionEvent JavaDoc e) {
298     try {
299       File JavaDoc storage = null;
300       if (configuration.getStorage() != null) {
301         storage = new File JavaDoc(configuration.getStorage());
302         if (!storage.exists() || !storage.isDirectory()) {
303           storage = null;
304         }
305       }
306       if (storage == null) {
307         JOptionPane.showMessageDialog(
308           null,
309           "A storage directory must be selected before files can be opened",
310           "Error",
311           JOptionPane.ERROR_MESSAGE);
312         return;
313       }
314       
315       if (configuration.getChangeCount() > 0) {
316         Object JavaDoc[] options = {"Yes", "No", "Cancel" };
317         int n = JOptionPane.showOptionDialog(
318           null,
319           "Do you want to save the current configuration " +
320           "before opening a new one ?",
321           "Warning",
322           JOptionPane.YES_NO_CANCEL_OPTION,
323           JOptionPane.QUESTION_MESSAGE,
324           null,
325           options,
326           options[0]);
327         if (n == 0) {
328           save.actionPerformed(e);
329         }
330         else if (n == 2) return;
331       }
332       JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
333       String JavaDoc dir = null;
334       if (userData != null) {
335         dir = userData.getStringData(UserData.LAST_OPEN_DIR);
336       }
337       fileChooser.setCurrentDirectory(dir == null ? storage : new File JavaDoc(dir));
338       String JavaDoc file = userData.getStringData(UserData.LAST_OPEN_FILE);
339       if (file != null) {
340         fileChooser.setSelectedFile(new File JavaDoc(file));
341       }
342       fileChooser.addChoosableFileFilter(
343         new SimpleFileFilter("fractal", "Fractal ADL files"));
344       if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
345         return;
346       }
347       File JavaDoc f = fileChooser.getSelectedFile();
348
349       File JavaDoc p = f;
350       String JavaDoc name = f.getName().substring(0, f.getName().indexOf('.'));
351       while (p.getParentFile() != null && !p.getParentFile().equals(storage)) {
352         name = p.getParentFile().getName() + "." + name;
353         p = p.getParentFile();
354       }
355       if (!storage.equals(p.getParentFile())) {
356         JOptionPane.showMessageDialog(
357           null,
358           "Cannot open a file which is not in the storage directory. " +
359           "Change the storage directory first.",
360           "Error",
361           JOptionPane.ERROR_MESSAGE);
362         return;
363       }
364       
365       if (userData != null) {
366         userData.setStringData(UserData.LAST_OPEN_FILE, f.getAbsolutePath());
367         userData.setStringData(UserData.LAST_OPEN_DIR, f.getParent());
368         userData.setStringData(UserData.LAST_OPEN_CONF, name);
369         userData.save();
370       }
371
372       new Thread JavaDoc(new Open(e, storage, name)).start();
373     } catch (Exception JavaDoc ignored) {
374       ignored.printStackTrace();
375     }
376   }
377
378   /**
379    * A runnable action to open a configuration. This action must be performed
380    * in a thread separated from the Swing event handler thread, otherwise the
381    * wait cursor is not displayed during the action.
382    */

383
384   class Open implements Runnable JavaDoc {
385
386     /**
387      * The root pane of the frame into which the wait cursor must be displayed.
388      */

389
390     private JRootPane JavaDoc rootPane;
391
392     /**
393      * The storage that must opened.
394      */

395
396     private File JavaDoc storage;
397     
398     /**
399      * The definition that must be opened.
400      */

401     
402     private String JavaDoc name;
403
404     /**
405      * Constructs a new {@link OpenAction} object.
406      *
407      * @param e the event that triggered this action.
408      */

409
410     public Open (final ActionEvent JavaDoc e, final File JavaDoc storage, final String JavaDoc name) {
411       JComponent JavaDoc src = (JComponent JavaDoc)e.getSource();
412       if (src instanceof JMenuItem JavaDoc) {
413         src = (JComponent JavaDoc)src.getParent();
414       }
415       if (src instanceof JPopupMenu JavaDoc) {
416         src = (JComponent JavaDoc)((JPopupMenu JavaDoc)src).getInvoker();
417       }
418       this.rootPane = src.getRootPane();
419       this.storage = storage;
420       this.name = name;
421     }
422
423     public void run () {
424       java.awt.Component JavaDoc glassPane = rootPane.getGlassPane();
425       rootPane.setGlassPane(new WaitGlassPane());
426       rootPane.getGlassPane().setVisible(true);
427
428       try {
429         OpenAction.this.storage.open(storage.getAbsolutePath());
430         try {
431           Component c = repository.loadComponent(name, graph);
432           configuration.setRootComponent(c);
433           selection.selectComponent(c);
434         } finally {
435           OpenAction.this.storage.close();
436         }
437       } catch (Exception JavaDoc ignored) {
438         ignored.printStackTrace();
439       }
440
441       rootPane.getGlassPane().setVisible(false);
442       rootPane.setGlassPane(glassPane);
443     }
444   }
445 }
446
Popular Tags