View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  package javax.swing.text;
26  
27  /**
28   * Represents a location within a document.  It is intended to abstract away
29   * implementation details of the document and enable specification of
30   * positions within the document that are capable of tracking of change as
31   * the document is edited.
32   * <p>
33   * A {@code Position} object points at a location between two characters.
34   * As the surrounding content is altered, the {@code Position} object
35   * adjusts its offset automatically to reflect the changes. If content is
36   * inserted or removed before the {@code Position} object's location, then the
37   * {@code Position} increments or decrements its offset, respectively,
38   * so as to point to the same location. If a portion of the document is removed
39   * that contains a {@code Position}'s offset, then the {@code Position}'s
40   * offset becomes that of the beginning of the removed region. For example, if
41   * a {@code Position} has an offset of 5 and the region 2-10 is removed, then
42   * the {@code Position}'s offset becomes 2.
43   * <p>
44   * {@code Position} with an offset of 0 is a special case. It never changes its
45   * offset while document content is altered.
46   *
47   * @author  Timothy Prinzing
48   */
49  public interface Position {
50  
51      /**
52       * Fetches the current offset within the document.
53       *
54       * @return the offset &gt;= 0
55       */
56      public int getOffset();
57  
58      /**
59       * A typesafe enumeration to indicate bias to a position
60       * in the model.  A position indicates a location between
61       * two characters.  The bias can be used to indicate an
62       * interest toward one of the two sides of the position
63       * in boundary conditions where a simple offset is
64       * ambiguous.
65       */
66      public static final class Bias {
67  
68          /**
69           * Indicates to bias toward the next character
70           * in the model.
71           */
72          public static final Bias Forward = new Bias("Forward");
73  
74          /**
75           * Indicates a bias toward the previous character
76           * in the model.
77           */
78          public static final Bias Backward = new Bias("Backward");
79  
80          /**
81           * string representation
82           */
83          public String toString() {
84              return name;
85          }
86  
87          private Bias(String name) {
88              this.name = name;
89          }
90  
91          private String name;
92      }
93  }