/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package eric.macros; import eric.FileTools; import eric.GUI.palette.PaletteManager; import eric.JZirkelCanvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Vector; import javax.swing.JComponent; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JSeparator; import javax.swing.plaf.SeparatorUI; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import rene.gui.Global; import rene.zirkel.ZirkelCanvas; import rene.zirkel.macro.Macro; import rene.zirkel.macro.MacroItem; /** * * @author erichake */ public class NodePopupMenu extends JPopupMenu { private final CTree macrostree; private JDefaultMutableTreeNode SelectedNode; private TreePath[] SelectedPath; private JMenuItem runitem, renitem, delitem, tolibitem, tofileitem, saveitem, updtitem, propitem, dupitem; public NodePopupMenu(CTree mytree) { macrostree=mytree; dupitem=new JMenuItem(Global.Loc("macros.popup.duplicate")); dupitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { duplicatenodes(); } }); propitem=new JMenuItem(Global.Loc("macros.popup.properties")); propitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { showproperties(); } }); runitem=new JMenuItem(Global.Loc("macros.popup.run")); runitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { // runmacro(); } }); renitem=new JMenuItem(Global.Loc("macros.popup.rename")); renitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { renamenode(); } }); delitem=new JMenuItem(Global.Loc("macros.popup.delete")); delitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { deletenodes(); } }); tolibitem=new JMenuItem(Global.Loc("macros.popup.addtolibrary")); tolibitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { changeMacroType(0); } }); tofileitem=new JMenuItem(Global.Loc("macros.popup.removefromlibrary")); tofileitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { changeMacroType(2); } }); saveitem=new JMenuItem(Global.Loc("macros.popup.saveas")); saveitem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent event) { saveMacros(); } }); } public void handleMouseClick(final MouseEvent e) { final TreePath path=macrostree.getPathForLocation(e.getX(), e.getY()); if (path!=null) { SelectedNode=(JDefaultMutableTreeNode) path.getLastPathComponent(); SelectedPath=macrostree.getSelectionPaths(); if ((SelectedPath.length==1)&&(SelectedNode.isLeaf())) { if (JMacrosInspector.isDialogVisible()) { JMacrosInspector.changemacro(SelectedNode); } else { PaletteManager.deselectgeomgroup(); SelectedNode.runZmacro(); } } } } void actualiseproperties() { // throw new UnsupportedOperationException("Not yet implemented"); } private void showproperties() { JMacrosInspector.openInspector(SelectedNode); // new JMacrosInspector(SelectedNode); } public void handlePopup(final MouseEvent e) { if (e.isPopupTrigger()) { final TreePath path=macrostree.getPathForLocation(e.getX(), e.getY()); if (path!=null) { macrostree.addSelectionPath(path); SelectedNode=(JDefaultMutableTreeNode) path.getLastPathComponent(); SelectedPath=macrostree.getSelectionPaths(); // initSelectedPath(); this.removeAll(); if (SelectedPath.length>1) { this.add(delitem); if (!JZirkelCanvas.isRestrictedSession()) { this.add(createSeparator()); this.add(tolibitem); this.add(tofileitem); this.add(createSeparator()); this.add(saveitem); } } else { if (SelectedNode.isLeaf()) { this.add(renitem); if (!JZirkelCanvas.isRestrictedSession()) { this.add(delitem); this.add(dupitem); } this.add(createSeparator()); if (!JZirkelCanvas.isRestrictedSession()) { this.add(tolibitem); this.add(tofileitem); this.add(createSeparator()); this.add(saveitem); this.add(createSeparator()); } this.add(runitem); this.add(createSeparator()); this.add(propitem); } else { this.add(renitem); this.add(delitem); this.add(createSeparator()); if (!JZirkelCanvas.isRestrictedSession()) { this.add(tolibitem); this.add(tofileitem); this.add(createSeparator()); this.add(saveitem); } } } this.show(e.getComponent(), e.getX(), e.getY()); } } } public void addfolder() { JDefaultMutableTreeNode root; final JDefaultMutableTreeNode node=new JDefaultMutableTreeNode(Global.Loc("macros.untitledfolder")); node.add(new JDefaultMutableTreeNode(Global.Loc("macros.emptynode"))); final TreePath[] paths=macrostree.getSelectionPaths(); if ((paths)!=null) { root=(JDefaultMutableTreeNode) paths[0].getLastPathComponent(); if (root.isLeaf()) { // if the first selected node is a leaf : final DefaultMutableTreeNode father=(DefaultMutableTreeNode) root.getParent(); final int i=father.getIndex(root)+1; ((DefaultTreeModel) macrostree.getModel()).insertNodeInto(node, father, i); } else { // if the first selected node is a folder : ((DefaultTreeModel) macrostree.getModel()).insertNodeInto(node, root, root.getChildCount()); } } else { // There is no selected node : ((DefaultTreeModel) macrostree.getModel()).insertNodeInto(node, macrostree.JML.getTopNode(), macrostree.JML.getTopNode().getChildCount()); } // Transformation d'un noeud en TreePath : final TreePath tp=new TreePath(node.getPath()); macrostree.setEditable(true); macrostree.startEditingAtPath(tp); } private void updateMacrosVector(JDefaultMutableTreeNode node) { if (node.m==null) { return; } String name=node.m.Name; if (node.m.isProtected()) { int ZCsSize=JZirkelCanvas.getZCsSize(); // delete the macro from all the ZirkelCanvas (in different tabs) : for (int size=0; size