source: other-projects/rsyntax-textarea/src/java/org/fife/ui/rtextarea/RTextScrollPane.java@ 25584

Last change on this file since 25584 was 25584, checked in by davidb, 12 years ago

Initial cut an a text edit area for GLI that supports color syntax highlighting

File size: 6.3 KB
Line 
1/*
2 * 11/14/2003
3 *
4 * RTextScrollPane.java - A JScrollPane that will only accept RTextAreas
5 * so that it can display line numbers, fold indicators, and icons.
6 *
7 * This library is distributed under a modified BSD license. See the included
8 * RSyntaxTextArea.License.txt file for details.
9 */
10package org.fife.ui.rtextarea;
11
12import java.awt.Color;
13import java.awt.Component;
14import java.awt.Font;
15import javax.swing.JScrollPane;
16
17
18/**
19 * An extension of <code>javax.swing.JScrollPane</code> that will only take
20 * <code>RTextArea</code>s for its view. This class has the ability to show:
21 * <ul>
22 * <li>Line numbers
23 * <li>Per-line icons (for bookmarks, debugging breakpoints, error markers, etc.)
24 * <li>+/- icons to denote code folding regions.
25 * </ul>
26 *
27 * The actual "meat" of these extras is contained in the {@link Gutter} class.
28 * Each <code>RTextScrollPane</code> has a <code>Gutter</code> instance that
29 * it uses as its row header. The gutter is only made visible when one of its
30 * features is being used (line numbering, folding, and/or icons).
31 *
32 * @author Robert Futrell
33 * @version 0.9
34 */
35public class RTextScrollPane extends JScrollPane {
36
37 private RTextArea textArea;
38 private Gutter gutter;
39
40
41 /**
42 * Constructor. If you use this constructor, you must call
43 * {@link #setViewportView(Component)} and pass in an {@link RTextArea}
44 * for this scroll pane to render line numbers properly.
45 */
46 public RTextScrollPane() {
47 this(null, true);
48 }
49
50
51 /**
52 * Creates a scroll pane. A default value will be used for line number
53 * color (gray), and the current line's line number will be highlighted.
54 *
55 * @param textArea The text area this scroll pane will contain.
56 */
57 public RTextScrollPane(RTextArea textArea) {
58 this(textArea, true);
59 }
60
61
62 /**
63 * Creates a scroll pane. A default value will be used for line number
64 * color (gray), and the current line's line number will be highlighted.
65 *
66 * @param textArea The text area this scroll pane will contain. If this is
67 * <code>null</code>, you must call
68 * {@link #setViewportView(Component)}, passing in an
69 * {@link RTextArea}.
70 * @param lineNumbers Whether line numbers should be enabled.
71 */
72 public RTextScrollPane(RTextArea textArea, boolean lineNumbers) {
73 this(textArea, lineNumbers, Color.GRAY);
74 }
75
76
77 /**
78 * Creates a scroll pane with preferred size (width, height).
79 *
80 * @param area The text area this scroll pane will contain. If this is
81 * <code>null</code>, you must call
82 * {@link #setViewportView(Component)}, passing in an
83 * {@link RTextArea}.
84 * @param lineNumbers Whether line numbers are initially enabled.
85 * @param lineNumberColor The color to use for line numbers.
86 */
87 public RTextScrollPane(RTextArea area, boolean lineNumbers,
88 Color lineNumberColor) {
89
90 super(area);
91
92 // Create the text area and set it inside this scroll bar area.
93 textArea = area;
94
95 // Create the gutter for this document.
96 Font defaultFont = new Font("Monospaced", Font.PLAIN, 12);
97 gutter = new Gutter(textArea);
98 gutter.setLineNumberFont(defaultFont);
99 gutter.setLineNumberColor(lineNumberColor);
100 setLineNumbersEnabled(lineNumbers);
101
102 // Set miscellaneous properties.
103 setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
104 setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
105
106 }
107
108
109 /**
110 * Ensures the gutter is visible if it's showing anything.
111 */
112 private void checkGutterVisibility() {
113 int count = gutter.getComponentCount();
114 if (count==0) {
115 if (getRowHeader()!=null && getRowHeader().getView()==gutter) {
116 setRowHeaderView(null);
117 }
118 }
119 else {
120 if (getRowHeader()==null || getRowHeader().getView()==null) {
121 setRowHeaderView(gutter);
122 }
123 }
124 }
125
126
127 /**
128 * Returns the gutter.
129 *
130 * @return The gutter.
131 */
132 public Gutter getGutter() {
133 return gutter;
134 }
135
136
137 /**
138 * Returns <code>true</code> if the line numbers are enabled and visible.
139 *
140 * @return Whether or not line numbers are visible.
141 * @see #setLineNumbersEnabled(boolean)
142 */
143 public boolean getLineNumbersEnabled() {
144 return gutter.getLineNumbersEnabled();
145 }
146
147
148 /**
149 * Returns the text area being displayed.
150 *
151 * @return The text area.
152 * @see #setViewportView(Component)
153 */
154 public RTextArea getTextArea() {
155 return (RTextArea)getViewport().getView();
156 }
157
158
159 /**
160 * Returns whether the fold indicator is enabled.
161 *
162 * @return Whether the fold indicator is enabled.
163 * @see #setFoldIndicatorEnabled(boolean)
164 */
165 public boolean isFoldIndicatorEnabled() {
166 return gutter.isFoldIndicatorEnabled();
167 }
168
169
170 /**
171 * Returns whether the icon row header is enabled.
172 *
173 * @return Whether the icon row header is enabled.
174 * @see #setIconRowHeaderEnabled(boolean)
175 */
176 public boolean isIconRowHeaderEnabled() {
177 return gutter.isIconRowHeaderEnabled();
178 }
179
180
181 /**
182 * Toggles whether the fold indicator is enabled.
183 *
184 * @param enabled Whether the fold indicator should be enabled.
185 * @see #isFoldIndicatorEnabled()
186 */
187 public void setFoldIndicatorEnabled(boolean enabled) {
188 gutter.setFoldIndicatorEnabled(enabled);
189 checkGutterVisibility();
190 }
191
192
193 /**
194 * Toggles whether the icon row header (used for breakpoints, bookmarks,
195 * etc.) is enabled.
196 *
197 * @param enabled Whether the icon row header is enabled.
198 * @see #isIconRowHeaderEnabled()
199 */
200 public void setIconRowHeaderEnabled(boolean enabled) {
201 gutter.setIconRowHeaderEnabled(enabled);
202 checkGutterVisibility();
203 }
204
205
206 /**
207 * Toggles whether or not line numbers are visible.
208 *
209 * @param enabled Whether or not line numbers should be visible.
210 * @see #getLineNumbersEnabled()
211 */
212 public void setLineNumbersEnabled(boolean enabled) {
213 gutter.setLineNumbersEnabled(enabled);
214 checkGutterVisibility();
215 }
216
217
218 /**
219 * Sets the view for this scroll pane. This must be an {@link RTextArea}.
220 *
221 * @param view The new view.
222 * @see #getTextArea()
223 */
224 public void setViewportView(Component view) {
225 if (!(view instanceof RTextArea)) {
226 throw new IllegalArgumentException("view must be an RTextArea");
227 }
228 super.setViewportView(view);
229 textArea = (RTextArea)view;
230 if (gutter!=null) {
231 gutter.setTextArea(textArea);
232 }
233 }
234
235
236}
Note: See TracBrowser for help on using the repository browser.