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 javax.swing.text;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.EventQueue;
025: import java.awt.Point;
026: import java.awt.SystemColor;
027: import java.awt.Toolkit;
028: import java.awt.event.MouseEvent;
029:
030: import javax.swing.text.Position.Bias;
031:
032: import org.apache.harmony.awt.PeriodicTimer;
033: import org.apache.harmony.awt.text.AWTHighlighter;
034: import org.apache.harmony.awt.text.TextCaret;
035:
036: final class AWTCaret extends DefaultCaret implements TextCaret {
037: AWTHighlighter highlighter = new AWTHighlighter();
038:
039: public AWTCaret() {
040: setBlinkRate(getCaretBlinkRate());
041: }
042:
043: public void setMagicCaretPosition(final int pos,
044: final int direction, final Point oldPoint) {
045: super .setMagicCaretPosition(pos, direction, oldPoint);
046: }
047:
048: public AWTHighlighter getHighlighter() {
049: return highlighter;
050: }
051:
052: public Bias getDotBias() {
053: return super .getDotBias();
054: }
055:
056: public void moveDot(final int pos, final Bias b) {
057: super .moveDot(pos, b);
058: }
059:
060: public void setDot(final int pos, final Bias b) {
061: super .setDot(pos, b);
062:
063: }
064:
065: public void setComponent(final Component c) {
066: super .setComponent(c);
067: highlighter.setComponent(c);
068: textKit.addCaretListeners(this );
069: }
070:
071: Object createTimer(final boolean isMagicTimer, final int delay) {
072: return isMagicTimer ? new PeriodicTimer(DEFAULT_MAGIC_DELAY,
073: (Runnable) getMagicAction()) : new PeriodicTimer(
074: getCaretBlinkRate(), (Runnable) getBlinkAction());
075: }
076:
077: void startTimer(final Object timer) {
078: ((PeriodicTimer) timer).start();
079: }
080:
081: void setTimerDelay(final Object timer, final int delay) {
082: }
083:
084: void stopTimer(final Object timer) {
085: ((PeriodicTimer) timer).stop();
086: }
087:
088: Object getMagicAction() {
089: if (magicAction == null) {
090: magicAction = new Runnable() {
091: public void run() {
092: if (magicCaretPosition == null) {
093: magicCaretPosition = new Point(x, y);
094: }
095: }
096: };
097: }
098: return magicAction;
099: }
100:
101: Object getBlinkAction() {
102: if (blinkAction == null) {
103: blinkAction = new Runnable() {
104: public void run() {
105: shouldDraw = !shouldDraw;
106: EventQueue.invokeLater(new Runnable() {
107: public void run() {
108: repaint();
109: }
110: });
111: }
112: };
113: }
114: return blinkAction;
115: }
116:
117: boolean isRestoreSelectionCondition(final Component c) {
118: return false;
119: }
120:
121: private int getCaretBlinkRate() {
122: Object blinkRateObj = Toolkit.getDefaultToolkit()
123: .getDesktopProperty("awt.cursorBlinkRate");
124: return blinkRateObj instanceof Integer ? ((Integer) blinkRateObj)
125: .intValue()
126: : 500;
127: }
128:
129: Color getCaretColor() {
130: return Color.BLACK;
131: }
132:
133: Color getSelectionColor() {
134: return SystemColor.textHighlight;
135: }
136:
137: boolean isComponentEditable() {
138: return true;
139: }
140:
141: boolean isDragEnabled() {
142: return false;
143: }
144:
145: Object addHighlight(final int p0, final int p1) {
146: Object result = null;
147: try {
148: result = highlighter.addHighlight(p0, p1);
149: } catch (BadLocationException e) {
150: }
151: return result;
152: }
153:
154: void changeHighlight(final Object tag, final int p0, final int p1) {
155: try {
156: highlighter.changeHighlight(p0, p1);
157: } catch (final BadLocationException e) {
158: }
159: }
160:
161: void removeHighlight(final Object tag) {
162: highlighter.removeHighlight();
163: }
164:
165: public void mouseClicked(final MouseEvent me) {
166: if (textKit.isScrollBarArea(me.getX(), me.getY())) {
167: return;
168: }
169: super .mouseClicked(me);
170: }
171:
172: public void mouseDragged(final MouseEvent me) {
173: if (textKit.isScrollBarArea(me.getX(), me.getY())) {
174: return;
175: }
176: super .mouseDragged(me);
177: }
178:
179: public void mousePressed(final MouseEvent me) {
180: if (textKit.isScrollBarArea(me.getX(), me.getY())) {
181: return;
182: }
183: super.mousePressed(me);
184: }
185: }
|