intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.core / src / net / sourceforge / phpeclipse / css / core / internal / parser / MutableRegion.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: MutableRegion.java,v 1.1 2004-09-02 18:07:13 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.core.internal.parser;
15
16 import org.eclipse.jface.text.IRegion;
17
18 /**
19  * 
20  */
21 public class MutableRegion implements IRegion {
22
23         // Instance Variables ------------------------------------------------------
24
25         /**
26          * The offset of the region.
27          */
28         private int offset = 0;
29
30         /**
31          * The length of the region.
32          */
33         private int length = 0;
34
35         // Constructors ------------------------------------------------------------
36
37         /**
38          * Default constructor.
39          */
40         public MutableRegion() {
41                 this(0);
42         }
43
44         /**
45          * Constructor with partial initialization.
46          * 
47          * @param offset the offset of the region
48          */
49         public MutableRegion(int offset) {
50                 this.offset = offset;
51         }
52
53         /**
54          * Constructor with initialization.
55          * 
56          * @param offset the offset of the region
57          * @param length the length of the region
58          */
59         public MutableRegion(int offset, int length) {
60                 this.offset = offset;
61                 this.length = length;
62         }
63
64         /**
65          * Constructor with initialization.
66          * 
67          * @param region another region to initialize this region with
68          */
69         public MutableRegion(IRegion region) {
70                 this.offset = region.getOffset();
71                 this.length = region.getLength();
72         }
73
74         // IRegion Implementation --------------------------------------------------
75
76         /*
77          * @see org.eclipse.jface.text.IRegion#getLength()
78          */
79         public int getLength() {
80                 return length;
81         }
82
83         /*
84          * @see org.eclipse.jface.text.IRegion#getOffset()
85          */
86         public int getOffset() {
87                 return offset;
88         }
89
90         // Public Methods ----------------------------------------------------------
91
92         /**
93          * Adds another region to this region, which will be enlarged so that the
94          * added region completely fits into this region.
95          * 
96          * @param region the region to add 
97          */
98         public void add(IRegion region) {
99                 int start1 = offset;
100                 int end1 = offset + length;
101                 int start2 = region.getOffset();
102                 int end2 = start2 + region.getLength();
103                 offset = (start1 < start2) ? start1 : start2;
104                 length = (end1 > end2) ? (end1 - offset) : (end2 - offset);
105         }
106
107         /**
108          * Sets the length of the region.
109          * 
110          * @param length the length to set
111          */
112         public void setLength(int length) {
113                 this.length = length;
114         }
115
116         /**
117          * Sets the offset of the region.
118          * 
119          * @param offset the offset to set
120          */
121         public void setOffset(int offset) {
122                 this.offset = offset;
123         }
124
125 }