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: package org.netbeans.lib.terminalemulator;
045:
046: /**
047: * A cartesian coordinate class, similar to Point.
048: * The equivalent of 'offset' in swing.text.Document.
049: * <br>
050: * Rows are 0-origin, columns are 0-origin.
051: * <p>
052: * Why not the regular Java Point? Because ...
053: * <ul>
054: * <li>Point with 'x' and 'y' is not as clear.
055: * <li>Point doesn't implement Comparable, which we depend on a lot.
056: * </ul>
057: */
058:
059: public class Coord implements Comparable {
060: public int row;
061: public int col;
062:
063: /**
064: * Create a Coord at the origin (top-left)
065: */
066: public Coord() {
067: this .row = 0;
068: this .col = 0;
069: }
070:
071: private Coord(int row, int col) {
072: // Note this is flipped from Points(x, y) sense
073: this .row = row;
074: this .col = col;
075: }
076:
077: public static Coord make(int row, int col) {
078: // 'row' is in absolute coordinates
079: return new Coord(row, col);
080: }
081:
082: public Coord(Coord coord) {
083: this .row = coord.row;
084: this .col = coord.col;
085: }
086:
087: Coord(BCoord coord, int bias) {
088: this .row = coord.row + bias;
089: this .col = coord.col;
090: }
091:
092: BCoord toBCoord(int bias) {
093: int new_row = row - bias;
094: if (new_row < 0)
095: return new BCoord(0, 0); // we're out of history
096: else
097: return new BCoord(new_row, col);
098: }
099:
100: public void copyFrom(Coord src) {
101: this .row = src.row;
102: this .col = src.col;
103: }
104:
105: // Overrides of Object:
106:
107: public Object clone() {
108: return new Coord(row, col);
109: }
110:
111: public boolean equals(Coord target) { // XXX param should be Object and also hashCode should be overriden
112: if (row != target.row)
113: return false;
114: return col == target.col;
115: }
116:
117: public String toString() {
118: return "(r=" + row + ",c=" + col + ")"; // NOI18N
119: }
120:
121: /**
122: * Examples:
123: * To satisfy Comparable.
124: * <p>
125: * <pre>
126: * a < b === a.compareTo(b) < 0
127: * a >= b === a.compareTo(b) >= 0
128: * </pre>
129: */
130: public int compareTo(Object o) throws ClassCastException {
131: Coord target = (Coord) o;
132:
133: // -1 or negative -> this < o
134: // 0 -> this == o
135: // +1 or positive -> this > o
136:
137: if (this .row < target.row)
138: return -1;
139: else if (this .row > target.row)
140: return +1;
141: else {
142: return this .col - target.col;
143: }
144: }
145:
146: /* OLD public */void clip(int rows, int cols, int bias) {
147: // BE CAREFUL and clip view Coords with a view box and buffer
148: // Coords with a buffer box!
149: /* OLD
150: if (row < 0)
151: row = 0;
152: else if (row > rows)
153: row = rows;
154: */
155:
156: if (row < bias)
157: row = bias;
158: else if (row > bias + rows)
159: row = bias + rows;
160:
161: if (col < 0)
162: col = 0;
163: else if (col > cols)
164: col = cols;
165: }
166: }
|