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 Alexey A. Ivanov
   19    */
   20   package javax.swing.text;
   21   
   22   import java.awt.Color;
   23   import java.awt.Component;
   24   import java.awt.Font;
   25   import java.awt.FontMetrics;
   26   
   27   import org.apache.harmony.awt.text.ComposedTextParams;
   28   import org.apache.harmony.awt.text.PropertyNames;
   29   import org.apache.harmony.awt.text.TextKit;
   30   
   31   
   32   /**
   33    * Stores parameters to paint text in plain text views.
   34    *
   35    */
   36   final class TextPaintParams {
   37       private static final int DEFAULT_TAB_SIZE = 8;
   38   
   39       private static final float MIN_TAB_SIZE = 1;
   40   
   41       final Segment buffer = new Segment();
   42   
   43       Color color;
   44   
   45       int composedEnd;
   46       int composedStart;
   47       ComposedTextParams composedText;
   48   
   49       FontMetrics metrics;
   50   
   51       Color selColor;
   52       int selEnd;
   53       int selStart;
   54   
   55       float tabSize;
   56   
   57       final View view;
   58   
   59       TextPaintParams(final View view) {
   60           this.view = view;
   61       }
   62   
   63       void conditionalUpdateMetrics() {
   64           if (areMetricsValid()) {
   65               updateMetrics();
   66               view.preferenceChanged(null, true, true);
   67           }
   68       }
   69   
   70       int getTabSize() {
   71           final Document doc = view.getDocument();
   72           final Object value = doc.getProperty(PlainDocument.tabSizeAttribute);
   73           return value != null ? ((Integer)value).intValue()
   74                                : DEFAULT_TAB_SIZE;
   75       }
   76   
   77       boolean areMetricsValid() {
   78           return metrics == null
   79                  || !view.getComponent().getFont().equals(metrics.getFont());
   80       }
   81   
   82       float nextTabStop(final float x) {
   83           conditionalUpdateMetrics();
   84           if (Math.abs(tabSize) <= MIN_TAB_SIZE) {
   85               return x;
   86           }
   87           final int count = (int)x / (int)tabSize;
   88           return (count + 1) * tabSize;
   89       }
   90   
   91       void updateFields() {
   92           final Component component = view.getComponent();
   93           final TextKit textKit = view.getTextKit();
   94           selStart = textKit.getSelectionStart();
   95           selEnd   = textKit.getSelectionEnd();
   96   
   97           composedText = (ComposedTextParams)view.getDocument().getProperty(
   98                   PropertyNames.COMPOSED_TEXT_PROPERTY);
   99   
  100           if (composedText != null) {
  101               composedStart = composedText.getComposedTextStart();
  102               composedEnd = composedStart + composedText.getComposedTextLength();
  103           }
  104   
  105           if (component.isEnabled()) {
  106               color = component.getForeground();
  107               selColor = textKit.getSelectedTextColor();
  108           } else {
  109               color = textKit.getDisabledTextColor();
  110               selColor = color;
  111           }
  112       }
  113   
  114       void updateMetrics() {
  115           final Component component = view.getComponent();
  116           final Font font = component.getFont();
  117           metrics = component.getFontMetrics(font);
  118           tabSize = metrics.charWidth('m') * getTabSize();
  119       }
  120   }

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