001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: * Contributor(s): Ivan Soleimanipour.
042: */
043:
044: /*
045: * "RegionManager.java"
046: * RegionManager.java 1.7 01/07/10
047: */
048:
049: package org.netbeans.lib.terminalemulator;
050:
051: public class RegionManager {
052: private ActiveRegion root = new ActiveRegion(null, new Coord(),
053: false);
054: private ActiveRegion parent = null;
055: private ActiveRegion region = root;
056:
057: /**
058: * Eliminates all regions.
059: */
060: public void reset() {
061: root = new ActiveRegion(null, new Coord(), false);
062: parent = null;
063: region = root;
064: }
065:
066: /**
067: * Returns the always-present "root" ActiveRegion.
068: */
069: public ActiveRegion root() {
070: return root;
071: }
072:
073: /**
074: * Creates a new active region.
075: * <p>
076: * Any text at and after 'begin' will belong to this region.
077: * <p>
078: * Active regions can be nested.
079: */
080: public ActiveRegion beginRegion(Coord begin) throws RegionException {
081: if (region != null) {
082: // begin new nested region
083: ActiveRegion child = new ActiveRegion(region, begin, true);
084: region.addChild(child);
085: parent = region;
086: region = child;
087:
088: } else {
089: // begin new region at current level of nesting
090: region = new ActiveRegion(parent, begin, false);
091: parent.addChild(region);
092: }
093:
094: return region;
095: }
096:
097: /**
098: * Declare the end of the current active region.
099: * <p>
100: * Any text before and at 'end' will belong to this region.
101: */
102: public void endRegion(Coord end) throws RegionException {
103: if (region == null) {
104: throw new RegionException("endRegion(): ", // NOI18N
105: "no current active region");// NOI18N
106: } else if (region == root) {
107: throw new RegionException("endRegion(): ", // NOI18N
108: "cannot end root region");// NOI18N
109: }
110:
111: region.setEnd(end);
112:
113: if (region.nested) {
114: region = parent;
115: parent = region.parent;
116: } else {
117: region = null;
118: }
119: }
120:
121: /**
122: * Eliminate the current region.
123: * <p>
124: * If cancelRegion is issued between a beginRegion and endRegion the
125: * region that was begun is cancelled as if beginregion was never called.
126: */
127: public void cancelRegion() throws RegionException {
128: if (region == null) {
129: throw new RegionException("cancelRegion(): ", // NOI18N
130: "no current active region"); // NOI18N
131: } else if (region == root) {
132: throw new RegionException("cancelRegion(): ", // NOI18N
133: "cannot cancel root region"); // NOI18N
134: }
135:
136: parent.removeChild(region);
137: region = null;
138: }
139:
140: /* OLD
141: public ActiveRegion findRegion(Point p) {
142: final Coord coord = new Coord();
143: coord.row = p.y;
144: coord.col = p.x;
145: return findRegion(coord);
146: }
147: */
148:
149: /*
150: * Return the most deeply nested ActiveRegion containing 'coord'.
151: * <p>
152: * If no ActiveRegion is found root is returned.
153: */
154: public ActiveRegion findRegion(Coord acoord) {
155: // What happens if we haven't closed the current/all regions?
156: return root.contains(acoord);
157: }
158:
159: /**
160: * Adjust coordinates when when absolute coordinates roll over.
161: */
162: void relocate(int from, int to) {
163: int delta = to - from;
164: root.relocate(delta);
165: }
166:
167: /**
168: * Cull any regions that are before origin.
169: * <p>
170: * For now we use an aggressive strategy where if any part is gone,
171: * all of the region will go. This simplifies the algorithm
172: * considerably and is forward compatible in the sense that if in
173: * the future we decide to delete based on the region end, and
174: * adjust 'begin', that would show up as an improvement not a regression.
175: *
176: */
177: void cull(int origin) {
178: root.cull(origin);
179: }
180: }
|