KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > admin > control > StopAction


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.admin.control;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.model.Component;
29 import org.objectweb.fractal.swing.AbstractAction;
30 import org.objectweb.fractal.gui.admin.model.AdminModel;
31 import org.objectweb.fractal.gui.selection.model.Selection;
32 import org.objectweb.fractal.gui.selection.model.SelectionListener;
33
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.net.URL JavaDoc;
36
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.KeyStroke JavaDoc;
39 import javax.swing.JOptionPane JavaDoc;
40
41 /**
42  * An action to call stop method on an AdminModel
43  */

44
45 public class StopAction extends AbstractAction
46   implements BindingController, SelectionListener
47 {
48
49   /**
50    * A mandatory client interface bound to a {@link AdminModel AdminModel}
51    * model.
52    */

53
54   public final static String JavaDoc ADMIN_MODEL_BINDING = "admin-model";
55
56   /**
57    * A mandatory client interface bound to a {@link Selection selection} model.
58    * This model is used to know the component or interface to be deleted.
59    */

60
61   public final static String JavaDoc SELECTION_BINDING = "selection";
62
63   /**
64    * The AdminModel client interface.
65    */

66
67   private AdminModel adminmodel;
68
69   /**
70    * The SelectionModel client interface.
71    */

72
73   private Selection selection;
74
75   /**
76    * Constructs a new {@link StopAction} component.
77    */

78
79   public StopAction () {
80     putValue(NAME, "Stop");
81     putValue(SHORT_DESCRIPTION, "Stop");
82     URL JavaDoc url = getClass().getResource(
83       "/org/objectweb/fractal/gui/resources/stop.gif");
84     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
85     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("F12"));
86   }
87
88   // -------------------------------------------------------------------------
89
// Implementation of the UserBindingController interface
90
// -------------------------------------------------------------------------
91

92   public String JavaDoc[] listFc () {
93     return new String JavaDoc[] {
94       ADMIN_MODEL_BINDING,
95       SELECTION_BINDING
96     };
97   }
98
99   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
100     if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
101       return adminmodel;
102     } else if (SELECTION_BINDING.equals(clientItfName)) {
103       return selection;
104     }
105     return null;
106   }
107
108   public void bindFc (final String JavaDoc clientItfName, final Object JavaDoc serverItf) {
109     if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
110       adminmodel = (AdminModel)serverItf;
111     } else if (SELECTION_BINDING.equals(clientItfName)) {
112       selection = (Selection)serverItf;
113     }
114   }
115
116   public void unbindFc (final String JavaDoc clientItfName)
117   {
118     if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
119       adminmodel = null;
120     } else if (SELECTION_BINDING.equals(clientItfName)) {
121       selection = null;
122     }
123   }
124
125   // -------------------------------------------------------------------------
126
// Implementation of the SelectionListener interface
127
// -------------------------------------------------------------------------
128

129   public void selectionChanged () {
130     Object JavaDoc o = selection.getSelection();
131     if (o instanceof Component) {
132       setEnabled (true);
133     } else {
134       setEnabled(false);
135     }
136   }
137
138   // -------------------------------------------------------------------------
139
// Implementation of the ActionListener interface
140
// -------------------------------------------------------------------------
141

142   public void actionPerformed (final ActionEvent JavaDoc e) {
143     Object JavaDoc o = selection.getSelection();
144     try {
145       if (o instanceof Component) {
146         Component c = (Component)o;
147
148         org.objectweb.fractal.api.Component ci = adminmodel.getInstance(c);
149         if (ci == null) {
150           JOptionPane.showMessageDialog (null,
151           "The instance you want stop doesn't exist!",
152           "Alert ...", JOptionPane.ERROR_MESSAGE);
153           return;
154         }
155
156         if (!adminmodel.isStarted(c)) {
157           JOptionPane.showMessageDialog (null,
158           "Instance not started!",
159           "Alert ...", JOptionPane.ERROR_MESSAGE);
160           return;
161         }
162         adminmodel.stop(c);
163       }
164     } catch (Exception JavaDoc ex) {
165       JOptionPane.showMessageDialog(
166         null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
167         return;
168     }
169   }
170 }
171
Popular Tags