001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.X11;
026:
027: import java.awt.*;
028:
029: class WindowDimensions {
030: private Point loc;
031: private Dimension size;
032: private Insets insets;
033: private boolean isClientSizeSet;
034:
035: public WindowDimensions(int x, int y, int width, int height,
036: boolean isClient) {
037: this (new Rectangle(x, y, width, height), null, isClient);
038: }
039:
040: public WindowDimensions(Rectangle rec, Insets ins, boolean isClient) {
041: if (rec == null) {
042: throw new IllegalArgumentException(
043: "Client bounds can't be null");
044: }
045: isClientSizeSet = isClient;
046: this .loc = rec.getLocation();
047: this .size = rec.getSize();
048: setInsets(ins);
049: }
050:
051: public WindowDimensions(Point loc, Dimension size, Insets in,
052: boolean isClient) {
053: this (new Rectangle(loc, size), in, isClient);
054: }
055:
056: public WindowDimensions(Rectangle bounds, boolean isClient) {
057: this (bounds, null, isClient);
058: }
059:
060: public WindowDimensions(final WindowDimensions dims) {
061: this .loc = new Point(dims.loc);
062: this .size = new Dimension(dims.size);
063: this .insets = (dims.insets != null) ? ((Insets) dims.insets
064: .clone()) : new Insets(0, 0, 0, 0);
065: this .isClientSizeSet = dims.isClientSizeSet;
066: }
067:
068: public Rectangle getClientRect() {
069: if (isClientSizeSet) {
070: return new Rectangle(loc, size);
071: } else {
072: // Calculate client bounds
073: if (insets != null) {
074: return new Rectangle(loc.x, loc.y, size.width
075: - (insets.left + insets.right), size.height
076: - (insets.top + insets.bottom));
077: } else {
078: return new Rectangle(loc, size);
079: }
080: }
081: }
082:
083: public void setClientSize(Dimension size) {
084: this .size = new Dimension(size);
085: isClientSizeSet = true;
086: }
087:
088: public void setClientSize(int width, int height) {
089: size = new Dimension(width, height);
090: isClientSizeSet = true;
091: }
092:
093: public Dimension getClientSize() {
094: return getClientRect().getSize();
095: }
096:
097: public void setSize(int width, int height) {
098: size = new Dimension(width, height);
099: isClientSizeSet = false;
100: }
101:
102: public Dimension getSize() {
103: return getBounds().getSize();
104: }
105:
106: public Insets getInsets() {
107: return (Insets) insets.clone();
108: }
109:
110: public Rectangle getBounds() {
111: if (isClientSizeSet) {
112: Rectangle res = new Rectangle(loc, size);
113: res.width += (insets.left + insets.right);
114: res.height += (insets.top + insets.bottom);
115: return res;
116: } else {
117: return new Rectangle(loc, size);
118: }
119: }
120:
121: public Point getLocation() {
122: return new Point(loc);
123: }
124:
125: public void setLocation(int x, int y) {
126: loc = new Point(x, y);
127: }
128:
129: public Rectangle getScreenBounds() {
130: Dimension size = getClientSize();
131: Point location = getLocation(); // this is Java location
132: location.x += insets.left;
133: location.y += insets.top;
134: return new Rectangle(location, size);
135: }
136:
137: public void setInsets(Insets in) {
138: insets = (in != null) ? ((Insets) in.clone()) : new Insets(0,
139: 0, 0, 0);
140: if (!isClientSizeSet) {
141: if (size.width < (insets.left + insets.right)) {
142: size.width = (insets.left + insets.right);
143: }
144: if (size.height < (insets.top + insets.bottom)) {
145: size.height = (insets.top + insets.bottom);
146: }
147: }
148: }
149:
150: public boolean isClientSizeSet() {
151: return isClientSizeSet;
152: }
153:
154: public String toString() {
155: return "[" + loc + ", " + size + "("
156: + (isClientSizeSet ? "client" : "bounds") + ")+"
157: + insets + "]";
158: }
159:
160: public boolean equals(Object o) {
161: if (!(o instanceof WindowDimensions)) {
162: return false;
163: }
164: WindowDimensions dims = (WindowDimensions) o;
165: return ((dims.insets.equals(insets)))
166: && (getClientRect().equals(dims.getClientRect()))
167: && (getBounds().equals(dims.getBounds()));
168: }
169:
170: public int hashCode() {
171: return ((insets == null) ? (0) : (insets.hashCode()))
172: ^ getClientRect().hashCode() ^ getBounds().hashCode();
173: }
174: }
|