Home » openjdk-7 » javax » swing » text » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   /**
   18    * @author Evgeniya G. Maenkova
   19    */
   20   package javax.swing.text;
   21   
   22   import java.awt.Color;
   23   import java.awt.Component;
   24   import java.awt.EventQueue;
   25   import java.awt.Point;
   26   import java.awt.SystemColor;
   27   import java.awt.Toolkit;
   28   import java.awt.event.MouseEvent;
   29   
   30   import javax.swing.text.Position.Bias;
   31   
   32   import org.apache.harmony.awt.PeriodicTimer;
   33   import org.apache.harmony.awt.text.AWTHighlighter;
   34   import org.apache.harmony.awt.text.TextCaret;
   35   
   36   
   37   
   38   
   39   final class AWTCaret extends DefaultCaret implements TextCaret {
   40       AWTHighlighter highlighter = new AWTHighlighter();
   41   
   42       public AWTCaret() {
   43           setBlinkRate(getCaretBlinkRate());
   44       }
   45   
   46       public void setMagicCaretPosition(final int pos, final int direction, final Point oldPoint) {
   47           super.setMagicCaretPosition(pos, direction, oldPoint);
   48       }
   49   
   50       public AWTHighlighter getHighlighter() {
   51           return highlighter;
   52       }
   53   
   54       public Bias getDotBias() {
   55           return super.getDotBias();
   56       }
   57   
   58       public void moveDot(final int pos, final Bias b) {
   59           super.moveDot(pos, b);
   60       }
   61   
   62       public void setDot(final int pos, final Bias b) {
   63           super.setDot(pos, b);
   64   
   65       }
   66   
   67       public void setComponent(final Component c) {
   68           super.setComponent(c);
   69           highlighter.setComponent(c);
   70           textKit.addCaretListeners(this);
   71       }
   72   
   73       Object createTimer(final boolean isMagicTimer, final int delay) {
   74           return isMagicTimer ? new PeriodicTimer(DEFAULT_MAGIC_DELAY,
   75                                                   (Runnable)getMagicAction())
   76               :  new PeriodicTimer(getCaretBlinkRate(),
   77                                    (Runnable)getBlinkAction());
   78       }
   79   
   80       void startTimer(final Object timer) {
   81            ((PeriodicTimer)timer).start();
   82       }
   83   
   84       void setTimerDelay(final Object timer, final int delay) {
   85       }
   86   
   87       void stopTimer(final Object timer) {
   88           ((PeriodicTimer)timer).stop();
   89       }
   90   
   91       Object getMagicAction() {
   92           if (magicAction == null) {
   93               magicAction = new Runnable() {
   94                   public void run() {
   95                       if (magicCaretPosition == null) {
   96                            magicCaretPosition = new Point(x, y);
   97                       }
   98                   }
   99               };
  100           }
  101           return magicAction;
  102       }
  103   
  104       Object getBlinkAction() {
  105           if (blinkAction == null) {
  106               blinkAction = new Runnable() {
  107                   public void run() {
  108                       shouldDraw = !shouldDraw;
  109                       EventQueue.invokeLater(new Runnable() {
  110                           public void run() {
  111                               repaint();
  112                           }
  113                       });
  114                   }
  115               };
  116          }
  117          return blinkAction;
  118       }
  119   
  120       boolean isRestoreSelectionCondition(final Component c) {
  121           return false;
  122       }
  123   
  124       private int getCaretBlinkRate() {
  125           Object blinkRateObj = Toolkit.getDefaultToolkit()
  126               .getDesktopProperty("awt.cursorBlinkRate");
  127           return blinkRateObj instanceof Integer
  128              ? ((Integer)blinkRateObj).intValue() : 500;
  129       }
  130   
  131       Color getCaretColor() {
  132           return Color.BLACK;
  133       }
  134   
  135       Color getSelectionColor() {
  136           return SystemColor.textHighlight;
  137       }
  138   
  139       boolean isComponentEditable() {
  140           return true;
  141       }
  142   
  143       boolean isDragEnabled() {
  144           return false;
  145       }
  146   
  147       Object addHighlight(final int p0, final int p1) {
  148           Object result = null;
  149           try {
  150              result = highlighter.addHighlight(p0, p1);
  151           } catch (BadLocationException e) {
  152           }
  153           return result;
  154       }
  155   
  156       void changeHighlight(final Object tag, final int p0, final int p1) {
  157           try {
  158                highlighter.changeHighlight(p0, p1);
  159           } catch (final BadLocationException e) {
  160           }
  161       }
  162   
  163       void removeHighlight(final Object tag) {
  164           highlighter.removeHighlight();
  165       }
  166   
  167       public void mouseClicked(final MouseEvent me) {
  168           if (textKit.isScrollBarArea(me.getX(), me.getY())) {
  169               return;
  170           }
  171           super.mouseClicked(me);
  172       }
  173   
  174       public void mouseDragged(final MouseEvent me) {
  175           if (textKit.isScrollBarArea(me.getX(), me.getY())) {
  176               return;
  177           }
  178           super.mouseDragged(me);
  179       }
  180   
  181       public void mousePressed(final MouseEvent me) {
  182           if (textKit.isScrollBarArea(me.getX(), me.getY())) {
  183               return;
  184           }
  185           super.mousePressed(me);
  186       }
  187   }
  188   

Home » openjdk-7 » javax » swing » text » [javadoc | source]