/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package eric.JSprogram; import eric.FileTools; import eric.GUI.palette.PaletteManager; import eric.GUI.themes; import eric.GUI.window.myJMenuItem; import eric.controls.JCanvasPanel; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import javax.swing.JMenu; import eric.JEricPanel; import java.awt.event.MouseAdapter; import javax.swing.JPopupMenu; import javax.swing.JSeparator; import rene.gui.Global; import rene.util.xml.XmlTree; import rene.zirkel.ZirkelCanvas; import rene.zirkel.objects.ConstructionObject; /** * * @author erichake, modified by PM Mazat */ public class ScriptPanel extends JEricPanel { private ArrayList BACKUPS=new ArrayList(); private ZirkelCanvas ZC; private ScriptItemsArray items=new ScriptItemsArray(); private ArrayList itemsbackup; private static Image icon=themes.getImage("scripts.png"); private int W=32, H=32, X=10, Y=10; private ScriptsManager ScriptsManagerPanel=null; private static int ScriptsManagerPanel_X=3, ScriptsManagerPanel_Y=45, ScriptsManagerPanel_WIDTH=3*12+180+90; public ScriptPanel(ZirkelCanvas zc) { ZC=zc; setBounds(X, Y, W, H); MouseListener ml = new MouseAdapter(){ @Override public void mousePressed(MouseEvent e){ showPopup(); } }; this.addMouseListener(ml); } @Override public void paintComponent(Graphics g) { g.drawImage(icon, 0, 0, W, H, this); } public void Backup() { try { BACKUPS.add(FileTools.getCurrentFileSource()); fixConsoleBackBtn(); } catch (Exception ex) { } } public boolean isBackup() { return (BACKUPS.size()>0); } public void Restore() { ZC.killAllScripts(); try { Thread.sleep(500); } catch (Exception ex) { } backupScriptItems(); if (BACKUPS.size()>0) { ZC.getConstruction().clear(); try { FileTools.setCurrentFileSource(BACKUPS.get(BACKUPS.size()-1)); } catch (final Exception e) { } BACKUPS.remove(BACKUPS.size()-1); } restoreScriptItems(); fixConsoleBackBtn(); /* * Si on a ajouté un script après avoir lancé d'un script de bibliothèque * le panneau n'est pas affiché après l'annulation */ if(items.size()!=0) ZC.add(this); } public void backupScriptItems() { itemsbackup=new ArrayList(); for (ScriptItem item : items) { item.fixmouseDragTargetsNames(); item.fixmouseUpTargetsNames(); itemsbackup.add(item); } } public void restoreScriptItems() { items.clear(); for (ScriptItem item : itemsbackup) { item.reloadMouseDragTargets(); item.reloadMouseUpTargets(); items.add(item); } } public void fixConsoleBackBtn() { for (ScriptItem item : items) { JSEditor jsc=item.getEditor(); if (jsc!=null) { jsc.setBackBtnEnabled(BACKUPS.size()>0); } } } public void showPopup() { JPopupMenu popup = new JPopupMenu(); // add Cancel Item : myJMenuItem item=new myJMenuItem(Global.Loc("JSmenu.cancel"), themes.resizeExistingIcon("/eric/GUI/icons/jswindow/restore.png", 16, 16)) { @Override public void action() { onlyRemoveScriptsManagerPanel(); Restore(); } }; item.setEnabled(isBackup()); popup.add(item); popup.add(new JSeparator()); // add New Script Item : item=new myJMenuItem(Global.Loc("JSmenu.add")) { @Override public void action() { newScript(); } }; popup.add(item); popup.add(new JSeparator()); // add Run script Items : for (int i=0; i0) { removeScript(items.get(0)); } } public void removeScript(ScriptItem si) { ZC.getConstruction().haveChanged(); items.remove(si); if (items.isEmpty()) { ZC.remove(this); removeScriptsManagerPanel(); ZC.repaint(); } if (ScriptsManagerPanel!=null) { addScriptsManagerPanel(); } } public void addScript(XmlTree tree) { items.add(new ScriptItem(this, tree)); // at first added script, button must be drawn in the canvas : if (items.size()==1) { ZC.add(this); } } private ScriptItem findScript(String nme) { for (int i=0; i