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.Graphics;
   23   
   24   import org.apache.harmony.x.swing.Utilities;
   25   
   26   /**
   27    * Represents a basic interval of text. There are three non-abstract
   28    * implementations:
   29    * <ol>
   30    *     <li><code>UnselectedTextInterval</code>,</li>
   31    *     <li><code>SelectedTextInterval</code>,</li>
   32    *     <li><code>ComposedTextInterval</code>.</li>
   33    * </ol>
   34    */
   35   abstract class TextInterval implements Cloneable {
   36   
   37       /**
   38        * Defines how text is actually painted.
   39        */
   40       interface TextIntervalPainter {
   41           /**
   42            * Paints selected range of text.
   43            *
   44            * @param g graphics to paint on.
   45            * @param start the start of the selected range.
   46            * @param end the end of the selected range.
   47            * @param x the <var>x</var> coordinate of the text.
   48            * @param y the <var>y</var> coordinate of the text.
   49            *          It assumed to be the baseline position.
   50            * @return the <var>x</var> coordinate where the next interval is
   51            *         to be painted.
   52            *
   53            * @throws BadLocationException if interval represents invalid
   54            *                              model range.
   55            */
   56           int paintSelected(Graphics g, int start, int end, int x, int y)
   57               throws BadLocationException;
   58   
   59           /**
   60            * Paints unselected range of text.
   61            *
   62            * @param g graphics to paint on.
   63            * @param start the start of the unselected range.
   64            * @param end the end of the unselected range.
   65            * @param x the <var>x</var> coordinate of the text.
   66            * @param y the <var>y</var> coordinate of the text.
   67            *          It assumed to be the baseline position.
   68            * @return the <var>x</var> coordinate where the next interval is
   69            *         to be painted.
   70            *
   71            * @throws BadLocationException if interval represents invalid
   72            *                              model range.
   73            */
   74           int paintUnselected(Graphics g, int start, int end, int x, int y)
   75               throws BadLocationException;
   76   
   77           /**
   78            * Paints composed text.
   79            *
   80            * @param g graphics to paint on.
   81            * @param start the start of the composed text.
   82            * @param end the end of the composed text.
   83            * @param x the <var>x</var> coordinate of the text.
   84            * @param y the <var>y</var> coordinate of the text.
   85            *          It assumed to be the baseline position.
   86            * @return the <var>x</var> coordinate where the next interval is
   87            *         to be painted.
   88            *
   89            * @throws BadLocationException if interval represents invalid
   90            *                              model range.
   91            */
   92           int paintComposed(Graphics g, int start, int end, int x, int y)
   93               throws BadLocationException;
   94       }
   95   
   96       /**
   97        * Start of the interval.
   98        */
   99       protected int start;
  100       /**
  101        * End of the interval.
  102        */
  103       protected int end;
  104       /**
  105        * Painter to do actual text painting.
  106        */
  107       protected final TextIntervalPainter painter;
  108   
  109       /**
  110        * Creates a text interval.
  111        *
  112        * @param start the start of the interval.
  113        * @param end the end of the interval.
  114        * @param painter the painter to use.
  115        */
  116       public TextInterval(final int start, final int end,
  117                           final TextIntervalPainter painter) {
  118           this.start = start;
  119           this.end = end;
  120           this.painter = painter;
  121       }
  122   
  123       /**
  124        * Returns dissection of this interval and <code>another</code>.
  125        *
  126        * @param another the interval to dissect with.
  127        * @return pairwise disjoint intervals.
  128        */
  129       public abstract TextInterval[] dissect(TextInterval another);
  130   
  131       /**
  132        * Returns the string type of the interval.
  133        * It is used in <code>toString</code> method.
  134        *
  135        * @return the string type of the interval.
  136        */
  137       public abstract String getType();
  138   
  139       /**
  140        * Checks whether this interval is empty.
  141        *
  142        * @return <code>true</code> if the interval start is equal to its end;
  143        *         <code>false</code> otherwise.
  144        */
  145       public final boolean isEmpty() {
  146           return start == end;
  147       }
  148   
  149       /**
  150        * Checks whether two intervals intersect.
  151        *
  152        * @param another the interval to check intersection with.
  153        * @return <code>true</code> if this interval intersects
  154        *         with <code>another</code>; <code>false</code> otherwise.
  155        */
  156       public final boolean intersects(final TextInterval another) {
  157           return Utilities.range(start, another.start, another.end)
  158                  != Utilities.range(end, another.start, another.end);
  159       }
  160   
  161       /**
  162        * Paints text which falls in this interval.
  163        *
  164        * @param g graphics to paint on.
  165        * @param x the <var>x</var> coordinate.
  166        * @param y the <var>y</var> coordinate.
  167        *        It assumed to be the baseline of text.
  168        * @return the <var>x</var> coordinate where the next text interval should
  169        *         be painted.
  170        * @throws BadLocationException if interval represents invalid model range.
  171        */
  172       public abstract int paint(final Graphics g, final int x, final int y)
  173           throws BadLocationException;
  174   
  175       /**
  176        * Creates a copy of the interval with new start and end.
  177        *
  178        * @param start the new start of the interval.
  179        * @param end the new end of the interval.
  180        * @return the interval with the new start and end.
  181        */
  182       public final TextInterval create(final int start, final int end) {
  183           TextInterval result;
  184           try {
  185               result = (TextInterval)clone();
  186           } catch (CloneNotSupportedException e) {
  187               e.printStackTrace();
  188               return null;
  189           }
  190           result.start = start;
  191           result.end = end;
  192           return result;
  193       }
  194   
  195       /**
  196        * Converts interval to a string.
  197        * @return the string representation of the interval.
  198        */
  199       public String toString() {
  200           return getType() + "[" + start + ", " + end + "]";
  201       }
  202   
  203       /**
  204        * Dissects <em>selected</em> and <em>composed</em> text intervals.
  205        * <p><i>This method is meant for internal usage only.</i>
  206        *
  207        * @param selected the interval with selected text.
  208        * @param composed the interval with composed text.
  209        * @return pairwise disjoint intervals.
  210        */
  211       static TextInterval[] dissect(final SelectedTextInterval selected,
  212                                     final ComposedTextInterval composed) {
  213           if (selected.isEmpty()) {
  214               return new TextInterval[] {};
  215           }
  216           if (composed.isEmpty()) {
  217               return new TextInterval[] {selected};
  218           }
  219           if (!selected.intersects(composed)) {
  220               return new TextInterval[] {selected};
  221           }
  222   
  223           if (composed.start > selected.start) {
  224               if (composed.end >= selected.end) {
  225                   return new TextInterval[] {
  226                       selected.create(selected.start, composed.start),
  227                       composed
  228                   };
  229               } else {
  230                   return new TextInterval[] {
  231                       selected.create(selected.start, composed.start),
  232                       composed,
  233                       selected.create(composed.end, selected.end),
  234                   };
  235               }
  236           } else {
  237               if (composed.end >= selected.end) {
  238                   return new TextInterval[] {composed};
  239               } else {
  240                   return new TextInterval[] {
  241                       composed,
  242                       selected.create(composed.end, selected.end)
  243                   };
  244               }
  245           }
  246       }
  247   
  248       /**
  249        * Dissects <em>unselected</em> and <em>selected or composed</em>
  250        * text intervals.
  251        * <p><i>This method is meant for internal usage only.</i>
  252        *
  253        * @param ordinary the interval with unselected text.
  254        * @param selectedOrComposed the interval with selected or composed text.
  255        * @return pairwise disjoint intervals.
  256        */
  257       static TextInterval[] dissect(final UnselectedTextInterval ordinary,
  258                                     final TextInterval selectedOrComposed) {
  259           if (selectedOrComposed.isEmpty()) {
  260               return new TextInterval[] {ordinary};
  261           }
  262           if (!ordinary.intersects(selectedOrComposed)) {
  263               return new TextInterval[] {ordinary};
  264           }
  265   
  266           if (ordinary.start >= selectedOrComposed.start) {
  267               if (ordinary.end > selectedOrComposed.end) {
  268                   return new TextInterval[] {
  269                       selectedOrComposed.create(ordinary.start,
  270                                                 selectedOrComposed.end),
  271                       ordinary.create(selectedOrComposed.end, ordinary.end)
  272                   };
  273               } else {
  274                   return new TextInterval[] {
  275                       selectedOrComposed.create(ordinary.start, ordinary.end)
  276                   };
  277               }
  278           } else {
  279               if (ordinary.end > selectedOrComposed.end) {
  280                   return new TextInterval[] {
  281                       ordinary.create(ordinary.start, selectedOrComposed.start),
  282                       selectedOrComposed,
  283                       ordinary.create(selectedOrComposed.end, ordinary.end)
  284                   };
  285               } else {
  286                   return new TextInterval[] {
  287                       ordinary.create(ordinary.start, selectedOrComposed.start),
  288                       selectedOrComposed.create(selectedOrComposed.start,
  289                                                 ordinary.end)
  290                   };
  291               }
  292           }
  293       }
  294   }

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