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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package cuchaz.enigma.gui.panels;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import de.sciss.syntaxpane.actions.ActionUtils;
import cuchaz.enigma.api.service.GuiService;
import cuchaz.enigma.gui.config.UiConfig;
public class GutterPanel extends JPanel {
private final JPanel markerPanel;
public GutterPanel(JEditorPane editor, JComponent lineNumbers) {
markerPanel = new MarkerPanel(editor);
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
add(lineNumbers);
add(markerPanel);
setBackground(UiConfig.getLineNumbersBackgroundColor());
setForeground(UiConfig.getLineNumbersForegroundColor());
markerPanel.setBackground(UiConfig.getLineNumbersBackgroundColor());
markerPanel.setForeground(UiConfig.getLineNumbersForegroundColor());
setBorder(lineNumbers.getBorder());
lineNumbers.setBorder(null);
}
public void clearMarkers() {
markerPanel.removeAll();
}
public void addMarker(int line, GuiService.GutterMarkerAlignment alignment, Component marker) {
markerPanel.add(marker, new MarkerLayout.Constraint(line, alignment));
}
private static class MarkerPanel extends JPanel implements CaretListener, DocumentListener, PropertyChangeListener {
private final JEditorPane editor;
private final Color currentLineColor = UiConfig.getLineNumbersSelectedColor();
private MarkerPanel(JEditorPane editor) {
this.editor = editor;
setLayout(new MarkerLayout());
Insets editorInsets = editor.getInsets();
if (editorInsets.top != 0 || editorInsets.bottom != 0) {
setBorder(BorderFactory.createEmptyBorder(editorInsets.top, 0, editorInsets.bottom, 0));
}
setFont(editor.getFont());
editor.addCaretListener(this);
editor.getDocument().addDocumentListener(this);
editor.addPropertyChangeListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int currentLine;
try {
currentLine = ActionUtils.getLineNumber(editor, editor.getCaretPosition());
} catch (BadLocationException ex) {
return; // no valid caret -> nothing to draw
}
FontMetrics fm = getFontMetrics(getFont());
int lh = fm.getHeight();
Insets insets = getInsets();
int y = currentLine * lh;
g.setColor(currentLineColor);
g.fillRect(0, y, getWidth(), lh);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("document")) {
repaint();
}
}
@Override
public void caretUpdate(CaretEvent e) {
repaint();
}
@Override
public void insertUpdate(DocumentEvent e) {
documentChanged();
}
@Override
public void removeUpdate(DocumentEvent e) {
documentChanged();
}
@Override
public void changedUpdate(DocumentEvent e) {
documentChanged();
}
private void documentChanged() {
SwingUtilities.invokeLater(this::repaint);
}
}
private static class MarkerLayout implements LayoutManager2 {
private static final int GAP = 2;
private static final int HUGE_HEIGHT = 0x100000; // taken from LineNumbersRuler
private final Map<Component, Constraint> constraints = new HashMap<>();
@Override
public void addLayoutComponent(Component comp, Object constraints) {
this.constraints.put(comp, (Constraint) constraints);
}
@Override
public Dimension maximumLayoutSize(Container target) {
return preferredLayoutSize(target);
}
@Override
public float getLayoutAlignmentX(Container target) {
return 0.5f;
}
@Override
public float getLayoutAlignmentY(Container target) {
return 0.5f;
}
@Override
public void invalidateLayout(Container target) {
}
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
constraints.remove(comp);
}
@Override
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
if (constraints.isEmpty()) {
return new Dimension(insets.left + insets.right, HUGE_HEIGHT);
}
Map<Integer, Integer> leftCount = new HashMap<>();
Map<Integer, Integer> rightCount = new HashMap<>();
for (Constraint constraint : constraints.values()) {
switch (constraint.alignment) {
case LEFT -> leftCount.merge(constraint.line, 1, Integer::sum);
case RIGHT -> rightCount.merge(constraint.line, 1, Integer::sum);
}
}
int maxLeft = leftCount.values().stream().mapToInt(Integer::intValue).max().orElse(0);
int maxRight = rightCount.values().stream().mapToInt(Integer::intValue).max().orElse(0);
int lineHeight = parent.getFontMetrics(parent.getFont()).getHeight();
return new Dimension(
GAP + insets.left + insets.right + (maxLeft + maxRight) * lineHeight,
HUGE_HEIGHT
);
}
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
@Override
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
Map<Integer, Integer> leftCount = new HashMap<>();
Map<Integer, Integer> rightCount = new HashMap<>();
int lineHeight = parent.getFontMetrics(parent.getFont()).getHeight();
int numComponents = parent.getComponentCount();
for (int i = 0; i < numComponents; i++) {
Component comp = parent.getComponent(i);
Constraint constraint = constraints.get(comp);
if (constraint == null) {
continue;
}
int left = switch (constraint.alignment) {
case LEFT -> GAP + (leftCount.merge(constraint.line, 1, Integer::sum) - 1) * lineHeight + GAP / 2;
case RIGHT -> parent.getWidth() - rightCount.merge(constraint.line, 1, Integer::sum) * lineHeight + GAP / 2;
};
comp.setBounds(left, constraint.line * lineHeight + GAP / 2, lineHeight - GAP, lineHeight - GAP);
}
}
}
private record Constraint(int line, GuiService.GutterMarkerAlignment alignment) {
}
}
}
|