From f42d4e67429ce4a12e813b437e771282bcdf458e Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Wed, 16 Apr 2014 09:17:49 -0400 Subject: [PATCH] Fix "switch mode" to edit current code in new mode when compatible. --- app/src/processing/app/Base.java | 52 ++++++++++++++++++++++-------- app/src/processing/app/Editor.java | 5 +-- app/src/processing/app/Sketch.java | 3 +- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 0a5f5a321..b19e6f340 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -607,13 +607,30 @@ public class Base { Base.showWarning("Save", "Please save the sketch before changing the mode.", null); + return; + } + nextMode = mode; + + // If the current editor contains file extensions that the new mode can handle, then + // write a sketch.properties file with the new mode specified, and reopen. + boolean newModeCanHandleCurrentSource = true; + for (final SketchCode code: sketch.getCode()) { + if (!mode.validExtension(code.getExtension())) { + newModeCanHandleCurrentSource = false; + break; + } + } + if (newModeCanHandleCurrentSource) { + final File props = new File(sketch.getCodeFolder(), "sketch.properties"); + saveModeSettings(props, nextMode); + handleClose(activeEditor, LastEditorClosePolicy.DO_NOT_QUIT); + handleOpen(sketch.getMainFilePath()); } else { // If you're changing modes, and there's nothing in the current sketch, you probably // don't intend to keep the old, wrong-mode editor around. if (sketch.isUntitled()) { - handleClose(activeEditor, true); + handleClose(activeEditor, LastEditorClosePolicy.DO_NOT_QUIT); } - nextMode = mode; handleNew(); } } @@ -729,15 +746,7 @@ public class Base { } // Create sketch properties. - final File sketchProps = new File(newbieDir, "sketch.properties"); - try { - final Settings settings = new Settings(sketchProps); - settings.set("mode", nextMode.getTitle()); - settings.set("mode.id", nextMode.getIdentifier()); - settings.save(); - } catch (IOException e) { - System.err.println("While creating " + sketchProps + ": " + e.getMessage()); - } + saveModeSettings(new File(newbieDir, "sketch.properties"), nextMode); String path = newbieFile.getAbsolutePath(); /*Editor editor =*/ handleOpen(path, true); @@ -749,6 +758,18 @@ public class Base { } } + // Create or modify a sketch.proprties file to specify the given Mode. + private void saveModeSettings(final File sketchProps, final Mode mode) { + try { + final Settings settings = new Settings(sketchProps); + settings.set("mode", mode.getTitle()); + settings.set("mode.id", mode.getIdentifier()); + settings.save(); + } catch (IOException e) { + System.err.println("While creating " + sketchProps + ": " + e.getMessage()); + } + } + // /** // * Replace the sketch in the current window with a new untitled document. @@ -1053,13 +1074,16 @@ public class Base { return null; } - + enum LastEditorClosePolicy { + QUIT, DO_NOT_QUIT + } + /** * Close a sketch as specified by its editor window. * @param editor Editor object of the sketch to be closed. * @return true if succeeded in closing, false if canceled. */ - public boolean handleClose(Editor editor, boolean modeSwitch) { + public boolean handleClose(Editor editor, LastEditorClosePolicy closePolicy) { // Check if modified // boolean immediate = editors.size() == 1; if (!editor.checkModified()) { @@ -1116,7 +1140,7 @@ public class Base { Preferences.save(); if (defaultFileMenu == null) { - if (modeSwitch) { + if (closePolicy == LastEditorClosePolicy.DO_NOT_QUIT) { // need to close this editor, ever so temporarily editor.setVisible(false); editor.dispose(); diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 5c6067b3f..b3124af3c 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -22,6 +22,7 @@ package processing.app; +import processing.app.Base.LastEditorClosePolicy; import processing.app.contrib.ToolContribution; import processing.app.syntax.*; import processing.app.tools.*; @@ -126,7 +127,7 @@ public abstract class Editor extends JFrame implements RunnerListener { // add listener to handle window close box hit event addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { - base.handleClose(Editor.this, false); + base.handleClose(Editor.this, LastEditorClosePolicy.QUIT); } }); // don't close the window when clicked, the app will take care @@ -601,7 +602,7 @@ public abstract class Editor extends JFrame implements RunnerListener { item = Toolkit.newJMenuItem("Close", 'W'); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - base.handleClose(Editor.this, false); + base.handleClose(Editor.this, LastEditorClosePolicy.QUIT); } }); fileMenu.add(item); diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 20e96b56e..1f33fe8ee 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -23,6 +23,7 @@ package processing.app; +import processing.app.Base.LastEditorClosePolicy; import processing.core.*; import java.awt.*; @@ -569,7 +570,7 @@ public class Sketch { // make a new sketch, and i think this will rebuild the sketch menu //editor.handleNewUnchecked(); //editor.handleClose2(); - editor.base.handleClose(editor, false); + editor.base.handleClose(editor, LastEditorClosePolicy.QUIT); } else { // delete the file