⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirpanel.java

📁 J[2]EE JavaJNDI jndi-1_1-ea2-demo
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	// Workaround for problem with URLs and HTML Editor.	// This might not work inside an applet.	// Workaround tries to get HREF's to work, gets further	// but still doesn't work./*	try {	    File f = new File(filename);	    url = new URL("file:" + f.getAbsolutePath());	} catch (MalformedURLException e) {	    System.out.println("Cannot open " + filename);	    return;	}*/// Note that the following generates URLs that the HTML Editor// doesn't seem to understand. It causes it to generate HyperlinkEvents// containing null URLs. But then, relative URLs don't// seem to work anyways. 	    URL url = Browser.browser.getResource(		Browser.BROWSER_DIR + "readme.html");	    if (url == null) {		setStatus("Help file not found");		return;	    }	    waitCursor();	    moveWindow(new HelpDialog(url));	    restoreCursor();	}    }    /**      * ReloadAction is used to reload from the selected node.  If nothing      * is selected, reload is not issued.      */    class ReloadAction implements ActionListener {	/**	  * Reload starting from the selected node.  If nothing	  * is selected, reload is not issued.          *          * Invoked when the user clicks on the Reload menu item.          * Determines the selection from the Tree and asks the treemodel          * to reload from that node.          */	public void actionPerformed(ActionEvent evt) {	    DefaultMutableTreeNode target = getSelectedNode();	    if(target != null) {		waitCursor();		treeModel.reload(target);		restoreCursor();	    } else		setStatus("Select tree node to be reloaded first");	}    }    /**      * RemoveAction removes the selected node from the tree.  If      * The root or nothing is selected nothing is removed.      */    class RemoveAction implements ActionListener {	/**	  * Removes the selected item as long as it isn't root.	  */	public void actionPerformed(ActionEvent evt) {	    removeSelectedNode();	}    }/**  * DirTreeModel extends DefaultTreeModel to override valueForPathChanged().  * This method is called as a result of the user editing the string name of  * a node in the tree, which in turn results in the node being renamed in  * the underlying namespace.  *  */    public class DirTreeModel extends DefaultTreeModel {    /**      * Creates a new instance of DirTreeModel with newRoot set      * to the root of this model.      */    public DirTreeModel(TreeNode newRoot) {	super(newRoot);    }    public void reload(TreeNode node) {	if (node != null) {	    ((DirTreeNode)node).loadChildren(true);	    super.reload(node);	}    }    /**     * Attempts to rename node as specified. If not successful, restore old name.     */    public void valueForPathChanged(TreePath path, Object newValue) {	DirTreeNode target = (DirTreeNode)path.getLastPathComponent();	if (((DirData)(target.getUserObject())).getName().equals(newValue)) {	    // nothing has changed	    return;	}	try {	    Context parentCtx = getParentContext(target);	    // Update namespace	    String oldName = 		((DirData)(target.getUserObject())).getName();DirPanel.debugName("rename", oldName);DirPanel.debugName("rename", (String)newValue);	    if (debug) 		System.err.println("renaming " + oldName + 		    " to " + (String)newValue);	    parentCtx.rename(oldName, (String)newValue);	    // Update tree node	    ((DirData)target.getUserObject()).setName((String)newValue);	    // Update tree model	    nodeChanged(target);	    // Reset status 	    resetStatus();	} catch (NamingException e) {	    // e.printStackTrace();	    setStatus(e);	}    }}/**  * This class represents a node in the namespace/directory.  * It is modified from DynamicTreeNode in Swing's SampleTreeexample.  *  * Each DirTreeNode has a user object that is of class DirData.  *  */public class DirTreeNode extends DefaultMutableTreeNode {    boolean hasLoaded = false;    /**      * Constructs a new DirTreeNode instance with userObj as the user      * object.      */    public DirTreeNode(Object userObj) {	super(userObj);    }    /**      * Determines whether this node is a leaf node.      * A node is a leaf if it is not a context.      * Overriding this seems to affect the display.      * When not overridden, empty contexts are displayed as "dots".      * When overridden as follows, empty contexts are displayed as folders.      */    public boolean isLeaf() {	return !((DirData)getUserObject()).isContext();    }    /**      * Determines whether this node can have children.      * Only contexts can have children.      */    public boolean getAllowsChildren() {	return ((DirData)getUserObject()).isContext();    }    /**      * Loads the children of this node and return the number of children.      *      * If the children have not been loaded yet, use loadChildren()      * to load the children before returning the number of children.      */    public int getChildCount() {	loadChildren(false); // should be empty to start	return super.getChildCount();    }    /**      * Loads children from the naming/directory service by using list().      *      * This method is invoked the first time getChildCount() is called.      * The list is then cached, so there might be slight inconsistencies      * between the actual state of the namespace/directory and what is      * shown.      * If higher level of consistency is desired, then the list should      * be refreshed (each time getChildCount()) is called but this      * can be prohibitively costly.      */    boolean loadChildren(boolean force) {	// If not forced to reload, and children have already	// been loaded, return true.	if (!force && hasLoaded) {	    resetStatus();	    return true;	}	waitCursor();	DirData dirData = (DirData)getUserObject();	String name = dirData.getName();	Object obj = dirData.getObject();	try {	    if (obj == null) {		// object not set yet, go and fetch it by		// doing a lookup() in the parent context		try {		    Context parentCtx = getParentContext(DirTreeNode.this);DirPanel.debugName("load children", name);		    obj = parentCtx.lookup(name);		} catch (NamingException e) {		    // e.printStackTrace();		    setStatus(e);		    return hasLoaded = false;		}		// Update object stored in DirData with newly gathered info		dirData.setObject(obj);		dirData.resetContextFlag();	    }	    if (!(obj instanceof Context)) {		setStatus(name + " is of class " + 		    dirData.getClassName(), false);		return hasLoaded = false;	    }	    DirData childData;	    DirTreeNode childNode;	    int counter = 0;	    if (force) {		removeAllChildren();	    }	    Context ctx = (Context)obj;	    try {		NamingEnumeration nl = ctx.list("");		while (nl.hasMoreElements()) {		    NameClassPair nc = (NameClassPair)nl.next();		    childData = new DirData(nc.getName(), nc.getClassName());		    childNode = new DirTreeNode(childData);		    insert(childNode, counter++);		}		resetStatus();		return hasLoaded = true;		    } catch (NamingException e) {		// e.printStackTrace();		setStatus(e);		return hasLoaded = false;	    }	} finally {	    restoreCursor();	}    }}/**  * This class is used to draw a tree node.  *   * @see DirIconMap  */    static Icon leafIcon = UIManager.getIcon("Tree.leafIcon");    static Icon closedIcon =  UIManager.getIcon("Tree.closedIcon");    static Icon openIcon = UIManager.getIcon("Tree.openIcon");    class DirTreeCellRenderer extends JLabel implements TreeCellRenderer {	DirTreeCellRenderer() {	    setOpaque(true);	}    public Component getTreeCellRendererComponent(	JTree tree,	Object value,	boolean isSelected,	boolean isExpanded,	boolean isLeaf,	int row,	boolean hasFocus) {	String stringValue = tree.convertValueToText(value, isSelected,	    isExpanded, isLeaf, row, hasFocus);	setText(stringValue);	DirTreeNode node = (DirTreeNode)value;	DirData realObj = (DirData)node.getUserObject();	String className = realObj.getClassName();	Icon icon = DirIconMap.get(className, isExpanded);	if (icon == null) {	    if (isLeaf)		icon = leafIcon;	    else if (isExpanded)		icon = openIcon;	    else 		icon = closedIcon;	}	setIcon(icon);	if (isSelected) {	    setForeground(list.getSelectionForeground());	    setBackground(list.getSelectionBackground());	} else {	    setBackground(tree.getBackground());	    setForeground(tree.getForeground());	}	if (hasFocus) {	    setBorder(focusBorder);	} else {	    setBorder(noFocusBorder);	}	return this;    }}/**  * This class controls how each cell in a list is rendered.  * DirIconMap contains a mapping of object class names to  * icons. If an object's class does not have an entry in  * this map, its icon is the default  * (either a folder if it is a context, or the a filled dot).  * <p>  * This class is almost identical to BasicListCellRenderer  * except for how it renders both the icon and text.  */    static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);    static Border focusBorder = new LineBorder(Color.red, 1);class DirListCellRenderer extends JLabel implements ListCellRenderer {    public DirListCellRenderer() {	setOpaque(true);	setBorder(noFocusBorder);    }    public Component getListCellRendererComponent(	JList list,	Object value,	int index,	boolean isSelected,	boolean hasFocus) {		// Set background depending whether cell is selected	if (isSelected) {	    setBackground(list.getSelectionBackground());	    setForeground(list.getSelectionForeground());	} else {	    setBackground(list.getBackground());	    setForeground(list.getForeground());	}		DirTreeNode node = (DirTreeNode)value;	DirData realObj = (DirData)node.getUserObject();	String className = realObj.getClassName();	setText(realObj.getName());	Icon icon = DirIconMap.get(className);	if (icon == null) {	    icon = (realObj.isContext() ? closedIcon : leafIcon);	}	if (icon != null) {	    setIcon(icon);	}	setFont(list.getFont());	setBorder((hasFocus) ? focusBorder : noFocusBorder);	return this;    }}    static public void debugName(String msg, String cn) {	if (debug) {	    System.out.print(msg + " [");	    //	    System.out.print("size: " + cn.size());	    System.out.println("name: " + cn.toString() + "]");	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -