KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > tools > admin > OpenJMSConsumer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: OpenJMSConsumer.java,v 1.1 2004/11/26 01:51:15 tanderson Exp $
44  *
45  * Date Author Changes
46  * $Date jimm Created
47  */

48
49 package org.exolab.jms.tools.admin;
50
51 import java.awt.Rectangle JavaDoc;
52 import java.awt.event.ActionEvent JavaDoc;
53 import java.awt.event.ActionListener JavaDoc;
54
55 import javax.swing.JMenuItem JavaDoc;
56 import javax.swing.JOptionPane JavaDoc;
57 import javax.swing.JPopupMenu JavaDoc;
58 import javax.swing.JTree JavaDoc;
59 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
60 import javax.swing.tree.DefaultTreeModel JavaDoc;
61
62
63 /**
64  * This class controls all dispay characteristics and menus related to a
65  * consumer. Currently only add, delete and edit are allowed. Add is added
66  * from the QueueTopic handler. Only one menu is created for all consumers
67  * since it is modal, it can be shared by all the consumer nodes.
68  *
69  * The menu is moved and displayed underneath the node that activated it.
70  *
71  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:15 $
72  * @author <a HREF="mailto:mourikis@exolab.org">Jim Mourikis</a>
73  * @see AdminMgr
74  * @see OpenJMSNode
75  */

