From 9674b8b8aae0ce3725d2c0919af16a808d35b397 Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 7 May 2005 17:11:04 +0000 Subject: [PATCH] fixing bugs with multi-tab undo, also adding loading.gif to the export --- app/Editor.java | 71 +++++++++++++++++++++++++++------------ app/Sketch.java | 24 +++++++++---- app/SketchCode.java | 12 ++++++- app/tools/AutoFormat.java | 8 ++--- todo.txt | 2 +- 5 files changed, 82 insertions(+), 35 deletions(-) diff --git a/app/Editor.java b/app/Editor.java index e6f6ae11d..8f8c46f90 100644 --- a/app/Editor.java +++ b/app/Editor.java @@ -112,7 +112,8 @@ public class Editor extends JFrame JMenuItem undoItem, redoItem; protected UndoAction undoAction; protected RedoAction redoAction; - static public UndoManager undo = new UndoManager(); // editor needs this guy + UndoManager undo; + //static public UndoManager undo = new UndoManager(); // editor needs this guy // @@ -232,7 +233,16 @@ public class Editor extends JFrame // set the undo stuff for this feller Document document = textarea.getDocument(); - document.addUndoableEditListener(new PdeUndoableEditListener()); + //document.addUndoableEditListener(new PdeUndoableEditListener()); + document.addUndoableEditListener(new UndoableEditListener() { + public void undoableEditHappened(UndoableEditEvent e) { + if (undo != null) { + undo.addEdit(e.getEdit()); + undoAction.updateUndoState(); + redoAction.updateRedoState(); + } + } + }); } @@ -783,17 +793,6 @@ public class Editor extends JFrame // ................................................................... - // This one listens for edits that can be undone. - protected class PdeUndoableEditListener implements UndoableEditListener { - public void undoableEditHappened(UndoableEditEvent e) { - // Remember the edit and update the menus. - undo.addEdit(e.getEdit()); - undoAction.updateUndoState(); - redoAction.updateRedoState(); - } - } - - class UndoAction extends AbstractAction { public UndoAction() { super("Undo"); @@ -815,10 +814,12 @@ public class Editor extends JFrame if (undo.canUndo()) { this.setEnabled(true); undoItem.setEnabled(true); + undoItem.setText(undo.getUndoPresentationName()); putValue(Action.NAME, undo.getUndoPresentationName()); } else { this.setEnabled(false); undoItem.setEnabled(false); + undoItem.setText("Undo"); putValue(Action.NAME, "Undo"); } } @@ -846,10 +847,12 @@ public class Editor extends JFrame if (undo.canRedo()) { this.setEnabled(true); redoItem.setEnabled(true); + redoItem.setText(undo.getRedoPresentationName()); putValue(Action.NAME, undo.getRedoPresentationName()); } else { this.setEnabled(false); redoItem.setEnabled(false); + redoItem.setText("Redo"); putValue(Action.NAME, "Redo"); } } @@ -923,20 +926,46 @@ public class Editor extends JFrame /** - * Called by EditorHeader when the tab is changed - * (or a new set of files are opened). - * @param discardUndo true if undo info to this point should be ignored + * Called to update the text but not switch to a different + * set of code (which would affect the undo manager). */ - public void setText(String what, boolean discardUndo) { + //public void setText(String what) { //, boolean discardUndo) { + //setText(what, 0, 0); + //} + + + /** + * Called to update the text but not switch to a different + * set of code (which would affect the undo manager). + */ + public void setText(String what, int selectionStart, int selectionEnd) { textarea.setText(what); - - if (discardUndo) undo.discardAllEdits(); - - textarea.select(0, 0); // move to the beginning of the document + textarea.select(selectionStart, selectionEnd); textarea.requestFocus(); // get the caret blinking } + /** + * Called by Sketch when the tab is changed or a new set of files are opened. + */ + public void setText(String currentProgram, + int selectionStart, int selectionEnd, + UndoManager currentUndo) { + this.undo = null; + + //if (discardUndo) undo.discardAllEdits(); + + // don't set the undo object yet otherwise gets hokey + textarea.setText(currentProgram); + textarea.select(selectionStart, selectionEnd); + textarea.requestFocus(); // get the caret blinking + + this.undo = currentUndo; + undoAction.updateUndoState(); + redoAction.updateRedoState(); + } + + public void handleRun(boolean present) { doClose(); running = true; diff --git a/app/Sketch.java b/app/Sketch.java index ea02e5dcc..aa6ccbac4 100644 --- a/app/Sketch.java +++ b/app/Sketch.java @@ -132,15 +132,15 @@ public class Sketch { /** * Build the list of files. - * + *

