Search in sources :

Example 6 with Library

use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.

the class Loader method loadJarFile.

Library loadJarFile(File request, String className) throws LoadFailedException {
    File actual = getSubstitution(request);
    // Up until 2.1.8, this was written to use a URLClassLoader, which
    // worked pretty well, except that the class never releases its file
    // handles. For this reason, with 2.2.0, it's been switched to use
    // a custom-written class ZipClassLoader instead. The ZipClassLoader
    // is based on something downloaded off a forum, and I'm not as sure
    // that it works as well. It certainly does more file accesses.
    // Anyway, here's the line for this new version:
    ZipClassLoader loader = new ZipClassLoader(actual);
    // And here's the code that was present up until 2.1.8, and which I
    // know to work well except for the closing-files bit. If necessary, we
    // can revert by deleting the above declaration and reinstating the
    // below.
    /*
		 * URL url; try { url = new URL("file", "localhost",
		 * file.getCanonicalPath()); } catch (MalformedURLException e1) { throw
		 * new LoadFailedException("Internal error: Malformed URL"); } catch
		 * (IOException e1) { throw new
		 * LoadFailedException(Strings.get("jarNotOpenedError")); }
		 * URLClassLoader loader = new URLClassLoader(new URL[] { url });
		 */
    // load library class from loader
    Class<?> retClass;
    try {
        retClass = loader.loadClass(className);
    } catch (ClassNotFoundException e) {
        throw new LoadFailedException(StringUtil.format(Strings.get("jarClassNotFoundError"), className));
    }
    if (!(Library.class.isAssignableFrom(retClass))) {
        throw new LoadFailedException(StringUtil.format(Strings.get("jarClassNotLibraryError"), className));
    }
    // instantiate library
    Library ret;
    try {
        ret = (Library) retClass.newInstance();
    } catch (Exception e) {
        throw new LoadFailedException(StringUtil.format(Strings.get("jarLibraryNotCreatedError"), className));
    }
    return ret;
}
Also used : ZipClassLoader(com.cburch.logisim.util.ZipClassLoader) Library(com.cburch.logisim.tools.Library) File(java.io.File) IOException(java.io.IOException)

Example 7 with Library

use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.

the class ProjectActions method checkValidFilename.

/**
 * Returns true if the filename contains valid characters only, that is,
 * alphanumeric characters and underscores.
 */
private static boolean checkValidFilename(String filename, Project proj, HashMap<String, String> Errors) {
    boolean IsOk = true;
    HashMap<String, Library> TempSet = new HashMap<String, Library>();
    HashSet<String> ForbiddenNames = new HashSet<String>();
    LibraryTools.BuildLibraryList(proj.getLogisimFile(), TempSet);
    LibraryTools.BuildToolList(proj.getLogisimFile(), ForbiddenNames);
    ForbiddenNames.addAll(TempSet.keySet());
    Pattern p = Pattern.compile("[^a-z0-9_.]", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(filename);
    if (m.find()) {
        IsOk = false;
        Errors.put(FILE_NAME_FORMAT_ERROR, "InvalidFileFormatError");
    }
    if (ForbiddenNames.contains(filename.toUpperCase())) {
        IsOk = false;
        Errors.put(FILE_NAME_KEYWORD_ERROR, "UsedLibraryToolnameError");
    }
    return IsOk;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) LoadedLibrary(com.cburch.logisim.file.LoadedLibrary) Library(com.cburch.logisim.tools.Library) HashSet(java.util.HashSet)

Example 8 with Library

use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.

the class TtyInterface method displayStatistics.

private static void displayStatistics(LogisimFile file) {
    FileStatistics stats = FileStatistics.compute(file, file.getMainCircuit());
    FileStatistics.Count total = stats.getTotalWithSubcircuits();
    int maxName = 0;
    for (FileStatistics.Count count : stats.getCounts()) {
        int nameLength = count.getFactory().getDisplayName().length();
        if (nameLength > maxName)
            maxName = nameLength;
    }
    String fmt = "%" + countDigits(total.getUniqueCount()) + "d\t" + "%" + countDigits(total.getRecursiveCount()) + "d\t";
    String fmtNormal = fmt + "%-" + maxName + "s\t%s\n";
    for (FileStatistics.Count count : stats.getCounts()) {
        Library lib = count.getLibrary();
        String libName = lib == null ? "-" : lib.getDisplayName();
        // OK
        System.out.printf(// OK
        fmtNormal, Integer.valueOf(count.getUniqueCount()), Integer.valueOf(count.getRecursiveCount()), count.getFactory().getDisplayName(), libName);
    }
    FileStatistics.Count totalWithout = stats.getTotalWithoutSubcircuits();
    System.out.printf(// OK
    fmt + "%s\n", Integer.valueOf(totalWithout.getUniqueCount()), Integer.valueOf(totalWithout.getRecursiveCount()), Strings.get("statsTotalWithout"));
    System.out.printf(// OK
    fmt + "%s\n", Integer.valueOf(total.getUniqueCount()), Integer.valueOf(total.getRecursiveCount()), Strings.get("statsTotalWith"));
}
Also used : FileStatistics(com.cburch.logisim.file.FileStatistics) Library(com.cburch.logisim.tools.Library)

Example 9 with Library

use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.

the class ToolboxManip method doubleClicked.

public void doubleClicked(ProjectExplorerEvent event) {
    Object clicked = event.getTarget();
    if (clicked instanceof ProjectExplorerToolNode) {
        Tool baseTool = ((ProjectExplorerToolNode) clicked).getValue();
        if (baseTool instanceof AddTool) {
            AddTool tool = (AddTool) baseTool;
            ComponentFactory source = tool.getFactory();
            if (source instanceof SubcircuitFactory) {
                SubcircuitFactory circFact = (SubcircuitFactory) source;
                proj.setCurrentCircuit(circFact.getSubcircuit());
                proj.getFrame().setEditorView(Frame.EDIT_LAYOUT);
                if (lastSelected != null) {
                    proj.setTool(lastSelected);
                } else {
                    Library base = proj.getLogisimFile().getLibrary("Base");
                    if (base != null)
                        proj.setTool(base.getTool("Edit Tool"));
                }
            }
        }
    }
}
Also used : ProjectExplorerToolNode(com.cburch.logisim.gui.generic.ProjectExplorerToolNode) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) SubcircuitFactory(com.cburch.logisim.circuit.SubcircuitFactory) AddTool(com.cburch.logisim.tools.AddTool) Library(com.cburch.logisim.tools.Library) Tool(com.cburch.logisim.tools.Tool) AddTool(com.cburch.logisim.tools.AddTool)

