Package net.helipilot50.stocktrade.displayproject.actions

Source Code of net.helipilot50.stocktrade.displayproject.actions.DefaultButton

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. 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 This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "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 net.helipilot50.stocktrade.displayproject.actions;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JRootPane;
/**
* This pending action implements the DefaultButton flag on a PushButton or PictureButton
*
*/
public class DefaultButton extends PendingAction {
    private boolean setAsDefault = false;

    public DefaultButton(JButton pComponent, boolean defaultButton) {
        super(pComponent);
        this.setAsDefault = defaultButton;
    }

    public boolean isDefaultButton() {
        return this.setAsDefault;
    }

    public void performAction() {
        // TF:21/11/07: Refactored and commented this method for readability
        JButton newDefaultButton = (JButton)this._component;
        JRootPane rootPane = newDefaultButton.getRootPane();
        if (rootPane == null) {
            // If the button is not parented onto a Window, we cannot set the default button.
            return;
        }
        // Get the button that used to be the default button
        JButton oldDefaultButton = rootPane.getDefaultButton();

        if (this.setAsDefault) {
            if (oldDefaultButton == newDefaultButton) {
                // We're just resetting the default button to the same thing, do nothing.
                return;
            }
            // Make this button the default button
            newDefaultButton.setDefaultCapable(true);
            rootPane.setDefaultButton(newDefaultButton);
            // Set the old button not as the default if applicable
            if (oldDefaultButton != null) {
                oldDefaultButton.setDefaultCapable(false);
            }
        }
        else {
            // See if we're the default button
            if (oldDefaultButton == newDefaultButton) {
                rootPane.setDefaultButton(null);
            }
            // DK:11/11/2008: reset the default button as far as we do not want the button to be default any more.
            JButton initialDefault = (JButton)rootPane.getClientProperty("initialDefaultButton");
            if (newDefaultButton == initialDefault) {
              rootPane.putClientProperty("initialDefaultButton", null);
      }
         
            newDefaultButton.setDefaultCapable(false);
        }
    }

    public static void set(JButton comp, boolean value) {
        ActionMgr.addAction(new DefaultButton(comp, value));
    }
   
    /**
     * Gets default button for root container of the component.
     * @param component the component to obtain root container.
     * @return default button of the container or null if root container did not obtained or default button is not defined.
     */
    public static JButton get(JComponent component) {
        JRootPane rootPane = component.getRootPane();
        if (rootPane != null) {
            return rootPane.getDefaultButton();
        }
    return null;
  }

    public static boolean is(JButton comp) {
        if (comp == null)
            return false;
        boolean result = false;
        DefaultButton action = (DefaultButton) ActionMgr.getAction(comp,
                DefaultButton.class);
        if (action != null)
            result = action.isDefaultButton();
        else {
            if (comp.getRootPane() != null)
                result = (comp.getRootPane().getDefaultButton() == comp);
        }
        return result;

    }

}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.actions.DefaultButton

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.