-
Notifications
You must be signed in to change notification settings - Fork 250
/
AbstractGutterComponent.java
executable file
·187 lines (152 loc) · 4.36 KB
/
AbstractGutterComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* 02/17/2009
*
* AbstractGutterComponent.java - A component that can be displayed in a Gutter.
*
* This library is distributed under a modified BSD license. See the included
* LICENSE file for details.
*/
package org.fife.ui.rtextarea;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.text.View;
/**
* A component that can be displayed in a {@link Gutter}.
*
* @author Robert Futrell
* @version 1.0
*/
abstract class AbstractGutterComponent extends JPanel {
/**
* The text area whose lines we are marking with icons.
*/
protected RTextArea textArea;
/**
* The number of lines in the text area.
*/
protected int currentLineCount;
private static Listener listener;
/**
* Constructor.
*
* @param textArea The text area.
*/
AbstractGutterComponent(RTextArea textArea) {
init(); // Called before setTextArea().
setTextArea(textArea);
}
@Override
public void addNotify() {
super.addNotify();
getListener().install(this);
}
/**
* Returns the bounds of a child view as a rectangle, since
* <code>View</code>s tend to use <code>Shape</code>.
*
* @param parent The parent view of the child whose bounds we're getting.
* @param line The index of the child view.
* @param editorRect Returned from the text area's
* <code>getVisibleEditorRect</code> method.
* @return The child view's bounds.
*/
protected static Rectangle getChildViewBounds(View parent, int line,
Rectangle editorRect) {
Shape alloc = parent.getChildAllocation(line, editorRect);
if (alloc==null) {
// WrappedSyntaxView can have this when made so small it's
// no longer visible
return new Rectangle();
}
return alloc instanceof Rectangle ? (Rectangle)alloc :
alloc.getBounds();
}
/**
* Returns the parent <code>Gutter</code> component.
*
* @return The parent <code>Gutter</code>.
*/
protected Gutter getGutter() {
Container parent = getParent();
return (parent instanceof Gutter) ? (Gutter)parent : null;
}
/**
* Returns the singleton instance of the listener for all gutter components.
*
* @return The singleton instance.
*/
private static Listener getListener() {
if (listener == null) {
listener = new Listener();
}
return listener;
}
/**
* Called when text is inserted to or removed from the text area.
* Implementations can take this opportunity to repaint, revalidate, etc.
*
* @param e The document event.
*/
abstract void handleDocumentEvent(DocumentEvent e);
/**
* Called by the constructor before the text area is set. This is a hook
* to allow subclasses to do any needed initialization. The default
* implementation does nothing.
*/
protected void init() {
}
/**
* Called when the line heights of the text area change. This is usually
* the result of one or more of the fonts in the editor changing.
*/
abstract void lineHeightsChanged();
@Override
public void removeNotify() {
getListener().uninstall(this);
super.removeNotify();
}
/**
* Sets the text area being displayed. Subclasses can override, but
* should call the super implementation.
*
* @param textArea The text area.
*/
public void setTextArea(RTextArea textArea) {
this.textArea = textArea;
int lineCount = textArea==null ? 0 : textArea.getLineCount();
if (currentLineCount!=lineCount) {
currentLineCount = lineCount;
repaint();
}
}
static class Listener extends MouseAdapter {
private boolean newArmed;
void install(AbstractGutterComponent component) {
component.addMouseListener(this);
component.addMouseMotionListener(this);
}
public void mouseExited(MouseEvent e) {
setArmed((AbstractGutterComponent)e.getComponent(), false);
}
public void mouseMoved(MouseEvent e) {
setArmed((AbstractGutterComponent)e.getComponent(), true);
}
private void setArmed(AbstractGutterComponent component, boolean armed) {
newArmed = armed;
SwingUtilities.invokeLater(() -> {
if (component.getGutter() != null && newArmed != component.getGutter().isArmed()) {
component.getGutter().setArmed(newArmed);
}
});
}
void uninstall(AbstractGutterComponent component) {
component.removeMouseListener(this);
component.removeMouseMotionListener(this);
}
}
}