KickJava   Java API By Example, From Geeks To Geeks.

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


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 start on AdminModel.
43  */

44
45 public class StartAction 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    * An mandatory client interface bound to a {@link Selection Selection}.
58    */

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

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

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

78
79   public StartAction () {
80     putValue(NAME, "Start");
81     putValue(SHORT_DESCRIPTION, "Start");
82     URL JavaDoc url = getClass().getResource(
83       "/org/objectweb/fractal/gui/resources/run.gif");
84     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
85     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("F11"));
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     if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
118       adminmodel = null;
119     } else if (SELECTION_BINDING.equals(clientItfName)) {
120       selection = null;
121     }
122   }
123
124   // -------------------------------------------------------------------------
125
// Implementation of the SelectionListener interface
126
// -------------------------------------------------------------------------
127

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

141   public void actionPerformed (final ActionEvent JavaDoc e) {
142     Object JavaDoc o = selection.getSelection();
143     try {
144       if (o instanceof Component) {
145         Component c = (Component)o;
146
147         org.objectweb.fractal.api.Component ci = adminmodel.getInstance(c);
148         if (ci == null) {
149           JOptionPane.showMessageDialog (null,
150           "You must create an instance before starting it!",
151           "Alert ...", JOptionPane.ERROR_MESSAGE);
152           return;
153         }
154         adminmodel.start(c);
155       }
156     } catch (Exception JavaDoc ex) {
157       JOptionPane.showMessageDialog(
158         null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
159         return;
160     }
161   }
162 }
163
Popular Tags