KickJava   Java API By Example, From Geeks To Geeks.

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


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

53
54 public class ImportAction extends AbstractAction implements BindingController {
55
56   /**
57    * A mandatory client interface bound to a {@link Configuration configuration}
58    * model. This is the configuration into which the imported configurations are
59    * loaded.
60    */

61
62   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
63
64   /**
65    * An optional client interface bound to a {@link GraphModel graph} model.
66    * This is the model into which the imported configuration graphical
67    * information are loaded.
68    */

69
70   public final static String JavaDoc GRAPH_BINDING = "graph";
71
72   /**
73    * A mandatory client interface bound to a {@link Selection selection} model.
74    */

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

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

89
90   public final static String JavaDoc STORAGE_BINDING = "storage";
91
92   /**
93    * The configuration client interface.
94    */

95
96   private Configuration configuration;
97
98   /**
99    * The graph client interface.
100    */

101
102   private GraphModel graph;
103
104   /**
105    * The selection client interface.
106    */

107
108   private Selection selection;
109
110   /**
111    * The repository client interface.
112    */

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

119
120   private Storage storage;
121
122   /**
123    * Constructs a new {@link OpenAction} component.
124    */

125
126   public ImportAction () {
127     putValue(NAME, "Import");
128     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control I"));
129     putValue(SHORT_DESCRIPTION, "Import");
130     URL JavaDoc url = getClass().getResource(
131       "/org/objectweb/fractal/gui/resources/fileopen.gif");
132     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
133   }
134
135   // -------------------------------------------------------------------------
136
// Implementation of the UserBindingController interface
137
// -------------------------------------------------------------------------
138

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

199   public void actionPerformed (final ActionEvent JavaDoc e) {
200     try {
201       if (!(selection.getSelection() instanceof Component)) {
202         JOptionPane.showMessageDialog(
203           null,
204           "A component must be selected, to which the imported component will be added",
205           "Error",
206           JOptionPane.ERROR_MESSAGE);
207         return;
208       }
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 imported",
220           "Error",
221           JOptionPane.ERROR_MESSAGE);
222         return;
223       }
224       
225       JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
226       fileChooser.addChoosableFileFilter(
227         new SimpleFileFilter("fractal", "Fractal ADL files"));
228       if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
229         return;
230       }
231       File JavaDoc f = fileChooser.getSelectedFile();
232
233       File JavaDoc p = f;
234       String JavaDoc name = f.getName().substring(0, f.getName().indexOf('.'));
235       while (p.getParentFile() != null && !p.getParentFile().equals(storage)) {
236         name = p.getParentFile().getName() + "." + name;
237         p = p.getParentFile();
238       }
239       if (!storage.equals(p.getParentFile())) {
240         JOptionPane.showMessageDialog(
241           null,
242           "Cannot open a file which is not in the storage directory. " +
243           "Change the storage directory first.",
244           "Error",
245           JOptionPane.ERROR_MESSAGE);
246         return;
247       }
248
249       new Thread JavaDoc(new Import(e, storage, name)).start();
250     } catch (Exception JavaDoc ignored) {
251       ignored.printStackTrace();
252     }
253   }
254
255   /**
256    * A runnable action to open a configuration. This action must be performed
257    * in a thread separated from the Swing event handler thread, otherwise the
258    * wait cursor is not displayed during the action.
259    */

260
261   class Import implements Runnable JavaDoc {
262
263     /**
264      * The root pane of the frame into which the wait cursor must be displayed.
265      */

266
267     private JRootPane JavaDoc rootPane;
268
269     /**
270      * The storage that must opened.
271      */

272
273     private File JavaDoc storage;
274     
275     /**
276      * The definition that must be opened.
277      */

278     
279     private String JavaDoc name;
280
281     /**
282      * Constructs a new {@link Import} object.
283      *
284      * @param e the event that triggered this action.
285      */

286
287     public Import (final ActionEvent JavaDoc e, final File JavaDoc storage, final String JavaDoc name) {
288       JComponent JavaDoc src = (JComponent JavaDoc)e.getSource();
289       if (src instanceof JMenuItem JavaDoc) {
290         src = (JComponent JavaDoc)src.getParent();
291       }
292       if (src instanceof JPopupMenu JavaDoc) {
293         src = (JComponent JavaDoc)((JPopupMenu JavaDoc)src).getInvoker();
294       }
295       this.rootPane = src.getRootPane();
296       this.storage = storage;
297       this.name = name;
298     }
299
300     public void run () {
301       java.awt.Component JavaDoc glassPane = rootPane.getGlassPane();
302       rootPane.setGlassPane(new WaitGlassPane());
303       rootPane.getGlassPane().setVisible(true);
304
305       try {
306         ImportAction.this.storage.open(storage.getAbsolutePath());
307         try {
308           Component c = repository.loadComponent(name, graph);
309           Component p = (Component)selection.getSelection();
310           p.addSubComponent(c);
311           selection.selectComponent(c);
312         } finally {
313           ImportAction.this.storage.close();
314         }
315       } catch (Exception JavaDoc ignored) {
316         ignored.printStackTrace();
317       }
318
319       rootPane.getGlassPane().setVisible(false);
320       rootPane.setGlassPane(glassPane);
321     }
322   }
323 }
324
Popular Tags