KickJava   Java API By Example, From Geeks To Geeks.

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


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 class BCoord implements Comparable JavaDoc {
39     public int row;
40     public int col;
41
42     /**
43      * Create a BCoord at the origin (top-left)
44      */

45     public BCoord() {
46     this.row = 0;
47     this.col = 0;
48     }
49
50     public BCoord(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 BCoord(BCoord coord) {
57     this.row = coord.row;
58     this.col = coord.col;
59     }
60
61     public void copyFrom(BCoord src) {
62     this.row = src.row;
63     this.col = src.col;
64     }
65
66     // Overrides of Object:
67

68     public Object JavaDoc clone() {
69     return new BCoord(row, col);
70     }
71
72     public boolean equals(BCoord target) { // XXX param should be Object and also hashCode should be overriden
73
if (row != target.row)
74         return false;
75     return col == target.col;
76     }
77
78     public String JavaDoc toString() {
79     return "(r=" + row + ",c=" + col + ")"; // NOI18N
80
}
81
82     /**
83      * Examples:
84      * To satisfy Comparable.
85      * <p>
86      * <pre>
87      * a &lt b === a.compareTo(b) &lt 0
88      * a &gt= b === a.compareTo(b) &gt= 0
89      * </pre>
90      */

91     public int compareTo(Object JavaDoc o) throws ClassCastException JavaDoc {
92     BCoord target = (BCoord) o;
93
94     // -1 or negative -> this < o
95
// 0 -> this == o
96
// +1 or positive -> this > o
97

98     if (this.row < target.row)
99         return -1;
100     else if (this.row > target.row)
101         return +1;
102     else {
103         return this.col - target.col;
104     }
105     }
106
107     public void clip(int rows, int cols) {
108     // BE CAREFUL and clip view BCoords with a view box and buffer
109
// BCoords with a buffer box!
110
if (row < 0)
111         row = 0;
112     else if (row > rows)
113         row = rows;
114     if (col < 0)
115         col = 0;
116     else if (col > cols)
117         col = cols;
118     }
119 }
120
Popular Tags