KickJava   Java API By Example, From Geeks To Geeks.

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


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.UserData;
29 import org.objectweb.fractal.gui.model.Configuration;
30 import org.objectweb.fractal.swing.AbstractAction;
31
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import javax.swing.JOptionPane JavaDoc;
38 import java.io.File JavaDoc;
39
40 import javax.swing.JFileChooser JavaDoc;
41
42 /**
43  * An action to exit FractalGUI.
44  */

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

54
55   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
56
57   public final static String JavaDoc USER_DATA_BINDING = "user-data";
58
59   /**
60    * The configuration client interface.
61    */

62
63   private Configuration configuration;
64
65   private UserData userData;
66   
67   /**
68    * Constructs a new {@link ChangeExecDirAction} component.
69    */

70
71   public ChangeExecDirAction () {
72     putValue(NAME, "Storage...");
73     putValue(SHORT_DESCRIPTION, "Storage...");
74     URL JavaDoc url = getClass().getResource(
75       "/org/objectweb/fractal/gui/resources/empty.gif");
76     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
77     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control E"));
78   }
79
80   // -------------------------------------------------------------------------
81
// Implementation of the BindingController interface
82
// -------------------------------------------------------------------------
83

84   public String JavaDoc[] listFc () {
85     return new String JavaDoc[] { CONFIGURATION_BINDING, USER_DATA_BINDING };
86   }
87
88   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
89     if (CONFIGURATION_BINDING.equals(clientItfName)) {
90       return configuration;
91     } else if (USER_DATA_BINDING.equals(clientItfName)) {
92       return userData;
93     }
94     return null;
95   }
96
97   public void bindFc (final String JavaDoc clientItfName, final Object JavaDoc serverItf) {
98     if (CONFIGURATION_BINDING.equals(clientItfName)) {
99       configuration = (Configuration)serverItf;
100     } else if (USER_DATA_BINDING.equals(clientItfName)) {
101       userData = (UserData)serverItf;
102     }
103   }
104
105   public void unbindFc (final String JavaDoc clientItfName) {
106     if (CONFIGURATION_BINDING.equals(clientItfName)) {
107       configuration = null;
108     } else if (USER_DATA_BINDING.equals(clientItfName)) {
109       userData = null;
110     }
111   }
112
113   // -------------------------------------------------------------------------
114
// Implementation of the ActionListener interface
115
// -------------------------------------------------------------------------
116

117   public void actionPerformed (final ActionEvent JavaDoc e) {
118     File JavaDoc f = null;
119     String JavaDoc UD = System.getProperty("user.dir");
120     File JavaDoc fud = new File JavaDoc(UD);
121     JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
122     String JavaDoc dir = null;
123     if (userData != null) {
124       try {
125         dir = userData.getStringData(UserData.LAST_EXEC_DIR);
126       } catch (Exception JavaDoc ignored) {
127       }
128     }
129     fileChooser.setCurrentDirectory(dir == null ? fud : new File JavaDoc(dir));
130     fileChooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
131     fileChooser.setDialogTitle ("Please, select the storage directory");
132     if (fileChooser.showOpenDialog (null) != JFileChooser.APPROVE_OPTION) {
133       return;
134     }
135     f = fileChooser.getSelectedFile();
136     if (!f.isDirectory()) {
137       JOptionPane.showMessageDialog (null,
138         f.getName()+": Invalid directory!",
139         "Alert ...", JOptionPane.ERROR_MESSAGE);
140       return;
141     }
142     configuration.setStorage(f.getPath());
143     if (userData != null) {
144       try {
145         userData.setStringData(UserData.LAST_EXEC_DIR, f.getAbsolutePath());
146         userData.save();
147       } catch (Exception JavaDoc ignored) {
148       }
149     }
150   }
151 }
152
Popular Tags