001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package org.apache.harmony.awt.text;
021:
022: import java.awt.Component;
023: import java.awt.Graphics;
024: import java.awt.Rectangle;
025: import java.awt.Shape;
026: import java.awt.SystemColor;
027:
028: import javax.swing.text.BadLocationException;
029: import javax.swing.text.Document;
030: import javax.swing.text.Position;
031: import javax.swing.text.View;
032:
033: import org.apache.harmony.awt.internal.nls.Messages;
034:
035: /**
036: *
037: * That's a simple highlighter for a text component.
038: */
039: public class AWTHighlighter {
040: private Component component;
041: private TextKit textKit;
042: private Position start;
043: private Position end;
044: private Document document;
045:
046: public void setComponent(final Component component) {
047: this .component = component;
048: textKit = TextUtils.getTextKit(component);
049: document = textKit.getDocument();
050: }
051:
052: public Object addHighlight(final int p0, final int p1)
053: throws BadLocationException {
054: if (p0 < 0 || p1 < p0 || p1 > getDocumentLength()) {
055: // awt.29=Invalid range
056: throw new BadLocationException(
057: Messages.getString("awt.29"), 0); //$NON-NLS-1$
058: }
059: start = document.createPosition(p0);
060: end = document.createPosition(p1);
061: repaintComponent(TextUtils.getBoundsByOffsets(textKit, p0, p1));
062: return Boolean.TRUE;
063: }
064:
065: public void changeHighlight(final int p0, final int p1)
066: throws BadLocationException {
067: if (p0 < 0 || p1 < p0 || p1 > getDocumentLength()) {
068: // awt.29=Invalid range
069: throw new BadLocationException(
070: Messages.getString("awt.29"), 0); //$NON-NLS-1$
071: }
072: int oldStart = getStartOffset();
073: int oldEnd = getEndOffset();
074: start = document.createPosition(p0);
075: end = document.createPosition(p1);
076: evaluateBounds(oldStart, oldEnd, p0, p1);
077: }
078:
079: public void removeHighlight() {
080: repaintComponent(TextUtils.getBoundsByOffsets(textKit,
081: getStartOffset(), getEndOffset()));
082: start = null;
083: end = null;
084: }
085:
086: public void paintLayeredHighlights(final Graphics g, final int p0,
087: final int p1, final Shape viewBounds, final View view) {
088: if (start == null || end == null) {
089: return;
090: }
091: int startOffset = getStartOffset();
092: int endOffset = getEndOffset();
093:
094: if (endOffset > getDocumentLength() || startOffset > p1
095: || endOffset < p0) {
096: return;
097: }
098: TextUtils.paintLayer(g, Math.max(p0, startOffset), Math.min(p1,
099: endOffset), viewBounds, SystemColor.textHighlight,
100: view, true);
101: }
102:
103: private int getDocumentLength() {
104: return textKit.getDocument().getLength();
105: }
106:
107: private int getStartOffset() {
108: return start.getOffset();
109: }
110:
111: private int getEndOffset() {
112: return end.getOffset();
113: }
114:
115: private void repaintComponent(final Rectangle r) {
116: if (r != null) {
117: component.repaint(0, r.x, r.y, r.width, r.height);
118: }
119: }
120:
121: private void evaluateBounds(final int oldStart, final int oldEnd,
122: final int newStart, final int newEnd) {
123: if (oldEnd < newStart || oldStart > oldEnd) {
124: repaintComponent(oldStart, oldEnd, newStart, newEnd);
125: } else {
126: repaintComponent(Math.min(oldStart, newStart), Math.max(
127: oldStart, newStart), Math.min(oldEnd, newEnd), Math
128: .max(oldEnd, newEnd));
129: }
130: }
131:
132: private void repaintComponent(final int p0, final int p1,
133: final int p2, final int p3) {
134: repaintComponent(TextUtils.getBoundsByOffsets(textKit, p0, p1));
135: repaintComponent(TextUtils.getBoundsByOffsets(textKit, p2, p3));
136: }
137: }
|