Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / dom / DOMRange.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.dom;\r
13 \r
14 import org.w3c.dom.Node;\r
15 \r
16 /**\r
17  * @author mengbo\r
18  */\r
19 public class DOMRange {\r
20         IDOMPosition _start;\r
21 \r
22         IDOMPosition _end;\r
23 \r
24         /**\r
25          * @param p1\r
26          * @param p2\r
27          */\r
28         public DOMRange(IDOMPosition p1, IDOMPosition p2) {\r
29                 _start = p1;\r
30                 _end = p2;\r
31         }\r
32 \r
33         /**\r
34          * @return the start position\r
35          */\r
36         public IDOMPosition getStartPosition() {\r
37                 return _start;\r
38         }\r
39 \r
40         /**\r
41          * @return the end position\r
42          */\r
43         public IDOMPosition getEndPosition() {\r
44                 return _end;\r
45         }\r
46 \r
47         /**\r
48          * @return true if is empty\r
49          */\r
50         public boolean isEmpty() {\r
51                 return _start.getContainerNode() == _end.getContainerNode()\r
52                                 && _start.getOffset() == _end.getOffset();\r
53         }\r
54 \r
55         /**\r
56          * @return true if is ordered\r
57          */\r
58         public boolean isOrdered() {\r
59                 Node common = DOMUtil.findCommonAncester(_start.getContainerNode(),\r
60                                 _end.getContainerNode());\r
61                 if (common == null) {\r
62                         return true;\r
63                 }\r
64                 IDOMPosition s = moveUp(_start, common);\r
65                 IDOMPosition e = moveUp(_end, common);\r
66                 return e.getOffset() >= s.getOffset();\r
67         }\r
68 \r
69         private IDOMPosition moveUp(IDOMPosition p, Node ancester) {\r
70                 while (p.getContainerNode() != ancester) {\r
71                         p = new DOMRefPosition(p.getContainerNode(), false);\r
72                 }\r
73                 return p;\r
74         }\r
75 }\r