View Javadoc

1   /*
2    * $Id: DisplaceableRegion.java,v 1.10 2005/06/01 17:38:38 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9   package com.bbn.swede.core.dom;
10  
11  import org.eclipse.jface.text.IRegion;
12  
13  /***
14   * A region that can adjust its offset or length to accomodate a character 
15   * displacement.  Otherwise, this class behaves exactly like the standard
16   * implementation of org.eclipse.jface.text.Region.
17   * @author jlerner
18   */
19  public class DisplaceableRegion implements IRegion
20  {
21     private int _offset, _length;
22     private boolean _bMoveIfEqual;
23     
24     /***
25      * <p>Creates a DisplaceableRegion with an initial offset and length.</p>
26      * 
27      * <p>This is a convenience method, fully equivalent to:
28      * <blockquote>DisplaceableRegion(offset, length, false)</blockquote></p>
29      * @param offset Initial offset
30      * @param length Initial length
31      */
32     public DisplaceableRegion(int offset, int length)
33     {
34        _offset = offset;
35        _length = length;
36        _bMoveIfEqual = false;
37     }
38     
39     /***
40      * Creates a DisplaceableRegion with an initial offset and length.  The
41      * boolean flags indicate how the region's offset should behave during a
42      * displacement.  <code>bMoveIfIqual</code> controls
43      * whether displacements at the region's starting offset are handled by
44      * moving the offset or increasing the length.
45      * @param offset Initial offset
46      * @param length Initial length
47      * @param bMoveIfEqual <code>true</code> to move the starting offset when a
48      *                     displacement starts at the beginning of the region,
49      *                     <code>false</code> to lengthen the node in this case.
50      */
51     public DisplaceableRegion(int offset, int length, boolean bMoveIfEqual)
52     {
53        _offset = offset;
54        _length = length;
55        _bMoveIfEqual = bMoveIfEqual;
56     }
57  
58     /***
59      * Returns the region's length in characters.
60      * @return Region length
61      */
62     public int getLength()
63     {
64        return _length;
65     }
66  
67     /***
68      * Returns the starting character offset of the region.
69      * @return Region offset
70      */
71     public int getOffset()
72     {
73        return _offset;
74     }
75     
76     /***
77      * Indicates whether displacements at the region's starting offset are
78      * handled by moving the offset or increasing the length.
79      * @return <code>true</code> if displacements at the start of the region
80      *         change its offset, <code>false</code> if they change its length.
81      */
82     public boolean moveIfEqual()
83     {
84        return _bMoveIfEqual;
85     }
86  
87     /***
88      * Adjusts the region's offset or length to reflect a character displacement.
89      * If the region begins after the displacement offset, or if it begins on
90      * the displacement offset and <code>moveIfEqual()</code> is <code>true</code>, 
91      * its offset will be adjusted.  If the displacement offset occurs within 
92      * the region, or if it matches the region's offset and <code>moveIfEqual()</code>
93      * is <code>false</code>, its length will be adjusted.  If the displacement 
94      * offset is beyond the end of the region, it is unaffected.
95      * @param offset Origin of the displacement
96      * @param length Magnitude of the displacement.  May be negative.
97      */
98     public void displace(int offset, int length)
99     {
100       if (_offset > offset)
101       {
102          _offset += length;
103       }
104       else if (_bMoveIfEqual && _offset == offset)
105       {
106          _offset += length;
107       }
108       else if (offset < (_offset + _length))
109       {
110          _length += length;
111       }
112    }
113 }