Search in sources :

Example 16 with Library

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

the class FileStatistics method sortCounts.

private static List<Count> sortCounts(Map<ComponentFactory, Count> counts, LogisimFile file) {
    List<Count> ret = new ArrayList<Count>();
    for (AddTool tool : file.getTools()) {
        ComponentFactory factory = tool.getFactory();
        Count count = counts.get(factory);
        if (count != null) {
            count.library = file;
            ret.add(count);
        }
    }
    for (Library lib : file.getLibraries()) {
        for (Tool tool : lib.getTools()) {
            if (tool instanceof AddTool) {
                ComponentFactory factory = ((AddTool) tool).getFactory();
                Count count = counts.get(factory);
                if (count != null) {
                    count.library = lib;
                    ret.add(count);
                }
            }
        }
    }
    return ret;
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory) ArrayList(java.util.ArrayList) 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 17 with Library

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

the class LibraryManager method loadLibrary.

public Library loadLibrary(Loader loader, String desc) {
    // It may already be loaded.
    // Otherwise we'll have to decode it.
    int sep = desc.indexOf(desc_sep);
    if (sep < 0) {
        loader.showError(StringUtil.format(Strings.get("fileDescriptorError"), desc));
        return null;
    }
    String type = desc.substring(0, sep);
    String name = desc.substring(sep + 1);
    if (type.equals("")) {
        Library ret = loader.getBuiltin().getLibrary(name);
        if (ret == null) {
            loader.showError(StringUtil.format(Strings.get("fileBuiltinMissingError"), name));
            return null;
        }
        return ret;
    } else if (type.equals("file")) {
        File toRead = loader.getFileFor(name, Loader.LOGISIM_FILTER);
        return loadLogisimLibrary(loader, toRead);
    } else if (type.equals("jar")) {
        int sepLoc = name.lastIndexOf(desc_sep);
        String fileName = name.substring(0, sepLoc);
        String className = name.substring(sepLoc + 1);
        File toRead = loader.getFileFor(fileName, Loader.JAR_FILTER);
        return loadJarLibrary(loader, toRead, className);
    } else {
        loader.showError(StringUtil.format(Strings.get("fileTypeError"), type, desc));
        return null;
    }
}
Also used : Library(com.cburch.logisim.tools.Library) File(java.io.File)

Example 18 with Library

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

the class LoadedLibrary method setBase.

void setBase(Library value) {
    if (base instanceof LibraryEventSource) {
        ((LibraryEventSource) base).removeLibraryListener(myListener);
    }
    Library old = base;
    base = value;
    resolveChanges(old);
    if (base instanceof LibraryEventSource) {
        ((LibraryEventSource) base).addLibraryListener(myListener);
    }
}
Also used : Library(com.cburch.logisim.tools.Library)

Example 19 with Library

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

the class LoadedLibrary method resolveChanges.

private void resolveChanges(Library old) {
    if (listeners.isEmpty())
        return;
    if (!base.getDisplayName().equals(old.getDisplayName())) {
        fireLibraryEvent(LibraryEvent.SET_NAME, base.getDisplayName());
    }
    HashSet<Library> changes = new HashSet<Library>(old.getLibraries());
    changes.removeAll(base.getLibraries());
    for (Library lib : changes) {
        fireLibraryEvent(LibraryEvent.REMOVE_LIBRARY, lib);
    }
    changes.clear();
    changes.addAll(base.getLibraries());
    changes.removeAll(old.getLibraries());
    for (Library lib : changes) {
        fireLibraryEvent(LibraryEvent.ADD_LIBRARY, lib);
    }
    HashMap<ComponentFactory, ComponentFactory> componentMap;
    HashMap<Tool, Tool> toolMap;
    componentMap = new HashMap<ComponentFactory, ComponentFactory>();
    toolMap = new HashMap<Tool, Tool>();
    for (Tool oldTool : old.getTools()) {
        Tool newTool = base.getTool(oldTool.getName());
        toolMap.put(oldTool, newTool);
        if (oldTool instanceof AddTool) {
            ComponentFactory oldFactory = ((AddTool) oldTool).getFactory();
            if (newTool != null && newTool instanceof AddTool) {
                ComponentFactory newFactory = ((AddTool) newTool).getFactory();
                componentMap.put(oldFactory, newFactory);
            } else {
                componentMap.put(oldFactory, null);
            }
        }
    }
    replaceAll(componentMap, toolMap);
    HashSet<Tool> toolChanges = new HashSet<Tool>(old.getTools());
    toolChanges.removeAll(toolMap.keySet());
    for (Tool tool : toolChanges) {
        fireLibraryEvent(LibraryEvent.REMOVE_TOOL, tool);
    }
    toolChanges = new HashSet<Tool>(base.getTools());
    toolChanges.removeAll(toolMap.values());
    for (Tool tool : toolChanges) {
        fireLibraryEvent(LibraryEvent.ADD_TOOL, tool);
    }
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Library(com.cburch.logisim.tools.Library) AddTool(com.cburch.logisim.tools.AddTool) HashSet(java.util.HashSet) Tool(com.cburch.logisim.tools.Tool) AddTool(com.cburch.logisim.tools.AddTool)

Example 20 with Library

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

the class Loader method save.

public boolean save(LogisimFile file, File dest) {
    Library reference = LibraryManager.instance.findReference(file, dest);
    if (reference != null) {
        JOptionPane.showMessageDialog(parent, StringUtil.format(Strings.get("fileCircularError"), reference.getDisplayName()), Strings.get("fileSaveErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return false;
    }
    File backup = determineBackupName(dest);
    boolean backupCreated = backup != null && dest.renameTo(backup);
    FileOutputStream fwrite = null;
    try {
        try {
            MacCompatibility.setFileCreatorAndType(dest, "LGSM", "circ");
        } catch (IOException e) {
        }
        fwrite = new FileOutputStream(dest);
        file.write(fwrite, this, dest);
        file.setName(toProjectName(dest));
        File oldFile = getMainFile();
        setMainFile(dest);
        LibraryManager.instance.fileSaved(this, dest, oldFile, file);
    } catch (IOException e) {
        if (backupCreated)
            recoverBackup(backup, dest);
        if (dest.exists() && dest.length() == 0)
            dest.delete();
        JOptionPane.showMessageDialog(parent, StringUtil.format(Strings.get("fileSaveError"), e.toString()), Strings.get("fileSaveErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return false;
    } finally {
        if (fwrite != null) {
            try {
                fwrite.close();
            } catch (IOException e) {
                if (backupCreated)
                    recoverBackup(backup, dest);
                if (dest.exists() && dest.length() == 0)
                    dest.delete();
                JOptionPane.showMessageDialog(parent, StringUtil.format(Strings.get("fileSaveCloseError"), e.toString()), Strings.get("fileSaveErrorTitle"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
    }
    if (!dest.exists() || dest.length() == 0) {
        if (backupCreated && backup != null && backup.exists()) {
            recoverBackup(backup, dest);
        } else {
            dest.delete();
        }
        JOptionPane.showMessageDialog(parent, Strings.get("fileSaveZeroError"), Strings.get("fileSaveErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return false;
    }
    if (backupCreated && backup.exists()) {
        backup.delete();
    }
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) Library(com.cburch.logisim.tools.Library) IOException(java.io.IOException) 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