alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

// $Id: Vertical.java,v 1.7 2004/08/19 20:54:56 mvw Exp $
// Copyright (c) 2003-2004 The Regents of the University of California. All
// Rights Reserved. Permission to use, copy, modify, and distribute this
// software and its documentation without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph appear in all copies.  This software program and
// documentation are copyrighted by The Regents of the University of
// California. The software program and documentation are supplied "AS
// IS", without any accompanying services from The Regents. The Regents
// does not warrant that the operation of the program will be
// uninterrupted or error-free. The end-user understands that the program
// was developed for research purposes and is advised not to rely
// exclusively on the program for any reason.  IN NO EVENT SHALL THE
// UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
// CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

/*
 * Orientation.java
 */
package org.argouml.swingext;

import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;

/**
 * Various utilities to aid components that are aware of their
 * horizontal/vertical orientation.  The Singleton pattern is used to
 * ensure that only one instance of a horizontal and one instance of a
 * vertical Orientation exist.

* * Operations performed using length or breadth are transposed to * width and height depending on whether this is a vertical or * horizontal orientation.

* * Horizontal treats length as width, breadth as height and position * as x.

* * Vertical treats length as height, breadth as width and position as * y.

* *

 *    HORIZONTAL                                          VERTICAL
 *
 *                                                    A
 *                                                    |
 *                                                 position = y
 *                                                    |
 *                                                    V
 *                +-------------+   A                 +-------------+   A
 *                |             |   |                 |             |   |
 * <--position--> |             | breadth = height    |             | length =
 *    = x         |             |   |                 |             |   |height
 *                +-------------+   V                 +-------------+   V
 *                 <-- length-->                       <--breadth-->
 *                    = width                             = width
 *
* * @author Bob Tarling */ public class Vertical extends Orientation { private static final Vertical VERTICAL = new Vertical(); /** * The constructor. */ protected Vertical() { } /** * Get an instance of an Orientation object.

* * @return An instance of Orientation. */ public static Orientation getInstance() { return VERTICAL; } /** * Get an instance of an Orientation perpendicular to * this instance.

* * If called on a horizontal instance then a vertical instance is * returned.

* * If called on a vertical instance then a horizontal instance is * returned.

* * @return A vertical or horizontal orientation. */ public Orientation getPerpendicular() { return Horizontal.getInstance(); } /** * Get the length of a Dimension. * * @param dim The Dimension of which to determine * the length * @return The length of the Dimension. */ public int getLength(Dimension dim) { return (int) dim.getHeight(); } /** * Get the length of a Component. * * @param comp The Component of which to * determine the length * @return The length of the Component. */ public int getLength(Component comp) { return comp.getHeight(); } /** * Get the usable length of a Container minus its * insets. * * @param cont The Container of which to * determine the length * @return The length of the Component. */ public int getLengthMinusInsets(Container cont) { Insets insets = cont.getInsets(); return cont.getHeight() - (insets.top + insets.bottom); } /** * Get the breadth of a Dimension. * * @param dim The Dimension of which to determine * the breadth * @return The breadth of the Dimension. */ public int getBreadth(Dimension dim) { return (int) dim.getWidth(); } /** * Get the breadth of a Component. * * @param comp The Component of which to * determine the breadth * @return The breadth of the Component. */ public int getBreadth(Component comp) { return comp.getWidth(); } /** * Get the position of a Point. * * @param point The Point of which to determine * the position * @return The position of the Point. */ public int getPosition(Point point) { return (int) point.getY(); } /** * Get the offset of a Point. * * @param point The Point of which to determine the offset. * @return The position of the Point. */ public int getOffset(Point point) { return (int) point.getX(); } /** * Determines the last usable position in a * Container. This takes into account the * Insets of the Container. * * @param cont the Container from which to * determine the last usable position. * @return The offset of the Container. */ public int getLastUsablePosition(Container cont) { return cont.getHeight() - cont.getInsets().bottom; } /** * Determines the first usable offset in a * Container. This takes into account the * Insets of the Container. * * @param cont the Container from which to * determine the first usable position. * @return The offset of the Container. */ public int getFirstUsableOffset(Container cont) { return cont.getInsets().top; } /** * Generate a new Point object from position and offset values. * * @param position the required position of the new Point. * @param offset the required offset of the new Point. * @return The newly created Point object. */ public Point newPoint(int position, int offset) { return new Point(offset, position); } /** * Get the position of a Component. * * @param comp The Component of which to * determine the position * @return The position of the Component. */ public int getPosition(Component comp) { return comp.getY(); } /** * Get the position of a MouseEvent. * * @param me The MouseEvent of which to determine * the position * @return The position of the MouseEvent. */ public int getPosition(MouseEvent me) { return me.getY(); } /** * Create a new Dimension from an existing * Dimension with its length increased by a given * value. * * @param original The Dimension to be added to. * @param add The amount to add to the Dimension. * @return The resulting Dimension. */ public Dimension addLength(Dimension original, int add) { double width = original.getWidth(); double height = original.getHeight() + add; return new Dimension((int) width, (int) height); } /** * Create a new Point from an existing * Point with its position increased by a given * value. * * @param original The Point to be added to. * @param add The amount to add to the Point. * @return The resulting Point. */ public Point addToPosition(Point original, int add) { double x = original.getX(); double y = original.getY() + add; return new Point((int) x, (int) y); } /** * Create a new Dimension from an existing * Dimension with its length changed to a given * value. * * @param original The Dimension to be added to. * @param length The length to assign to the new * Dimension. * @return The resulting Dimension. */ public Dimension setLength(Dimension original, int length) { return new Dimension((int) original.getWidth(), length); } /** * Create a new Dimension from an existing * Dimension with its length changed to the length of * another given Dimension. * * @param original The Dimension to be added to. * @param length The Dimension whose length is to * be assigned to the new Dimension. * @return The resulting Dimension. */ public Dimension setLength(Dimension original, Dimension length) { return new Dimension((int) original.getWidth(), (int) length.getHeight()); } /** * Create a new Dimension from an existing * Dimension with its breadth changed to a given * value. * * @param original The Dimension to be added to. * @param breadth The breadth to assign to the new * Dimension. * @return The resulting Dimension. */ public Dimension setBreadth(Dimension original, int breadth) { return new Dimension(breadth, (int) original.getHeight()); } /** * Create a new Dimension from an existing * Dimension with its breadth changed to the breadth * of another given Dimension. * * @param original The Dimension to be added to. * @param breadth The Dimension whose breadth is * to be assigned to the new Dimension. * @return The resulting Dimension. */ public Dimension setBreadth(Dimension original, Dimension breadth) { return new Dimension((int) breadth.getWidth(), (int) original.getHeight()); } /** * Create a new Point from an existing * Point with its position changed to a given value. * * @param original The Point whose position is to * be modified. * @param position The value to assign as the new position. * @return The resulting Point. */ public Point setPosition(Point original, int position) { return new Point((int) original.getX(), position); } /** * Get a cursor object pointing in the same direction as the orientation. * * @return The resulting Cursor. */ public Cursor getCursor() { return new Cursor(Cursor.N_RESIZE_CURSOR); } /** * Get an arrow button pointing to the start of the orientation. * * @return The resulting ArrowButton. */ public ArrowButton getStartArrowButton() { return new ArrowButton(ArrowButton.NORTH, (Border) null); } /** * Get an arrow button pointing to the end of the orientation. * * @return The resulting ArrowButton. */ public ArrowButton getEndArrowButton() { return new ArrowButton(ArrowButton.SOUTH, (Border) null); } }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.