Search in sources :

Example 1 with Loader

use of com.cburch.logisim.file.Loader in project logisim-evolution by reds-heig.

the class ProjectActions method doMerge.

public static void doMerge(Component parent, Project baseProject) {
    JFileChooser chooser;
    LogisimFile mergelib;
    Loader loader = null;
    if (baseProject != null) {
        Loader oldLoader = baseProject.getLogisimFile().getLoader();
        chooser = oldLoader.createChooser();
        if (oldLoader.getMainFile() != null) {
            chooser.setSelectedFile(oldLoader.getMainFile());
        }
    } else {
        chooser = JFileChoosers.create();
    }
    chooser.setFileFilter(Loader.LOGISIM_FILTER);
    chooser.setDialogTitle(Strings.get("FileMergeItem"));
    int returnVal = chooser.showOpenDialog(parent);
    if (returnVal != JFileChooser.APPROVE_OPTION)
        return;
    File selected = chooser.getSelectedFile();
    loader = new Loader(baseProject == null ? parent : baseProject.getFrame());
    try {
        mergelib = loader.openLogisimFile(selected);
        if (mergelib == null)
            return;
    } catch (LoadFailedException ex) {
        if (!ex.isShown()) {
            JOptionPane.showMessageDialog(parent, StringUtil.format(Strings.get("fileMergeError"), ex.toString()), Strings.get("FileMergeErrorItem"), JOptionPane.ERROR_MESSAGE);
        }
        return;
    }
    updatecircs(mergelib, baseProject);
    baseProject.doAction(LogisimFileActions.MergeFile(mergelib, baseProject.getLogisimFile()));
}
Also used : LogisimFile(com.cburch.logisim.file.LogisimFile) JFileChooser(javax.swing.JFileChooser) Loader(com.cburch.logisim.file.Loader) LogisimFile(com.cburch.logisim.file.LogisimFile) File(java.io.File) LoadFailedException(com.cburch.logisim.file.LoadFailedException)

Example 2 with Loader

use of com.cburch.logisim.file.Loader in project logisim-evolution by reds-heig.

the class ProjectActions method doNew.

public static Project doNew(SplashScreen monitor, boolean isStartupScreen) {
    if (monitor != null)
        monitor.setProgress(SplashScreen.FILE_CREATE);
    Loader loader = new Loader(monitor);
    InputStream templReader = AppPreferences.getTemplate().createStream();
    LogisimFile file = null;
    try {
        file = loader.openLogisimFile(templReader);
    } catch (IOException ex) {
        displayException(monitor, ex);
    } catch (LoadFailedException ex) {
        displayException(monitor, ex);
    } finally {
        try {
            templReader.close();
        } catch (IOException e) {
        }
    }
    if (file == null)
        file = createEmptyFile(loader, null);
    return completeProject(monitor, loader, file, isStartupScreen);
}
Also used : LogisimFile(com.cburch.logisim.file.LogisimFile) InputStream(java.io.InputStream) Loader(com.cburch.logisim.file.Loader) IOException(java.io.IOException) LoadFailedException(com.cburch.logisim.file.LoadFailedException)

Example 3 with Loader

use of com.cburch.logisim.file.Loader in project logisim-evolution by reds-heig.

the class ProjectActions method doSaveAs.

/**
 * Saves a Logisim project in a .circ file.
 *
 * It is the action listener for the File->Save as... menu option.
 *
 * @param proj
 *            project to be saved
 * @return true if success, false otherwise
 */
