KickJava   Java API By Example, From Geeks To Geeks.

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


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  * "ActiveRegion.java"
25  * ActiveRegion.java 1.8 01/07/16
26  */

27
28 package org.netbeans.lib.terminalemulator;
29
30 import java.util.LinkedList JavaDoc;
31 import java.util.ListIterator JavaDoc;
32
33 public class ActiveRegion {
34     public Coord begin = new Coord();
35     public Coord end = new Coord();
36
37     // accessible to RegionManager
38
ActiveRegion parent;
39     boolean nested;
40
41     private LinkedList JavaDoc children;
42     private boolean has_end;
43
44     ActiveRegion(ActiveRegion parent, Coord begin, boolean nested) {
45     this.parent = parent;
46     this.begin.copyFrom(begin);
47     this.nested = nested;
48     }
49
50     public ActiveRegion parent() {
51     return this.parent;
52     }
53
54     public Extent getExtent() {
55     if (has_end)
56         return new Extent(begin, end);
57     else
58         return new Extent(begin, begin);
59     }
60
61     void setEnd(Coord end) {
62     this.end.copyFrom(end);
63     has_end = true;
64     }
65
66     void addChild(ActiveRegion child) {
67     if (children == null)
68         children = new LinkedList JavaDoc();
69     children.add(child);
70     }
71
72     void removeChild(ActiveRegion child) {
73     if (children == null)
74         return;
75     children.remove(child);
76     }
77
78     ActiveRegion contains(Coord coord) {
79
80     // System.out.println("ActiveRegion [ " + begin + "-" + end + "] vs " + // NOI18N
81
// coord);
82
boolean coord_past_end = has_end && coord.compareTo(end) > 0;
83     if (this.parent != null && (coord.compareTo(begin) < 0 ||
84                     coord_past_end)) {
85         return null; // outside of us entirely
86
}
87
88     if (children != null) {
89         ListIterator JavaDoc iter = children.listIterator();
90         while(iter.hasNext()) {
91         ActiveRegion child = (ActiveRegion) iter.next();
92         if (coord.compareTo(child.begin) < 0)
93             break; // short circuit
94
// 'target' is 'child' or one if it's children
95
ActiveRegion target = child.contains(coord);
96         if (target != null)
97             return target;
98         }
99     }
100     return this;
101     }
102
103
104     // absolute coordinate mgmt
105

106     void relocate(int delta) {
107     this.begin.row += delta;
108     this.end.row += delta;
109     if (children != null) {
110         ListIterator JavaDoc iter = children.listIterator();
111         while(iter.hasNext()) {
112         ActiveRegion child = (ActiveRegion) iter.next();
113         child.relocate(delta);
114         }
115     }
116     }
117
118     void cull(int origin) {
119
120     // See RegionManager.cull() for culling strategy.
121
// This function isn't recursive. It's called only once on root.
122

123     if (children == null)
124         return;
125
126     int nculled = 0;
127
128     ListIterator JavaDoc iter = children.listIterator();
129     while(iter.hasNext()) {
130         ActiveRegion child = (ActiveRegion) iter.next();
131         if (child.begin.row < origin) {
132         iter.remove();
133         nculled++;
134         } else
135         break; // short circuit out
136
}
137
138     // System.out.println("cull'ed " + nculled + " regions"); // NOI18N
139
}
140
141
142     /**
143      * Mark this region as one that may be converted to a selection.
144      * <p>
145      * This is just a convenience state-keeping flag
146      */

147     public void setSelectable(boolean selectable) {
148     this.selectable = selectable;
149     }
150
151     /**
152      * Return the value set using setSelectable().
153      */

154     public boolean isSelectable() {
155     return selectable;
156     }
157     private boolean selectable;
158
159
160     /**
161      * Mark this region as one that will provide feedback when the mouse moves
162      * over it.
163      * <p>
164      * This is just a convenience state-keeping flag
165      */

166     public void setFeedbackEnabled(boolean feedback) {
167     this.feedback_enabled = feedback;
168     }
169
170     /**
171      * Return the value set using setFeedback().
172      */

173     public boolean isFeedbackEnabled() {
174     return feedback_enabled;
175     }
176     private boolean feedback_enabled;
177
178     /**
179      * Mark this region as one that will provide feedback in it's containing
180      * region.
181      */

182     public void setFeedbackViaParent(boolean feedback_via_parent) {
183     this.feedback_via_parent = feedback_via_parent;
184     }
185
186     public boolean isFeedbackViaParent() {
187     return feedback_via_parent;
188     }
189
190     private boolean feedback_via_parent;
191
192     /*
193      * Mark this region as a "link".
194      */

195     public void setLink(boolean link) {
196     this.link = link;
197     }
198     public boolean isLink() {
199     return link;
200     }
201     private boolean link;
202
203
204
205     /**
206      * Associate additional data with this ActiveRegion.
207      */

208     public void setUserObject(Object JavaDoc object) {
209     this.user_object = object;
210     }
211
212     /**
213      * Retrieve the additional data associated with this ActiveRegion through
214      * setUserObject.
215      */

216     public Object JavaDoc getUserObject() {
217     return user_object;
218     }
219     private Object JavaDoc user_object;
220
221
222
223     // siblings
224
//
225
// I've chosen to delegate finding of siblings to the parent instead of
226
// using explicit sibling links. Here are the reasons:
227
// - explicit links take up memory. I believe in the long run that is
228
// more costly than dumb searches.
229
// - AR's get created when stuff gets rendered. We want to render stuff
230
// as fast as possible. So instead of doing work at creation time
231
// which needs to be supra-human speed, we do the work at query time
232
// which is usualy in reponse to human actions and need not be too fast.
233
//
234
// Ultimately a Vector is probably a better substitute for LinkedList
235
// In that case each child could remember it's index in it's parents
236
// vector (less storage than two pointers, and vector has less overhead
237
// than linkedlist to compensate). Note that this will work only
238
// because ActiveRegions are not editable.
239

240
241     /**
242      * Return the first child of this
243      */

244     public ActiveRegion firstChild() {
245     if (children == null)
246         return null;
247     return (ActiveRegion) children.getFirst();
248     }
249
250     /**
251      * Return the last child of this
252      */

253     public ActiveRegion lastChild() {
254     if (children == null)
255         return null;
256     return (ActiveRegion) children.getLast();
257     }
258
259
260     /**
261      * Get the previous sibling of this region
262      */

263     public ActiveRegion getPreviousSibling() {
264     if (parent != null)
265         return parent.previous_sibling_of(this);
266     else
267         return null;
268     }
269
270     /**
271      * Get the next sibling of this region
272      */

273     public ActiveRegion getNextSibling() {
274     if (parent != null)
275         return parent.next_sibling_of(this);
276     else
277         return null;
278     }
279
280     private ActiveRegion previous_sibling_of(ActiveRegion child) {
281     ListIterator JavaDoc iter = children.listIterator();
282         ActiveRegion previousChild = null;
283     while (iter.hasNext()) {
284         ActiveRegion candidate = (ActiveRegion) iter.next();
285         if (candidate == child) {
286                 // bug: iterator is already shifted, so following command
287
// returns again child
288
//return (ActiveRegion) (iter.hasPrevious()? iter.previous(): null);
289
return previousChild;
290         }
291             previousChild = candidate;
292     }
293     return null;
294     }
295
296     private ActiveRegion next_sibling_of(ActiveRegion child) {
297     ListIterator JavaDoc iter = children.listIterator();
298     while (iter.hasNext()) {
299         ActiveRegion candidate = (ActiveRegion) iter.next();
300         if (candidate == child) {
301         return (ActiveRegion) (iter.hasNext()? iter.next(): null);
302         }
303     }
304     return null;
305     }
306 }
307
Popular Tags