1 | /*
|
---|
2 | * 02/17/2009
|
---|
3 | *
|
---|
4 | * Gutter.java - Manages line numbers, icons, etc. on the left-hand side of
|
---|
5 | * an RTextArea.
|
---|
6 | *
|
---|
7 | * This library is distributed under a modified BSD license. See the included
|
---|
8 | * RSyntaxTextArea.License.txt file for details.
|
---|
9 | */
|
---|
10 | package org.fife.ui.rtextarea;
|
---|
11 |
|
---|
12 | import java.awt.BorderLayout;
|
---|
13 | import java.awt.Color;
|
---|
14 | import java.awt.Component;
|
---|
15 | import java.awt.ComponentOrientation;
|
---|
16 | import java.awt.Font;
|
---|
17 | import java.awt.Graphics;
|
---|
18 | import java.awt.Point;
|
---|
19 | import java.awt.event.ComponentAdapter;
|
---|
20 | import java.beans.PropertyChangeEvent;
|
---|
21 | import java.beans.PropertyChangeListener;
|
---|
22 | import javax.swing.Icon;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.border.EmptyBorder;
|
---|
25 | import javax.swing.event.DocumentEvent;
|
---|
26 | import javax.swing.event.DocumentListener;
|
---|
27 | import javax.swing.text.BadLocationException;
|
---|
28 |
|
---|
29 | import org.fife.ui.rsyntaxtextarea.ActiveLineRangeEvent;
|
---|
30 | import org.fife.ui.rsyntaxtextarea.ActiveLineRangeListener;
|
---|
31 | import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
---|
32 | import org.fife.ui.rsyntaxtextarea.folding.FoldManager;
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * The gutter is the component on the left-hand side of the text area that
|
---|
37 | * displays optional information such as line numbers, fold regions, and icons
|
---|
38 | * (for bookmarks, debugging breakpoints, error markers, etc.).<p>
|
---|
39 | *
|
---|
40 | * Icons can be added on a per-line basis to visually mark syntax errors, lines
|
---|
41 | * with breakpoints set on them, etc. To add icons to the gutter, you must
|
---|
42 | * first call {@link RTextScrollPane#setIconRowHeaderEnabled(boolean)} on the
|
---|
43 | * parent scroll pane, to make the icon area visible. Then, you can add icons
|
---|
44 | * that track either lines in the document, or offsets, via
|
---|
45 | * {@link #addLineTrackingIcon(int, Icon)} and
|
---|
46 | * {@link #addOffsetTrackingIcon(int, Icon)}, respectively. To remove an
|
---|
47 | * icon you've added, use {@link #removeTrackingIcon(GutterIconInfo)}.<p>
|
---|
48 | *
|
---|
49 | * In addition to support for arbitrary per-line icons, this component also
|
---|
50 | * has built-in support for displaying icons representing "bookmarks;" that is,
|
---|
51 | * lines a user can cycle through via F2 and Shift+F2. Bookmarked lines are
|
---|
52 | * toggled via Ctrl+F2. In order to enable bookmarking, you must first assign
|
---|
53 | * an icon to represent a bookmarked line, then actually enable the feature:
|
---|
54 | *
|
---|
55 | * <pre>
|
---|
56 | * Gutter gutter = scrollPane.getGutter();
|
---|
57 | * gutter.setBookmarkIcon(new ImageIcon("bookmark.png"));
|
---|
58 | * gutter.setBookmarkingEnabled(true);
|
---|
59 | * </pre>
|
---|
60 | *
|
---|
61 | * @author Robert Futrell
|
---|
62 | * @version 1.0
|
---|
63 | * @see GutterIconInfo
|
---|
64 | */
|
---|
65 | public class Gutter extends JPanel {
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * The color used to highlight active line ranges if none is specified.
|
---|
69 | */
|
---|
70 | public static final Color DEFAULT_ACTIVE_LINE_RANGE_COLOR =
|
---|
71 | new Color(51, 153, 255);
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * The text area.
|
---|
75 | */
|
---|
76 | private RTextArea textArea;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Renders line numbers.
|
---|
80 | */
|
---|
81 | private LineNumberList lineNumberList;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Renders bookmark icons, breakpoints, error icons, etc.
|
---|
85 | */
|
---|
86 | private IconRowHeader iconArea;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Shows lines that are code-foldable.
|
---|
90 | */
|
---|
91 | private FoldIndicator foldIndicator;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Listens for events in our text area.
|
---|
95 | */
|
---|
96 | private TextAreaListener listener;
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Constructor.
|
---|
101 | *
|
---|
102 | * @param textArea The parent text area. If this is
|
---|
103 | * <code>null</code>, you must call {@link #setTextArea(RTextArea)}.
|
---|
104 | */
|
---|
105 | public Gutter(RTextArea textArea) {
|
---|
106 |
|
---|
107 | listener = new TextAreaListener();
|
---|
108 | setTextArea(textArea);
|
---|
109 | setLayout(new BorderLayout());
|
---|
110 | if (this.textArea!=null) {
|
---|
111 | // Enable line numbers our first time through if they give us
|
---|
112 | // a text area.
|
---|
113 | setLineNumbersEnabled(true);
|
---|
114 | if (this.textArea instanceof RSyntaxTextArea) {
|
---|
115 | RSyntaxTextArea rsta = (RSyntaxTextArea)this.textArea;
|
---|
116 | setFoldIndicatorEnabled(rsta.isCodeFoldingEnabled());
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | setBorder(new GutterBorder(0, 0, 0, 1)); // Assume ltr
|
---|
121 |
|
---|
122 | Color bg = null;
|
---|
123 | if (textArea!=null) {
|
---|
124 | bg = textArea.getBackground(); // May return null if image bg
|
---|
125 | }
|
---|
126 | setBackground(bg!=null ? bg : Color.WHITE);
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Adds an icon that tracks an offset in the document, and is displayed
|
---|
133 | * adjacent to the line numbers. This is useful for marking things such
|
---|
134 | * as source code errors.
|
---|
135 | *
|
---|
136 | * @param line The line to track (zero-based).
|
---|
137 | * @param icon The icon to display. This should be small (say 16x16).
|
---|
138 | * @return A tag for this icon. This can later be used in a call to
|
---|
139 | * {@link #removeTrackingIcon(GutterIconInfo)} to remove this
|
---|
140 | * icon.
|
---|
141 | * @throws BadLocationException If <code>offs</code> is an invalid offset
|
---|
142 | * into the text area.
|
---|
143 | * @see #addOffsetTrackingIcon(int, Icon)
|
---|
144 | * @see #removeTrackingIcon(GutterIconInfo)
|
---|
145 | */
|
---|
146 | public GutterIconInfo addLineTrackingIcon(int line, Icon icon)
|
---|
147 | throws BadLocationException {
|
---|
148 | int offs = textArea.getLineStartOffset(line);
|
---|
149 | return addOffsetTrackingIcon(offs, icon);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Adds an icon that tracks an offset in the document, and is displayed
|
---|
155 | * adjacent to the line numbers. This is useful for marking things such
|
---|
156 | * as source code errors.
|
---|
157 | *
|
---|
158 | * @param offs The offset to track.
|
---|
159 | * @param icon The icon to display. This should be small (say 16x16).
|
---|
160 | * @return A tag for this icon.
|
---|
161 | * @throws BadLocationException If <code>offs</code> is an invalid offset
|
---|
162 | * into the text area.
|
---|
163 | * @see #addLineTrackingIcon(int, Icon)
|
---|
164 | * @see #removeTrackingIcon(GutterIconInfo)
|
---|
165 | */
|
---|
166 | public GutterIconInfo addOffsetTrackingIcon(int offs, Icon icon)
|
---|
167 | throws BadLocationException {
|
---|
168 | return iconArea.addOffsetTrackingIcon(offs, icon);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Clears the active line range.
|
---|
174 | *
|
---|
175 | * @see #setActiveLineRange(int, int)
|
---|
176 | */
|
---|
177 | private void clearActiveLineRange() {
|
---|
178 | iconArea.clearActiveLineRange();
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Returns the color used to paint the active line range, if any.
|
---|
184 | *
|
---|
185 | * @return The color.
|
---|
186 | * @see #setActiveLineRangeColor(Color)
|
---|
187 | */
|
---|
188 | public Color getActiveLineRangeColor() {
|
---|
189 | return iconArea.getActiveLineRangeColor();
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Returns the icon to use for bookmarks.
|
---|
195 | *
|
---|
196 | * @return The icon to use for bookmarks. If this is <code>null</code>,
|
---|
197 | * bookmarking is effectively disabled.
|
---|
198 | * @see #setBookmarkIcon(Icon)
|
---|
199 | * @see #isBookmarkingEnabled()
|
---|
200 | */
|
---|
201 | public Icon getBookmarkIcon() {
|
---|
202 | return iconArea.getBookmarkIcon();
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Returns the bookmarks known to this gutter.
|
---|
208 | *
|
---|
209 | * @return The bookmarks. If there are no bookmarks, an empty array is
|
---|
210 | * returned.
|
---|
211 | */
|
---|
212 | public GutterIconInfo[] getBookmarks() {
|
---|
213 | return iconArea.getBookmarks();
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns the color of the "border" line.
|
---|
219 | *
|
---|
220 | * @return The color.
|
---|
221 | * @see #setBorderColor(Color)
|
---|
222 | */
|
---|
223 | public Color getBorderColor() {
|
---|
224 | return ((GutterBorder)getBorder()).getColor();
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Returns the background color used by the (default) fold icons.
|
---|
230 | *
|
---|
231 | * @return The background color.
|
---|
232 | * @see #setFoldBackground(Color)
|
---|
233 | */
|
---|
234 | public Color getFoldBackground() {
|
---|
235 | return foldIndicator.getFoldIconBackground();
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Returns the foreground color of the fold indicator.
|
---|
241 | *
|
---|
242 | * @return The foreground color of the fold indicator.
|
---|
243 | * @see #setFoldIndicatorForeground(Color)
|
---|
244 | */
|
---|
245 | public Color getFoldIndicatorForeground() {
|
---|
246 | return foldIndicator.getForeground();
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Returns the color to use to paint line numbers.
|
---|
252 | *
|
---|
253 | * @return The color used when painting line numbers.
|
---|
254 | * @see #setLineNumberColor(Color)
|
---|
255 | */
|
---|
256 | public Color getLineNumberColor() {
|
---|
257 | return lineNumberList.getForeground();
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Returns the font used for line numbers.
|
---|
263 | *
|
---|
264 | * @return The font used for line numbers.
|
---|
265 | * @see #setLineNumberFont(Font)
|
---|
266 | */
|
---|
267 | public Font getLineNumberFont() {
|
---|
268 | return lineNumberList.getFont();
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Returns the starting line's line number. The default value is
|
---|
274 | * <code>1</code>.
|
---|
275 | *
|
---|
276 | * @return The index
|
---|
277 | * @see #setLineNumberingStartIndex(int)
|
---|
278 | */
|
---|
279 | public int getLineNumberingStartIndex() {
|
---|
280 | return lineNumberList.getLineNumberingStartIndex();
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Returns <code>true</code> if the line numbers are enabled and visible.
|
---|
286 | *
|
---|
287 | * @return Whether or not line numbers are visible.
|
---|
288 | */
|
---|
289 | public boolean getLineNumbersEnabled() {
|
---|
290 | for (int i=0; i<getComponentCount(); i++) {
|
---|
291 | if (getComponent(i)==lineNumberList) {
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | return false;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Returns whether tool tips are displayed showing the contents of
|
---|
301 | * collapsed fold regions when the mouse hovers over a +/- icon.
|
---|
302 | *
|
---|
303 | * @return Whether these tool tips are displayed.
|
---|
304 | * @see #setShowCollapsedRegionToolTips(boolean)
|
---|
305 | */
|
---|
306 | public boolean getShowCollapsedRegionToolTips() {
|
---|
307 | return foldIndicator.getShowCollapsedRegionToolTips();
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Returns the tracking icons at the specified view position.
|
---|
313 | *
|
---|
314 | * @param p The view position.
|
---|
315 | * @return The tracking icons at that position. If there are no tracking
|
---|
316 | * icons there, this will be an empty array.
|
---|
317 | * @throws BadLocationException If <code>p</code> is invalid.
|
---|
318 | */
|
---|
319 | public Object[] getTrackingIcons(Point p) throws BadLocationException {
|
---|
320 | int offs = textArea.viewToModel(new Point(0, p.y));
|
---|
321 | int line = textArea.getLineOfOffset(offs);
|
---|
322 | return iconArea.getTrackingIcons(line);
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Returns whether the fold indicator is enabled.
|
---|
328 | *
|
---|
329 | * @return Whether the fold indicator is enabled.
|
---|
330 | * @see #setFoldIndicatorEnabled(boolean)
|
---|
331 | */
|
---|
332 | public boolean isFoldIndicatorEnabled() {
|
---|
333 | for (int i=0; i<getComponentCount(); i++) {
|
---|
334 | if (getComponent(i)==foldIndicator) {
|
---|
335 | return true;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | return false;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Returns whether bookmarking is enabled.
|
---|
344 | *
|
---|
345 | * @return Whether bookmarking is enabled.
|
---|
346 | * @see #setBookmarkingEnabled(boolean)
|
---|
347 | */
|
---|
348 | public boolean isBookmarkingEnabled() {
|
---|
349 | return iconArea.isBookmarkingEnabled();
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Returns whether the icon row header is enabled.
|
---|
355 | *
|
---|
356 | * @return Whether the icon row header is enabled.
|
---|
357 | */
|
---|
358 | public boolean isIconRowHeaderEnabled() {
|
---|
359 | for (int i=0; i<getComponentCount(); i++) {
|
---|
360 | if (getComponent(i)==iconArea) {
|
---|
361 | return true;
|
---|
362 | }
|
---|
363 | }
|
---|
364 | return false;
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Removes the specified tracking icon.
|
---|
370 | *
|
---|
371 | * @param tag A tag for an icon in the gutter, as returned from either
|
---|
372 | * {@link #addLineTrackingIcon(int, Icon)} or
|
---|
373 | * {@link #addOffsetTrackingIcon(int, Icon)}.
|
---|
374 | * @see #removeAllTrackingIcons()
|
---|
375 | * @see #addLineTrackingIcon(int, Icon)
|
---|
376 | * @see #addOffsetTrackingIcon(int, Icon)
|
---|
377 | */
|
---|
378 | public void removeTrackingIcon(GutterIconInfo tag) {
|
---|
379 | iconArea.removeTrackingIcon(tag);
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Removes all tracking icons.
|
---|
385 | *
|
---|
386 | * @see #removeTrackingIcon(GutterIconInfo)
|
---|
387 | * @see #addOffsetTrackingIcon(int, Icon)
|
---|
388 | */
|
---|
389 | public void removeAllTrackingIcons() {
|
---|
390 | iconArea.removeAllTrackingIcons();
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Sets the color to use to render active line ranges.
|
---|
396 | *
|
---|
397 | * @param color The color to use. If this is null, then the default
|
---|
398 | * color is used.
|
---|
399 | * @see #getActiveLineRangeColor()
|
---|
400 | * @see #DEFAULT_ACTIVE_LINE_RANGE_COLOR
|
---|
401 | */
|
---|
402 | public void setActiveLineRangeColor(Color color) {
|
---|
403 | iconArea.setActiveLineRangeColor(color);
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Highlights a range of lines in the icon area. This, of course, will
|
---|
409 | * only be visible if the icon area is visible.
|
---|
410 | *
|
---|
411 | * @param startLine The start of the line range.
|
---|
412 | * @param endLine The end of the line range.
|
---|
413 | * @see #clearActiveLineRange()
|
---|
414 | */
|
---|
415 | private void setActiveLineRange(int startLine, int endLine) {
|
---|
416 | iconArea.setActiveLineRange(startLine, endLine);
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Sets the icon to use for bookmarks.
|
---|
422 | *
|
---|
423 | * @param icon The new bookmark icon. If this is <code>null</code>,
|
---|
424 | * bookmarking is effectively disabled.
|
---|
425 | * @see #getBookmarkIcon()
|
---|
426 | * @see #isBookmarkingEnabled()
|
---|
427 | */
|
---|
428 | public void setBookmarkIcon(Icon icon) {
|
---|
429 | iconArea.setBookmarkIcon(icon);
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Sets whether bookmarking is enabled. Note that a bookmarking icon
|
---|
435 | * must be set via {@link #setBookmarkIcon(Icon)} before bookmarks are
|
---|
436 | * truly enabled.
|
---|
437 | *
|
---|
438 | * @param enabled Whether bookmarking is enabled.
|
---|
439 | * @see #isBookmarkingEnabled()
|
---|
440 | * @see #setBookmarkIcon(Icon)
|
---|
441 | */
|
---|
442 | public void setBookmarkingEnabled(boolean enabled) {
|
---|
443 | iconArea.setBookmarkingEnabled(enabled);
|
---|
444 | if (enabled && !isIconRowHeaderEnabled()) {
|
---|
445 | setIconRowHeaderEnabled(true);
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Sets the color for the "border" line.
|
---|
452 | *
|
---|
453 | * @param color The new color.
|
---|
454 | * @see #getBorderColor()
|
---|
455 | */
|
---|
456 | public void setBorderColor(Color color) {
|
---|
457 | ((GutterBorder)getBorder()).setColor(color);
|
---|
458 | repaint();
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * {@inheritDoc}
|
---|
464 | */
|
---|
465 | public void setComponentOrientation(ComponentOrientation o) {
|
---|
466 | // Reuse the border to preserve its color.
|
---|
467 | if (o.isLeftToRight()) {
|
---|
468 | ((GutterBorder)getBorder()).setEdges(0, 0, 0, 1);
|
---|
469 | }
|
---|
470 | else {
|
---|
471 | ((GutterBorder)getBorder()).setEdges(0, 1, 0, 0);
|
---|
472 | }
|
---|
473 | super.setComponentOrientation(o);
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Sets the icons to use to represent collapsed and expanded folds.
|
---|
479 | *
|
---|
480 | * @param collapsedIcon The collapsed fold icon. This cannot be
|
---|
481 | * <code>null</code>.
|
---|
482 | * @param expandedIcon The expanded fold icon. This cannot be
|
---|
483 | * <code>null</code>.
|
---|
484 | */
|
---|
485 | public void setFoldIcons(Icon collapsedIcon, Icon expandedIcon) {
|
---|
486 | if (foldIndicator!=null) {
|
---|
487 | foldIndicator.setFoldIcons(collapsedIcon, expandedIcon);
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Toggles whether the fold indicator is enabled.
|
---|
494 | *
|
---|
495 | * @param enabled Whether the fold indicator should be enabled.
|
---|
496 | * @see #isFoldIndicatorEnabled()
|
---|
497 | */
|
---|
498 | public void setFoldIndicatorEnabled(boolean enabled) {
|
---|
499 | if (foldIndicator!=null) {
|
---|
500 | if (enabled) {
|
---|
501 | add(foldIndicator, BorderLayout.LINE_END);
|
---|
502 | }
|
---|
503 | else {
|
---|
504 | remove(foldIndicator);
|
---|
505 | }
|
---|
506 | revalidate();
|
---|
507 | }
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Sets the background color used by the (default) fold icons.
|
---|
513 | *
|
---|
514 | * @param bg The new background color.
|
---|
515 | * @see #getFoldBackground()
|
---|
516 | */
|
---|
517 | public void setFoldBackground(Color bg) {
|
---|
518 | if (bg==null) {
|
---|
519 | bg = FoldIndicator.DEFAULT_FOLD_BACKGROUND;
|
---|
520 | }
|
---|
521 | foldIndicator.setFoldIconBackground(bg);
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Sets the foreground color used by the fold indicator.
|
---|
527 | *
|
---|
528 | * @param fg The new fold indicator foreground.
|
---|
529 | * @see #getFoldIndicatorForeground()
|
---|
530 | */
|
---|
531 | public void setFoldIndicatorForeground(Color fg) {
|
---|
532 | if (fg==null) {
|
---|
533 | fg = FoldIndicator.DEFAULT_FOREGROUND;
|
---|
534 | }
|
---|
535 | foldIndicator.setForeground(fg);
|
---|
536 | }
|
---|
537 |
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Toggles whether the icon row header (used for breakpoints, bookmarks,
|
---|
541 | * etc.) is enabled.
|
---|
542 | *
|
---|
543 | * @param enabled Whether the icon row header is enabled.
|
---|
544 | * @see #isIconRowHeaderEnabled()
|
---|
545 | */
|
---|
546 | void setIconRowHeaderEnabled(boolean enabled) {
|
---|
547 | if (iconArea!=null) {
|
---|
548 | if (enabled) {
|
---|
549 | add(iconArea, BorderLayout.LINE_START);
|
---|
550 | }
|
---|
551 | else {
|
---|
552 | remove(iconArea);
|
---|
553 | }
|
---|
554 | revalidate();
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Sets the color to use to paint line numbers.
|
---|
561 | *
|
---|
562 | * @param color The color to use when painting line numbers.
|
---|
563 | * @see #getLineNumberColor()
|
---|
564 | */
|
---|
565 | public void setLineNumberColor(Color color) {
|
---|
566 | lineNumberList.setForeground(color);
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Sets the font used for line numbers.
|
---|
572 | *
|
---|
573 | * @param font The font to use. This cannot be <code>null</code>.
|
---|
574 | * @see #getLineNumberFont()
|
---|
575 | */
|
---|
576 | public void setLineNumberFont(Font font) {
|
---|
577 | if (font==null) {
|
---|
578 | throw new IllegalArgumentException("font cannot be null");
|
---|
579 | }
|
---|
580 | lineNumberList.setFont(font);
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * Sets the starting line's line number. The default value is
|
---|
586 | * <code>1</code>. Applications can call this method to change this value
|
---|
587 | * if they are displaying a subset of lines in a file, for example.
|
---|
588 | *
|
---|
589 | * @param index The new index.
|
---|
590 | * @see #getLineNumberingStartIndex()
|
---|
591 | */
|
---|
592 | public void setLineNumberingStartIndex(int index) {
|
---|
593 | lineNumberList.setLineNumberingStartIndex(index);
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * Toggles whether or not line numbers are visible.
|
---|
599 | *
|
---|
600 | * @param enabled Whether or not line numbers should be visible.
|
---|
601 | * @see #getLineNumbersEnabled()
|
---|
602 | */
|
---|
603 | void setLineNumbersEnabled(boolean enabled) {
|
---|
604 | if (lineNumberList!=null) {
|
---|
605 | if (enabled) {
|
---|
606 | add(lineNumberList);
|
---|
607 | }
|
---|
608 | else {
|
---|
609 | remove(lineNumberList);
|
---|
610 | }
|
---|
611 | revalidate();
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Toggles whether tool tips should be displayed showing the contents of
|
---|
618 | * collapsed fold regions when the mouse hovers over a +/- icon.
|
---|
619 | *
|
---|
620 | * @param show Whether to show these tool tips.
|
---|
621 | * @see #getShowCollapsedRegionToolTips()
|
---|
622 | */
|
---|
623 | public void setShowCollapsedRegionToolTips(boolean show) {
|
---|
624 | if (foldIndicator!=null) {
|
---|
625 | foldIndicator.setShowCollapsedRegionToolTips(show);
|
---|
626 | }
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * Sets the text area being displayed. This will clear any tracking
|
---|
632 | * icons currently displayed.
|
---|
633 | *
|
---|
634 | * @param textArea The text area.
|
---|
635 | */
|
---|
636 | void setTextArea(RTextArea textArea) {
|
---|
637 |
|
---|
638 | if (this.textArea!=null) {
|
---|
639 | listener.uninstall();
|
---|
640 | }
|
---|
641 |
|
---|
642 | if (textArea!=null) {
|
---|
643 |
|
---|
644 | RTextAreaEditorKit kit = (RTextAreaEditorKit)textArea.getUI().
|
---|
645 | getEditorKit(textArea);
|
---|
646 |
|
---|
647 | if (lineNumberList==null) {
|
---|
648 | lineNumberList = kit.createLineNumberList(textArea);
|
---|
649 | }
|
---|
650 | else {
|
---|
651 | lineNumberList.setTextArea(textArea);
|
---|
652 | }
|
---|
653 | if (iconArea==null) {
|
---|
654 | iconArea = kit.createIconRowHeader(textArea);
|
---|
655 | }
|
---|
656 | else {
|
---|
657 | iconArea.setTextArea(textArea);
|
---|
658 | }
|
---|
659 | if (foldIndicator==null) {
|
---|
660 | foldIndicator = new FoldIndicator(textArea);
|
---|
661 | }
|
---|
662 | else {
|
---|
663 | foldIndicator.setTextArea(textArea);
|
---|
664 | }
|
---|
665 |
|
---|
666 | listener.install(textArea);
|
---|
667 |
|
---|
668 | }
|
---|
669 |
|
---|
670 | this.textArea = textArea;
|
---|
671 |
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Programatically toggles whether there is a bookmark for the specified
|
---|
677 | * line. If bookmarking is not enabled, this method does nothing.
|
---|
678 | *
|
---|
679 | * @param line The line.
|
---|
680 | * @return Whether a bookmark is now at the specified line.
|
---|
681 | * @throws BadLocationException If <code>line</code> is an invalid line
|
---|
682 | * number in the text area.
|
---|
683 | */
|
---|
684 | public boolean toggleBookmark(int line) throws BadLocationException {
|
---|
685 | return iconArea.toggleBookmark(line);
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | /**
|
---|
690 | * The border used by the gutter.
|
---|
691 | */
|
---|
692 | private static class GutterBorder extends EmptyBorder {
|
---|
693 |
|
---|
694 | private Color color;
|
---|
695 |
|
---|
696 | public GutterBorder(int top, int left, int bottom, int right) {
|
---|
697 | super(top, left, bottom, right);
|
---|
698 | color = new Color(221, 221, 221);
|
---|
699 | }
|
---|
700 |
|
---|
701 | public Color getColor() {
|
---|
702 | return color;
|
---|
703 | }
|
---|
704 |
|
---|
705 | public void paintBorder(Component c, Graphics g, int x, int y,
|
---|
706 | int width, int height) {
|
---|
707 | g.setColor(color);
|
---|
708 | if (left==1) {
|
---|
709 | g.drawLine(0,0, 0,height);
|
---|
710 | }
|
---|
711 | else {
|
---|
712 | g.drawLine(width-1,0, width-1,height);
|
---|
713 | }
|
---|
714 | }
|
---|
715 |
|
---|
716 | public void setColor(Color color) {
|
---|
717 | this.color = color;
|
---|
718 | }
|
---|
719 |
|
---|
720 | public void setEdges(int top, int left, int bottom, int right) {
|
---|
721 | this.top = top;
|
---|
722 | this.left = left;
|
---|
723 | this.bottom = bottom;
|
---|
724 | this.right = right;
|
---|
725 | }
|
---|
726 |
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * Listens for the text area resizing.
|
---|
732 | */
|
---|
733 | /*
|
---|
734 | * This is necessary to keep child components the same height as the text
|
---|
735 | * area. The worse case is when the user toggles word-wrap and it changes
|
---|
736 | * the height of the text area. In that case, if we listen for the
|
---|
737 | * "lineWrap" property change, we get notified BEFORE the text area
|
---|
738 | * decides on its new size, thus we cannot resize properly. We listen
|
---|
739 | * instead for ComponentEvents so we change size after the text area has
|
---|
740 | * resized.
|
---|
741 | */
|
---|
742 | private class TextAreaListener extends ComponentAdapter
|
---|
743 | implements DocumentListener, PropertyChangeListener,
|
---|
744 | ActiveLineRangeListener {
|
---|
745 |
|
---|
746 | private boolean installed;
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Modifies the "active line range" that is painted in this component.
|
---|
750 | *
|
---|
751 | * @param e Information about the new "active line range."
|
---|
752 | */
|
---|
753 | public void activeLineRangeChanged(ActiveLineRangeEvent e) {
|
---|
754 | if (e.getMin()==-1) {
|
---|
755 | clearActiveLineRange();
|
---|
756 | }
|
---|
757 | else {
|
---|
758 | setActiveLineRange(e.getMin(), e.getMax());
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | public void changedUpdate(DocumentEvent e) {}
|
---|
763 |
|
---|
764 | public void componentResized(java.awt.event.ComponentEvent e) {
|
---|
765 | revalidate();
|
---|
766 | }
|
---|
767 |
|
---|
768 | protected void handleDocumentEvent(DocumentEvent e) {
|
---|
769 | for (int i=0; i<getComponentCount(); i++) {
|
---|
770 | AbstractGutterComponent agc =
|
---|
771 | (AbstractGutterComponent)getComponent(i);
|
---|
772 | agc.handleDocumentEvent(e);
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | public void insertUpdate(DocumentEvent e) {
|
---|
777 | handleDocumentEvent(e);
|
---|
778 | }
|
---|
779 |
|
---|
780 | public void install(RTextArea textArea) {
|
---|
781 | if (installed) {
|
---|
782 | uninstall();
|
---|
783 | }
|
---|
784 | textArea.addComponentListener(this);
|
---|
785 | textArea.getDocument().addDocumentListener(this);
|
---|
786 | textArea.addPropertyChangeListener(this);
|
---|
787 | if (textArea instanceof RSyntaxTextArea) {
|
---|
788 | RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
|
---|
789 | rsta.addActiveLineRangeListener(this);
|
---|
790 | rsta.getFoldManager().addPropertyChangeListener(this);
|
---|
791 | }
|
---|
792 | installed = true;
|
---|
793 | }
|
---|
794 |
|
---|
795 | public void propertyChange(PropertyChangeEvent e) {
|
---|
796 |
|
---|
797 | String name = e.getPropertyName();
|
---|
798 |
|
---|
799 | // If they change the text area's font, we need to update cell
|
---|
800 | // heights to match the font's height.
|
---|
801 | if ("font".equals(name) ||
|
---|
802 | RSyntaxTextArea.SYNTAX_SCHEME_PROPERTY.equals(name)) {
|
---|
803 | for (int i=0; i<getComponentCount(); i++) {
|
---|
804 | AbstractGutterComponent agc =
|
---|
805 | (AbstractGutterComponent)getComponent(i);
|
---|
806 | agc.lineHeightsChanged();
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | // If they toggle whether code folding is enabled...
|
---|
811 | else if (RSyntaxTextArea.CODE_FOLDING_PROPERTY.equals(name)) {
|
---|
812 | boolean foldingEnabled = ((Boolean)e.getNewValue()).
|
---|
813 | booleanValue();
|
---|
814 | if (lineNumberList!=null) { // Its size depends on folding
|
---|
815 | //lineNumberList.revalidate();
|
---|
816 | lineNumberList.updateCellWidths();
|
---|
817 | }
|
---|
818 | setFoldIndicatorEnabled(foldingEnabled);
|
---|
819 | }
|
---|
820 |
|
---|
821 | // If code folds are updated...
|
---|
822 | else if (FoldManager.PROPERTY_FOLDS_UPDATED.equals(name)) {
|
---|
823 | repaint();
|
---|
824 | }
|
---|
825 |
|
---|
826 | }
|
---|
827 |
|
---|
828 | public void removeUpdate(DocumentEvent e) {
|
---|
829 | handleDocumentEvent(e);
|
---|
830 | }
|
---|
831 |
|
---|
832 | public void uninstall() {
|
---|
833 | if (installed) {
|
---|
834 | textArea.removeComponentListener(this);
|
---|
835 | textArea.getDocument().removeDocumentListener(this);
|
---|
836 | if (textArea instanceof RSyntaxTextArea) {
|
---|
837 | RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
|
---|
838 | rsta.removeActiveLineRangeListener(this);
|
---|
839 | rsta.getFoldManager().removePropertyChangeListener(this);
|
---|
840 | }
|
---|
841 | installed = false;
|
---|
842 | }
|
---|
843 | }
|
---|
844 |
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 | } |
---|