KickJava   Java API By Example, From Geeks To Geeks.

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


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.UserData;
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 javax.swing.Action JavaDoc;
39
40 /**
41  * An action to exit FractalGUI.
42  */

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

52
53   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
54
55   /**
56    * An optional client interface bound to a {@link UserData userdata}. This is
57    * the storage into/from which some personal data peculiar to each user are
58    * written/read.
59    */

60
61   public final static String JavaDoc USER_DATA_BINDING = "user-data";
62
63   /**
64    * An optional client interface bound to a save {@link Action action}. This
65    * action is used to save the current configuration, before exiting
66    * FractalGUI.
67    */

68
69   public final static String JavaDoc SAVE_ACTION_BINDING = "save-action";
70
71   /**
72    * The configuration client interface.
73    */

74
75   private Configuration configuration;
76
77   /**
78    * The user data client interface.
79    */

80
81   private UserData userData;
82
83   /**
84    * The save action client interface.
85    */

86
87   private Action save;
88
89   /**
90    * Constructs a new {@link QuitAction} component.
91    */

92
93   public QuitAction () {
94     putValue(NAME, "Quit");
95     putValue(SHORT_DESCRIPTION, "Quit");
96     URL JavaDoc url = getClass().getResource(
97       "/org/objectweb/fractal/gui/resources/empty.gif");
98     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
99     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Q"));
100   }
101
102   // -------------------------------------------------------------------------
103
// Implementation of the BindingController interface
104
// -------------------------------------------------------------------------
105

106   public String JavaDoc[] listFc () {
107     return new String JavaDoc[] {
108       CONFIGURATION_BINDING,
109       USER_DATA_BINDING,
110       SAVE_ACTION_BINDING
111     };
112   }
113
114   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
115     if (CONFIGURATION_BINDING.equals(clientItfName)) {
116       return configuration;
117     } else if (USER_DATA_BINDING.equals(clientItfName)) {
118       return userData;
119     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
120       return save;
121     }
122     return null;
123   }
124
125   public void bindFc (
126     final String JavaDoc clientItfName,
127     final Object JavaDoc serverItf) {
128     if (CONFIGURATION_BINDING.equals(clientItfName)) {
129       configuration = (Configuration)serverItf;
130     } else if (USER_DATA_BINDING.equals(clientItfName)) {
131       userData = (UserData)serverItf;
132     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
133       save = (Action)serverItf;
134     }
135   }
136
137   public void unbindFc (final String JavaDoc clientItfName) {
138     if (CONFIGURATION_BINDING.equals(clientItfName)) {
139       configuration = null;
140     } else if (USER_DATA_BINDING.equals(clientItfName)) {
141       userData = null;
142     } else if (SAVE_ACTION_BINDING.equals(clientItfName)) {
143       save = null;
144     }
145   }
146
147   // -------------------------------------------------------------------------
148
// Implementation of the ActionListener interface
149
// -------------------------------------------------------------------------
150

151   public void actionPerformed (final ActionEvent JavaDoc e) {
152     try {
153       if (configuration.getChangeCount() > 0) {
154         Object JavaDoc[] options = { "Yes", "No", "Cancel" };
155         int n = JOptionPane.showOptionDialog (
156           null,
157           "Do you want to save the current configuration before exit ?",
158           "Warning",
159           JOptionPane.YES_NO_CANCEL_OPTION,
160           JOptionPane.QUESTION_MESSAGE,
161           null,
162           options,
163           options[0]);
164         if (n == 0) {
165           save.actionPerformed(e);
166         }
167         else if (n == 2) return;
168       }
169       /*if (userData != null) {
170         userData.setStringData(UserData.LAST_OPEN_CONF, null);
171       }*/

172     } catch (Exception JavaDoc ex) { }
173     if (userData != null) {
174       userData.save();
175     }
176     System.exit(0);
177   }
178 }
179
Popular Tags