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: class BCoord implements Comparable {
060: public int row;
061: public int col;
062:
063: /**
064: * Create a BCoord at the origin (top-left)
065: */
066: public BCoord() {
067: this .row = 0;
068: this .col = 0;
069: }
070:
071: public BCoord(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 BCoord(BCoord coord) {
078: this .row = coord.row;
079: this .col = coord.col;
080: }
081:
082: public void copyFrom(BCoord src) {
083: this .row = src.row;
084: this .col = src.col;
085: }
086:
087: // Overrides of Object:
088:
089: public Object clone() {
090: return new BCoord(row, col);
091: }
092:
093: public boolean equals(BCoord target) { // XXX param should be Object and also hashCode should be overriden
094: if (row != target.row)
095: return false;
096: return col == target.col;
097: }
098:
099: public String toString() {
100: return "(r=" + row + ",c=" + col + ")"; // NOI18N
101: }
102:
103: /**
104: * Examples:
105: * To satisfy Comparable.
106: * <p>
107: * <pre>
108: * a < b === a.compareTo(b) < 0
109: * a >= b === a.compareTo(b) >= 0
110: * </pre>
111: */
112: public int compareTo(Object o) throws ClassCastException {
113: BCoord target = (BCoord) o;
114:
115: // -1 or negative -> this < o
116: // 0 -> this == o
117: // +1 or positive -> this > o
118:
119: if (this .row < target.row)
120: return -1;
121: else if (this .row > target.row)
122: return +1;
123: else {
124: return this .col - target.col;
125: }
126: }
127:
128: public void clip(int rows, int cols) {
129: // BE CAREFUL and clip view BCoords with a view box and buffer
130: // BCoords with a buffer box!
131: if (row < 0)
132: row = 0;
133: else if (row > rows)
134: row = rows;
135: if (col < 0)
136: col = 0;
137: else if (col > cols)
138: col = cols;
139: }
140: }
|