KickJava   Java API By Example, From Geeks To Geeks.

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


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  * "ActiveTerm.java"
25  * ActiveTerm.java 1.9 01/07/30
26  */

27
28 package org.netbeans.lib.terminalemulator;
29
30 import java.awt.*;
31 import java.awt.event.*;
32
33 public class ActiveTerm extends StreamTerm {
34
35     private ActiveTermListener at_listener;
36
37     private RegionManager rm;
38
39     private Coord last_begin = null;
40     private Coord last_end = null;
41
42     public ActiveTerm() {
43     super();
44
45     setCursorVisible(false);
46
47     rm = regionManager();
48
49     getScreen().addMouseListener(new MouseAdapter() {
50         public void mouseClicked(MouseEvent e) {
51         if ( (e.getModifiers() & InputEvent.BUTTON1_MASK) !=
52             InputEvent.BUTTON1_MASK) {
53             // ignore if not left button
54
return;
55         }
56         Point p = mapToBufRowCol(e.getPoint());
57         BCoord c = new BCoord(p.y, p.x);
58         Coord ac = new Coord(c, firsta);
59         ActiveRegion region = rm.findRegion(ac);
60         if (region != null) {
61             if (region.isSelectable())
62             setSelectionExtent(region.getExtent());
63             if (at_listener != null)
64             at_listener.action(region, e);
65         }
66         }
67     } );
68
69     getScreen().addMouseMotionListener(new MouseMotionAdapter() {
70         public void mouseMoved(MouseEvent e) {
71         Point p = mapToBufRowCol(e.getPoint());
72         BCoord c = new BCoord(p.y, p.x);
73         Coord ac = new Coord(c, firsta);
74         ActiveRegion region = rm.findRegion(ac);
75         ActiveRegion hl_region = findRegionToHilite(region);
76
77         if (hl_region == null)
78             hilite(null, null);
79         else
80             hilite(hl_region.begin, hl_region.end);
81         }
82     } );
83     }
84
85     private ActiveRegion findRegionToHilite(ActiveRegion region) {
86     if (region == null)
87         return null;
88     else if (region.isFeedbackEnabled())
89         return region;
90     else if (region.isFeedbackViaParent())
91         return findRegionToHilite(region.parent());
92     else
93         return null;
94     }
95
96     public void setActionListener(ActiveTermListener listener) {
97     this.at_listener = listener;
98     }
99
100     private void hilite_help(Coord begin, Coord end, boolean on) {
101     if (begin == null && end == null)
102         return; // nothing to do
103
setCharacterAttribute(begin, end, 9, on);
104     }
105
106     public void hilite(Coord begin, Coord end) {
107     if (end != null && end.row == 1 && end.col == 0)
108         end = getCursorCoord();
109     hilite_help(last_begin, last_end, false);
110     last_begin = (begin == null)? null: (Coord) begin.clone();
111     last_end = (end == null)? null: (Coord) end.clone();
112         hilite_help(begin, end, true);
113     }
114
115     public void hilite(ActiveRegion region) {
116     hilite(region.begin, region.end);
117     }
118
119     public ActiveRegion beginRegion(boolean hyperlink) {
120     ActiveRegion region = null;
121     try {
122         region = rm.beginRegion(getCursorCoord());
123     } catch (RegionException x) {
124         ;
125     }
126     if (hyperlink) {
127         setAttribute(34); // fg -> blue
128
setAttribute(4); // underline
129
}
130     return region;
131     }
132
133     public void endRegion() {
134     Coord cursor = getCursorCoord();
135     Coord bcursor = backup(cursor);
136
137     // This only happens if we begin and end a region w/o any output
138
// in between
139
if (bcursor == null)
140         bcursor = cursor;
141
142     try {
143         rm.endRegion(bcursor);
144     } catch (RegionException x) {
145         ;
146     }
147     setAttribute(0); // reset
148
}
149
150     public ActiveRegion findRegion(Coord coord) {
151     return rm.findRegion(coord);
152     }
153
154     public void cancelRegion() {
155     try {
156         rm.cancelRegion();
157     } catch (RegionException x) {
158         ;
159     }
160     }
161     
162     public void clear() {
163         nullLasts();
164         super.clear ();
165     }
166
167     public void clearHistoryNoRefresh() {
168         nullLasts();
169     super.clearHistoryNoRefresh ();
170     }
171     
172     private void nullLasts() {
173         last_begin = null;
174         last_end = null;
175     }
176 }
177
Popular Tags