package ij.plugin;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import ij.*;
import ij.gui.*;
import ij.io.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import ij.plugin.frame.PlugInFrame;
import ij.util.*;
import ij.text.TextWindow;
public class ControlPanel implements PlugIn {
private static final String fileSeparator=System.getProperty("file.separator");
private static final char sep=fileSeparator.charAt(0);
private Hashtable panels = new Hashtable();
private Vector visiblePanels = new Vector();
private Vector expandedNodes = new Vector();
private String defaultArg = "";
private boolean savePropsUponClose=true;
private boolean propertiesChanged=true;
private boolean closeChildPanelOnExpand = true;
private boolean requireDoubleClick;
private boolean quitting = true;
Vector menus = new Vector();
Vector allMenus = new Vector();
Hashtable commands=new Hashtable();
Hashtable menuCommands=new Hashtable();
String[] pluginsArray;
Hashtable treeCommands = new Hashtable();
int argLength;
private String path=null;
private DefaultMutableTreeNode root;
MenuItem reloadMI = null;
public ControlPanel() {
}
public void run(String arg) {
load();
}
synchronized void load() {
commands = Menus.getCommands();
pluginsArray = Menus.getPlugins();
root=doRootFromMenus();
if (root==null || root.getChildCount()==0 ) return; loadProperties();
restoreVisiblePanels();
if (panels.isEmpty())
newPanel(root);
}
private synchronized DefaultMutableTreeNode doRootFromMenus() {
DefaultMutableTreeNode node = new DefaultMutableTreeNode("ImageJ Menus");
if(argLength==0) node.setUserObject("Control Panel");
MenuBar menuBar = Menus.getMenuBar();
for (int i=0; i<menuBar.getMenuCount(); i++) {
Menu menu = menuBar.getMenu(i);
DefaultMutableTreeNode menuNode = new DefaultMutableTreeNode(menu.getLabel());
recurseSubMenu(menu, menuNode);
node.add(menuNode);
}
return node;
}
private void recurseSubMenu(Menu menu, DefaultMutableTreeNode node) {
int items = menu.getItemCount();
if(items==0) return;
for (int i=0; i<items; i++) {
MenuItem mItem = menu.getItem(i);
String label = mItem.getActionCommand();
if (mItem instanceof Menu) {
DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(label);
recurseSubMenu((Menu)mItem,subNode);
node.add(subNode);
} else if (mItem instanceof MenuItem) {
if (!(label.equals("-"))) {
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(label);
node.add(leaf);
if(treeCommands==null) treeCommands = new Hashtable();
if (label.equals("Reload Plugins")) {
reloadMI = mItem;
treeCommands.put(label,"Reload Plugins From Panel");
}
}
}
}
}
private void populateNode(Hashtable collection, DefaultMutableTreeNode node) {
Vector labelVector = new Vector();
for (Enumeration e=collection.keys(); e.hasMoreElements();) {
String key = (String)e.nextElement();
labelVector.addElement(key);
}
String[] labels = new String[labelVector.size()];
String[] items = new String[labelVector.size()];
labelVector.copyInto((String[])labels); StringSorter.sort(labels);
for(int i=0; i<labels.length; i++) {
items[i] = (String)collection.get(labels[i]); }
populateNode(items,labels,node);
}
private void populateNode(String[] items, String[] labels, DefaultMutableTreeNode node) {
if (items.length==0 || items.length!=labels.length) return;
String label=null;
for (int i=0; i<items.length; i++) {
if(labels!=null && i<labels.length)
label = labels[i];
buildTreePath(items[i], label, node);
}
}
private void buildTreePath(String source, String label, DefaultMutableTreeNode topNode) {
buildTreePath(source, label, null, topNode);
}
private void buildTreePath(String source, String label, String command, DefaultMutableTreeNode topNode) {
String local=source; String argument=""; String delimiter = fileSeparator;
int leftParen=source.indexOf('(');
int rightParen = source.indexOf(')');
if (leftParen>-1 && rightParen>leftParen) {
argument = source.substring(leftParen+1, rightParen);
local = source.substring(0,leftParen);
}
String pluginsPath=Menus.getPlugInsPath();
if (local.startsWith(pluginsPath))
local = local.substring(pluginsPath.length(),local.length());
local=local.replace('.',delimiter.charAt(0));
if (argument.length()>0)
local=local.concat(fileSeparator).concat(argument);
DefaultMutableTreeNode node=null;
StringTokenizer pathParser = new StringTokenizer(local,delimiter);
int tokens = pathParser.countTokens();
while(pathParser.hasMoreTokens()) {
String token = pathParser.nextToken();
tokens--;
if (topNode.isLeaf()&&topNode.getAllowsChildren()) {
if(token.indexOf("ControlPanel")==-1) { if(tokens==0) {
if(label!=null) token=label; token=token.replace('_',' ');
if(token.endsWith(".class"))
token = token.substring(0,token.length()-6); }
node = new DefaultMutableTreeNode(token);
if (tokens==0) {
String cmd = (command==null) ? token : command;
if(treeCommands==null) treeCommands = new Hashtable();
if(!treeCommands.containsKey(token)) treeCommands.put(token,cmd);
}
topNode.add(node);
topNode=node;
}
continue;
} else {
boolean hasTokenAsNode=false;
Enumeration nodes = topNode.children();
while(nodes.hasMoreElements()) {
node = (DefaultMutableTreeNode)nodes.nextElement();
if(((String)node.getUserObject()).equals(token)) {
hasTokenAsNode = true;
topNode = node;
break;
}
}
if (!hasTokenAsNode) {
if (token.indexOf("ControlPanel")==-1) {
if (tokens==0) { if(label!=null) token = label;
token=token.replace('_',' ');
if (token.endsWith(".class"))
token=token.substring(0,token.length()-6); }
node = new DefaultMutableTreeNode(token);
topNode.add(node);
topNode=node;
}
}
}
}
}
TreePanel newPanel(DefaultMutableTreeNode node) {
boolean main = node.getUserObject().equals(root.getUserObject());
TreePanel panel = new TreePanel(node, this, main);
return panel;
}
TreePanel newPanel(DefaultMutableTreeNode node, Point location) {
boolean main = node.getUserObject().equals(root.getUserObject());
TreePanel panel = new TreePanel(node, this, main, location);
return panel;
}
TreePanel newPanel(String path) {
if (path.equals("Control_Panel.@Main")) path = "Control_Panel";
path = key2pStr(path);
TreePanel pnl = null;
for(Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements();) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode)e.nextElement();
TreePath p = new TreePath(n.getPath());
if (p.toString().equals(path))
pnl=newPanel(n);
}
return pnl;
}
boolean requiresDoubleClick() {return requireDoubleClick;}
void setDoubleClick(boolean dc) {requireDoubleClick = dc;}
boolean hasPanelForNode(DefaultMutableTreeNode node) {
TreePath path = new TreePath(node.getPath());
return panels.containsKey(pStr2Key(path.toString()));
}
TreePanel getPanelForNode(DefaultMutableTreeNode node) {
TreePath path = new TreePath(node.getPath());
String pathString = path.toString();
if (panels.containsKey(pStr2Key(pathString)))
return (TreePanel)panels.get(pStr2Key(pathString));
else return null;
}
public DefaultMutableTreeNode getRoot() {return root;}
Hashtable getPanels() {return panels;}
Hashtable getTreeCommands() {
return treeCommands;
}
boolean hasVisiblePanels() {
return visiblePanels.size()>0;
}
int getVisiblePanelsCount() { return visiblePanels.size(); }
void registerPanel(TreePanel panel) {
String key = pStr2Key(panel.getRootPath().toString());
panels.put(key,panel);
setPanelShowingProperty(panel.getRootPath().toString());
propertiesChanged=true;
}
void loadProperties() {
if (IJ.debugMode) IJ.log("CP.loadProperties");
visiblePanels.removeAllElements();
expandedNodes.removeAllElements();
panels.clear();
Properties properties = Prefs.getControlPanelProperties();
for (Enumeration e=properties.keys(); e.hasMoreElements();) {
String key = (String)e.nextElement();
if (key.startsWith(".Control_Panel.")) {
key = key.substring(1, key.length());
String val = Prefs.get(key, null);
if (IJ.debugMode) IJ.log(" "+key+": "+val);
if (Character.isDigit(val.charAt(0))) visiblePanels.addElement(key);
else if (val.equals("expand"))
expandedNodes.addElement(key);
}
}
}
void saveProperties() {
if (IJ.debugMode) IJ.log("CP.saveProperties: "+propertiesChanged);
if (propertiesChanged) {
clearProperties();
for (Enumeration e=visiblePanels.elements(); e.hasMoreElements();) {
String s = (String)e.nextElement();
TreePanel p = (TreePanel)panels.get(s);
if (p!=null) recordGeometry(p);
}
for(Enumeration e=expandedNodes.elements(); e.hasMoreElements();)
Prefs.set((String)e.nextElement(),"expand");
}
propertiesChanged=false;
}
void clearProperties() {
Properties properties = Prefs.getControlPanelProperties();
for (Enumeration e=properties.keys(); e.hasMoreElements();) {
String key = (String)e.nextElement();
if (key.startsWith(".Control_Panel."))
properties.remove(key);
}
}
void setExpandedStateProperty(String item) {
String s = pStr2Key(item);
expandedNodes.addElement(s);
propertiesChanged=true;
}
boolean hasExpandedStateProperty(String item) {
String s = pStr2Key(item);
return expandedNodes.contains(s);
}
void unsetExpandedStateProperty(String item) {
String s = pStr2Key(item);
expandedNodes.remove(s);
propertiesChanged=true;
}
void setPanelShowingProperty(String item) {
String s = pStr2Key(item);
if (!(visiblePanels.contains(s)))
visiblePanels.addElement(s);
propertiesChanged=true;
}
void unsetPanelShowingProperty(String item) {
String s = pStr2Key(item);
visiblePanels.remove(s);
}
boolean hasPanelShowingProperty(String item) {
String s = pStr2Key(item);
return visiblePanels.contains(s);
}
void restoreVisiblePanels() {
String[] visPanls = new String[visiblePanels.size()];
visiblePanels.toArray(visPanls);
Arrays.sort(visPanls);
for (int i=0; i<visPanls.length; i++) {
if (!panels.containsKey(visPanls[i])) {
TreePanel p = newPanel(visPanls[i]);
}
}
}
void recordGeometry(TreePanel panel) {
String pTitle = panel.getRootPath().toString();
pTitle = pStr2Key(pTitle);
JFrame frame = panel.getFrame();
if (frame!=null) {
Rectangle rect=frame.getBounds();
String xCoord = (Integer.valueOf(rect.x)).toString();
String yCoord = (Integer.valueOf(rect.y)).toString();
String width = (Integer.valueOf(rect.width)).toString();
String height = (Integer.valueOf(rect.height)).toString();
if (pTitle.equals("Control_Panel")) pTitle = "Control_Panel.@Main";
String geometry = xCoord+" "+yCoord+" "+width+" "+height;
if (IJ.debugMode) IJ.log("CP.recordGeometry: "+pTitle+" "+geometry);
Prefs.set(pTitle, geometry);
}
}
void restoreGeometry(TreePanel panel) {
String pTitle = panel.getRootPath().toString();
pTitle = pStr2Key(pTitle);
if (pTitle.equals("Control_Panel")) pTitle = "Control_Panel.@Main";
if (IJ.debugMode) IJ.log("CP.restoreGeometry: "+pTitle);
String geom = Prefs.get(pTitle, null);
if (geom!=null) {
int[] coords = s2ints(geom);
if (coords!=null && coords.length==4)
panel.setBounds(coords[0],coords[1],coords[2],coords[3]);
else {
Point pnt = panel.getDefaultLocation();
if (pnt!=null)
panel.getFrame().setLocation((int)pnt.getX(),(int)pnt.getY());
}
}
}
void closeAll(boolean die) {
quitting = die;
if (!visiblePanels.isEmpty()) {
propertiesChanged = true;
saveProperties();
}
for (Enumeration e = panels.elements(); e.hasMoreElements();) {
TreePanel p = (TreePanel)e.nextElement();
p.close();
}
quitting = true;
}
void showHelp() {
IJ.showMessage("About Control Panel...",
"This plugin displays a panel with ImageJ commands in a hierarchical tree structure.\n"+" \n"+
"Usage:\n"+" \n"+
" Click on a leaf node to launch the corresponding ImageJ command (or plugin)\n"+
" (double-click on X Window Systems)\n"+" \n"+
" Double-click on a tree branch node (folder) to expand or collapse it\n"+" \n"+
" Click and drag on a tree branch node (folder) to display its descendants,\n"+
" in a separate (child) panel (\"tear-off\" mock-up)\n"+" \n"+
" In a child panel, use the \"Show Parent\" menu item to re-open the parent panel\n"+
" if it was accidentally closed\n"+" \n"+
"Author: Cezar M. Tigaret (c.tigaret@ucl.ac.uk)\n"+
"This code is in the public domain."
);
}
String pStr2Key(String pathString) {
String keyword = pathString;
if(keyword.startsWith("["))
keyword = keyword.substring(keyword.indexOf("[")+1,keyword.length());
if(keyword.endsWith("]"))
keyword = keyword.substring(0,keyword.lastIndexOf("]"));
StringTokenizer st = new StringTokenizer(keyword,",");
String result = "";
while(st.hasMoreTokens()) {
String token = st.nextToken();
if(token.startsWith(" ")) token = token.substring(1,token.length()); result+=token+".";
}
result = result.substring(0,result.length()-1); result = result.replace(' ','_');
return result;
}
String key2pStr(String keyword) {
StringTokenizer st = new StringTokenizer(keyword,".");
String result = "";
while(st.hasMoreTokens()) {
String token = st.nextToken();
result += token +", ";
}
result = result.substring(0,result.length()-2); result = "["+result+"]";
result = result.replace('_', ' ');
return result;
}
public int[] s2ints(String s) {
StringTokenizer st = new StringTokenizer(s, ", \t");
int nInts = st.countTokens();
if (nInts==0) return null;
int[] ints = new int[nInts];
for(int i=0; i<nInts; i++) {
try {ints[i] = Integer.parseInt(st.nextToken());}
catch (NumberFormatException e) {return null;}
}
return ints;
}
}
class TreePanel implements
ActionListener, WindowListener, TreeExpansionListener, TreeWillExpandListener {
ControlPanel pcp;
boolean isMainPanel;
String title;
boolean isDragging=false;
Point defaultLocation;
private JTree pTree;
private JMenuBar pMenuBar;
private DefaultMutableTreeNode root;
private DefaultMutableTreeNode draggingNode=null;
private DefaultTreeModel pTreeModel;
private ActionListener listener;
private JFrame pFrame;
private JCheckBoxMenuItem pMenu_saveOnClose, pMenu_noClutter;
private TreePath rootPath;
private static final int _uparrow1_data[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0d,0x0e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0d,0x01,0x01,0x0d,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x0e,0x02,0x01,0x03,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x0e,0x04,0x05,0x06,0x01,0x07,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x08,0x04,0x09,0x0e,0x02,0x06,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x08,0x04,0x09,0x0e,0x0e,0x0e,
0x02,0x06,0x01,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x04,0x09,0x0e,0x0e,
0x0e,0x0e,0x0e,0x02,0x06,0x02,0x00,0x00,0x00,0x08,0x0a,0x0e,0x08,0x0a,
0x0b,0x0b,0x0c,0x0c,0x0c,0x0c,0x0c,0x06,0x02,0x00,0x0e,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x0e,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00
};
private static final int _uparrow1_ctable[] = {
0x21,0xff000000,0xff303030,0xffaaaaaa,0xffffffff,0xff3c3c3c,0xff252525,0xffb6b6b6,0xff585858,0xffc3c3c3,0xff222222,0xff2b2b2b,0xff2e2e2e,0xffa0a0a0,
0xff808080
};
private static IndexColorModel iconCM = new IndexColorModel(8,_uparrow1_ctable.length,_uparrow1_ctable,0,true,255,DataBuffer.TYPE_BYTE);
private static final ImageIcon upIcon = new ImageIcon( Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16,16,iconCM,_uparrow1_data,0,16)));
public TreePanel (DefaultMutableTreeNode root, ControlPanel pcp, boolean isMainPanel) {
new TreePanel(root,pcp,isMainPanel,null);
}
public TreePanel(DefaultMutableTreeNode root, ControlPanel pcp, boolean isMainPanel, Point location) {
this.root=root;
this.pcp=pcp;
this.isMainPanel = isMainPanel;
defaultLocation = location;
rootPath=new TreePath(root.getPath());
title = (String)root.getUserObject();
buildTreePanel();
pcp.registerPanel(this);
}
public void buildTreePanel() {
pFrame=new JFrame(title);
pFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
pTreeModel = new DefaultTreeModel(root);
pTree=new JTree(pTreeModel);
pTree.setEditable(false);
pTree.putClientProperty("JTree.lineStyle","Angled");
pTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
pTree.setRootVisible(false);
pTree.setShowsRootHandles(true);
JScrollPane ptView=new JScrollPane(pTree);
addMenu();
pFrame.getContentPane().add(ptView, BorderLayout.CENTER);
addListeners();
pFrame.pack();
if (defaultLocation!=null) {
if (IJ.debugMode) IJ.log("CP.buildTreePanel: "+defaultLocation);
pFrame.setLocation(defaultLocation.x, defaultLocation.y);
} else
pcp.restoreGeometry(this);
if (pFrame.getLocation().x==0)
GUI.center(pFrame);
setVisible();
ImageJ ij = IJ.getInstance();
ij.addWindowListener(this);
pFrame.addKeyListener(ij);
pTree.addKeyListener(ij);
}
void addMenu() {
pMenuBar=new JMenuBar();
Insets ins = new Insets(0,0,0,10);
pMenuBar.setMargin(ins);
if (isMainPanel) {
JMenuItem helpMI = new JMenuItem("Help");
helpMI.addActionListener(this);
helpMI.setActionCommand("Help");
pMenuBar.add(helpMI);
}
else {
JMenuItem spMI = new JMenuItem("Show Parent",upIcon);
spMI.addActionListener(this);
spMI.setActionCommand("Show Parent");
pMenuBar.add(spMI);
}
pFrame.setJMenuBar(pMenuBar);
}
void addListeners() {
addActionListener(this);
pFrame.addWindowListener(this);
pFrame.addComponentListener(new ComponentAdapter() {
public void componentMoved(ComponentEvent e) {
Rectangle r = e.getComponent().getBounds();
if (IJ.debugMode) IJ.log("CP.componentMoved: "+r);
if (r.x>0) {
defaultLocation = new Point(r.x, r.y);
recordGeometry();
}
}
});
pTree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
isDragging = false;
if (e.getClickCount()!=2)
return;
int selRow=pTree.getRowForLocation(e.getX(),e.getY());
if (selRow!=-1) toAction();
}
public void mouseReleased(MouseEvent e) {
if (isDragging) {
Point pnt = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(pnt,pTree);
tearOff(null,pnt);
}
isDragging = false;
}
});
pTree.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
int selRow = pTree.getRowForLocation(e.getX(), e.getY());
if(selRow!=-1)
{
if(((DefaultMutableTreeNode)pTree.getLastSelectedPathComponent()).isLeaf()) return;
pFrame.setCursor(new Cursor(Cursor.MOVE_CURSOR));
isDragging = true;
}
}
});
pTree.addTreeExpansionListener(this);
pTree.addTreeWillExpandListener(this);
}
public String getTitle() {return title;}
public TreePath getRootPath() {return rootPath;}
public boolean isTheMainPanel() {return isMainPanel;}
public JFrame getFrame() {return pFrame;}
public JTree getTree() {return pTree;}
public DefaultMutableTreeNode getRootNode() {return root;}
public Point getDefaultLocation() {return defaultLocation;}
boolean isVisible() {
return pFrame.isVisible();
}
void setBounds(int x, int y, int w, int h) {
pFrame.setBounds(new Rectangle(x,y,w,h));
defaultLocation = new Point(x,y);
}
void setAutoSaveProps(boolean autoSave) {
if(isTheMainPanel()) pMenu_saveOnClose.setSelected(autoSave);
}
boolean getAutoSaveProps() {return pMenu_saveOnClose.isSelected();}
void restoreExpandedNodes() {
if (pTree==null || root==null)
return;
pTree.removeTreeExpansionListener(this);
TreeNode[] rootPath = root.getPath();
for(Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements();)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
if(!node.isLeaf() && node != root)
{
TreeNode[] nodePath = node.getPath();
TreePath nTreePath = new TreePath(nodePath);
String npS = nTreePath.toString();
DefaultMutableTreeNode[] localPath = new DefaultMutableTreeNode[nodePath.length-rootPath.length+1];
for(int i=0; i<localPath.length; i++)
{
localPath[i]=(DefaultMutableTreeNode)nodePath[i+rootPath.length-1];
}
TreePath newPath = new TreePath(localPath);
if(pcp.hasExpandedStateProperty(npS) && !pcp.hasPanelShowingProperty(npS))
{
if(newPath!=null)
{
try
{
pTree.expandPath(newPath);
}catch(Throwable t){}
}
}
else if((pcp.hasExpandedStateProperty(npS) || pTree.isExpanded(newPath)) && pcp.hasPanelShowingProperty(npS))
{
pTree.collapsePath(newPath);
pcp.unsetExpandedStateProperty(npS);
}
}
}
pTree.addTreeExpansionListener(this);
}
public void processEvent(ActionEvent e) {
if (listener != null) listener.actionPerformed(e);
}
public void addActionListener(ActionListener al) {
listener=AWTEventMulticaster.add(listener, al);
}
public void removeActionListener(ActionListener al) {
listener=AWTEventMulticaster.remove(listener, al);
}
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if(cmd==null) return;
if (cmd.equals("Help")) {
showHelp();
return;
}
if(cmd.equals("Show Parent")) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)root.getParent();
if (parent!=null) {
TreePanel panel = pcp.getPanelForNode(parent);
if(panel==null) panel = pcp.newPanel(parent);
if(panel!=null) panel.setVisible();
}
return;
}
if(cmd.equals("Reload Plugins From Panel")) { pcp.closeAll(false);
IJ.doCommand("Reload Plugins");
}
else {
if(cmd.equals("Reload Plugins")) pcp.closeAll(false);
else
IJ.doCommand(cmd);
return;
}
}
public void windowClosing(WindowEvent e) {
if (IJ.debugMode) IJ.log("CP.windowClosing: "+isMainPanel);
if (isMainPanel)
pcp.saveProperties();
pcp.unsetPanelShowingProperty(getRootPath().toString());
}
public void windowActivated(WindowEvent e) {
WindowManager.setWindow(getFrame());
}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void treeCollapsed (TreeExpansionEvent ev) {
String evPathString = ev.getPath().toString();
evPathString = evPathString.substring(evPathString.indexOf("[")+1,evPathString.lastIndexOf("]"));
evPathString = evPathString.substring(getTitle().length()+2,evPathString.length());
String rootPath = getRootPath().toString();
rootPath = rootPath.substring(rootPath.indexOf("[")+1,rootPath.lastIndexOf("]"));
String path = "["+rootPath +", "+evPathString+"]";
pcp.unsetExpandedStateProperty(path);
}
public void treeExpanded(TreeExpansionEvent ev) {
TreePath evPath = ev.getPath();
String evPathString = ev.getPath().toString();
evPathString = pcp.pStr2Key(evPathString);
evPathString = evPathString.substring(getTitle().length()+1,evPathString.length());
String rootPath = getRootPath().toString();
rootPath = pcp.pStr2Key(rootPath);
String path = rootPath+"."+evPathString;
if (pcp.hasPanelShowingProperty(path)) {
Hashtable panels = pcp.getPanels();
TreePanel p = (TreePanel)panels.get(path);
if(p!=null) p.close();
}
pcp.setExpandedStateProperty(path);
}
public void treeWillExpand(TreeExpansionEvent ev) {}
public void treeWillCollapse(TreeExpansionEvent ev) {}
void recordGeometry() {pcp.recordGeometry(this);}
void refreshTree() {
pTreeModel.reload();
}
void tearOff() {
tearOff(null);
}
void tearOff(DefaultMutableTreeNode node) {
tearOff(node, null);
}
void tearOff(DefaultMutableTreeNode node, Point pnt) {
isDragging = false;
pFrame.setCursor(Cursor.getDefaultCursor());
if(node==null)
node = (DefaultMutableTreeNode)pTree.getLastSelectedPathComponent();
if(node.isLeaf()) return;
TreeNode[] nPath = node.getPath();
TreeNode[] rPath = root.getPath();
DefaultMutableTreeNode[] tPath = new DefaultMutableTreeNode[nPath.length-rPath.length+1];
for(int i=0; i<tPath.length; i++)
tPath[i] = (DefaultMutableTreeNode)nPath[i+rPath.length-1];
TreePath path = new TreePath(nPath);
TreePath localPath = new TreePath(tPath);
String pathString = localPath.toString();
TreePanel p = pcp.getPanelForNode(node);
if (p==null) {
if(pnt!=null)
p = pcp.newPanel(node, pnt);
else
p = pcp.newPanel(node);
pTree.collapsePath(localPath);
} else {
if (pnt!=null)
p.setLocation(pnt);
p.setVisible();
pTree.collapsePath(localPath);
}
}
void toAction() {
DefaultMutableTreeNode nde=(DefaultMutableTreeNode)pTree.getLastSelectedPathComponent();
if (nde.getChildCount()>0) return;
String aCmd=nde.toString();
String cmd= aCmd;
if(pcp.treeCommands.containsKey(aCmd))
cmd = (String)pcp.treeCommands.get(aCmd);
processEvent(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,cmd));
}
void setVisible() {
if (pFrame!=null && !pFrame.isVisible()) {
restoreExpandedNodes();
if (defaultLocation!=null) pFrame.setLocation(defaultLocation);
pFrame.setVisible(true);
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)root.getParent();
if (parent!=null) {
TreePanel pnl = pcp.getPanelForNode(parent);
if (pnl!=null && pnl.isVisible()) {
TreeNode[] rPath = root.getPath();
TreeNode[] pPath = pnl.getRootNode().getPath();
DefaultMutableTreeNode[] tPath = new DefaultMutableTreeNode[rPath.length-pPath.length+1];
for(int i=0; i<tPath.length; i++)
tPath[i] = (DefaultMutableTreeNode)rPath[i+pPath.length-1];
TreePath localPath = new TreePath(tPath);
pnl.getTree().collapsePath(localPath);
}
}
}
if (pcp!=null) pcp.setPanelShowingProperty(getRootPath().toString());
}
void setLocation(Point p) {
if (p!=null) defaultLocation = p;
}
void close() {
pFrame.dispatchEvent(new WindowEvent(pFrame,WindowEvent.WINDOW_CLOSING));
pcp.unsetPanelShowingProperty(getRootPath().toString());
}
private void showHelp() {pcp.showHelp();}
}