Fix "switch mode" to edit current code in new mode when compatible.

This commit is contained in:
Jonathan Feinberg
2014-04-16 09:17:49 -04:00
parent 20ce7605e3
commit f42d4e6742
3 changed files with 43 additions and 17 deletions

View File

@@ -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();

View File

@@ -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);

View File

@@ -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