📄 openjmscontext.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: OpenJMSContext.java,v 1.6 2003/08/17 01:32:23 tanderson Exp $
*
* Date Author Changes
* $Date jimm Created
*/
package org.exolab.jms.jndiadministration;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingException;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/**
* This class controls all dispay characteristics and menus related to a
* queue/topic object. Currently only add/delete, edit and purge is supported.
* Add is added from the OpenJMSServer node. Only one menu is created for
* all queue/topics since it is modal, it can be shared by all the nodes.
*
* @version $Revision: 1.6 $ $Date: 2003/08/17 01:32:23 $
* @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
* @see AdminMgr
*/
public class OpenJMSContext extends DefaultMutableTreeNode
implements OpenJMSNode {
// The Context Name
private String contextName_;
// The display name
private String displayName_;
// Does this context have any nodes
private boolean isLeaf_;
// Whether this node has been opened and explored already.
private boolean isExplored_ = false;
// A reference to the tree this node belongs to.
static private JTree tree_ = null;
// A flag indicating if the menu has been created yet.
static private boolean commandsCreated_ = false;
// The popup menu for all contexts
static private JPopupMenu commands_ = null;
/**
* The constructor gets its unique name for this context and a
* reference to its parent tree.
*
* <P>If this is the first context call, the menu for all contexts
* is created.
*
* @param contextName This context name.
* @param tree The parent tree this context belongs to.
*
*/
public OpenJMSContext(String contextName, String parentName, JTree tree) {
if (parentName != null) {
contextName_ = parentName + "." + contextName;
} else {
contextName_ = contextName;
}
displayName_ = contextName;
isLeaf_ = false;
if (!commandsCreated_) {
tree_ = tree;
createCommands();
commandsCreated_ = true;
}
}
/**
* Create the menu for all contexts and set up the Action events for
* each menu item. Since menus are shared, the callbacks called are
* static. Once a menu is slected, the slected node can be determined
* from the parent object.
*
*/
protected void createCommands() {
commands_ = new JPopupMenu();
JMenuItem m = new JMenuItem("Add Context");
m.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
createContext();
}
});
commands_.add(m);
m = new JMenuItem("Bind Object");
m.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
bindObject();
}
});
commands_.add(m);
m = new JMenuItem("Rename Context");
m.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
renameContext();
}
});
commands_.add(m);
m = new JMenuItem("Delete Context");
m.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
deleteContext();
}
});
commands_.add(m);
}
/**
* Children are allowed for all contexts
*
* @return boolean Always returns true.
*
*/
public boolean getAllowsChildren() {
return true;
}
/**
* Contexts are leaves iff they have no objects registered against
* them.
*
* @return boolean true if no objects are registered.
*/
public boolean isLeaf() {
return isLeaf_;
}
/**
* As a performance enhancement, no child is added to the context until
* after it is expanded.
*/
public void update() {
if (!isExplored_) {
Object ob = AdminConnection.instance().lookup(contextName_);
if (ob != null) {
if (ob instanceof Context) {
Enumeration e = AdminConnection.instance().getAllContexts
(contextName_);
if (e != null) {
while (e.hasMoreElements()) {
NameClassPair pair =
(NameClassPair) e.nextElement();
add(new OpenJMSContext
(pair.getName(), contextName_, tree_));
}
}
} else {
add(new OpenJMSObject(ob.getClass().getName(), tree_));
}
refresh();
}
isExplored_ = true;
}
}
/**
* This node has been right clicked. The locations of this node is given
* by the loc object. Use this location to popup the context
* menu.
*
* @param The location of this node.
*
*/
public void displayCommands(Rectangle loc) {
double x;
double y;
x = loc.getX();
y = loc.getY();
y += loc.getHeight();
commands_.show(tree_, (int) x, (int) y);
}
/**
* The unique name of this Context
*
* @return String the context name
*/
public String toString() {
return displayName_;
}
/**
* This node has changed. Inform the parent tree that it needs to be
* re-drawn.
*/
private void refresh() {
DefaultTreeModel model = (DefaultTreeModel) tree_.getModel();
model.nodeStructureChanged((DefaultMutableTreeNode) this);
}
/**
* Get the particular instance of the context that has been selected.
*
* @return OpenJMSContext the instance selected.
*/
static private OpenJMSContext getInstanceSelected() {
Object loc = tree_.getLastSelectedPathComponent();
return (OpenJMSContext) loc;
}
/**
* Create a new context. Popup the dialog, and prompt the user for the
* context name. Attempt to create the context. If creation is successfull
* add the node to the tree.
*/
static private void createContext() {
OpenJMSContext This = getInstanceSelected();
ObjectDialog.instance().display("Enter a unique context name",
"Context Creation");
if (ObjectDialog.instance().isConfirmed()) {
try {
AdminConnection.instance().createContext
(This.contextName_ + "." +
ObjectDialog.instance().getName());
This.add(new OpenJMSContext
(ObjectDialog.instance().getName(), This.contextName_,
tree_));
This.refresh();
} catch (NamingException exception) {
JOptionPane.showMessageDialog(
tree_, exception.getMessage(), "Context Create Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* Rename the selected context.
*/
static private void renameContext() {
OpenJMSContext This = getInstanceSelected();
ObjectDialog.instance().display(
"Enter a new context name\n" +
"for context: " + This.displayName_, "Rename Context");
if (ObjectDialog.instance().isConfirmed()) {
try {
String newName = This.contextName_.substring
(0, This.contextName_.indexOf(This.displayName_));
newName += ObjectDialog.instance().getName();
AdminConnection.instance().renameContext(
This.contextName_, newName);
This.contextName_ = newName;
This.displayName_ = ObjectDialog.instance().getName();
This.refresh();
} catch (NamingException exception) {
JOptionPane.showMessageDialog(
tree_, exception.getMessage(),
"Context Rename Error", JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* Create a new Object. The object must be given its fully qualified name
* and have a default constructor.
* For this release no paramaters are passed in.
*/
static private void bindObject() {
String errorString = null;
OpenJMSContext This = getInstanceSelected();
ObjectDialog.instance().display(
"Enter the objects fully qualified name", "Bind Object");
if (ObjectDialog.instance().isConfirmed()) {
try {
Object ob = null;
if (ObjectDialog.instance().getName().equals(
"org.exolab.jms.client.JmsTopic")) {
ob = new org.exolab.jms.client.JmsTopic(This.contextName_);
} else if (ObjectDialog.instance().getName().equals(
"org.exolab.jms.client.JmsQueue")) {
ob = new org.exolab.jms.client.JmsQueue(This.contextName_);
((org.exolab.jms.client.JmsDestination)
ob).setPersistent(true);
} else {
ob = Class.forName(
ObjectDialog.instance().getName()).newInstance();
}
AdminConnection.instance().rebind(This.contextName_, ob);
This.removeAllChildren();
This.add(new OpenJMSObject(
ObjectDialog.instance().getName(), tree_));
This.refresh();
} catch (NamingException exception) {
errorString = exception.getMessage();
} catch (IllegalAccessException exception) {
errorString = "Illegal Access error for\n" + "Object: " +
ObjectDialog.instance().getName();
} catch (InstantiationException exception) {
errorString = "Object: " + ObjectDialog.instance().getName() +
"\nCannot be instantaiated";
} catch (ClassNotFoundException exception) {
errorString = "Object: " + ObjectDialog.instance().getName() +
"\nCannot be found";
}
if (errorString != null) {
JOptionPane.showMessageDialog(
tree_, errorString, "Object bind error",
JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* Delete the selected context object. Display a confirmation dialog
* and wait for its return. If the user has confirmed the action, first
* delete it from the database and if that is successful remove the node
* from the tree.
*
* <P>Note: deleting a context also deletes all subcontexts and bound
* objects.
*/
static private void deleteContext() {
OpenJMSContext This = getInstanceSelected();
QueryDialog.instance().display(
"Are you sure you want to delete \nselected Context: "
+ This.displayName_);
if (QueryDialog.instance().isConfirmed()) {
try {
AdminConnection.instance().destroyContext(This.contextName_);
This.removeFromParent();
This.refresh();
} catch (NamingException exception) {
JOptionPane.showMessageDialog(
tree_, exception.getMessage(), "Context Destroy Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
} //-- OpenJMSContext
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -