nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | 923ee60 | 2007-04-10 18:32:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2005 IBM Corporation and others. |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 3 | * 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 |
amywu | 923ee60 | 2007-04-10 18:32:07 +0000 | [diff] [blame] | 7 | * |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.css.core.internal.util; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 15 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| 16 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
| 17 | import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * |
| 21 | */ |
| 22 | public 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 | } |
amywu | 923ee60 | 2007-04-10 18:32:07 +0000 | [diff] [blame] | 140 | } |