KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClientInterface;
29 import org.objectweb.fractal.gui.model.Component;
30 import org.objectweb.fractal.gui.model.Interface;
31 import org.objectweb.fractal.gui.model.ServerInterface;
32 import org.objectweb.fractal.gui.model.IllegalOperationException;
33 import org.objectweb.fractal.gui.selection.model.Selection;
34 import org.objectweb.fractal.gui.selection.model.SelectionListener;
35 import org.objectweb.fractal.swing.AbstractAction;
36
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.net.URL JavaDoc;
39
40 import javax.swing.ImageIcon JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42 import javax.swing.KeyStroke JavaDoc;
43
44 /**
45  * An action to delete the currently selected component or interface. This
46  * action listens to the selection model in order to enable or disable itfself
47  * when the selection changes.
48  */

49
50 public class DeleteAction extends AbstractAction implements
51   SelectionListener,
52   BindingController
53 {
54
55   /**
56    * A mandatory client interface bound to a {@link Selection selection} model.
57    * This model is used to know the component or interface to be deleted.
58    */

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

65
66   private Selection selection;
67
68   /**
69    * Constructs a new {@link DeleteAction} component.
70    */

71
72   public DeleteAction () {
73     putValue(NAME, "Delete");
74     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("DELETE"));
75     putValue(SHORT_DESCRIPTION, "Delete");
76     URL JavaDoc url = getClass().getResource(
77       "/org/objectweb/fractal/gui/resources/edittrash.gif");
78     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
79     setEnabled(false);
80   }
81
82   // -------------------------------------------------------------------------
83
// Implementation of the BindingController interface
84
// -------------------------------------------------------------------------
85

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

116   public void selectionChanged () {
117     boolean enabled = false;
118     Object JavaDoc o = selection.getSelection();
119     if (o instanceof Component) {
120       Component c = (Component)o;
121       enabled = c.getParent() != null;
122     } else if (o instanceof Interface) {
123       enabled = true;
124     }
125     setEnabled(enabled);
126   }
127
128   // -------------------------------------------------------------------------
129
// Implementation of the ActionListener interface
130
// -------------------------------------------------------------------------
131

132   public void actionPerformed (final ActionEvent JavaDoc e) {
133     Object JavaDoc o = selection.getSelection();
134     try {
135       if (o instanceof Component) {
136         Component c = (Component)o;
137         c.getParent().removeSubComponent(c);
138       } else if (o instanceof Interface) {
139         Interface i = (Interface)o;
140         if (i.isInternal()) {
141           i = i.getComplementaryInterface();
142         }
143         if (i instanceof ClientInterface) {
144           i.getOwner().removeClientInterface((ClientInterface)i);
145         } else {
146           i.getOwner().removeServerInterface((ServerInterface)i);
147         }
148       }
149       selection.clearSelection();
150     } catch (IllegalOperationException ioe) {
151       JOptionPane.showMessageDialog(
152         null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
153     }
154   }
155 }
156
Popular Tags