Skip to content

Commit 0707053

Browse files
committed
untangling JEditTextArea from the modes a bit more
1 parent e1ea6f9 commit 0707053

14 files changed

Lines changed: 306 additions & 183 deletions

app/src/processing/app/Editor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ protected ArrayList<ToolContribution> getCoreTools() {
304304
* solution where the listeners are handled properly.
305305
*/
306306
protected JEditTextArea createTextArea() {
307-
return new JEditTextArea(new PdeTextAreaDefaults(mode)) {
307+
return new JEditTextArea(new PdeTextAreaDefaults(mode), new PdeInputHandler()) {
308308
// this is a kludge that needs to be removed [fry 150120]
309309
public void processKeyEvent(KeyEvent evt) {
310310
// this had to be added in Processing 007X, because the menu key

app/src/processing/app/Settings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Settings {
3939
/**
4040
* Copy of the defaults in case the user mangles a preference.
4141
* It's necessary to keep a copy of the defaults around, because the user may
42-
* have mangled a setting on their own. In the past, we used to load the
42+
* have replaced a setting on their own. In the past, we used to load the
4343
* defaults, then replace those with what was in the user's preferences file.
4444
* Problem is, if something like a font entry in the user's file no longer
4545
* parses properly, we need to be able to get back to a clean version of that

app/src/processing/app/syntax/JEditTextArea.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class JEditTextArea extends JComponent
8686
* Creates a new JEditTextArea with the specified settings.
8787
* @param defaults The default settings
8888
*/
89-
public JEditTextArea(TextAreaDefaults defaults) {
89+
public JEditTextArea(TextAreaDefaults defaults, InputHandler inputHandler) {
9090
// Enable the necessary events
9191
enableEvents(AWTEvent.KEY_EVENT_MASK);
9292

@@ -129,7 +129,7 @@ public void actionPerformed(ActionEvent e) {
129129
setFocusTraversalKeysEnabled(false);
130130

131131
// Load the defaults
132-
setInputHandler(defaults.inputHandler);
132+
setInputHandler(inputHandler);
133133
setDocument(defaults.document);
134134
// editable = defaults.editable;
135135
caretVisible = defaults.caretVisible;
@@ -1963,6 +1963,27 @@ public void processKeyEvent(KeyEvent evt) {
19631963
}
19641964
}
19651965
*/
1966+
1967+
1968+
public void processKeyEvent(KeyEvent event) {
1969+
// this had to be added in Processing 007X, because the menu key
1970+
// events weren't making it up to the frame.
1971+
super.processKeyEvent(event);
1972+
1973+
if (inputHandler != null) {
1974+
switch (event.getID()) {
1975+
case KeyEvent.KEY_TYPED:
1976+
inputHandler.keyTyped(event);
1977+
break;
1978+
case KeyEvent.KEY_PRESSED:
1979+
inputHandler.keyPressed(event);
1980+
break;
1981+
case KeyEvent.KEY_RELEASED:
1982+
inputHandler.keyReleased(event);
1983+
break;
1984+
}
1985+
}
1986+
}
19661987

19671988

19681989
// protected members
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
PdeInputHandler - PDE-specific handling of keys
5+
Part of the Processing project - http://processing.org
6+
7+
Copyright (c) 2012-14 The Processing Foundation
8+
Copyright (c) 2004-12 Ben Fry and Casey Reas
9+
Copyright (c) 2001-03 Massachusetts Institute of Technology
10+
11+
This program is free software; you can redistribute it and/or modify
12+
it under the terms of the GNU General Public License as published by
13+
the Free Software Foundation; either version 2 of the License, or
14+
(at your option) any later version.
15+
16+
This program is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
GNU General Public License for more details.
20+
21+
You should have received a copy of the GNU General Public License
22+
along with this program; if not, write to the Free Software Foundation, Inc.
23+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24+
*/
25+
26+
package processing.app.syntax;
27+
28+
import processing.app.Base;
29+
import processing.app.Preferences;
30+
31+
32+
/**
33+
* Sets key bindings used by the PDE, except for those that are Mode-specific.
34+
*/
35+
public class PdeInputHandler extends DefaultInputHandler {
36+
37+
public PdeInputHandler() {
38+
// Use option on mac for text edit controls that are ctrl on Windows/Linux.
39+
// (i.e. ctrl-left/right is option-left/right on OS X)
40+
String mod = Base.isMacOS() ? "A" : "C";
41+
42+
// right now, ctrl-up/down is select up/down, but mod should be
43+
// used instead, because the mac expects it to be option(alt)
44+
45+
addKeyBinding("BACK_SPACE", InputHandler.BACKSPACE);
46+
// for 0122, shift-backspace is delete, for 0176, it's now a preference,
47+
// to prevent holy warriors from attacking me for it.
48+
if (Preferences.getBoolean("editor.keys.shift_backspace_is_delete")) {
49+
addKeyBinding("S+BACK_SPACE", InputHandler.DELETE);
50+
} else {
51+
// Made the default for 0215, deemed better for our audience.
52+
addKeyBinding("S+BACK_SPACE", InputHandler.BACKSPACE);
53+
}
54+
55+
addKeyBinding("DELETE", InputHandler.DELETE);
56+
addKeyBinding("S+DELETE", InputHandler.DELETE);
57+
58+
// the following two were changed for 0122 for better mac/pc compatability
59+
addKeyBinding(mod + "+BACK_SPACE", InputHandler.BACKSPACE_WORD); // 0122
60+
addKeyBinding(mod + "S+BACK_SPACE", InputHandler.BACKSPACE_WORD); // 0215
61+
addKeyBinding(mod + "+DELETE", InputHandler.DELETE_WORD); // 0122
62+
addKeyBinding(mod + "S+DELETE", InputHandler.DELETE_WORD); // 0215
63+
64+
// handled by listener, don't bother here
65+
//addKeyBinding("ENTER", InputHandler.INSERT_BREAK);
66+
//addKeyBinding("TAB", InputHandler.INSERT_TAB);
67+
68+
addKeyBinding("INSERT", InputHandler.OVERWRITE);
69+
70+
// http://dev.processing.org/bugs/show_bug.cgi?id=162
71+
// added for 0176, though the bindings do not appear relevant for osx
72+
if (Preferences.getBoolean("editor.keys.alternative_cut_copy_paste")) {
73+
addKeyBinding("C+INSERT", InputHandler.CLIPBOARD_COPY);
74+
addKeyBinding("S+INSERT", InputHandler.CLIPBOARD_PASTE);
75+
addKeyBinding("S+DELETE", InputHandler.CLIPBOARD_CUT);
76+
}
77+
78+
// disabling for 0122, not sure what this does
79+
//addKeyBinding("C+\\", InputHandler.TOGGLE_RECT);
80+
81+
// for 0122, these have been changed for better compatibility
82+
// HOME and END now mean the beginning/end of the document
83+
// for 0176 changed this to a preference so that the Mac OS X people
84+
// can get the "normal" behavior as well if they prefer.
85+
if (Preferences.getBoolean("editor.keys.home_and_end_travel_far")) {
86+
addKeyBinding("HOME", InputHandler.DOCUMENT_HOME);
87+
addKeyBinding("END", InputHandler.DOCUMENT_END);
88+
addKeyBinding("S+HOME", InputHandler.SELECT_DOC_HOME);
89+
addKeyBinding("S+END", InputHandler.SELECT_DOC_END);
90+
} else {
91+
// for 0123 added the proper windows defaults
92+
addKeyBinding("HOME", InputHandler.HOME);
93+
addKeyBinding("END", InputHandler.END);
94+
addKeyBinding("S+HOME", InputHandler.SELECT_HOME);
95+
addKeyBinding("S+END", InputHandler.SELECT_END);
96+
addKeyBinding("C+HOME", InputHandler.DOCUMENT_HOME);
97+
addKeyBinding("C+END", InputHandler.DOCUMENT_END);
98+
addKeyBinding("CS+HOME", InputHandler.SELECT_DOC_HOME);
99+
addKeyBinding("CS+END", InputHandler.SELECT_DOC_END);
100+
}
101+
102+
if (Base.isMacOS()) {
103+
// Additional OS X key bindings added for 0215.
104+
// Also note that two more are added above and marked 0215.
105+
// http://code.google.com/p/processing/issues/detail?id=1354
106+
// Could not find a proper Apple guide, but a partial reference is here:
107+
// http://guides.macrumors.com/Keyboard_shortcuts&section=10#Text_Shortcuts
108+
109+
// control-A move to start of current paragraph
110+
addKeyBinding("C+A", InputHandler.HOME);
111+
addKeyBinding("CS+A", InputHandler.SELECT_HOME);
112+
// control-E move to end of current paragraph
113+
addKeyBinding("C+E", InputHandler.END);
114+
addKeyBinding("CS+E", InputHandler.SELECT_END);
115+
116+
// control-D forward delete
117+
addKeyBinding("C+D", InputHandler.DELETE);
118+
119+
// control-B move left one character
120+
addKeyBinding("C+B", InputHandler.PREV_CHAR);
121+
addKeyBinding("CS+B", InputHandler.SELECT_PREV_CHAR);
122+
// control-F move right one character
123+
addKeyBinding("C+F", InputHandler.NEXT_CHAR);
124+
addKeyBinding("CS+F", InputHandler.SELECT_NEXT_CHAR);
125+
126+
// control-H delete (just ASCII for backspace)
127+
addKeyBinding("C+H", InputHandler.BACKSPACE);
128+
129+
// control-N move down one line
130+
addKeyBinding("C+N", InputHandler.NEXT_LINE);
131+
addKeyBinding("CS+N", InputHandler.SELECT_NEXT_LINE);
132+
// control-P move up one line
133+
addKeyBinding("C+P", InputHandler.PREV_LINE);
134+
addKeyBinding("CS+P", InputHandler.SELECT_PREV_LINE);
135+
136+
// might be nice, but no handlers currently available
137+
// control-O insert new line after cursor
138+
// control-T transpose (swap) two surrounding character
139+
// control-V move to end, then left one character
140+
// control-K delete remainder of current paragraph
141+
// control-Y paste text previously deleted with control-K
142+
}
143+
144+
if (Base.isMacOS()) {
145+
addKeyBinding("M+LEFT", InputHandler.HOME);
146+
addKeyBinding("M+RIGHT", InputHandler.END);
147+
addKeyBinding("MS+LEFT", InputHandler.SELECT_HOME); // 0122
148+
addKeyBinding("MS+RIGHT", InputHandler.SELECT_END); // 0122
149+
} else {
150+
addKeyBinding("C+LEFT", InputHandler.HOME); // 0122
151+
addKeyBinding("C+RIGHT", InputHandler.END); // 0122
152+
addKeyBinding("CS+HOME", InputHandler.SELECT_HOME); // 0122
153+
addKeyBinding("CS+END", InputHandler.SELECT_END); // 0122
154+
}
155+
156+
addKeyBinding("PAGE_UP", InputHandler.PREV_PAGE);
157+
addKeyBinding("PAGE_DOWN", InputHandler.NEXT_PAGE);
158+
addKeyBinding("S+PAGE_UP", InputHandler.SELECT_PREV_PAGE);
159+
addKeyBinding("S+PAGE_DOWN", InputHandler.SELECT_NEXT_PAGE);
160+
161+
addKeyBinding("LEFT", InputHandler.PREV_CHAR);
162+
addKeyBinding("S+LEFT", InputHandler.SELECT_PREV_CHAR);
163+
addKeyBinding(mod + "+LEFT", InputHandler.PREV_WORD);
164+
addKeyBinding(mod + "S+LEFT", InputHandler.SELECT_PREV_WORD);
165+
addKeyBinding("RIGHT", InputHandler.NEXT_CHAR);
166+
addKeyBinding("S+RIGHT", InputHandler.SELECT_NEXT_CHAR);
167+
addKeyBinding(mod + "+RIGHT", InputHandler.NEXT_WORD);
168+
addKeyBinding(mod + "S+RIGHT", InputHandler.SELECT_NEXT_WORD);
169+
170+
addKeyBinding("UP", InputHandler.PREV_LINE);
171+
addKeyBinding(mod + "+UP", InputHandler.PREV_LINE); // p5
172+
addKeyBinding("S+UP", InputHandler.SELECT_PREV_LINE);
173+
addKeyBinding("DOWN", InputHandler.NEXT_LINE);
174+
addKeyBinding(mod + "+DOWN", InputHandler.NEXT_LINE); // p5
175+
addKeyBinding("S+DOWN", InputHandler.SELECT_NEXT_LINE);
176+
177+
addKeyBinding("MS+UP", InputHandler.SELECT_DOC_HOME);
178+
addKeyBinding("CS+UP", InputHandler.SELECT_DOC_HOME);
179+
addKeyBinding("MS+DOWN", InputHandler.SELECT_DOC_END);
180+
addKeyBinding("CS+DOWN", InputHandler.SELECT_DOC_END);
181+
182+
addKeyBinding(mod + "+ENTER", InputHandler.REPEAT);
183+
}
184+
}

app/src/processing/app/syntax/PdeTextAreaDefaults.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
PdeTextAreaDefaults - grabs font/color settings for the editor
55
Part of the Processing project - http://processing.org
66
7-
Copyright (c) 2004-06 Ben Fry and Casey Reas
7+
Copyright (c) 2012-14 The Processing Foundation
8+
Copyright (c) 2004-12 Ben Fry and Casey Reas
89
Copyright (c) 2001-03 Massachusetts Institute of Technology
910
1011
This program is free software; you can redistribute it and/or modify
@@ -15,11 +16,11 @@
1516
This program is distributed in the hope that it will be useful,
1617
but WITHOUT ANY WARRANTY; without even the implied warranty of
1718
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18-
GNU General Public License for more details.
19+
GNU General Public License for more details.
1920
2021
You should have received a copy of the GNU General Public License
21-
along with this program; if not, write to the Free Software Foundation,
22-
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22+
along with this program; if not, write to the Free Software Foundation, Inc.
23+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2324
*/
2425

2526
package processing.app.syntax;
@@ -30,6 +31,7 @@
3031
public class PdeTextAreaDefaults extends TextAreaDefaults {
3132

3233
public PdeTextAreaDefaults(Mode mode) {
34+
/*
3335
inputHandler = new DefaultInputHandler();
3436
//inputHandler.addDefaultKeyBindings(); // 0122
3537
@@ -178,6 +180,7 @@ public PdeTextAreaDefaults(Mode mode) {
178180
inputHandler.addKeyBinding("CS+DOWN", InputHandler.SELECT_DOC_END);
179181
180182
inputHandler.addKeyBinding(mod + "+ENTER", InputHandler.REPEAT);
183+
*/
181184

182185
document = new SyntaxDocument();
183186
// editable = true;

app/src/processing/app/syntax/TextAreaDefaults.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* creating the text area is that this method is faster.
1919
*/
2020
public class TextAreaDefaults {
21-
public InputHandler inputHandler;
21+
//public InputHandler inputHandler;
2222
public SyntaxDocument document;
2323
// public boolean editable;
2424

java/src/processing/mode/java/JavaEditor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ protected JavaEditor(Base base, String path, EditorState state, Mode mode) {
4242

4343

4444
protected JEditTextArea createTextArea() {
45-
return new JEditTextArea(new PdeTextAreaDefaults(mode)) {
45+
return new JEditTextArea(new PdeTextAreaDefaults(mode), new PdeKeyListener(this));
46+
/*
47+
return new JEditTextArea(new PdeTextAreaDefaults(mode), new PdeInputHandler()) {
4648
// Forwards key events directly to the input handler. This is slightly
4749
// faster than using a KeyListener because some Swing overhead is avoided.
4850
PdeKeyListener editorListener = new PdeKeyListener(JavaEditor.this, this);
@@ -72,6 +74,7 @@ public void processKeyEvent(KeyEvent evt) {
7274
}
7375
}
7476
};
77+
*/
7578
}
7679

7780

0 commit comments

Comments
 (0)