jdk/src/solaris/classes/sun/awt/X11/WindowDimensions.java
changeset 2 90ce3da70b43
child 1977 3a9980ccb120
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package sun.awt.X11;
       
    26 
       
    27 import java.awt.*;
       
    28 
       
    29 class WindowDimensions {
       
    30     private Point loc;
       
    31     private Dimension size;
       
    32     private Insets insets;
       
    33     private boolean isClientSizeSet;
       
    34 
       
    35     public WindowDimensions(int x, int y, int width, int height, boolean isClient) {
       
    36         this(new Rectangle(x, y, width, height), null, isClient);
       
    37     }
       
    38 
       
    39     public WindowDimensions(Rectangle rec, Insets ins, boolean isClient) {
       
    40         if (rec == null) {
       
    41             throw new IllegalArgumentException("Client bounds can't be null");
       
    42         }
       
    43         isClientSizeSet = isClient;
       
    44         this.loc = rec.getLocation();
       
    45         this.size = rec.getSize();
       
    46         setInsets(ins);
       
    47     }
       
    48 
       
    49     public WindowDimensions(Point loc, Dimension size, Insets in, boolean isClient) {
       
    50         this(new Rectangle(loc, size), in, isClient);
       
    51     }
       
    52 
       
    53     public WindowDimensions(Rectangle bounds, boolean isClient) {
       
    54         this(bounds, null, isClient);
       
    55     }
       
    56 
       
    57     public WindowDimensions(final WindowDimensions dims) {
       
    58         this.loc = new Point(dims.loc);
       
    59         this.size = new Dimension(dims.size);
       
    60         this.insets = (dims.insets != null)?((Insets)dims.insets.clone()):new Insets(0, 0, 0, 0);
       
    61         this.isClientSizeSet = dims.isClientSizeSet;
       
    62     }
       
    63 
       
    64     public Rectangle getClientRect() {
       
    65         if (isClientSizeSet) {
       
    66             return new Rectangle(loc, size);
       
    67         } else {
       
    68             // Calculate client bounds
       
    69             if (insets != null) {
       
    70                 return new Rectangle(loc.x, loc.y,
       
    71                                      size.width-(insets.left+insets.right),
       
    72                                      size.height-(insets.top+insets.bottom));
       
    73             } else {
       
    74                 return new Rectangle(loc, size);
       
    75             }
       
    76         }
       
    77     }
       
    78 
       
    79     public void setClientSize(Dimension size) {
       
    80         this.size = new Dimension(size);
       
    81         isClientSizeSet = true;
       
    82     }
       
    83 
       
    84     public void setClientSize(int width, int height) {
       
    85         size = new Dimension(width, height);
       
    86         isClientSizeSet = true;
       
    87     }
       
    88 
       
    89     public Dimension getClientSize() {
       
    90         return getClientRect().getSize();
       
    91     }
       
    92 
       
    93     public void setSize(int width, int height) {
       
    94         size = new Dimension(width, height);
       
    95         isClientSizeSet = false;
       
    96     }
       
    97 
       
    98     public Dimension getSize() {
       
    99         return getBounds().getSize();
       
   100     }
       
   101 
       
   102     public Insets getInsets() {
       
   103         return (Insets)insets.clone();
       
   104     }
       
   105     public Rectangle getBounds() {
       
   106         if (isClientSizeSet) {
       
   107             Rectangle res = new Rectangle(loc, size);
       
   108             res.width += (insets.left + insets.right);
       
   109             res.height += (insets.top + insets.bottom);
       
   110             return res;
       
   111         } else {
       
   112             return new Rectangle(loc, size);
       
   113         }
       
   114     }
       
   115 
       
   116     public Point getLocation() {
       
   117         return new Point(loc);
       
   118     }
       
   119     public void setLocation(int x, int y) {
       
   120         loc = new Point(x, y);
       
   121     }
       
   122 
       
   123     public Rectangle getScreenBounds() {
       
   124         Dimension size = getClientSize();
       
   125         Point location = getLocation(); // this is Java location
       
   126         location.x += insets.left;
       
   127         location.y += insets.top;
       
   128         return new Rectangle(location, size);
       
   129     }
       
   130 
       
   131     public void setInsets(Insets in) {
       
   132         insets = (in != null)?((Insets)in.clone()):new Insets(0, 0, 0, 0);
       
   133         if (!isClientSizeSet) {
       
   134             if (size.width < (insets.left+insets.right)) {
       
   135                 size.width = (insets.left+insets.right);
       
   136             }
       
   137             if (size.height < (insets.top+insets.bottom)) {
       
   138                 size.height = (insets.top+insets.bottom);
       
   139             }
       
   140         }
       
   141     }
       
   142 
       
   143     public boolean isClientSizeSet() {
       
   144         return isClientSizeSet;
       
   145     }
       
   146 
       
   147     public String toString() {
       
   148         return "[" + loc + ", " + size + "(" +(isClientSizeSet?"client":"bounds") + ")+" + insets + "]";
       
   149     }
       
   150 
       
   151     public boolean equals(Object o) {
       
   152         if (!(o instanceof WindowDimensions)) {
       
   153             return false;
       
   154         }
       
   155         WindowDimensions dims = (WindowDimensions)o;
       
   156         return ((dims.insets.equals(insets)))
       
   157             && (getClientRect().equals(dims.getClientRect()))
       
   158             && (getBounds().equals(dims.getBounds()));
       
   159     }
       
   160 
       
   161     public int hashCode() {
       
   162         return ((insets == null)? (0):(insets.hashCode())) ^ getClientRect().hashCode() ^ getBounds().hashCode();
       
   163     }
       
   164 }