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.Component;
   23   import java.awt.Container;
   24   import java.awt.Graphics;
   25   import java.awt.Shape;
   26   import javax.swing.event.DocumentEvent;
   27   import javax.swing.text.Position.Bias;
   28   
   29   import org.apache.harmony.awt.ComponentInternals;
   30   import org.apache.harmony.awt.text.RootViewContext;
   31   
   32   
   33   class RootView extends View {
   34       private View son;
   35       private Document document;
   36       private RootViewContext.ViewFactoryGetter viewFactoryGetter;
   37   
   38       /**
   39        * Parent of the view hierarchy. If model changes, only child of root view
   40        * is replaced.
   41        */
   42       RootViewContext rootViewContext = new RootViewContext() {
   43           public View getView() {
   44               return RootView.this;
   45           }
   46   
   47           public void setComponent(final Component c) {
   48               component = c;
   49           }
   50   
   51           public void setDocument(final Document d) {
   52               document = d;
   53           }
   54   
   55           public void setViewFactoryGetter(final ViewFactoryGetter vfg) {
   56               viewFactoryGetter = vfg;
   57           }
   58   
   59       };
   60   
   61       RootView(final Element e) {
   62           super(e);
   63       }
   64   
   65       public void preferenceChanged(final View view, final boolean b1,
   66                                     final boolean b2) {
   67           if (component != null && (b1 || b2)) {
   68               ComponentInternals.getComponentInternals().getTextKit(component)
   69               .revalidate();
   70           }
   71       }
   72   
   73       public Shape getChildAllocation(final int i, final Shape shape) {
   74           return shape;
   75       }
   76   
   77       public int getResizeWeight(final int i) {
   78           int resizeWeight;
   79           readLock();
   80           try {
   81               resizeWeight = son.getResizeWeight(i);
   82           } finally {
   83               readUnlock();
   84           }
   85           return resizeWeight;
   86       }
   87   
   88       public int getNextVisualPositionFrom(final int p, final Bias b,
   89                                            final Shape shape,
   90                                            final int direction,
   91                                            final Bias[] biasRet)
   92               throws BadLocationException {
   93           int position;
   94           readLock();
   95           try {
   96               position =  (son != null)
   97               ? son.getNextVisualPositionFrom(p, b, shape, direction,
   98                                               biasRet) : 0;
   99           } finally {
  100               readUnlock();
  101           }
  102           return position;
  103       }
  104   
  105       public float getMaximumSpan(final int axis) {
  106           //son.getMaximumSpan() sometimes returns very strange things
  107           //So getMaximumSpan(View.Y_AXIS) may be more than
  108           //getMinimumSpan(View.Y_AXIS) (TextComponentDemo, for example).
  109           return Integer.MAX_VALUE;
  110       }
  111   
  112       public float getMinimumSpan(final int axis) {
  113           float minSpan;
  114           readLock();
  115           try {
  116               minSpan = (son != null) ? son.getMinimumSpan(axis) : 10;
  117           } finally {
  118               readUnlock();
  119           }
  120           return minSpan;
  121       }
  122   
  123       public Element getElement() {
  124           return son.getElement();
  125       }
  126   
  127   
  128       public int getEndOffset() {
  129           int result;
  130           readLock();
  131           try {
  132               result = (son != null) ? son.getEndOffset() : 0;
  133           } finally {
  134               readUnlock();
  135           }
  136           return result;
  137       }
  138   
  139       public int getStartOffset() {
  140           int result;
  141           readLock();
  142           try {
  143               result = (son != null) ? son.getStartOffset() : 0;
  144           } finally {
  145               readUnlock();
  146           }
  147           return result;
  148       }
  149   
  150   
  151       public Document getDocument() {
  152           return document;
  153       }
  154   
  155       public ViewFactory getViewFactory() {
  156           return viewFactoryGetter.getViewFactory();
  157       }
  158   
  159       public View getParent() {
  160           return null;
  161       }
  162   
  163       public float getPreferredSpan(final int axis) {
  164           float prefSpan;
  165           readLock();
  166           try {
  167               prefSpan = (son != null) ? son.getPreferredSpan(axis) : 10;
  168           } finally {
  169               readUnlock();
  170           }
  171           return prefSpan;
  172       }
  173   
  174       public void insertUpdate(final DocumentEvent changes, final Shape a,
  175                                final ViewFactory f) {
  176           if (son != null) {
  177               son.insertUpdate(changes, a, f);
  178           }
  179       }
  180   
  181       public Shape modelToView(final int p, final Shape shape,
  182                                final Position.Bias b)
  183               throws BadLocationException {
  184           Shape sh = null;
  185           readLock();
  186           try {
  187               sh = (son != null) ? son.modelToView(p, shape, b) : null;
  188           } finally {
  189               readUnlock();
  190           }
  191           return sh;
  192       }
  193   
  194       public Shape modelToView(final int p0, final Bias b0, final int p1,
  195                                final Bias b1, final Shape shape)
  196               throws BadLocationException {
  197           Shape sh = null;
  198           readLock();
  199           try {
  200               sh = (son != null) ? son.modelToView(p0, b0, p1, b1, shape)
  201                   : null;
  202           } finally {
  203               readUnlock();
  204           }
  205           return sh;
  206       }
  207   
  208       public void paint(final Graphics g, final Shape a) {
  209           if (son != null) {
  210               son.paint(g, a);
  211           }
  212       }
  213   
  214       public void removeUpdate(final DocumentEvent changes, final Shape a,
  215                                final ViewFactory f) {
  216           son.removeUpdate(changes, a, f);
  217       }
  218   
  219       public void changedUpdate(final DocumentEvent changes, final Shape a,
  220                                 final ViewFactory f) {
  221           son.changedUpdate(changes, a, f);
  222       }
  223   
  224       public void setSize(final float width, final float height) {
  225           if (son == null) {
  226               return;
  227           }
  228           readLock();
  229           try {
  230               son.setSize(width, height);
  231           } finally {
  232               readUnlock();
  233           }
  234       }
  235   
  236       public int viewToModel(final float fx, final float fy, final Shape a,
  237                              final Position.Bias[] bias) {
  238           int offset;
  239           readLock();
  240           try {
  241               offset = (son != null) ? son.viewToModel(fx, fy, a, bias) : 0;
  242           } finally {
  243               readUnlock();
  244           }
  245           return offset;
  246       }
  247   
  248       public Container getContainer() {
  249           if (component instanceof Container) {
  250               return (Container)component;
  251           }
  252   
  253           return null;
  254       }
  255   
  256       Component getComponent() {
  257           return component;
  258       }
  259   
  260       public void replace(final int i1, final int i2, final View[] views) {
  261           if (son != null) {
  262               son.setParent(null);
  263           }
  264           son = views[0];
  265           son.setParent(this);
  266       }
  267   
  268       public int getViewCount() {
  269           return 1;
  270       }
  271   
  272       public View getView(final int index) {
  273           return son;
  274       }
  275   
  276       public AttributeSet getAttributes() {
  277           return null;
  278       }
  279   
  280       public String getToolTipText(final float x, final float y,
  281                                    final Shape shape) {
  282           String text = null;
  283           readLock();
  284           try {
  285              text  = (son != null) ? son.getToolTipText(x, y, shape) : null;
  286           } finally {
  287               readUnlock();
  288           }
  289           return text;
  290       }
  291   
  292       private void readLock() {
  293           if (document instanceof AbstractDocument) {
  294               ((AbstractDocument)document).readLock();
  295           }
  296       }
  297   
  298       private void readUnlock() {
  299           if (document instanceof AbstractDocument) {
  300               ((AbstractDocument)document).readUnlock();
  301           }
  302       }
  303   }

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