Example 10 with Library

use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.

the class ProjectLibraryActions method doLoadJarLibrary.

public static void doLoadJarLibrary(Project proj) {
    Loader loader = proj.getLogisimFile().getLoader();
    JFileChooser chooser = loader.createChooser();
    chooser.setDialogTitle(Strings.get("loadJarDialogTitle"));
    chooser.setFileFilter(Loader.JAR_FILTER);
    int check = chooser.showOpenDialog(proj.getFrame());
    if (check == JFileChooser.APPROVE_OPTION) {
        File f = chooser.getSelectedFile();
        String className = null;
        // try to retrieve the class name from the "Library-Class"
        // attribute in the manifest. This section of code was contributed
        // by Christophe Jacquet (Request Tracker #2024431).
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(f);
            Manifest manifest = jarFile.getManifest();
            className = manifest.getMainAttributes().getValue("Library-Class");
        } catch (IOException e) {
        // if opening the JAR file failed, do nothing
        } finally {
            if (jarFile != null) {
                try {
                    jarFile.close();
                } catch (IOException e) {
                }
            }
        }
        // if the class name was not found, go back to the good old dialog
        if (className == null) {
            className = JOptionPane.showInputDialog(proj.getFrame(), Strings.get("jarClassNamePrompt"), Strings.get("jarClassNameTitle"), JOptionPane.QUESTION_MESSAGE);
            // if user canceled selection, abort
            if (className == null)
                return;
        }
        Library lib = loader.loadJarLibrary(f, className);
        if (lib != null) {
            proj.doAction(LogisimFileActions.loadLibrary(lib, proj.getLogisimFile()));
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) Loader(com.cburch.logisim.file.Loader) IOException(java.io.IOException) Library(com.cburch.logisim.tools.Library) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) LogisimFile(com.cburch.logisim.file.LogisimFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

Library (com.cburch.logisim.tools.Library)26 ComponentFactory (com.cburch.logisim.comp.ComponentFactory)8 Tool (com.cburch.logisim.tools.Tool)7 LogisimFile (com.cburch.logisim.file.LogisimFile)6 AddTool (com.cburch.logisim.tools.AddTool)6 File (java.io.File)5 ArrayList (java.util.ArrayList)4 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)3 Loader (com.cburch.logisim.file.Loader)3 ProjectExplorerToolNode (com.cburch.logisim.gui.generic.ProjectExplorerToolNode)3 IOException (java.io.IOException)3 JScrollPane (javax.swing.JScrollPane)3 Element (org.w3c.dom.Element)3 Circuit (com.cburch.logisim.circuit.Circuit)2 AttributeSet (com.cburch.logisim.data.AttributeSet)2 Location (com.cburch.logisim.data.Location)2 LoadedLibrary (com.cburch.logisim.file.LoadedLibrary)2 ProjectExplorerLibraryNode (com.cburch.logisim.gui.generic.ProjectExplorerLibraryNode)2 Base (com.cburch.logisim.std.base.Base)2 HashSet (java.util.HashSet)2