001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.common;
032:
033: import java.awt.Component;
034: import java.awt.Container;
035: import java.awt.Dimension;
036: import java.awt.Insets;
037: import java.awt.LayoutManager;
038:
039: import javax.swing.UIManager;
040:
041: /**
042: * A simple layout manager for the editor and the next/previous buttons.
043: * See the BasicSpinnerUI javadoc for more information about exactly how
044: * the components are arranged.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.3 $
048: */
049: public final class ExtBasicSpinnerLayout implements LayoutManager {
050:
051: /**
052: * Used by the default LayoutManager class - SpinnerLayout for
053: * missing (null) editor/nextButton/previousButton children.
054: */
055: private static final Dimension ZERO_SIZE = new Dimension(0, 0);
056:
057: private Component nextButton = null;
058: private Component previousButton = null;
059: private Component editor = null;
060:
061: public void addLayoutComponent(String name, Component c) {
062: if ("Next".equals(name)) {
063: nextButton = c;
064: } else if ("Previous".equals(name)) {
065: previousButton = c;
066: } else if ("Editor".equals(name)) {
067: editor = c;
068: }
069: }
070:
071: public void removeLayoutComponent(Component c) {
072: if (c == nextButton) {
073: c = null;
074: } else if (c == previousButton) {
075: previousButton = null;
076: } else if (c == editor) {
077: editor = null;
078: }
079: }
080:
081: private Dimension preferredSize(Component c) {
082: return (c == null) ? ZERO_SIZE : c.getPreferredSize();
083: }
084:
085: public Dimension preferredLayoutSize(Container parent) {
086: Dimension nextD = preferredSize(nextButton);
087: Dimension previousD = preferredSize(previousButton);
088: Dimension editorD = preferredSize(editor);
089:
090: Dimension size = new Dimension(editorD.width, editorD.height);
091: size.width += Math.max(nextD.width, previousD.width);
092: Insets insets = parent.getInsets();
093: size.width += insets.left + insets.right;
094: size.height += insets.top + insets.bottom;
095: return size;
096: }
097:
098: public Dimension minimumLayoutSize(Container parent) {
099: return preferredLayoutSize(parent);
100: }
101:
102: private void setBounds(Component c, int x, int y, int width,
103: int height) {
104: if (c != null) {
105: c.setBounds(x, y, width, height);
106: }
107: }
108:
109: public void layoutContainer(Container parent) {
110: int width = parent.getWidth();
111: int height = parent.getHeight();
112:
113: Insets insets = parent.getInsets();
114: Dimension nextD = preferredSize(nextButton);
115: Dimension previousD = preferredSize(previousButton);
116: int buttonsWidth = Math.max(nextD.width, previousD.width);
117: int editorHeight = height - (insets.top + insets.bottom);
118:
119: // The arrowButtonInsets value is used instead of the JSpinner's
120: // insets if not null. Defining this to be (0, 0, 0, 0) causes the
121: // buttons to be aligned with the outer edge of the spinner's
122: // border, and leaving it as "null" places the buttons completely
123: // inside the spinner's border.
124: Insets buttonInsets = UIManager
125: .getInsets("Spinner.arrowButtonInsets");
126: if (buttonInsets == null) {
127: buttonInsets = insets;
128: }
129:
130: /*
131: * Deal with the spinner's componentOrientation property.
132: */
133: int editorX, editorWidth, buttonsX;
134: if (parent.getComponentOrientation().isLeftToRight()) {
135: editorX = insets.left;
136: editorWidth = width - insets.left - buttonsWidth
137: - buttonInsets.right;
138: buttonsX = width - buttonsWidth - buttonInsets.right;
139: } else {
140: buttonsX = buttonInsets.left;
141: editorX = buttonsX + buttonsWidth;
142: editorWidth = width - buttonInsets.left - buttonsWidth
143: - insets.right;
144: }
145:
146: int nextY = buttonInsets.top;
147: int nextHeight = (height / 2) + (height % 2) - nextY;
148: int previousY = buttonInsets.top + nextHeight;
149: int previousHeight = height - previousY - buttonInsets.bottom;
150:
151: setBounds(editor, editorX, insets.top, editorWidth,
152: editorHeight);
153: setBounds(nextButton, buttonsX, nextY, buttonsWidth, nextHeight);
154: setBounds(previousButton, buttonsX, previousY, buttonsWidth,
155: previousHeight);
156: }
157: }
|