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: * "ActiveTerm.java"
046: * ActiveTerm.java 1.9 01/07/30
047: */
048:
049: package org.netbeans.lib.terminalemulator;
050:
051: import java.awt.*;
052: import java.awt.event.*;
053:
054: public class ActiveTerm extends StreamTerm {
055:
056: private ActiveTermListener at_listener;
057:
058: private RegionManager rm;
059:
060: private Coord last_begin = null;
061: private Coord last_end = null;
062:
063: public ActiveTerm() {
064: super ();
065:
066: setCursorVisible(false);
067:
068: rm = regionManager();
069:
070: getScreen().addMouseListener(new MouseAdapter() {
071: public void mouseClicked(MouseEvent e) {
072: if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) {
073: // ignore if not left button
074: return;
075: }
076: Point p = mapToBufRowCol(e.getPoint());
077: BCoord c = new BCoord(p.y, p.x);
078: Coord ac = new Coord(c, firsta);
079: ActiveRegion region = rm.findRegion(ac);
080: if (region != null) {
081: if (region.isSelectable())
082: setSelectionExtent(region.getExtent());
083: if (at_listener != null)
084: at_listener.action(region, e);
085: }
086: }
087: });
088:
089: getScreen().addMouseMotionListener(new MouseMotionAdapter() {
090: public void mouseMoved(MouseEvent e) {
091: Point p = mapToBufRowCol(e.getPoint());
092: BCoord c = new BCoord(p.y, p.x);
093: Coord ac = new Coord(c, firsta);
094: ActiveRegion region = rm.findRegion(ac);
095: ActiveRegion hl_region = findRegionToHilite(region);
096:
097: if (hl_region == null)
098: hilite(null, null);
099: else
100: hilite(hl_region.begin, hl_region.end);
101: }
102: });
103: }
104:
105: private ActiveRegion findRegionToHilite(ActiveRegion region) {
106: if (region == null)
107: return null;
108: else if (region.isFeedbackEnabled())
109: return region;
110: else if (region.isFeedbackViaParent())
111: return findRegionToHilite(region.parent());
112: else
113: return null;
114: }
115:
116: public void setActionListener(ActiveTermListener listener) {
117: this .at_listener = listener;
118: }
119:
120: private void hilite_help(Coord begin, Coord end, boolean on) {
121: if (begin == null && end == null)
122: return; // nothing to do
123: setCharacterAttribute(begin, end, 9, on);
124: }
125:
126: public void hilite(Coord begin, Coord end) {
127: if (end != null && end.row == 1 && end.col == 0)
128: end = getCursorCoord();
129: hilite_help(last_begin, last_end, false);
130: last_begin = (begin == null) ? null : (Coord) begin.clone();
131: last_end = (end == null) ? null : (Coord) end.clone();
132: hilite_help(begin, end, true);
133: }
134:
135: public void hilite(ActiveRegion region) {
136: hilite(region.begin, region.end);
137: }
138:
139: public ActiveRegion beginRegion(boolean hyperlink) {
140: ActiveRegion region = null;
141: try {
142: region = rm.beginRegion(getCursorCoord());
143: } catch (RegionException x) {
144: ;
145: }
146: if (hyperlink) {
147: setAttribute(34); // fg -> blue
148: setAttribute(4); // underline
149: }
150: return region;
151: }
152:
153: public void endRegion() {
154: Coord cursor = getCursorCoord();
155: Coord bcursor = backup(cursor);
156:
157: // This only happens if we begin and end a region w/o any output
158: // in between
159: if (bcursor == null)
160: bcursor = cursor;
161:
162: try {
163: rm.endRegion(bcursor);
164: } catch (RegionException x) {
165: ;
166: }
167: setAttribute(0); // reset
168: }
169:
170: public ActiveRegion findRegion(Coord coord) {
171: return rm.findRegion(coord);
172: }
173:
174: public void cancelRegion() {
175: try {
176: rm.cancelRegion();
177: } catch (RegionException x) {
178: ;
179: }
180: }
181:
182: public void clear() {
183: nullLasts();
184: super .clear();
185: }
186:
187: public void clearHistoryNoRefresh() {
188: nullLasts();
189: super .clearHistoryNoRefresh();
190: }
191:
192: private void nullLasts() {
193: last_begin = null;
194: last_end = null;
195: }
196: }
|