KickJava   Java API By Example, From Geeks To Geeks.

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


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-2004.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 package org.netbeans.lib.terminalemulator;
24
25 /**
26  * A cartesian coordinate class, similar to Point.
27  * The equivalent of 'offset' in swing.text.Document.
28  * <br>
29  * Rows are 0-origin, columns are 0-origin.
30  * <p>
31  * Why not the regular Java Point? Because ...
32  * <ul>
33  * <li>Point with 'x' and 'y' is not as clear.
34  * <li>Point doesn't implement Comparable, which we depend on a lot.
35  * </ul>
36  */

37
38 public class Coord implements Comparable JavaDoc {
39     public int row;
40     public int col;
41
42     /**
43      * Create a Coord at the origin (top-left)
44      */

45     public Coord() {
46     this.row = 0;
47     this.col = 0;
48     }
49
50     private Coord(int row, int col) {
51     // Note this is flipped from Points(x, y) sense
52
this.row = row;
53     this.col = col;
54     }
55
56     public static Coord make(int row, int col) {
57     // 'row' is in absolute coordinates
58
return new Coord(row, col);
59     }
60
61     public Coord(Coord coord) {
62     this.row = coord.row;
63     this.col = coord.col;
64     }
65
66     public Coord(BCoord coord, int bias) {
67     this.row = coord.row + bias;
68     this.col = coord.col;
69     }
70
71     public BCoord toBCoord(int bias) {
72     int new_row = row - bias;
73     if (new_row < 0)
74         return new BCoord(0, 0); // we're out of history
75
else
76         return new BCoord(new_row, col);
77     }
78
79     public void copyFrom(Coord src) {
80     this.row = src.row;
81     this.col = src.col;
82     }
83
84     // Overrides of Object:
85

86     public Object JavaDoc clone() {
87     return new Coord(row, col);
88     }
89
90     public boolean equals(Coord target) { // XXX param should be Object and also hashCode should be overriden
91
if (row != target.row)
92         return false;
93     return col == target.col;
94     }
95
96     public String JavaDoc toString() {
97     return "(r=" + row + ",c=" + col + ")"; // NOI18N
98
}
99
100     /**
101      * Examples:
102      * To satisfy Comparable.
103      * <p>
104      * <pre>
105      * a &lt b === a.compareTo(b) &lt 0
106      * a &gt= b === a.compareTo(b) &gt= 0
107      * </pre>
108      */

109     public int compareTo(Object JavaDoc o) throws ClassCastException JavaDoc {
110     Coord target = (Coord) o;
111
112     // -1 or negative -> this < o
113
// 0 -> this == o
114
// +1 or positive -> this > o
115

116     if (this.row < target.row)
117         return -1;
118     else if (this.row > target.row)
119         return +1;
120     else {
121         return this.col - target.col;
122     }
123     }
124
125     /* OLD public */ void clip(int rows, int cols, int bias) {
126     // BE CAREFUL and clip view Coords with a view box and buffer
127
// Coords with a buffer box!
128
/* OLD
129     if (row < 0)
130         row = 0;
131     else if (row > rows)
132         row = rows;
133     */

134
135     if (row < bias)
136         row = bias;
137     else if (row > bias + rows)
138         row = bias + rows;
139
140     if (col < 0)
141         col = 0;
142     else if (col > cols)
143         col = cols;
144     }
145 }
146
Popular Tags