Skip to content

Commit 57060e1

Browse files
committed
heavy refactoring to separate Java and non-Java code for Modes
1 parent e9f2840 commit 57060e1

File tree

13 files changed

+552
-258
lines changed

13 files changed

+552
-258
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
Copyright (c) 2012-16 The Processing Foundation
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License version 2
9+
as published by the Free Software Foundation.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software Foundation, Inc.
18+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
package processing.app;
22+
23+
24+
public interface Problem {
25+
public boolean isError();
26+
public boolean isWarning();
27+
28+
public int getTabIndex();
29+
public int getLineNumber();
30+
public String getMessage();
31+
32+
public int getStartOffset();
33+
public int getStopOffset();
34+
}
35+
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
Copyright (c) 2012-16 The Processing Foundation
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License version 2
9+
as published by the Free Software Foundation.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software Foundation, Inc.
18+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
package processing.app.syntax;
22+
23+
import java.awt.Cursor;
24+
import java.awt.Image;
25+
import java.awt.event.MouseEvent;
26+
import java.awt.event.MouseMotionAdapter;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
import processing.app.Mode;
31+
import processing.app.ui.Editor;
32+
33+
34+
/**
35+
* Extensions to JEditTextArea to for the PDE. These were moved out of
36+
* JavaTextArea because they were not Java-specific and would be helpful
37+
* for other Mode implementations.
38+
*/
39+
public class PdeTextArea extends JEditTextArea {
40+
protected final Editor editor;
41+
42+
protected Image gutterGradient;
43+
44+
/// the text marker for highlighting breakpoints in the gutter
45+
static public final String BREAK_MARKER = "<>";
46+
/// the text marker for highlighting the current line in the gutter
47+
static public final String STEP_MARKER = "->";
48+
49+
/// maps line index to gutter text
50+
protected final Map<Integer, String> gutterText = new HashMap<>();
51+
52+
53+
public PdeTextArea(TextAreaDefaults defaults, InputHandler inputHandler,
54+
Editor editor) {
55+
super(defaults, inputHandler);
56+
this.editor = editor;
57+
58+
// change cursor to pointer in the gutter area
59+
painter.addMouseMotionListener(gutterCursorMouseAdapter);
60+
61+
add(CENTER, painter);
62+
63+
// load settings from theme.txt
64+
Mode mode = editor.getMode();
65+
gutterGradient = mode.makeGradient("editor", Editor.LEFT_GUTTER, 500);
66+
}
67+
68+
69+
public Image getGutterGradient() {
70+
return gutterGradient;
71+
}
72+
73+
74+
public void setMode(Mode mode) {
75+
((PdeTextAreaPainter) painter).setMode(mode);
76+
}
77+
78+
79+
/**
80+
* Set the gutter text of a specific line.
81+
*
82+
* @param lineIdx
83+
* the line index (0-based)
84+
* @param text
85+
* the text
86+
*/
87+
public void setGutterText(int lineIdx, String text) {
88+
gutterText.put(lineIdx, text);
89+
painter.invalidateLine(lineIdx);
90+
}
91+
92+
93+
/**
94+
* Clear the gutter text of a specific line.
95+
*
96+
* @param lineIdx
97+
* the line index (0-based)
98+
*/
99+
public void clearGutterText(int lineIdx) {
100+
gutterText.remove(lineIdx);
101+
painter.invalidateLine(lineIdx);
102+
}
103+
104+
105+
/**
106+
* Clear all gutter text.
107+
*/
108+
public void clearGutterText() {
109+
for (int lineIdx : gutterText.keySet()) {
110+
painter.invalidateLine(lineIdx);
111+
}
112+
gutterText.clear();
113+
}
114+
115+
116+
/**
117+
* Retrieve the gutter text of a specific line.
118+
* @param lineIdx the line index (0-based)
119+
* @return the gutter text
120+
*/
121+
public String getGutterText(int lineIdx) {
122+
return gutterText.get(lineIdx);
123+
}
124+
125+
126+
/**
127+
* Convert a character offset to a horizontal pixel position inside
128+
* the text area. Overridden to take gutter width into account.
129+
* @param line the 0-based line number
130+
* @param offset the character offset (0 is the first character on a line)
131+
* @return the horizontal position
132+
*/
133+
@Override
134+
public int _offsetToX(int line, int offset) {
135+
return super._offsetToX(line, offset) + Editor.LEFT_GUTTER;
136+
}
137+
138+
139+
/**
140+
* Convert a horizontal pixel position to a character offset. Overridden to
141+
* take gutter width into account.
142+
* @param line the 0-based line number
143+
* @param x the horizontal pixel position
144+
* @return he character offset (0 is the first character on a line)
145+
*/
146+
@Override
147+
public int xToOffset(int line, int x) {
148+
return super.xToOffset(line, x - Editor.LEFT_GUTTER);
149+
}
150+
151+
152+
/**
153+
* Sets default cursor (instead of text cursor) in the gutter area.
154+
*/
155+
protected final MouseMotionAdapter gutterCursorMouseAdapter = new MouseMotionAdapter() {
156+
private int lastX; // previous horizontal position of the mouse cursor
157+
158+
@Override
159+
public void mouseMoved(MouseEvent me) {
160+
if (me.getX() < Editor.LEFT_GUTTER) {
161+
if (lastX >= Editor.LEFT_GUTTER) {
162+
painter.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
163+
}
164+
} else {
165+
if (lastX < Editor.LEFT_GUTTER) {
166+
painter.setCursor(new Cursor(Cursor.TEXT_CURSOR));
167+
}
168+
}
169+
lastX = me.getX();
170+
}
171+
};
172+
173+
174+
public Editor getEditor() {
175+
return editor;
176+
}
177+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
Copyright (c) 2012-16 The Processing Foundation
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License version 2
9+
as published by the Free Software Foundation.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software Foundation, Inc.
18+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
package processing.app.syntax;
22+
23+
import java.awt.Color;
24+
import java.awt.Font;
25+
import java.awt.event.MouseAdapter;
26+
import java.awt.event.MouseEvent;
27+
28+
import processing.app.Mode;
29+
import processing.app.ui.Editor;
30+
31+
32+
public class PdeTextAreaPainter extends TextAreaPainter {
33+
public Color errorUnderlineColor;
34+
public Color warningUnderlineColor;
35+
36+
protected Font gutterTextFont;
37+
protected Color gutterTextColor;
38+
protected Color gutterPastColor;
39+
protected Color gutterLineHighlightColor;
40+
41+
42+
public PdeTextAreaPainter(JEditTextArea textArea, TextAreaDefaults defaults) {
43+
super(textArea, defaults);
44+
45+
// Handle mouse clicks to toggle breakpoints
46+
addMouseListener(new MouseAdapter() {
47+
long lastTime; // OS X seems to be firing multiple mouse events
48+
49+
public void mousePressed(MouseEvent event) {
50+
// Don't toggle breakpoints when the debugger isn't enabled
51+
// https://github.com/processing/processing/issues/3306
52+
if (getEditor().isDebuggerEnabled()) {
53+
long thisTime = event.getWhen();
54+
if (thisTime - lastTime > 100) {
55+
if (event.getX() < Editor.LEFT_GUTTER) {
56+
int offset = textArea.xyToOffset(event.getX(), event.getY());
57+
if (offset >= 0) {
58+
int lineIndex = textArea.getLineOfOffset(offset);
59+
getEditor().toggleBreakpoint(lineIndex);
60+
}
61+
}
62+
lastTime = thisTime;
63+
}
64+
}
65+
}
66+
});
67+
}
68+
69+
70+
/**
71+
* Loads theme for TextAreaPainter
72+
*/
73+
public void setMode(Mode mode) {
74+
errorUnderlineColor = mode.getColor("editor.error.underline.color");
75+
warningUnderlineColor = mode.getColor("editor.warning.underline.color");
76+
77+
gutterTextFont = mode.getFont("editor.gutter.text.font");
78+
gutterTextColor = mode.getColor("editor.gutter.text.color");
79+
gutterPastColor = new Color(gutterTextColor.getRed(),
80+
gutterTextColor.getGreen(),
81+
gutterTextColor.getBlue(),
82+
96);
83+
gutterLineHighlightColor = mode.getColor("editor.gutter.linehighlight.color");
84+
}
85+
86+
87+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
88+
89+
90+
@Override
91+
public int getScrollWidth() {
92+
// https://github.com/processing/processing/issues/3591
93+
return super.getWidth() - Editor.LEFT_GUTTER;
94+
}
95+
96+
97+
public Editor getEditor() {
98+
return ((PdeTextArea) textArea).editor;
99+
}
100+
}

0 commit comments

Comments
 (0)