All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jgoodies.looks.plastic.PlasticSpinnerUI Maven / Gradle / Ivy

/*
 * Copyright (c) 2001-2008 JGoodies Karsten Lentzsch. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  o Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  o Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  o Neither the name of JGoodies Karsten Lentzsch nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jgoodies.looks.plastic;

import java.awt.Component;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicSpinnerUI;

import com.jgoodies.looks.common.ExtBasicArrowButtonHandler;
import com.jgoodies.looks.common.ExtBasicSpinnerLayout;


/**
 * The JGoodies Plastic Look&Feel implementation of SpinnerUI.
 * Configures the default editor to adjust font baselines and component
 * bounds. Also, changes the border of the buttons and the size of the arrows.
 *
 * @author Karsten Lentzsch
 * @version $Revision: 1.6 $
 */
public class PlasticSpinnerUI extends BasicSpinnerUI {


	public static ComponentUI createUI(JComponent b) {
		return new PlasticSpinnerUI();
	}


    /**
     * The mouse/action listeners that are added to the spinner's
     * arrow buttons.  These listeners are shared by all
     * spinner arrow buttons.
     *
     * @see #createNextButton
     * @see #createPreviousButton
     */
    private static final ExtBasicArrowButtonHandler nextButtonHandler
    							= new ExtBasicArrowButtonHandler("increment", true);
    private static final ExtBasicArrowButtonHandler previousButtonHandler
    							= new ExtBasicArrowButtonHandler("decrement", false);


    /**
     * Create a component that will replace the spinner models value
     * with the object returned by spinner.getPreviousValue.
     * By default the previousButton is a JButton
     * who's ActionListener updates it's JSpinner
     * ancestors model.  If a previousButton isn't needed (in a subclass)
     * then override this method to return null.
     *
     * @return a component that will replace the spinners model with the
     *     next value in the sequence, or null
     * @see #installUI
     * @see #createNextButton
     */
    protected Component createPreviousButton() {
        return new SpinnerArrowButton(SwingConstants.SOUTH, previousButtonHandler);
    }


    /**
     * Create a component that will replace the spinner models value
     * with the object returned by spinner.getNextValue.
     * By default the nextButton is a JButton
     * who's ActionListener updates it's JSpinner
     * ancestors model.  If a nextButton isn't needed (in a subclass)
     * then override this method to return null.
     *
     * @return a component that will replace the spinners model with the
     *     next value in the sequence, or null
     * @see #installUI
     * @see #createPreviousButton
     */
    protected Component createNextButton() {
        return new SpinnerArrowButton(SwingConstants.NORTH, nextButtonHandler);
    }


    /**
     * Create a LayoutManager that manages the editor,
     * nextButton, and previousButton children
     * of the JSpinner. These three children must be added with a constraint
     * that identifies their role: "Editor", "Next", and "Previous". The
     * default layout manager can handle the absence of any of these children.
     *
     * @return a LayoutManager for the editor, next button, and previous
     *         button.
     * @see #createNextButton
     * @see #createPreviousButton
     * @see #createEditor
     */
    protected LayoutManager createLayout() {
        return new ExtBasicSpinnerLayout();
    }


    /**
     * This method is called by installUI to get the editor component
     * of the JSpinner.  By default it just returns
     * JSpinner.getEditor().  Subclasses can override
     * createEditor to return a component that contains
     * the spinner's editor or null, if they're going to handle adding
     * the editor to the JSpinner in an
     * installUI override.
     * 

* Typically this method would be overridden to wrap the editor * with a container with a custom border, since one can't assume * that the editors border can be set directly. *

* The replaceEditor method is called when the spinners * editor is changed with JSpinner.setEditor. If you've * overriden this method, then you'll probably want to override * replaceEditor as well. * * @return the JSpinners editor JComponent, spinner.getEditor() by default * @see #installUI * @see #replaceEditor * @see JSpinner#getEditor */ protected JComponent createEditor() { JComponent editor = spinner.getEditor(); configureEditorBorder(editor); return editor; } /** * Called by the PropertyChangeListener when the * JSpinner editor property changes. It's the responsibility * of this method to remove the old editor and add the new one. By * default this operation is just: *

     * spinner.remove(oldEditor);
     * spinner.add(newEditor, "Editor");
     * 
* The implementation of replaceEditor should be coordinated * with the createEditor method. * * @see #createEditor * @see #createPropertyChangeListener */ protected void replaceEditor(JComponent oldEditor, JComponent newEditor) { spinner.remove(oldEditor); configureEditorBorder(newEditor); spinner.add(newEditor, "Editor"); } /** * Sets an empty border with the default text insets. */ private void configureEditorBorder(JComponent editor) { if ((editor instanceof JSpinner.DefaultEditor)) { JSpinner.DefaultEditor defaultEditor = (JSpinner.DefaultEditor) editor; JTextField editorField = defaultEditor.getTextField(); Insets insets = UIManager.getInsets("Spinner.defaultEditorInsets"); editorField.setBorder(new EmptyBorder(insets)); } else if ( (editor instanceof JPanel) && (editor.getBorder() == null) && (editor.getComponentCount() > 0)) { JComponent editorField = (JComponent) editor.getComponent(0); Insets insets = UIManager.getInsets("Spinner.defaultEditorInsets"); editorField.setBorder(new EmptyBorder(insets)); } } /** * It differs from its superclass in that it uses the same formula as JDK * to calculate the arrow height. */ private static final class SpinnerArrowButton extends PlasticArrowButton { private SpinnerArrowButton(int direction, ExtBasicArrowButtonHandler handler) { super(direction, UIManager.getInt("ScrollBar.width"), true); addActionListener(handler); addMouseListener(handler); } protected int calculateArrowHeight(int height, int width) { int arrowHeight = Math.min((height - 4) / 3, (width - 4) / 3); return Math.max(arrowHeight, 3); } protected int calculateArrowOffset() { return 1; } protected boolean isPaintingNorthBottom() { return true; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy