untangling JEditTextArea from the modes a bit more

This commit is contained in:
Ben Fry
2015-01-22 11:15:51 -05:00
parent 1d01ba4ce9
commit 378f43de44
14 changed files with 307 additions and 184 deletions

View File

@@ -42,7 +42,9 @@ public class JavaEditor extends Editor {
protected JEditTextArea createTextArea() {
return new JEditTextArea(new PdeTextAreaDefaults(mode)) {
return new JEditTextArea(new PdeTextAreaDefaults(mode), new PdeKeyListener(this));
/*
return new JEditTextArea(new PdeTextAreaDefaults(mode), new PdeInputHandler()) {
// Forwards key events directly to the input handler. This is slightly
// faster than using a KeyListener because some Swing overhead is avoided.
PdeKeyListener editorListener = new PdeKeyListener(JavaEditor.this, this);
@@ -72,6 +74,7 @@ public class JavaEditor extends Editor {
}
}
};
*/
}

View File

@@ -3,7 +3,8 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-10 Ben Fry and Casey Reas
Copyright (c) 2012-15 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
@@ -34,21 +35,11 @@ import java.util.Arrays;
/**
* Filters key events for tab expansion/indent/etc.
* <p/>
* For version 0099, some changes have been made to make the indents
* smarter. There are still issues though:
* <UL>
* <LI> indent happens when it picks up a curly brace on the previous line,
* but not if there's a blank line between them.
* <LI> It also doesn't handle single indent situations where a brace
* isn't used (i.e. an if statement or for loop that's a single line).
* It shouldn't actually be using braces.
* </UL>
* Solving these issues, however, would probably best be done by a
* smarter parser/formatter, rather than continuing to hack this class.
* Filters key events for tab expansion/indent/etc. This is very old code
* that we'd love to replace with a smarter parser/formatter, rather than
* continuing to hack this class.
*/
public class PdeKeyListener {
public class PdeKeyListener extends PdeInputHandler {
private Editor editor;
private JEditTextArea textarea;
@@ -57,13 +48,26 @@ public class PdeKeyListener {
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
public PdeKeyListener(Editor editor, JEditTextArea textarea) {
this.editor = editor;
this.textarea = textarea;
// // let him know that i'm leechin'
// textarea.editorListener = this;
public PdeKeyListener(Editor editor) {
this.editor = editor;
}
public void keyPressed(KeyEvent event) {
if (!pressed(event)) {
super.keyPressed(event);
}
}
public void keyTyped(KeyEvent event) {
if (!typed(event)) {
super.keyTyped(event);
}
}
// we don't need keyReleased(), so that's passed through automatically
/**
@@ -74,36 +78,25 @@ public class PdeKeyListener {
* keyTyped().
* @return true if the event has been handled (to remove it from the queue)
*/
public boolean keyPressed(KeyEvent event) {
protected boolean pressed(KeyEvent event) {
if (textarea == null) {
textarea = editor.getTextArea();
}
char c = event.getKeyChar();
int code = event.getKeyCode();
Sketch sketch = editor.getSketch();
/*
if ((event.getModifiers() & CTRL_ALT) == CTRL_ALT) {
if (code == KeyEvent.VK_LEFT) {
sketch.handlePrevCode();
return true;
} else if (code == KeyEvent.VK_RIGHT) {
sketch.handleNextCode();
return true;
}
}
*/
if ((event.getModifiers() & InputEvent.META_MASK) != 0) {
//event.consume(); // does nothing
return false;
}
// TODO i don't like these accessors. clean em up later.
// if (!editor.getSketch().isModified()) {
if ((code == KeyEvent.VK_BACK_SPACE) || (code == KeyEvent.VK_TAB) ||
(code == KeyEvent.VK_ENTER) || ((c >= 32) && (c < 128))) {
sketch.setModified(true);
}
// }
if ((code == KeyEvent.VK_UP) &&
((event.getModifiers() & InputEvent.CTRL_MASK) != 0)) {
@@ -140,7 +133,7 @@ public class PdeKeyListener {
textarea.setCaretPosition(index);
}
event.consume();
return true;
// return true;
} else if ((code == KeyEvent.VK_DOWN) &&
((event.getModifiers() & InputEvent.CTRL_MASK) != 0)) {
@@ -164,11 +157,7 @@ public class PdeKeyListener {
}
index++;
}
// if the first char, index will be -2
//if (index < 0) index = 0;
//textarea.setSelectionStart(index);
//textarea.setSelectionEnd(index);
if ((event.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
textarea.setSelectionStart(caretIndex);
textarea.setSelectionEnd(index);
@@ -176,13 +165,9 @@ public class PdeKeyListener {
textarea.setCaretPosition(index);
}
event.consume();
return true;
}
// return true;
switch (c) {
case 9: // TAB
} else if (c == 9) {
if ((event.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
// if shift is down, the user always expects an outdent
// http://code.google.com/p/processing/issues/detail?id=458
@@ -195,15 +180,13 @@ public class PdeKeyListener {
int tabSize = Preferences.getInteger("editor.tabs.size");
textarea.setSelectedText(spaces(tabSize));
event.consume();
return true;
} else if (!Preferences.getBoolean("editor.tabs.expand")) {
textarea.setSelectedText("\t");
event.consume();
}
break;
case 10: // auto-indent
case 13:
} else if (c == 10 || c == 13) { // auto-indent
if (Preferences.getBoolean("editor.indent")) {
char contents[] = textarea.getText().toCharArray();
int tabSize = Preferences.getInteger("editor.tabs.size");
@@ -213,26 +196,6 @@ public class PdeKeyListener {
// just before where the newline will be inserted)
int origIndex = textarea.getCaretPosition() - 1;
// NOTE all this cursing about CRLF stuff is probably moot
// NOTE since the switch to JEditTextArea, which seems to use
// NOTE only LFs internally (thank god). disabling for 0099.
// walk through the array to the current caret position,
// and count how many weirdo windows line endings there are,
// which would be throwing off the caret position number
/*
int offset = 0;
int realIndex = origIndex;
for (int i = 0; i < realIndex-1; i++) {
if ((contents[i] == 13) && (contents[i+1] == 10)) {
offset++;
realIndex++;
}
}
// back up until \r \r\n or \n.. @#($* cross platform
//System.out.println(origIndex + " offset = " + offset);
origIndex += offset; // ARGH!#(* WINDOWS#@($*
*/
// if the previous thing is a brace (whether prev line or
// up farther) then the correct indent is the number of spaces
// on that line + 'indent'.
@@ -244,10 +207,9 @@ public class PdeKeyListener {
int spaceCount = calcSpaceCount(origIndex, contents);
// If the last character was a left curly brace, then indent.
// For 0122, walk backwards a bit to make sure that the there
// isn't a curly brace several spaces (or lines) back. Also
// moved this before calculating extraCount, since it'll affect
// that as well.
// For 0122, walk backwards a bit to make sure that the there isn't a
// curly brace several spaces (or lines) back. Also moved this before
// calculating extraCount, since it'll affect that as well.
int index2 = origIndex;
while ((index2 >= 0) &&
Character.isWhitespace(contents[index2])) {
@@ -262,7 +224,6 @@ public class PdeKeyListener {
spaceCount += tabSize;
}
}
//System.out.println("spaceCount should be " + spaceCount);
// now before inserting this many spaces, walk forward from
// the caret position and count the number of spaces,
@@ -283,23 +244,10 @@ public class PdeKeyListener {
index++;
}
// hitting return on a line with spaces *after* the caret
// can cause trouble. for 0099, was ignoring the case, but this is
// Hitting return on a line with spaces *after* the caret
// can cause trouble. For 0099, was ignoring the case, but this is
// annoying, so in 0122 we're trying to fix that.
/*
if (spaceCount - extraCount > 0) {
spaceCount -= extraCount;
}
*/
spaceCount -= extraCount;
//if (spaceCount < 0) spaceCount = 0;
//System.out.println("extraCount is " + extraCount);
// now, check to see if the current line contains a } and if so,
// outdent again by indent
//if (braceCount > 0) {
//spaceCount -= 2;
//}
if (spaceCount < 0) {
// for rev 0122, actually delete extra space
@@ -337,9 +285,9 @@ public class PdeKeyListener {
}
// mark this event as already handled (all but ignored)
event.consume();
return true;
// return true;
case '}':
} else if (c == '}') {
if (Preferences.getBoolean("editor.indent")) {
// first remove anything that was there (in case this multiple
// characters are selected, so that it's not in the way of the
@@ -383,13 +331,12 @@ public class PdeKeyListener {
event.consume();
return true;
}
break;
}
return false;
}
public boolean keyTyped(KeyEvent event) {
protected boolean typed(KeyEvent event) {
char c = event.getKeyChar();
if ((event.getModifiers() & InputEvent.CTRL_MASK) != 0) {
@@ -447,7 +394,7 @@ public class PdeKeyListener {
* the beginning of the current block, and return the number of
* spaces found on that line.
*/
protected int calcBraceIndent(int index, char contents[]) {
protected int calcBraceIndent(int index, char[] contents) {
// now that we know things are ok to be indented, walk
// backwards to the last { to see how far its line is indented.
// this isn't perfect cuz it'll pick up commented areas,
@@ -479,44 +426,11 @@ public class PdeKeyListener {
// check how many spaces on the line with the matching open brace
//int pairedSpaceCount = calcSpaceCount(index, contents);
//System.out.println(pairedSpaceCount);
return calcSpaceCount(index, contents);
}
// /**
// * Get the character array and blank out the commented areas.
// * This hasn't yet been tested, the plan was to make auto-indent
// * less gullible (it gets fooled by braces that are commented out).
// */
// protected char[] getCleanedContents() {
// char c[] = textarea.getText().toCharArray();
//
// int index = 0;
// while (index < c.length - 1) {
// if ((c[index] == '/') && (c[index+1] == '*')) {
// c[index++] = 0;
// c[index++] = 0;
// while ((index < c.length - 1) &&
// !((c[index] == '*') && (c[index+1] == '/'))) {
// c[index++] = 0;
// }
//
// } else if ((c[index] == '/') && (c[index+1] == '/')) {
// // clear out until the end of the line
// while ((index < c.length) && (c[index] != 10)) {
// c[index++] = 0;
// }
// if (index != c.length) {
// index++; // skip over the newline
// }
// }
// }
// return c;
// }
static String spaces(int count) {
static private String spaces(int count) {
char[] c = new char[count];
Arrays.fill(c, ' ');
return new String(c);

View File

@@ -1303,7 +1303,9 @@ public class DebugEditor extends JavaEditor implements ActionListener {
// return new TextArea(new PdeTextAreaDefaults(mode), this);
// }
protected JEditTextArea createTextArea() {
return new TextArea(new PdeTextAreaDefaults(mode), this) {
return new TextArea(new PdeTextAreaDefaults(mode), new PdeKeyListener(this), this);
/*
return new TextArea(new PdeTextAreaDefaults(mode), new PdeInputHandler(), this) {
// Forwards key events directly to the input handler. This is slightly
// faster than using a KeyListener because some Swing overhead is avoided.
PdeKeyListener editorListener = new PdeKeyListener(DebugEditor.this, this);
@@ -1333,6 +1335,7 @@ public class DebugEditor extends JavaEditor implements ActionListener {
}
}
};
*/
}

View File

@@ -1,21 +1,21 @@
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.mode.java.debug;
@@ -28,6 +28,7 @@ import com.sun.jdi.Value;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Specialized {@link VariableNode} for representing fields. Overrides
* {@link #setValue} to properly change the value of the encapsulated field.
@@ -35,10 +36,10 @@ import java.util.logging.Logger;
* @author Martin Leopold <m@martinleopold.com>
*/
public class FieldNode extends VariableNode {
protected Field field;
protected ObjectReference obj;
/**
* Construct a {@link FieldNode}.
*
@@ -54,6 +55,7 @@ public class FieldNode extends VariableNode {
this.obj = obj;
}
@Override
public void setValue(Value value) {
try {

View File

@@ -37,6 +37,7 @@ import javax.swing.DefaultCellEditor;
import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIDefaults;
@@ -68,7 +69,7 @@ import processing.mode.java.pdex.ExperimentalMode;
*
* @author Martin Leopold <m@martinleopold.com>
*/
public class VariableInspector extends javax.swing.JFrame {
public class VariableInspector extends JFrame {
protected DefaultMutableTreeNode rootNode; // the root node (invisible)
protected DefaultMutableTreeNode builtins; // node for Processing built-in variables

View File

@@ -32,17 +32,8 @@ import java.util.logging.Logger;
import javax.swing.ImageIcon;
import processing.app.Base;
import processing.app.Editor;
import processing.app.EditorState;
import processing.app.Mode;
import processing.app.Preferences;
import processing.app.RunnerListener;
import processing.app.Sketch;
import processing.app.SketchCode;
import processing.app.SketchException;
import processing.mode.java.JavaBuild;
import processing.mode.java.JavaMode;
import processing.app.*;
import processing.mode.java.*;
import processing.mode.java.debug.DebugEditor;
import processing.mode.java.runner.Runner;
@@ -281,9 +272,7 @@ public class ExperimentalMode extends JavaMode {
protected ImageIcon classIcon, fieldIcon, methodIcon, localVarIcon;
protected void loadIcons(){
String iconPath = getContentFile("data")
.getAbsolutePath()
+ File.separator + "icons";
String iconPath = getContentFile("data").getAbsolutePath() + File.separator + "icons";
classIcon = new ImageIcon(iconPath + File.separator + "class_obj.png");
methodIcon = new ImageIcon(iconPath + File.separator
+ "methpub_obj.png");
@@ -295,6 +284,7 @@ public class ExperimentalMode extends JavaMode {
}
/*
public ClassLoader getJavaModeClassLoader() {
for (Mode m : base.getModeList()) {
if (m.getClass() == JavaMode.class) {
@@ -305,6 +295,7 @@ public class ExperimentalMode extends JavaMode {
// badness
return null;
}
*/
/**

View File

@@ -22,7 +22,6 @@ package processing.mode.java.pdex;
import static processing.mode.java.pdex.ExperimentalMode.log;
import static processing.mode.java.pdex.ExperimentalMode.log2;
import processing.mode.java.debug.DebugEditor;
import processing.mode.java.tweak.ColorControlBox;
import processing.mode.java.tweak.Handle;
@@ -45,6 +44,7 @@ import javax.swing.DefaultListModel;
import javax.swing.SwingWorker;
import processing.app.Base;
import processing.app.syntax.InputHandler;
import processing.app.syntax.JEditTextArea;
import processing.app.syntax.TextAreaDefaults;
@@ -81,16 +81,15 @@ public class TextArea extends JEditTextArea {
protected ErrorCheckerService errorCheckerService;
public TextArea(TextAreaDefaults defaults, DebugEditor editor) {
super(defaults);
public TextArea(TextAreaDefaults defaults, InputHandler inputHandler, DebugEditor editor) {
super(defaults, inputHandler);
this.editor = editor;
// replace the painter:
// first save listeners, these are package-private in JEditTextArea, so not accessible
ComponentListener[] componentListeners = painter.getComponentListeners();
mouseListeners = painter.getMouseListeners();
MouseMotionListener[] mouseMotionListeners = painter
.getMouseMotionListeners();
MouseMotionListener[] mouseMotionListeners = painter.getMouseMotionListeners();
remove(painter);
@@ -119,10 +118,8 @@ public class TextArea extends JEditTextArea {
gutterBgColor = theme.getThemeColor("gutter.bgcolor", gutterBgColor);
gutterLineColor = theme.getThemeColor("gutter.linecolor", gutterLineColor);
gutterPadding = theme.getInteger("gutter.padding");
breakpointMarker = theme.loadThemeString("breakpoint.marker",
breakpointMarker);
currentLineMarker = theme.loadThemeString("currentline.marker",
currentLineMarker);
breakpointMarker = theme.loadThemeString("breakpoint.marker", breakpointMarker);
currentLineMarker = theme.loadThemeString("currentline.marker", currentLineMarker);
// TweakMode code