mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Merge pull request #70 from joelmoniz/savePrompt
Added AutoSave feature as an enhancement
This commit is contained in:
@@ -22,7 +22,10 @@ import java.awt.BorderLayout;
|
||||
import java.awt.CardLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Container;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Font;
|
||||
import java.awt.Frame;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
@@ -42,8 +45,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JCheckBoxMenuItem;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JOptionPane;
|
||||
@@ -51,6 +60,7 @@ import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.SwingWorker;
|
||||
import javax.swing.border.Border;
|
||||
import javax.swing.border.EtchedBorder;
|
||||
import javax.swing.table.TableModel;
|
||||
import javax.swing.text.Document;
|
||||
@@ -61,6 +71,7 @@ import processing.app.Base;
|
||||
import processing.app.EditorState;
|
||||
import processing.app.EditorToolbar;
|
||||
import processing.app.Mode;
|
||||
import processing.app.Preferences;
|
||||
import processing.app.Sketch;
|
||||
import processing.app.SketchCode;
|
||||
import processing.app.Toolkit;
|
||||
@@ -1074,6 +1085,119 @@ public class DebugEditor extends JavaEditor implements ActionListener {
|
||||
return ta;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Grab current contents of the sketch window, advance the console, stop any
|
||||
* other running sketches, auto-save the user's code... not in that order.
|
||||
*/
|
||||
@Override
|
||||
public void prepareRun() {
|
||||
autoSave();
|
||||
super.prepareRun();
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a JDialog prompting the user to save when the user hits
|
||||
* run/present/etc.
|
||||
*/
|
||||
protected void autoSave() {
|
||||
if (!ExperimentalMode.autoSaveEnabled)
|
||||
return;
|
||||
|
||||
try {
|
||||
// if (sketch.isUntitled() &&
|
||||
// ExperimentalMode.untitledAutoSaveEnabled) {
|
||||
// if (handleSave(true))
|
||||
// statusTimedNotice("Saved. Running...", 5);
|
||||
// else
|
||||
// statusTimedNotice("Save Canceled. Running anyway...", 5);
|
||||
// }
|
||||
// else
|
||||
if (sketch.isModified() && !sketch.isUntitled()) {
|
||||
if (ExperimentalMode.autoSavePromptEnabled) {
|
||||
final JDialog autoSaveDialog = new JDialog(
|
||||
base.getActiveEditor(), this.getSketch().getName(),
|
||||
true);
|
||||
Container container = autoSaveDialog.getContentPane();
|
||||
|
||||
JPanel panelMain = new JPanel();
|
||||
panelMain.setBorder(BorderFactory.createEmptyBorder(4, 0,
|
||||
2, 2));
|
||||
panelMain.setLayout(new BoxLayout(panelMain,
|
||||
BoxLayout.PAGE_AXIS));
|
||||
|
||||
JPanel panelLabel = new JPanel(new FlowLayout(
|
||||
FlowLayout.LEFT));
|
||||
JLabel label = new JLabel(
|
||||
"<html><body> There are unsaved"
|
||||
+ " changes in your sketch.<br />"
|
||||
+ " Do you want to save it before"
|
||||
+ " running? </body></html>");
|
||||
label.setFont(new Font(label.getFont().getName(),
|
||||
Font.PLAIN, label.getFont().getSize() + 1));
|
||||
panelLabel.add(label);
|
||||
panelMain.add(panelLabel);
|
||||
final JCheckBox dontRedisplay = new JCheckBox(
|
||||
"Remember this decision");
|
||||
|
||||
JPanel panelButtons = new JPanel(new FlowLayout(
|
||||
FlowLayout.CENTER, 8, 2));
|
||||
JButton btnRunSave = new JButton("Save and Run");
|
||||
btnRunSave.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
handleSave(true);
|
||||
if (dontRedisplay.isSelected()) {
|
||||
ExperimentalMode.autoSavePromptEnabled = !dontRedisplay
|
||||
.isSelected();
|
||||
ExperimentalMode.defaultAutoSaveEnabled = true;
|
||||
dmode.savePreferences();
|
||||
}
|
||||
autoSaveDialog.dispose();
|
||||
}
|
||||
});
|
||||
panelButtons.add(btnRunSave);
|
||||
JButton btnRunNoSave = new JButton("Run, Don't Save");
|
||||
btnRunNoSave.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (dontRedisplay.isSelected()) {
|
||||
ExperimentalMode.autoSavePromptEnabled = !dontRedisplay
|
||||
.isSelected();
|
||||
ExperimentalMode.defaultAutoSaveEnabled = false;
|
||||
dmode.savePreferences();
|
||||
}
|
||||
autoSaveDialog.dispose();
|
||||
}
|
||||
});
|
||||
panelButtons.add(btnRunNoSave);
|
||||
panelMain.add(panelButtons);
|
||||
|
||||
JPanel panelCheck = new JPanel();
|
||||
panelCheck
|
||||
.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
|
||||
panelCheck.add(dontRedisplay);
|
||||
panelMain.add(panelCheck);
|
||||
|
||||
container.add(panelMain);
|
||||
|
||||
autoSaveDialog.setResizable(false);
|
||||
autoSaveDialog.pack();
|
||||
autoSaveDialog
|
||||
.setLocationRelativeTo(base.getActiveEditor());
|
||||
autoSaveDialog.setVisible(true);
|
||||
|
||||
} else if (ExperimentalMode.defaultAutoSaveEnabled)
|
||||
handleSave(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
statusError(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Access variable inspector window.
|
||||
*
|
||||
|
||||
@@ -127,13 +127,17 @@ public class ExperimentalMode extends JavaMode {
|
||||
}
|
||||
|
||||
volatile public static boolean errorCheckEnabled = true, warningsEnabled = true,
|
||||
codeCompletionsEnabled = true, debugOutputEnabled = false, errorLogsEnabled = false;
|
||||
codeCompletionsEnabled = true, debugOutputEnabled = false, errorLogsEnabled = false,
|
||||
autoSaveEnabled = true, autoSavePromptEnabled = true,
|
||||
defaultAutoSaveEnabled = true; // ,untitledAutoSaveEnabled;
|
||||
public static int autoSaveInterval = 3; //in minutes
|
||||
|
||||
public static final String prefErrorCheck = "pdex.errorCheckEnabled",
|
||||
prefWarnings = "pdex.warningsEnabled",
|
||||
prefCodeCompletionEnabled = "pdex.ccEnabled",
|
||||
prefDebugOP = "pdex.dbgOutput", prefErrorLogs = "pdex.writeErrorLogs", prefAutoSaveInterval = "pdex.autoSaveInterval";
|
||||
prefDebugOP = "pdex.dbgOutput", prefErrorLogs = "pdex.writeErrorLogs", prefAutoSaveInterval = "pdex.autoSaveInterval",
|
||||
prefAutoSave = "pdex.autoSave.autoSaveEnabled", // prefUntitledAutoSave = "pdex.autoSave.untitledAutoSaveEnabled",
|
||||
prefAutoSavePrompt = "pdex.autoSave.promptDisplay", prefDefaultAutoSave = "pdex.autoSave.autoSaveByDefault";
|
||||
|
||||
public void loadPreferences(){
|
||||
log("Load PDEX prefs");
|
||||
@@ -144,6 +148,10 @@ public class ExperimentalMode extends JavaMode {
|
||||
DEBUG = Preferences.getBoolean(prefDebugOP);
|
||||
errorLogsEnabled = Preferences.getBoolean(prefErrorLogs);
|
||||
autoSaveInterval = Preferences.getInteger(prefAutoSaveInterval);
|
||||
// untitledAutoSaveEnabled = Preferences.getBoolean(prefUntitledAutoSave);
|
||||
autoSaveEnabled = Preferences.getBoolean(prefAutoSave);
|
||||
autoSavePromptEnabled = Preferences.getBoolean(prefAutoSavePrompt);
|
||||
defaultAutoSaveEnabled = Preferences.getBoolean(prefDefaultAutoSave);
|
||||
}
|
||||
|
||||
public void savePreferences(){
|
||||
@@ -154,6 +162,10 @@ public class ExperimentalMode extends JavaMode {
|
||||
Preferences.setBoolean(prefDebugOP, DEBUG);
|
||||
Preferences.setBoolean(prefErrorLogs,errorLogsEnabled);
|
||||
Preferences.setInteger(prefAutoSaveInterval,autoSaveInterval);
|
||||
// Preferences.setBoolean(prefUntitledAutoSave,untitledAutoSaveEnabled);
|
||||
Preferences.setBoolean(prefAutoSave,autoSaveEnabled);
|
||||
Preferences.setBoolean(prefAutoSavePrompt, autoSavePromptEnabled);
|
||||
Preferences.setBoolean(prefDefaultAutoSave, defaultAutoSaveEnabled);
|
||||
}
|
||||
|
||||
public void ensurePrefsExist(){
|
||||
@@ -169,6 +181,14 @@ public class ExperimentalMode extends JavaMode {
|
||||
Preferences.setBoolean(prefErrorLogs,errorLogsEnabled);
|
||||
if(Preferences.get(prefAutoSaveInterval) == null)
|
||||
Preferences.setInteger(prefAutoSaveInterval,autoSaveInterval);
|
||||
// if(Preferences.get(prefUntitledAutoSave) == null)
|
||||
// Preferences.setBoolean(prefUntitledAutoSave,untitledAutoSaveEnabled);
|
||||
if(Preferences.get(prefAutoSave) == null)
|
||||
Preferences.setBoolean(prefAutoSave,autoSaveEnabled);
|
||||
if(Preferences.get(prefAutoSavePrompt) == null)
|
||||
Preferences.setBoolean(prefAutoSavePrompt,autoSavePromptEnabled);
|
||||
if(Preferences.get(prefDefaultAutoSave) == null)
|
||||
Preferences.setBoolean(prefDefaultAutoSave,defaultAutoSaveEnabled);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user