ScrollTextArea.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21: package xeij;
22:
23: import java.awt.*;
24: import java.awt.event.*;
25: import java.awt.geom.*;
26: import java.awt.image.*;
27: import javax.swing.*;
28: import javax.swing.event.*;
29: import javax.swing.text.*;
30:
31: public class ScrollTextArea extends JScrollPane {
32:
33: private boolean opaqueOn;
34: private boolean gridOn;
35: private boolean highlightCursorOn;
36: private boolean underlineCursorOn;
37:
38: private Color foregroundColor;
39: private Color backgroundColor;
40: private Color rowGridColor;
41: private Color columnGridColor;
42: private Color tabColumnGridColor;
43: private Color highlightAreaColor;
44: private Color highlightCursorColor;
45: private Color underlineCursorColor;
46:
47: private Paint gridPaint;
48: private int highlightAreaStart;
49: private int highlightAreaEnd;
50: private Stroke underlineCursorStroke;
51:
52: private JTextArea textArea;
53:
54: private int fontWidth;
55: private int fontHeight;
56: private int marginTop;
57: private int marginLeft;
58:
59: private DefaultCaret caret;
60:
61:
62:
63: @SuppressWarnings ("this-escape") public ScrollTextArea () {
64: super ();
65:
66: opaqueOn = true;
67: gridOn = true;
68: highlightCursorOn = false;
69: underlineCursorOn = false;
70:
71: foregroundColor = new Color (LnF.lnfRGB[14]);
72: backgroundColor = new Color (LnF.lnfRGB[0]);
73: rowGridColor = new Color (LnF.lnfRGB[2]);
74: columnGridColor = new Color (LnF.lnfRGB[1]);
75: tabColumnGridColor = new Color (LnF.lnfRGB[2]);
76: highlightAreaColor = new Color (LnF.lnfRGB[3]);
77: highlightCursorColor = new Color (LnF.lnfRGB[6]);
78: underlineCursorColor = new Color (LnF.lnfRGB[10]);
79:
80: gridPaint = backgroundColor;
81: highlightAreaStart = highlightAreaEnd = -1;
82: underlineCursorStroke = new BasicStroke (1.0F,
83: BasicStroke.CAP_SQUARE,
84: BasicStroke.JOIN_MITER,
85: 10.0F,
86: new float[] { 0.0F, 2.0F },
87: 0.0F);
88:
89: textArea = new JTextArea () {
90:
91: @Override public void paintComponent (Graphics g) {
92: Graphics2D g2 = (Graphics2D) g;
93: int width = getWidth ();
94: int height = getHeight ();
95:
96: if (opaqueOn) {
97: g2.setPaint (gridOn ? gridPaint : backgroundColor);
98: g2.fillRect (0, 0, width, height);
99: }
100: paintAfterBackground (this, g2);
101:
102: if (0 <= highlightAreaEnd) {
103: try {
104:
105: Rectangle startRect = modelToView2D (highlightAreaStart).getBounds ();
106:
107: Rectangle endRect = modelToView2D (highlightAreaEnd).getBounds ();
108: Insets margin = getMargin ();
109: g2.setPaint (highlightAreaColor);
110: g2.fillRect (margin.left, startRect.y,
111: width - margin.left - margin.right, endRect.y - startRect.y + endRect.height);
112: } catch (BadLocationException ble) {
113: }
114: }
115:
116: if (highlightCursorOn || underlineCursorOn) {
117: try {
118:
119: Rectangle caretRect = modelToView2D (getCaretPosition ()).getBounds ();
120: Insets margin = getMargin ();
121: if (highlightCursorOn) {
122: g2.setPaint (highlightCursorColor);
123: g2.fillRect (margin.left, caretRect.y,
124: width - margin.left - margin.right, caretRect.height);
125: }
126: if (underlineCursorOn) {
127: g2.setPaint (underlineCursorColor);
128: g2.setStroke (underlineCursorStroke);
129: g2.drawLine (margin.left, caretRect.y + caretRect.height - 1,
130: width - margin.right - 1, caretRect.y + caretRect.height - 1);
131: }
132: } catch (BadLocationException ble) {
133: }
134: }
135:
136: super.paintComponent (g);
137: paintAfterText (this, g2);
138: }
139: };
140: textArea.setOpaque (false);
141: super.setOpaque (false);
142:
143: textArea.setForeground (foregroundColor);
144: textArea.setSelectionColor (new Color (LnF.lnfRGB[7]));
145:
146:
147: Font font = textArea.getFont ();
148: fontWidth = font.getSize () + 1 >> 1;
149: fontHeight = textArea.getFontMetrics (font).getHeight ();
150: Insets margin = textArea.getMargin ();
151: marginTop = margin.top;
152: marginLeft = margin.left;
153:
154: createGrid ();
155:
156: caret = new DefaultCaret () {
157: @Override protected void damage (Rectangle r) {
158: if (r != null) {
159: if (highlightCursorOn || underlineCursorOn) {
160: x = 0;
161: y = r.y;
162: width = textArea.getWidth ();
163: height = r.height;
164: textArea.repaint ();
165: } else {
166: super.damage (r);
167: }
168: }
169: }
170: };
171: caret.setBlinkRate (500);
172: textArea.setCaret (caret);
173:
174: getViewport ().setView (textArea);
175: }
176:
177:
178:
179: public void paintAfterBackground (JTextArea textArea, Graphics2D g2) {
180: }
181:
182:
183:
184: public void paintAfterText (JTextArea textArea, Graphics2D g2) {
185: }
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203: private void createGrid () {
204: int backgroundRGB = backgroundColor.getRGB ();
205: int rowGridRGB = rowGridColor.getRGB ();
206: int columnGridRGB = columnGridColor.getRGB ();
207: int tabColumnGridRGB = tabColumnGridColor.getRGB ();
208: int imageWidth = fontWidth * 8;
209: BufferedImage image = new BufferedImage (imageWidth, fontHeight, BufferedImage.TYPE_INT_RGB);
210: int[] bitmap = ((DataBufferInt) image.getRaster ().getDataBuffer ()).getData ();
211: for (int y = 0; y < fontHeight; y++) {
212: for (int x = 0; x < imageWidth; x++) {
213: bitmap[(marginLeft + x) % imageWidth + imageWidth * ((marginTop + y) % fontHeight)] =
214: (((y ^ fontHeight) & 1) == 0 ? backgroundRGB :
215: y == fontHeight - 1 ? ((x % fontWidth ^ fontWidth) & 1) != 0 ? rowGridRGB : backgroundRGB :
216: x == imageWidth - 1 ? tabColumnGridRGB : x % fontWidth == fontWidth - 1 ? columnGridRGB : backgroundRGB);
217: }
218: }
219: gridPaint = new TexturePaint (image, new Rectangle (0, 0, imageWidth, fontHeight));
220: }
221:
222:
223: @Override public void setMaximumSize (Dimension size) {
224: super.setMaximumSize (size);
225: getViewport ().setMaximumSize (size);
226: }
227: @Override public void setMinimumSize (Dimension size) {
228: super.setMinimumSize (size);
229: getViewport ().setMinimumSize (size);
230: }
231: @Override public void setPreferredSize (Dimension size) {
232: super.setPreferredSize (size);
233: getViewport ().setPreferredSize (size);
234: }
235:
236:
237: public Insets getMargin () {
238: return textArea.getMargin ();
239: }
240: public void setMargin (Insets m) {
241: textArea.setMargin (m);
242: int t = m.top;
243: int l = m.left;
244: if (marginTop != t || marginLeft != l) {
245: marginTop = t;
246: marginLeft = l;
247: createGrid ();
248: }
249: }
250:
251:
252: @Override public boolean isOpaque () {
253: return opaqueOn;
254: }
255: @Override public void setOpaque (boolean opaque) {
256: opaqueOn = opaque;
257: }
258:
259:
260: @Override public Color getForeground () {
261: return foregroundColor;
262: }
263: @Override public void setForeground (Color color) {
264: foregroundColor = color;
265: if (textArea != null) {
266: textArea.setForeground (color);
267: }
268: }
269: @Override public Color getBackground () {
270: return backgroundColor;
271: }
272: @Override public void setBackground (Color color) {
273: backgroundColor = color;
274: if (textArea != null) {
275: createGrid ();
276: }
277: }
278:
279:
280: @Override public Font getFont () {
281: if (textArea != null) {
282: return textArea.getFont ();
283: }
284: return super.getFont ();
285: }
286: @Override public void setFont (Font font) {
287: if (textArea != null) {
288: textArea.setFont (font);
289: int w = font.getSize () + 1 >> 1;
290: int h = textArea.getFontMetrics (font).getHeight ();
291: if (fontWidth != w || fontHeight != h) {
292: fontWidth = w;
293: fontHeight = h;
294: createGrid ();
295: }
296: }
297: }
298:
299:
300: public void addCaretListener (CaretListener listener) {
301: textArea.addCaretListener (listener);
302: }
303: public void removeCaretListener (CaretListener listener) {
304: textArea.removeCaretListener (listener);
305: }
306: public void addDocumentListener (DocumentListener listener) {
307: textArea.getDocument ().addDocumentListener (listener);
308: }
309: public void removeDocumentListener (DocumentListener listener) {
310: textArea.getDocument ().removeDocumentListener (listener);
311: }
312: @Override public void addKeyListener (KeyListener listener) {
313: textArea.addKeyListener (listener);
314: }
315: @Override public void removeKeyListener (KeyListener listener) {
316: textArea.removeKeyListener (listener);
317: }
318: @Override public void addMouseListener (MouseListener listener) {
319: textArea.addMouseListener (listener);
320: }
321: @Override public void removeMouseListener (MouseListener listener) {
322: textArea.removeMouseListener (listener);
323: }
324: @Override public void addFocusListener (FocusListener listener) {
325: textArea.addFocusListener (listener);
326: }
327: @Override public void removeFocusListener (FocusListener listener) {
328: textArea.removeFocusListener (listener);
329: }
330:
331:
332: public boolean getLineWrap () {
333: return textArea.getLineWrap ();
334: }
335: public void setLineWrap (boolean wrap) {
336: textArea.setLineWrap (wrap);
337: }
338:
339:
340: public Color getCaretColor () {
341: return textArea.getCaretColor ();
342: }
343: public void setCaretColor (Color color) {
344: textArea.setCaretColor (color);
345: }
346: public int getCaretPosition () {
347: return textArea.getCaretPosition ();
348: }
349: public void setCaretPosition (int pos) {
350: textArea.setCaretPosition (pos);
351: }
352: public boolean isCaretVisible () {
353: return caret.isVisible ();
354: }
355: public void setCaretVisible (boolean visible) {
356: caret.setVisible (visible);
357: }
358:
359:
360: public JTextArea getTextArea () {
361: return textArea;
362: }
363: public String getText () {
364: return textArea.getText ();
365: }
366: public void setText (String text) {
367: highlightAreaEnd = -1;
368: textArea.setText (text);
369: }
370:
371:
372: public void append (String text) {
373: highlightAreaEnd = -1;
374: textArea.append (text);
375: }
376: public void insert (String text, int pos) {
377: highlightAreaEnd = -1;
378: textArea.insert (text, pos);
379: }
380: public void replaceRange (String text, int start, int end) {
381: highlightAreaEnd = -1;
382: textArea.replaceRange (text, start, end);
383: }
384:
385:
386: public void selectAll () {
387: textArea.selectAll ();
388: }
389: public String getSelectedText () {
390: return textArea.getSelectedText ();
391: }
392: public int getSelectionStart () {
393: return textArea.getSelectionStart ();
394: }
395: public int getSelectionEnd () {
396: return textArea.getSelectionEnd ();
397: }
398:
399:
400: public boolean isEditable () {
401: return textArea.isEditable ();
402: }
403: public void setEditable (boolean editable) {
404: textArea.setEditable (editable);
405: }
406:
407:
408: public boolean isGridOn () {
409: return gridOn;
410: }
411: public void setGridOn (boolean on) {
412: gridOn = on;
413: }
414: public Color getRowGridColor () {
415: return rowGridColor;
416: }
417: public void setRowGridColor (Color color) {
418: rowGridColor = color;
419: createGrid ();
420: }
421: public Color getColumnGridColor () {
422: return columnGridColor;
423: }
424: public void setColumnGridColor (Color color) {
425: columnGridColor = color;
426: createGrid ();
427: }
428: public Color getTabColumnGridColor () {
429: return tabColumnGridColor;
430: }
431: public void setTabColumnGridColor (Color color) {
432: tabColumnGridColor = color;
433: createGrid ();
434: }
435:
436:
437: public void setHighlightArea (int start, int end) {
438: highlightAreaStart = start;
439: highlightAreaEnd = end;
440: }
441:
442:
443: public boolean isHighlightCursorOn () {
444: return highlightCursorOn;
445: }
446: public void setHighlightCursorOn (boolean on) {
447: highlightCursorOn = on;
448: }
449: public Color getHighlightCursorColor () {
450: return highlightCursorColor;
451: }
452: public void setHighlightCursorColor (Color color) {
453: highlightCursorColor = color;
454: }
455:
456:
457: public boolean isUnderlineCursorOn () {
458: return underlineCursorOn;
459: }
460: public void setUnderlineCursorOn (boolean on) {
461: underlineCursorOn = on;
462: }
463: public Color getUnderlineCursorColor () {
464: return underlineCursorColor;
465: }
466: public void setUnderlineCursorColor (Color color) {
467: underlineCursorColor = color;
468: }
469:
470: }
471:
472:
473: