001: package org.underworldlabs.swing.plaf.base;
002:
003: import java.awt.Rectangle;
004: import java.awt.Shape;
005:
006: import javax.swing.JComponent;
007: import javax.swing.UIManager;
008: import javax.swing.plaf.ComponentUI;
009: import javax.swing.plaf.basic.BasicTextAreaUI;
010: import javax.swing.text.AbstractDocument;
011: import javax.swing.text.BadLocationException;
012: import javax.swing.text.DefaultHighlighter;
013: import javax.swing.text.Document;
014: import javax.swing.text.Highlighter;
015: import javax.swing.text.JTextComponent;
016: import javax.swing.text.Position;
017: import javax.swing.text.View;
018:
019: /*
020: * CustomTextAreaUI.java
021: *
022: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
023: *
024: * This program is free software; you can redistribute it and/or
025: * modify it under the terms of the GNU General Public License
026: * as published by the Free Software Foundation; either version 2
027: * of the License, or any later version.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
032: * GNU General Public License for more details.
033: *
034: * You should have received a copy of the GNU General Public License
035: * along with this program; if not, write to the Free Software
036: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
037: *
038: */
039:
040: /**
041: * An alternative UI delegate for JTextArea that paints the
042: * selection highlight correctly. For example, when a selection
043: * starts on one line and ends on the next, the highlight on the
044: * first line will extend all the way to the right margin.
045: *
046: * @author Alan Moore
047: */
048: /**
049: *
050: * @author Takis Diakoumis
051: * @version $Revision: 1.4 $
052: * @date $Date: 2006/05/14 06:56:07 $
053: */
054: public class CustomTextAreaUI extends BasicTextAreaUI {
055: /**
056: * Informs the UIManager that this class should be used as the UI
057: * delegate for JTextArea's. This method should be called during
058: * app initialization, before any components are created.
059: */
060: public static void initialize() {
061: String key = "TextAreaUI";
062: Class cls = CustomTextAreaUI.class;
063: String name = cls.getName();
064: UIManager.put(key, name);
065: UIManager.put(name, cls);
066: }
067:
068: /**
069: * Creates a UI for a JTextArea.
070: *
071: * @param c a text area
072: * @return a CustomTextAreaUI instance
073: */
074: public static ComponentUI createUI(JComponent c) {
075: return new CustomTextAreaUI();
076: }
077:
078: /**
079: * Creates the object to use for adding highlights. This will
080: * be a non-layered version of DefaultHighlighter, so that
081: * multi-line selections will be painted properly.
082: *
083: * @return the highlighter
084: */
085: protected Highlighter createHighlighter() {
086: DefaultHighlighter h = new DefaultHighlighter();
087: h.setDrawsLayeredHighlights(false);
088: return h;
089: }
090:
091: /**
092: * Causes the portion of the view responsible for the given part
093: * of the model to be repainted. This is overridden to repaint the
094: * whole width of the textarea, so that selection highlighting will
095: * be rendered properly.
096: *
097: * @param p0 the beginning of the range >= 0
098: * @param p1 the end of the range >= p0
099: */
100: public void damageRange(JTextComponent t, int p0, int p1,
101: Position.Bias p0Bias, Position.Bias p1Bias) {
102: View rv = getRootView(t);
103: Rectangle alloc = getVisibleEditorRect();
104: Document doc = t.getDocument();
105: if (rv != null && alloc != null && doc != null) {
106: if (doc instanceof AbstractDocument) {
107: ((AbstractDocument) doc).readLock();
108: }
109: try {
110: rv.setSize(alloc.width, alloc.height);
111: Shape toDamage = rv.modelToView(p0, p0Bias, p1, p1Bias,
112: alloc);
113: Rectangle rect = (toDamage instanceof Rectangle) ? (Rectangle) toDamage
114: : toDamage.getBounds();
115: t.repaint(alloc.x, rect.y, alloc.width, rect.height);
116: } catch (BadLocationException ex) {
117: } finally {
118: if (doc instanceof AbstractDocument) {
119: ((AbstractDocument) doc).readUnlock();
120: }
121: }
122: }
123: }
124: }
|