* Generally this is only done once, rather than * each time a change is made, because otherwise it gets to be * a nightmare to keep track of what files went where, because * not all the data will be saved to disk. - * + *

* This also gets called when the main sketch file is renamed, * because the sketch has to be reloaded from a different folder. - * + *

* Another exception is when an external editor is in use, * in which case the load happens each time "run" is hit. */ @@ -1055,7 +1055,7 @@ public class Sketch { } buffer.append('\n'); buffer.append(editor.getText()); - editor.setText(buffer.toString(), false); + editor.setText(buffer.toString(), 0, 0); // scroll to start setModified(); } @@ -1082,11 +1082,13 @@ public class Sketch { // set to the text for this file // 'true' means to wipe out the undo buffer // (so they don't undo back to the other file.. whups!) - editor.setText(current.program, true); + editor.setText(current.program, + current.selectionStart, current.selectionStop, + current.undo); // set stored caret and scroll positions editor.textarea.setScrollPosition(current.scrollPosition); - editor.textarea.select(current.selectionStart, current.selectionStop); + //editor.textarea.select(current.selectionStart, current.selectionStop); //editor.textarea.setSelectionStart(current.selectionStart); //editor.textarea.setSelectionEnd(current.selectionStop); @@ -1682,12 +1684,20 @@ public class Sketch { ps.flush(); ps.close(); + // copy the loading gif to the applet + String LOADING_IMAGE = "loading.gif"; + File loadingImage = new File(folder, LOADING_IMAGE); + if (!loadingImage.exists()) { + loadingImage = new File("lib", LOADING_IMAGE); + } + Base.copyFile(loadingImage, new File(appletFolder, LOADING_IMAGE)); + // copy the source files to the target, since we like // to encourage people to share their code for (int i = 0; i < codeCount; i++) { try { Base.copyFile(code[i].file, - new File(appletFolder, code[i].file.getName())); + new File(appletFolder, code[i].file.getName())); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/SketchCode.java b/app/SketchCode.java index b9db6dff5..9e466ca57 100644 --- a/app/SketchCode.java +++ b/app/SketchCode.java @@ -25,15 +25,25 @@ package processing.app; import java.io.*; +import javax.swing.undo.*; public class SketchCode { - public String name; // pretty name (no extension), not the full file name + /** Pretty name (no extension), not the full file name */ + public String name; + + /** File object for where this code is located */ public File file; + + /** Type of code in this tab, Sketch.PDE or Sketch.JAVA */ public int flavor; + /** Text of the program text for this tab */ public String program; + /** Undo Manager for this tab, each tab keeps track of their own */ + public UndoManager undo = new UndoManager(); + // saved positions from last time this tab was used public int selectionStart; public int selectionStop; diff --git a/app/tools/AutoFormat.java b/app/tools/AutoFormat.java index 3e1a2f8da..93abede6c 100644 --- a/app/tools/AutoFormat.java +++ b/app/tools/AutoFormat.java @@ -727,16 +727,14 @@ public class AutoFormat { // save current (rough) selection point int selectionEnd = editor.textarea.getSelectionEnd(); - // replace with new bootiful text - editor.setText(strOut.toString(), false); - // make sure the caret would be past the end of the text if (strOut.length() < selectionEnd - 1) { selectionEnd = strOut.length() - 1; } - // at least in the neighborhood - editor.textarea.select(selectionEnd, selectionEnd); + // replace with new bootiful text + // selectionEnd hopefully at least in the neighborhood + editor.setText(strOut.toString(), selectionEnd, selectionEnd); editor.sketch.setModified(); diff --git a/todo.txt b/todo.txt index ea1585be5..651ad16bb 100644 --- a/todo.txt +++ b/todo.txt @@ -119,7 +119,7 @@ X registerDispose() does the trick _ http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;num=1083574943 _ http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;num=1067383998 _ write an example that uses Hashtable -_ and another that uses Vector +_ and another that uses Vector _ beginShape() defaults to beginShape(POLYGON) _ needs to be noted for the reference _ make a note that u/v coordinates clamp at 0 and 1