From 5d2ed0f59deb8efffcef145791f5b78cc4d7871b Mon Sep 17 00:00:00 2001 From: benfry Date: Sun, 17 Aug 2008 14:36:19 +0000 Subject: [PATCH] more cleanup of editor api to make way for tools --- app/src/processing/app/Editor.java | 112 +++++++++++++++--- app/src/processing/app/Sketch.java | 3 +- .../processing/app/syntax/JEditTextArea.java | 88 +++++--------- app/src/processing/app/tools/AutoFormat.java | 7 +- .../processing/app/tools/DiscourseFormat.java | 2 +- 5 files changed, 127 insertions(+), 85 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 12828a791..c9ee1da16 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -53,7 +53,7 @@ public class Editor extends JFrame { // otherwise, if the window is resized with the message label // set to blank, it's preferredSize() will be fukered - static public final String EMPTY = + static protected final String EMPTY = " " + " " + " "; @@ -62,10 +62,10 @@ public class Editor extends JFrame { static final int SHORTCUT_KEY_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); /** Command-W on Mac OS X, Ctrl-W on Windows and Linux */ - static public final KeyStroke WINDOW_CLOSE_KEYSTROKE = + static final KeyStroke WINDOW_CLOSE_KEYSTROKE = KeyStroke.getKeyStroke('W', SHORTCUT_KEY_MASK); /** Command-Option on Mac OS X, Ctrl-Alt on Windows and Linux */ - static public final int SHORTCUT_ALT_KEY_MASK = ActionEvent.ALT_MASK | + static final int SHORTCUT_ALT_KEY_MASK = ActionEvent.ALT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); /** @@ -102,7 +102,7 @@ public class Editor extends JFrame { EditorLineStatus lineStatus; - public JEditTextArea textarea; + protected JEditTextArea textarea; EditorListener listener; // runtime information and window placement @@ -1031,14 +1031,32 @@ public class Editor extends JFrame { return sketch; } + + /** + * Get the JEditTextArea object for use. This should only be used in obscure + * cases that really need to hack the internals of the JEditTextArea. Most + * tools should only interface via the get/set functions found in this class. + * This will maintain compatibility with future releases, which may not use + * JEditTextArea. + */ + public JEditTextArea getTextArea() { + return textarea; + } + + /** * Get the contents of the current buffer. Used by the Sketch class. */ public String getText() { return textarea.getText(); } - - + + + public String getText(int start, int stop) { + return textarea.getText(start, stop - start); + } + + /** * Replace the entire contents of the front-most tab. */ @@ -1048,25 +1066,81 @@ public class Editor extends JFrame { endCompoundEdit(); } + /** - * Called to update the text but not switch to a different - * set of code (which would affect the undo manager). + * 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 selectionStop) { - beginCompoundEdit(); - textarea.setText(what); - endCompoundEdit(); +// public void setText2(String what, int start, int stop) { +// beginCompoundEdit(); +// textarea.setText(what); +// endCompoundEdit(); +// +// // make sure that a tool isn't asking for a bad location +// start = Math.max(0, Math.min(start, textarea.getDocumentLength())); +// stop = Math.max(0, Math.min(start, textarea.getDocumentLength())); +// textarea.select(start, stop); +// +// textarea.requestFocus(); // get the caret blinking +// } - // make sure that a tool isn't asking for a bad location - selectionStart = - Math.max(0, Math.min(selectionStart, textarea.getDocumentLength())); - selectionStop = - Math.max(0, Math.min(selectionStart, textarea.getDocumentLength())); - textarea.select(selectionStart, selectionStop); - textarea.requestFocus(); // get the caret blinking + public String getSelectedText() { + return textarea.getSelectedText(); } + + + public void setSelectedText(String what) { + textarea.setSelectedText(what); + } + + + public void setSelection(int start, int stop) { + // make sure that a tool isn't asking for a bad location + start = PApplet.constrain(start, 0, textarea.getDocumentLength()); + stop = PApplet.constrain(stop, 0, textarea.getDocumentLength()); + textarea.select(start, stop); + } + + + public int getSelectionStart() { + return textarea.getSelectionStart(); + } + + + public int getSelectionStop() { + return textarea.getSelectionStop(); + } + + + public String getLineText(int line) { + return textarea.getLineText(line); + } + + + public void setLineText(int line, String what) { + beginCompoundEdit(); + textarea.select(getLineStartOffset(line), getLineStopOffset(line)); + textarea.setSelectedText(what); + endCompoundEdit(); + } + + + public int getLineStartOffset(int line) { + return textarea.getLineStartOffset(line); + } + + + public int getLineStopOffset(int line) { + return textarea.getLineStopOffset(line); + } + + + public int getLineCount() { + return textarea.getLineCount(); + } + /** * Switch between tabs, this swaps out the Document object diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index a066bc63a..a031cebc2 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -1174,7 +1174,8 @@ public class Sketch { } buffer.append('\n'); buffer.append(editor.getText()); - editor.setText(buffer.toString(), 0, 0); // scroll to start + editor.setText(buffer.toString()); + editor.setSelection(0, 0); // scroll to start setModified(true); } diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index e71071d51..2400049a7 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -115,21 +115,6 @@ public class JEditTextArea extends JComponent // We don't seem to get the initial focus event? focusedComponent = this; - // no more need for reflection, pde requires use of java 1.4 - /* - if (System.getProperty("java.version").startsWith("1.4")) { - try { - Class kWheelHandler = - Class.forName("processing.app.syntax.WheelHandler"); - java.lang.reflect.Constructor konstructor = - kWheelHandler.getConstructor(new Class[] { getClass() }); - konstructor.newInstance(new Object[] { this }); - } catch (Exception e) { - System.err.println(e); - } - } - */ - //new WheelHandler(this); addMouseWheelListener(new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { if (!scrollBarsInitialized) return; @@ -160,24 +145,21 @@ public class JEditTextArea extends JComponent * Returns if this component can be traversed by pressing * the Tab key. This returns false. */ - public final boolean isManagingFocus() - { + public final boolean isManagingFocus() { return true; } /** * Returns the object responsible for painting this text area. */ - public final TextAreaPainter getPainter() - { + public final TextAreaPainter getPainter() { return painter; } /** * Returns the input handler. */ - public final InputHandler getInputHandler() - { + public final InputHandler getInputHandler() { return inputHandler; } @@ -185,16 +167,14 @@ public class JEditTextArea extends JComponent * Sets the input handler. * @param inputHandler The new input handler */ - public void setInputHandler(InputHandler inputHandler) - { + public void setInputHandler(InputHandler inputHandler) { this.inputHandler = inputHandler; } /** * Returns true if the caret is blinking, false otherwise. */ - public final boolean isCaretBlinkEnabled() - { + public final boolean isCaretBlinkEnabled() { return caretBlinks; } @@ -202,8 +182,7 @@ public class JEditTextArea extends JComponent * Toggles caret blinking. * @param caretBlinks True if the caret should blink, false otherwise */ - public void setCaretBlinkEnabled(boolean caretBlinks) - { + public void setCaretBlinkEnabled(boolean caretBlinks) { this.caretBlinks = caretBlinks; if(!caretBlinks) blink = false; @@ -214,8 +193,7 @@ public class JEditTextArea extends JComponent /** * Returns true if the caret is visible, false otherwise. */ - public final boolean isCaretVisible() - { + public final boolean isCaretVisible() { return (!caretBlinks || blink) && caretVisible; } @@ -224,8 +202,7 @@ public class JEditTextArea extends JComponent * @param caretVisible True if the caret should be visible, false * otherwise */ - public void setCaretVisible(boolean caretVisible) - { + public void setCaretVisible(boolean caretVisible) { this.caretVisible = caretVisible; blink = true; @@ -235,23 +212,20 @@ public class JEditTextArea extends JComponent /** * Blinks the caret. */ - public final void blinkCaret() - { - if(caretBlinks) - { - blink = !blink; - painter.invalidateSelectedLines(); - } - else + public final void blinkCaret() { + if (caretBlinks) { + blink = !blink; + painter.invalidateSelectedLines(); + } else { blink = true; + } } /** * Returns the number of lines from the top and button of the * text area that are always visible. */ - public final int getElectricScroll() - { + public final int getElectricScroll() { return electricScroll; } @@ -261,8 +235,7 @@ public class JEditTextArea extends JComponent * @param electricScroll The number of lines always visible from * the top or bottom */ - public final void setElectricScroll(int electricScroll) - { + public final void setElectricScroll(int electricScroll) { this.electricScroll = electricScroll; } @@ -327,12 +300,10 @@ public class JEditTextArea extends JComponent } } - /** * Returns the line displayed at the text area's origin. */ - public final int getFirstLine() - { + public final int getFirstLine() { return firstLine; } @@ -340,22 +311,20 @@ public class JEditTextArea extends JComponent * Sets the line displayed at the text area's origin without * updating the scroll bars. */ - public void setFirstLine(int firstLine) - { - if(firstLine == this.firstLine) - return; - //int oldFirstLine = this.firstLine; + public void setFirstLine(int firstLine) { + if (firstLine == this.firstLine) return; + this.firstLine = firstLine; - if(firstLine != vertical.getValue()) + if (firstLine != vertical.getValue()) { updateScrollBars(); + } painter.repaint(); } /** * Returns the number of lines visible in this text area. */ - public final int getVisibleLines() - { + public final int getVisibleLines() { return visibleLines; } @@ -363,13 +332,11 @@ public class JEditTextArea extends JComponent * Recalculates the number of visible lines. This should not * be called directly. */ - public final void recalculateVisibleLines() - { - if(painter == null) - return; + public final void recalculateVisibleLines() { + if (painter == null) return; + int height = painter.getHeight(); int lineHeight = painter.getFontMetrics().getHeight(); - //int oldVisibleLines = visibleLines; visibleLines = height / lineHeight; updateScrollBars(); } @@ -377,8 +344,7 @@ public class JEditTextArea extends JComponent /** * Returns the horizontal offset of drawn lines. */ - public final int getHorizontalOffset() - { + public final int getHorizontalOffset() { return horizontalOffset; } diff --git a/app/src/processing/app/tools/AutoFormat.java b/app/src/processing/app/tools/AutoFormat.java index b9e2dc139..a8260bf40 100644 --- a/app/src/processing/app/tools/AutoFormat.java +++ b/app/src/processing/app/tools/AutoFormat.java @@ -435,7 +435,7 @@ public class AutoFormat { public void show() { StringBuffer onechar; - String originalText = editor.textarea.getText(); + String originalText = editor.getText(); strOut = new StringBuffer(); indentValue = Preferences.getInteger("editor.tabs.size"); indentChar = new String(" "); @@ -900,7 +900,7 @@ public class AutoFormat { */ // save current (rough) selection point - int selectionEnd = editor.textarea.getSelectionStop(); + int selectionEnd = editor.getSelectionStop(); // make sure the caret would be past the end of the text if (strOut.length() < selectionEnd - 1) { @@ -927,7 +927,8 @@ public class AutoFormat { } else { // replace with new bootiful text // selectionEnd hopefully at least in the neighborhood - editor.setText(formattedText, selectionEnd, selectionEnd); + editor.setText(formattedText); + editor.setSelection(selectionEnd, selectionEnd); editor.getSketch().setModified(true); // mark as finished editor.statusNotice("Auto Format finished."); diff --git a/app/src/processing/app/tools/DiscourseFormat.java b/app/src/processing/app/tools/DiscourseFormat.java index 089d740b7..f65451dda 100644 --- a/app/src/processing/app/tools/DiscourseFormat.java +++ b/app/src/processing/app/tools/DiscourseFormat.java @@ -64,7 +64,7 @@ public class DiscourseFormat { */ public DiscourseFormat(Editor editor) { this.editor = editor; - this.textarea = editor.textarea; + this.textarea = editor.getTextArea(); }