blob: 44d002299983940407a014a6e50ba95f39bf22eb [file] [log] [blame]
nitind958d79a2004-11-23 19:23:00 +00001/*******************************************************************************
amywu923ee602007-04-10 18:32:07 +00002 * Copyright (c) 2004, 2005 IBM Corporation and others.
nitind958d79a2004-11-23 19:23:00 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
amywu923ee602007-04-10 18:32:07 +00007 *
nitind958d79a2004-11-23 19:23:00 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
david_williams63219a22005-04-10 01:59:51 +000011package org.eclipse.wst.css.core.internal.util;
nitind958d79a2004-11-23 19:23:00 +000012
13
14
david_williams4ad020f2005-04-18 08:00:30 +000015import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
16import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
17import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
nitind958d79a2004-11-23 19:23:00 +000018
19/**
20 *
21 */
22public class RegionIterator {
23
24 private IStructuredDocumentRegion documentRegion = null;
25 private IStructuredDocumentRegion curDocumentRegion = null;
26 private int current = -1;
27
28 /**
29 *
30 */
31 public RegionIterator(IStructuredDocument structuredDocument, int index) {
32 super();
33
34 reset(structuredDocument, index);
35 }
36
37 /**
38 *
39 */
40 public RegionIterator(IStructuredDocumentRegion flatNode, ITextRegion region) {
41 super();
42 reset(flatNode, region);
43 }
44
45 /**
46 *
47 */
48 public IStructuredDocumentRegion getStructuredDocumentRegion() {
49 return curDocumentRegion;
50 }
51
52 /**
53 *
54 */
55 public boolean hasNext() {
56 if (documentRegion == null)
57 return false;
58 if (current < 0)
59 return false;
60 if (current < documentRegion.getRegions().size())
61 return true;
62 return false;
63 }
64
65 /**
66 *
67 */
68 public boolean hasPrev() {
69 // the same as hasNext()
70 return hasNext();
71 }
72
73 /**
74 *
75 */
76 public ITextRegion next() {
77 if (documentRegion == null)
78 return null;
79 if (current < 0 || documentRegion.getRegions() == null || documentRegion.getRegions().size() <= current)
80 return null;
81
82 ITextRegion region = documentRegion.getRegions().get(current);
83 curDocumentRegion = documentRegion;
84
85 if (current >= documentRegion.getRegions().size() - 1) {
86 documentRegion = documentRegion.getNext();
87 current = -1;
88 }
89 current++;
90
91 return region;
92 }
93
94 /**
95 *
96 */
97 public ITextRegion prev() {
98 if (documentRegion == null)
99 return null;
100 if (current < 0 || documentRegion.getRegions() == null || documentRegion.getRegions().size() <= current)
101 return null;
102
103 ITextRegion region = documentRegion.getRegions().get(current);
104 curDocumentRegion = documentRegion;
105
106 if (current == 0) {
107 documentRegion = documentRegion.getPrevious();
108 if (documentRegion != null)
109 current = documentRegion.getRegions().size();
110 else
111 current = 0;
112 }
113 current--;
114
115 return region;
116 }
117
118 /**
119 *
120 */
121 public void reset(IStructuredDocument structuredDocument, int index) {
122 documentRegion = structuredDocument.getRegionAtCharacterOffset(index);
123 curDocumentRegion = documentRegion;
124 if (documentRegion != null) {
125 ITextRegion region = documentRegion.getRegionAtCharacterOffset(index);
126 current = documentRegion.getRegions().indexOf(region);
127 }
128 }
129
130 /**
131 *
132 */
133 public void reset(IStructuredDocumentRegion flatNode, ITextRegion region) {
134 if (region != null && flatNode != null) {
135 this.documentRegion = flatNode;
136 curDocumentRegion = flatNode;
137 current = flatNode.getRegions().indexOf(region);
138 }
139 }
amywu923ee602007-04-10 18:32:07 +0000140}