public static boolean doSaveAs(Project proj) {
    Loader loader = proj.getLogisimFile().getLoader();
    JFileChooser chooser = loader.createChooser();
    chooser.setFileFilter(Loader.LOGISIM_FILTER);
    if (loader.getMainFile() != null) {
        chooser.setSelectedFile(loader.getMainFile());
    }
    int returnVal;
    boolean validFilename = false;
    HashMap<String, String> Error = new HashMap<String, String>();
    do {
        Error.clear();
        returnVal = chooser.showSaveDialog(proj.getFrame());
        if (returnVal != JFileChooser.APPROVE_OPTION) {
            return false;
        }
        validFilename = checkValidFilename(chooser.getSelectedFile().getName(), proj, Error);
        if (!validFilename) {
            String Message = "\"" + chooser.getSelectedFile() + "\":\n";
            for (String key : Error.keySet()) Message = Message.concat("=> " + Strings.get(Error.get(key)) + "\n");
            JOptionPane.showMessageDialog(chooser, Message, Strings.get("FileSaveAsItem"), JOptionPane.ERROR_MESSAGE);
        }
    } while (!validFilename);
    File f = chooser.getSelectedFile();
    String circExt = Loader.LOGISIM_EXTENSION;
    if (!f.getName().endsWith(circExt)) {
        String old = f.getName();
        int ext0 = old.lastIndexOf('.');
        if (ext0 < 0 || !Pattern.matches("\\.\\p{L}{2,}[0-9]?", old.substring(ext0))) {
            f = new File(f.getParentFile(), old + circExt);
        } else {
            String ext = old.substring(ext0);
            String ttl = Strings.get("replaceExtensionTitle");
            String msg = Strings.get("replaceExtensionMessage", ext);
            Object[] options = { Strings.get("replaceExtensionReplaceOpt", ext), Strings.get("replaceExtensionAddOpt", circExt), Strings.get("replaceExtensionKeepOpt") };
            JOptionPane dlog = new JOptionPane(msg);
            dlog.setMessageType(JOptionPane.QUESTION_MESSAGE);
            dlog.setOptions(options);
            dlog.createDialog(proj.getFrame(), ttl).setVisible(true);
            Object result = dlog.getValue();
            if (result == options[0]) {
                String name = old.substring(0, ext0) + circExt;
                f = new File(f.getParentFile(), name);
            } else if (result == options[1]) {
                f = new File(f.getParentFile(), old + circExt);
            }
        }
    }
    if (f.exists()) {
        int confirm = JOptionPane.showConfirmDialog(proj.getFrame(), Strings.get("confirmOverwriteMessage"), Strings.get("confirmOverwriteTitle"), JOptionPane.YES_NO_OPTION);
        if (confirm != JOptionPane.YES_OPTION)
            return false;
    }
    return doSave(proj, f);
}
Also used : JFileChooser(javax.swing.JFileChooser) HashMap(java.util.HashMap) Loader(com.cburch.logisim.file.Loader) LogisimFile(com.cburch.logisim.file.LogisimFile) File(java.io.File) JOptionPane(javax.swing.JOptionPane)

Example 4 with Loader

use of com.cburch.logisim.file.Loader in project logisim-evolution by reds-heig.

the class ProjectActions method doOpenNoWindow.

public static Project doOpenNoWindow(SplashScreen monitor, File source) throws LoadFailedException {
    Loader loader = new Loader(monitor);
    LogisimFile file = loader.openLogisimFile(source);
    Project ret = new Project(file);
    updatecircs(file, ret);
    return ret;
}
Also used : LogisimFile(com.cburch.logisim.file.LogisimFile) Loader(com.cburch.logisim.file.Loader)

Example 5 with Loader

use of com.cburch.logisim.file.Loader in project logisim-evolution by reds-heig.

the class ProjectActions method doOpen.

public static boolean doOpen(Component parent, Project baseProject) {
    JFileChooser chooser;
    if (baseProject != null) {
        Loader oldLoader = baseProject.getLogisimFile().getLoader();
        chooser = oldLoader.createChooser();
        if (oldLoader.getMainFile() != null) {
            chooser.setSelectedFile(oldLoader.getMainFile());
        }
    } else {
        chooser = JFileChoosers.create();
    }
    chooser.setFileFilter(Loader.LOGISIM_FILTER);
    chooser.setDialogTitle(Strings.get("FileOpenItem"));
    int returnVal = chooser.showOpenDialog(parent);
    if (returnVal != JFileChooser.APPROVE_OPTION)
        return false;
    File selected = chooser.getSelectedFile();
    if (selected != null) {
        doOpen(parent, baseProject, selected);
    }
    return true;
}
Also used : JFileChooser(javax.swing.JFileChooser) Loader(com.cburch.logisim.file.Loader) LogisimFile(com.cburch.logisim.file.LogisimFile) File(java.io.File)

Aggregations

Loader (com.cburch.logisim.file.Loader)16 LogisimFile (com.cburch.logisim.file.LogisimFile)12 File (java.io.File)10 LoadFailedException (com.cburch.logisim.file.LoadFailedException)6 JFileChooser (javax.swing.JFileChooser)6 IOException (java.io.IOException)5 Library (com.cburch.logisim.tools.Library)3 Circuit (com.cburch.logisim.circuit.Circuit)2 Project (com.cburch.logisim.proj.Project)2 InputStream (java.io.InputStream)2 JarFile (java.util.jar.JarFile)2 CircuitState (com.cburch.logisim.circuit.CircuitState)1 LoadedLibrary (com.cburch.logisim.file.LoadedLibrary)1 Frame (com.cburch.logisim.gui.main.Frame)1 Print (com.cburch.logisim.gui.main.Print)1 LogisimMenuBar (com.cburch.logisim.gui.menu.LogisimMenuBar)1 Instance (com.cburch.logisim.instance.Instance)1 Tool (com.cburch.logisim.tools.Tool)1 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1