76 public class OpenJMSConsumer extends DefaultMutableTreeNode JavaDoc
77     implements OpenJMSNode {
78
79     // The unique consumer name.
80
private String JavaDoc consumerName_;
81
82     // A reference to the tree this node belongs to.
83
static private JTree JavaDoc tree_ = null;
84
85     // A flag indicating if the menu has been created yet.
86
static private boolean commandsCreated_ = false;
87
88     // The popup menu for all consumers.
89
static private JPopupMenu JavaDoc commands_ = null;
90
91     /**
92      * The constructor gets its unique name for this consumer plus a
93      * reference to its parent tree.
94      *
95      * <P>If this is the first consumer call the menu for all consumers
96      * is created.
97      *
98      * @param consumerName This consumers unique name.
99      * @param tree The parent tree this consumer belons to.
100      *
101      */

102     public OpenJMSConsumer(String JavaDoc consumerName, JTree JavaDoc tree) {
103         consumerName_ = consumerName;
104         if (!commandsCreated_) {
105             tree_ = tree;
106             createCommands();
107             commandsCreated_ = true;
108         }
109     }
110
111     /**
112      * Create the menu for all consumers and set up the Action events for
113      * each menu item. Since menus are shared, the callbacks called are
114      * static. Once a menu is slected, the slected node can be determined
115      * from the parent object.
116      *
117      */

118     protected void createCommands() {
119         commands_ = new JPopupMenu JavaDoc();
120
121         JMenuItem JavaDoc m = new JMenuItem JavaDoc("De-Activate Consumer");
122         m.addActionListener(new ActionListener JavaDoc() {
123
124             public void actionPerformed(ActionEvent JavaDoc evt) {
125                 unregisterConsumer();
126             }
127         }
128         );
129         commands_.add(m);
130
131         m = new JMenuItem JavaDoc("Delete Consumer");
132         m.addActionListener(new ActionListener JavaDoc() {
133
134             public void actionPerformed(ActionEvent JavaDoc evt) {
135                 deleteConsumer();
136             }
137         }
138         );
139         commands_.add(m);
140     }
141
142
143     /**
144      * No children are allowed for consumers at this point.
145      *
146      * @return boolean Always returns false.
147      *
148      */

149     public boolean getAllowsChildren() {
150         return false;
151     }
152
153
154     /**
155      * All consumers are leaves in the tree for this release.
156      *
157      * @return boolean Always returns true.
158      *
159      */

160     public boolean isLeaf() {
161         return true;
162     }
163
164
165     /**
166      * This method is defined by the interface, but is not required by
167      * consumers for this release.
168      *
169      */

170     public void update() {
171         // do nothing for the moment
172
}
173
174     /**
175      * This node has been right clicked. The locations of this node is given
176      * by the loc object. Use this location to popup the consumer message
177      * menu.
178      *
179      * @param The location of this Consumer node.
180      *
181      */

182     public void displayCommands(Rectangle JavaDoc loc) {
183         double x;
184         double y;
185
186         x = loc.getX();
187         y = loc.getY();
188         y += loc.getHeight();
189
190         commands_.show(tree_, (int) x, (int) y);
191     }
192
193
194     /**
195      * The unique name of this consumer.
196      *
197      * @return String the consumer name.
198      *
199      */

200     public String JavaDoc toString() {
201         return consumerName_;
202     }
203
204     /**
205      * This node has changed. Inform the parent tree that it needs to be
206      * re-drawn.
207      *
208      */

209     private void refresh() {
210         DefaultTreeModel JavaDoc model = (DefaultTreeModel JavaDoc) tree_.getModel();
211         model.nodeStructureChanged((DefaultMutableTreeNode JavaDoc) this);
212     }
213
214     /**
215      * Get the particular instance of the consumer that has been selected.
216      *
217      * @return OpenJMSConsumer the instance selected.
218      *
219      */

220     static private OpenJMSConsumer getInstanceSelected() {
221         Object JavaDoc loc = tree_.getLastSelectedPathComponent();
222         return (OpenJMSConsumer) loc;
223     }
224
225
226     /**
227      * The edit consumer option has been selected.
228      * Not currently implemented.
229      *
230      */

231     static private void editConsumer() {
232         OpenJMSConsumer This = getInstanceSelected();
233         System.out.println("editConsumer");
234     }
235
236     /**
237      * Delete the selected consumer object. Display a confirmation dialog
238      * and wait for its return. If the user has confirmed the action, first
239      * delete it from the database and if that is successful remove the node
240      * from the tree.
241      *
242      */

243     static private void deleteConsumer() {
244         OpenJMSConsumer This = getInstanceSelected();
245         QueryDialog.instance().display
246             ("Are you sure you want to delete \nselected Consumer: "
247             + This.consumerName_);
248         if (org.exolab.jms.tools.admin.QueryDialog.instance().isConfirmed()) {
249             OpenJMSTopic topic = (OpenJMSTopic) This.parent;
250
251             if (AbstractAdminConnection.instance().removeDurableConsumer(This.consumerName_)) {
252                 This.removeFromParent();
253                 This.refresh();
254             } else {
255                 JOptionPane.showMessageDialog
256                     (tree_, "Failed to remove Consumer",
257                         "Remove Consumer Error", JOptionPane.ERROR_MESSAGE);
258             }
259         }
260     }
261
262
263     /**
264      * Unregister the selected consumer object. Display a confirmation dialog
265      * and wait for its return. If the user has confirmed the action, then
266      * proceed with the action. This is currently the only way to unregister
267      * a persistent consumer.
268      *
269      */

270     static private void unregisterConsumer() {
271         OpenJMSConsumer This = getInstanceSelected();
272         org.exolab.jms.tools.admin.QueryDialog.instance().display
273             ("Are you sure you want to De-Activate \nselected Consumer: "
274             + This.consumerName_);
275         if (org.exolab.jms.tools.admin.QueryDialog.instance().isConfirmed()) {
276             OpenJMSTopic topic = (OpenJMSTopic) This.parent;
277
278             if (!AbstractAdminConnection.instance().unregisterConsumer(This.consumerName_)) {
279                 JOptionPane.showMessageDialog
280                     (tree_, "Consumer is not currently active",
281                         "De-Activate Consumer Error", JOptionPane.ERROR_MESSAGE);
282             }
283         }
284     }
285
286 } // End OpenJMSConsumer
287
Popular Tags