📄 smartpopuptool.java
字号:
package com.ca.directory.jxplorer.tree;
import com.ca.commons.cbutil.CBIntText;
import com.ca.commons.cbutil.CBUtility;
import com.ca.commons.naming.*;
import com.ca.directory.jxplorer.ButtonRegister;
import com.ca.directory.jxplorer.JXplorer;
import com.ca.directory.jxplorer.event.JXplorerEvent;
import javax.swing.*;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
/**
* This is the small popup menu that appears when a manager left-clicks
* (or system-dependant-whatever-s) on the display tree, allowing them
* to cut/copy/paste/delete/rename tree elements
*/
public class SmartPopupTool extends JPopupMenu
implements ActionListener
{
DN cutDN; // DN of node that was *previously* selected to be cut
DN copyDN; // DN of node that was *previously* selected to copy
DN selectDN; // DN of node grabbed by 'copy DN'
boolean newEnabled=true; // whether the 'new' option is on or not
JMenuItem cut,copy,paste,pasteAlias,delete,rename,search,newEntry,refresh,copydn,bookmark;
ButtonRegister br = null;
SmartTree tree;
private static Logger log = Logger.getLogger(SmartPopupTool.class.getName());
/**
* Constructor initialises the drop down menu and menu items,
* and registers 'this'
* component as being the listener for all the menu items.
* @param owningTree
*/
public SmartPopupTool(SmartTree owningTree)
{
tree = owningTree;
String dirImage = JXplorer.getProperty("dir.images");
String dirIcons = JXplorer.getProperty("dir.icons");
add(bookmark = new JMenuItem(CBIntText.get("Add to Bookmarks"), new ImageIcon(dirImage + "plus.gif")));
add(search = new JMenuItem(CBIntText.get("Search"), new ImageIcon( dirImage + "find.gif")));
add(new JPopupMenu.Separator());
add(newEntry = new JMenuItem(CBIntText.get("New"), new ImageIcon( dirImage + "new.gif")));
add(copydn = new JMenuItem(CBIntText.get("Copy DN"), new ImageIcon(dirImage + "copy_dn.gif")));
add(new JPopupMenu.Separator());
add(cut = new JMenuItem(CBIntText.get("Cut Branch"), new ImageIcon( dirImage + "cut.gif")));
add(copy = new JMenuItem(CBIntText.get("Copy Branch"), new ImageIcon( dirImage + "copy.gif")));
add(paste = new JMenuItem(CBIntText.get("Paste Branch"), new ImageIcon( dirImage + "paste.gif")));
add(pasteAlias = new JMenuItem(CBIntText.get("Paste Alias"), new ImageIcon(dirIcons + "alias.gif")));
add(new JPopupMenu.Separator());
add(delete = new JMenuItem(CBIntText.get("Delete"), new ImageIcon( dirImage + "delete.gif")));
add(rename = new JMenuItem(CBIntText.get("Rename"), new ImageIcon( dirImage + "rename.gif")));
add(refresh = new JMenuItem(CBIntText.get("Refresh"), new ImageIcon( dirImage + "refresh.gif")));
bookmark.setToolTipText(CBIntText.get("Bookmark an entry"));
bookmark.setAccelerator(KeyStroke.getKeyStroke("B".charAt(0), java.awt.Event.CTRL_MASK, false));
search.setAccelerator(KeyStroke.getKeyStroke("F".charAt(0), java.awt.Event.CTRL_MASK, false));
search.setToolTipText(CBIntText.get("Search for an entry in the directory."));
cut.setAccelerator(KeyStroke.getKeyStroke("U".charAt(0), java.awt.Event.CTRL_MASK, false));
cut.setToolTipText(CBIntText.get("Select a subtree to move."));
copy.setAccelerator(KeyStroke.getKeyStroke("O".charAt(0), java.awt.Event.CTRL_MASK, false));
copy.setToolTipText(CBIntText.get("Select a subtree to copy."));
paste.setAccelerator(KeyStroke.getKeyStroke("P".charAt(0), java.awt.Event.CTRL_MASK, false));
paste.setToolTipText(CBIntText.get("Paste a previously selected subtree."));
delete.setAccelerator(KeyStroke.getKeyStroke("D".charAt(0), java.awt.Event.CTRL_MASK, false));
delete.setToolTipText(CBIntText.get("Delete an entry."));
rename.setAccelerator(KeyStroke.getKeyStroke("M".charAt(0), java.awt.Event.CTRL_MASK, false));
rename.setToolTipText(CBIntText.get("Rename an entry."));
copydn.setAccelerator(KeyStroke.getKeyStroke("Y".charAt(0), java.awt.Event.CTRL_MASK, false));
copydn.setToolTipText(CBIntText.get("Copy the distinguished name of an entry to the clipboard"));
newEntry.setAccelerator(KeyStroke.getKeyStroke("N".charAt(0), java.awt.Event.CTRL_MASK, false));
newEntry.setToolTipText(CBIntText.get("Create a new entry."));
refresh.setAccelerator(KeyStroke.getKeyStroke("R".charAt(0), java.awt.Event.CTRL_MASK, false));
refresh.setToolTipText(CBIntText.get("Refresh an entry."));
for (int i=0; i<getComponentCount(); i++)
if (getComponent(i) instanceof JMenuItem)
((JMenuItem)getComponent(i)).addActionListener(this);
setVisible(false);
cutDN = null; copyDN = null; //activeDN = null;
br = JXplorer.getButtonRegister();
br.registerItem(br.PASTE, paste);
br.registerItem(br.PASTE_ALIAS, pasteAlias);
br.registerItem(br.COPY, copy);
br.registerItem(br.COPY_DN, copydn);
br.registerItem(br.CUT, cut);
br.registerItem(br.DELETE, delete);
br.registerItem(br.NEW, newEntry);
br.registerItem(br.RENAME, rename);
br.registerItem(br.REFRESH, refresh);
br.registerItem(br.BOOKMARKS, bookmark);
br.registerItem(br.SEARCH, search);
br.setItemEnabled(br.PASTE, false);
br.setItemEnabled(br.PASTE_ALIAS, false);
}
/**
* This displays the popup tool at the right spot. Some special
* magic here allows us to set the state of the popup tool if we want.
* @param invoker
* @param x
* @param y
*/
public void show(Component invoker, int x, int y)
{
SmartTree tree = (SmartTree) invoker;
SmartNode node = tree.getSelectedNode();
boolean modifiable = (node != null && (node.isStructural() == false) && !tree.getName().equalsIgnoreCase("Schema"));
br.setItemEnabled(br.RENAME, modifiable);
super.show(invoker, x, y);
}
/**
* Returns the last set active path (e.g. the TreePath corresponding to
* the last highlighted tree node).
*/
public TreePath getActivePath()
{
return tree.getSelectionPath();
}
/**
* Returns the most recently selected tree node.
*/
public SmartNode getActiveNode()
{
TreePath path = getActivePath();
return (path==null)?null:(SmartNode)path.getLastPathComponent();
}
/**
*
* @return
*/
public DN getActiveDN()
{
return tree.getTreeModel().getDNForPath(getActivePath());
}
/**
* This is called externally to popupTreeTool. It highlights
* the path, and records the highlighted node's DN for use
* by the menu items. This ftn is called from the mouse
* listener that kicks off the popupTreeTool, or by @SmartTree.valueChanged.
*
* @param ActivePath the set of nodes to select.
*/
/*
public void setActivePath(TreePath ActivePath)
{
activePath = ActivePath;
activeDN = tree.getTreeModel().getDNForPath(activePath);
}
*/
/**
* This sets whether the popup tool will show functions that will modify the directory.
* @param canModify
*/
public void setModifiable(boolean canModify)
{
if (canModify == false)
{
br.setItemEnabled(br.CUT, false);
br.setItemEnabled(br.COPY, false);
br.setItemEnabled(br.DELETE, false);
br.setItemEnabled(br.RENAME, false);
br.setItemEnabled(br.NEW, false);
br.setItemEnabled(br.SEARCH, false);
br.setItemEnabled(br.BOOKMARKS, false);
br.setItemEnabled(br.PASTE, false);
br.setItemEnabled(br.PASTE_ALIAS, false);
}
else
{
br.setItemEnabled(br.CUT, true);
br.setItemEnabled(br.COPY, true);
br.setItemEnabled(br.DELETE, true);
br.setItemEnabled(br.RENAME, true);
br.setItemEnabled(br.NEW, true);
br.setItemEnabled(br.SEARCH, true);
br.setItemEnabled(br.BOOKMARKS, true);
}
}
/**
*
* @param enable
*/
public void setNewEntryEnabled(boolean enable)
{
newEnabled = enable;
br.setItemEnabled(br.NEW, newEnabled);
}
/**
* This handles the menu item actions. They rely on
* 'activeDN' being set prior to this method being called
* (usually by setActivePath() above). Most of the action
* handling is simply tossing arguments to the treeDataSource,
* and any required tree methods to reflect the changes made.
*
* @param ev the active event, i.e. the menu item selected
*/
public void actionPerformed(ActionEvent ev)
{
Object event = ev.getSource();
setVisible(false);
repaint();
if (event == cut)
cut();
else if (event == copy)
copy();
else if (event == paste)
paste();
else if (event == pasteAlias)
pasteAlias();
else if (event == delete)
delete();
else if (event == rename)
rename();
else if (event == newEntry)
newEntry();
else if (event == refresh)
refresh();
else if (event == copydn)
copyDN();
else if (event == bookmark)
tree.openAddBookmarkDialog(getActiveDN()); //TE: open the bookmark dialog via SmartTree.
else if (event == search)
tree.openSearch(getActiveDN()); //TE: open the search dialog via SmartTree.
else // should never happen...
log.warning("Unknown event in popup menu:\n" + ev.toString());
// repaint();
}
// XXX All this code below needs to be hived off into a separate 'SmartTreeOperations'
// XXX class of something...
/**
* 'Cuts' a subtree by registering the DN of the cut branch,
* which is accessed by the paste ftn. - the @paste ftn then
* has responsibility for moving the subtree.
*/
public void cut()
{
DN activeDN = getActiveDN();
log.fine("Cut "+ activeDN);
cutDN = activeDN;
copyDN = null;
selectDN = null;
br.setItemEnabled(br.PASTE, true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -