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: Horizontal.java,v 1.7 2004/08/19 19:27:20 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.

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

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

/**
 * A horizontal implementation of Orientation.  The
 * Singleton pattern is used to ensure that only one instance of this
 * class can exist.

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

* *

 *    HORIZONTAL
 *
 *                                                    A
 *                                                    |
 *                                                 position = y
 *                                                    |
 *                                                    V
 *                +-------------+   A                 +-------------+   A
 *                |             |   |                 |             |   |
 * <--position--> |             | breadth = height    |             | length =
 *    = x         |             |   |                 |             |   |height
 *                +-------------+   V                 +-------------+   V
 *                 <-- length-->                       <--breadth-->
 *                    = width                             = width
 *
* * @author Bob Tarling */ public class Horizontal extends Orientation { private static final Horizontal HORIZONTAL = new Horizontal(); /** * The constructor. */ protected Horizontal() { } /** * Get an instance of a Horizontal object. * * @return An instance of Orientation. */ public static Orientation getInstance() { return HORIZONTAL; } /** * 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 Vertical.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.getWidth(); } /** * 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.getWidth(); } /** * 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.getWidth() - (insets.left + insets.right); } /** * 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.getHeight(); } /** * 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.getHeight(); } /** * 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.getX(); } /** * 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.getX(); } /** * Get the offset of a Component.

* * @param point The Component of which to * determine the offset. * @return The position of the Component. */ public int getOffset(Point point) { return (int) point.getY(); } /** * 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.getWidth() - cont.getInsets().right; } /** * 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().left; } /** * 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(position, offset); } /** * 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.getX(); } /** * 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() + add; double height = original.getHeight(); return new Dimension((int) width, (int) height); } /** * Create a new Dimension from an existing * Dimension with its length increased by the length * of another Dimension. * * @param original The Dimension to be added to. * @param add The Dimension whose length is to be * taken as the added value. * @return The resulting Dimension. */ public Dimension addLength(Dimension original, Dimension add) { return addLength(original, getLength(add)); } /** * Create a new Dimension from an existing * Dimension with its length increased by the length * of a Component. * * @param original The Dimension to be added to. * @param add The Component whose length is to be * taken as the added value. * @return The resulting Dimension. */ public Dimension addLength(Dimension original, Component add) { return addLength(original, getLength(add.getSize())); } /** * Create a new Dimension from an existing * Dimension with its length decreased by a given * value. * * @param original The Dimension to be subtracted from. * @param subtract The amount to subtract from the * Dimension. * @return The resulting Dimension. */ public Dimension subtractLength(Dimension original, int subtract) { return addLength(original, -subtract); } /** * Create a new Dimension from an existing * Dimension with its length decreased by the length * of another Dimension. * * @param original The Dimension to be subtracted from. * @param subtract The Dimension whose length is * to be taken as the subtracted value. * @return The resulting Dimension. */ public Dimension subtractLength(Dimension original, Dimension subtract) { return subtractLength(original, getLength(subtract)); } /** * Create a new Dimension from an existing * Dimension with its length decreased by the length * of a Component. * * @param original The Dimension to be subtracted from. * @param subtract The Component whose length is * to be taken as the subtracted value. * @return The resulting Dimension. */ public Dimension subtractLength(Dimension original, Component subtract) { return subtractLength(original, getLength(subtract.getSize())); } /** * 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() + add; double y = original.getY(); return new Point((int) x, (int) y); } /** * Create a new Point from an existing * Point with its length increased by the length of a * Dimension. * * @param original The Point to be added to. * @param add The Dimension whose length is to be * taken as the added value. * @return The resulting Point. */ public Point addToPosition(Point original, Dimension add) { return addToPosition(original, getLength(add)); } /** * Create a new Point from an existing * Point with its length increased by the length of a * Component. * * @param original The Point to be added to. * @param add The Dimension whose length is to be * taken as the added value. * @return The resulting Point. */ public Point addToPosition(Point original, Component add) { return addToPosition(original, getLength(add.getSize())); } /** * Create a new Point from an existing * Point with its position decreased by a given * value. * * @param original The Point to be added to. * @param subtract The amount to subtract to the Point. * @return The resulting Point. */ public Point subtractFromPosition(Point original, int subtract) { return addToPosition(original, -subtract); } /** * Create a new Point from an existing * Point with its length decreased by the length of a * Dimension. * * @param original The Point to be added to. * @param subtract The Dimension whose length is * to be taken as the subtracted value. * @return The resulting Point. */ public Point subtractFromPosition(Point original, Dimension subtract) { return addToPosition(original, -getLength(subtract)); } /** * Create a new Point from an existing * Point with its length decreased by the length of a * Component. * * @param original The Point to be added to. * @param subtract The Component whose length is * to be taken as the subtracted value. * @return The resulting Point. */ public Point subtractFromPosition(Point original, Component subtract) { return addToPosition(original, -getLength(subtract.getSize())); } /** * 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(length, (int) original.getHeight()); } /** * 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) length.getWidth(), (int) original.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((int) original.getWidth(), breadth); } /** * 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) original.getWidth(), (int) breadth.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(position, (int) original.getY()); } /** * Get a cursor object pointing in the same direction as the orientation. * * @return The resulting Cursor. */ public Cursor getCursor() { return new Cursor(Cursor.W_RESIZE_CURSOR); } /** * Get an arrow button pointing to the start of the orientation. * * @return The resulting ArrowButton. */ public ArrowButton getStartArrowButton() { return new ArrowButton(ArrowButton.WEST, (Border) null); } /** * Get an arrow button pointing to the end of the orientation. * * @return The resulting ArrowButton. */ public ArrowButton getEndArrowButton() { return new ArrowButton(ArrowButton.EAST, (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.