/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package eric.macros; import eric.GUI.pipe_tools; import eric.GUI.themes; import eric.GUI.windowComponent; import eric.JZirkelCanvas; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Vector; import rene.gui.Global; import rene.util.xml.XmlReader; import rene.util.xml.XmlTag; import rene.util.xml.XmlTagPI; import rene.util.xml.XmlTree; import rene.zirkel.ZirkelCanvas; import rene.zirkel.ZirkelFrame; import rene.zirkel.construction.ConstructionException; import rene.zirkel.construction.Count; import rene.zirkel.macro.Macro; import rene.zirkel.macro.MacroItem; import rene.zirkel.macro.MacroRunner; import rene.zirkel.objects.ConstructionObject; import rene.zirkel.objects.TextObject; /** * * @author erichake */ public class MacroTools { static public final int LIBRARY_MACRO=0; static public final int FILE_MACRO=2; static private Vector librarymacros=new Vector(); static public Vector builtinmacros=new Vector(); static public String MacrosLibraryFileName=""; static public String MacrosBackupLibraryFileName=""; public static void createLocalDirectory() { // Setting (if necessary) home directory name and home library macros // file name : final String mypath=Global.AppPath(); // Place the help files in the local directory : if ((Global.isNewVersion())||(!(new File(Global.getHomeDirectory()+"docs").exists()))) { try { Global.copyFiles(new File(mypath+"docs"), new File( Global.getHomeDirectory()+"docs")); } catch (final IOException ex) { System.out.println("bug : createLocalDirectory()"); } } // Place the javascript files in the local directory : if ((Global.isNewVersion())||(!(new File(Global.getHomeDirectory()+"scripts").exists()))) { try { Global.copyFiles(new File(mypath+"scripts"), new File( Global.getHomeDirectory()+"scripts")); } catch (final IOException ex) { System.out.println("bug : createLocalDirectory()"); } } String Filename="library.mcr"; if (new File(mypath+Global.name("language", "")+"library.mcr").exists()) { Filename=Global.name("language", "")+"library.mcr"; } else if (new File(Global.getHomeDirectory()+Global.name("language", "")+"library.mcr").exists()) { Filename=Global.name("language", "")+"library.mcr"; } MacrosLibraryFileName=Global.getHomeDirectory()+Filename; // is there a library in home folder ? if (new File(MacrosLibraryFileName).exists()) { // Is it a new version at this startup ? if (Global.isNewVersion()) { MacrosBackupLibraryFileName=Global.getHomeDirectory()+"library_backup.mcr"; Global.copyFile(MacrosLibraryFileName, MacrosBackupLibraryFileName); Global.copyFile(mypath+Filename, MacrosLibraryFileName); } } else { new File(Global.getHomeDirectory()).mkdirs(); Global.copyFile(mypath+Filename, MacrosLibraryFileName); } Global.makeWindowConfigFolderInvisible(); } public static Vector getBuiltinMacros() { return builtinmacros; } public static Vector getLibraryMacros() { return librarymacros; } public static void clearLibraryMacros() { librarymacros.removeAllElements(); } public static void addToLibraryMacros(MacroItem mi) { librarymacros.add(mi); } public static void updateLibraryMacros() { ZirkelCanvas zc=JZirkelCanvas.getCurrentZC(); if ((!(zc==null))) { librarymacros.clear(); final Vector V=zc.getMacros(); for (int i=0; i mc; Macro m; TextObject t; ZirkelFrame ZF=JZirkelCanvas.getCurrentZF(); if (ZF==null) { return; } mc=builtinmacros; for (int i=0; i=Macros.size()) { m.setProtected(true); final MacroItem mi=new MacroItem(m, null); Macros.addElement(mi); } } catch (final ConstructionException ex) { Count.setAllAlternate(false); throw ex; } Count.setAllAlternate(false); } else { throw new ConstructionException("Construction not found"); } } } catch (final Exception e) { throw e; } } public static void LoadDefaultMacrosAtStartup() { // Loading builtin macros (for some icons in palette, like symetry) try { final InputStream o=MacroTools.class.getResourceAsStream("/builtin.mcr"); LoadMacros(o, builtinmacros); o.close(); } catch (final Exception e) { System.out.println("builtinmacros bug"); } if ((!themes.isApplet())&&(new File(MacrosLibraryFileName).exists())) { try { final InputStream o=new FileInputStream(MacrosLibraryFileName); LoadMacros(o, librarymacros); o.close(); if (!MacrosBackupLibraryFileName.equals("")) { final InputStream o2=new FileInputStream( MacrosBackupLibraryFileName); LoadMacros(o2, librarymacros); o2.close(); final File f=new File(MacrosBackupLibraryFileName); f.delete(); } return; } catch (final Exception e) { System.out.println("librarymacros bug"); } } try { final InputStream o=MacroTools.class.getResourceAsStream("/default.mcr"); LoadMacros(o, librarymacros); o.close(); return; } catch (final Exception e) { System.out.println("default macros bug"); } } public static void saveLibraryToDisk() { if (JZirkelCanvas.getCurrentZF()!=null) { ZirkelFrame ZF=new ZirkelFrame(pipe_tools.isApplet()); ZF.dosave(MacrosLibraryFileName, false, true, true, false, librarymacros); } } /* If user changes macro type in the tree (e.g. "add to library") from one figure, * library macros vector is changed, so we must transmit all changes to * others figures : */ public static void populateMacrosTypeChanges() { int max=JZirkelCanvas.getZCsSize(); for (int i=0; i