KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > terminalemulator > RegionManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Terminal Emulator.
16  * The Initial Developer of the Original Software is Sun Microsystems, Inc..
17  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 /*
24  * "RegionManager.java"
25  * RegionManager.java 1.7 01/07/10
26  */

27
28 package org.netbeans.lib.terminalemulator;
29
30
31 public class RegionManager {
32     private ActiveRegion root = new ActiveRegion(null, new Coord(), false);
33     private ActiveRegion parent = null;
34     private ActiveRegion region = root;
35
36     /**
37      * Eliminates all regions.
38      */

39     public void reset() {
40     root = new ActiveRegion(null, new Coord(), false);
41     parent = null;
42     region = root;
43     }
44
45     /**
46      * Returns the always-present "root" ActiveRegion.
47      */

48     public ActiveRegion root() {
49     return root;
50     }
51
52     /**
53      * Creates a new active region.
54      * <p>
55      * Any text at and after 'begin' will belong to this region.
56      * <p>
57      * Active regions can be nested.
58      */

59     public ActiveRegion beginRegion(Coord begin) throws RegionException {
60     if (region != null) {
61         // begin new nested region
62
ActiveRegion child = new ActiveRegion(region, begin, true);
63         region.addChild(child);
64         parent = region;
65         region = child;
66         
67     } else {
68         // begin new region at current level of nesting
69
region = new ActiveRegion(parent, begin, false);
70         parent.addChild(region);
71     }
72
73     return region;
74     }
75
76     /**
77      * Declare the end of the current active region.
78      * <p>
79      * Any text before and at 'end' will belong to this region.
80      */

81     public void endRegion(Coord end) throws RegionException {
82     if (region == null) {
83         throw new RegionException("endRegion(): ", // NOI18N
84
"no current active region");// NOI18N
85
} else if (region == root) {
86         throw new RegionException("endRegion(): ", // NOI18N
87
"cannot end root region");// NOI18N
88
}
89
90     region.setEnd(end);
91
92     if (region.nested) {
93         region = parent;
94         parent = region.parent;
95     } else {
96         region = null;
97     }
98     }
99
100     /**
101      * Eliminate the current region.
102      * <p>
103      * If cancelRegion is issued between a beginRegion and endRegion the
104      * region that was begun is cancelled as if beginregion was never called.
105      */

106     public void cancelRegion() throws RegionException {
107     if (region == null) {
108         throw new RegionException("cancelRegion(): ", // NOI18N
109
"no current active region"); // NOI18N
110
} else if (region == root) {
111         throw new RegionException("cancelRegion(): ", // NOI18N
112
"cannot cancel root region"); // NOI18N
113
}
114
115     parent.removeChild(region);
116     region = null;
117     }
118
119     /* OLD
120     public ActiveRegion findRegion(Point p) {
121     final Coord coord = new Coord();
122     coord.row = p.y;
123     coord.col = p.x;
124     return findRegion(coord);
125     }
126     */

127
128     /*
129      * Return the most deeply nested ActiveRegion containing 'coord'.
130      * <p>
131      * If no ActiveRegion is found root is returned.
132      */

133     public ActiveRegion findRegion(Coord acoord) {
134     // What happens if we haven't closed the current/all regions?
135
return root.contains(acoord);
136     }
137
138     /**
139      * Adjust coordinates when when absolute coordinates roll over.
140      */

141     void relocate(int from, int to) {
142     int delta = to - from;
143     root.relocate(delta);
144     }
145
146     /**
147      * Cull any regions that are before origin.
148      * <p>
149      * For now we use an aggressive strategy where if any part is gone,
150      * all of the region will go. This simplifies the algorithm
151      * considerably and is forward compatible in the sense that if in
152      * the future we decide to delete based on the region end, and
153      * adjust 'begin', that would show up as an improvement not a regression.
154      *
155      */

156     void cull(int origin) {
157     root.cull(origin);
158     }
159 }
160
Popular Tags