Package com.nexirius.framework.swing

Source Code of com.nexirius.framework.swing.CFJButton

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.swing;

import com.nexirius.util.assertion.Assert;
import com.nexirius.util.resource.ClientResource;
import com.nexirius.util.resource.ClientResourceToolkit;
import com.nexirius.util.resource.ResourceChangeEvent;

import javax.swing.*;
import java.awt.*;


/**
* A JButton which responds to changes in client resources.
* Changes can include text, icon, mnemonic etc.
* <p/>
* Property name           Component Property
* -------------           ------------------
* resourceKey.buttonText  text
* resourceKey.icon        icon
* resourceKey.tip         tooltiptext
* <p/>
*
* @author Marcel Baumann
*         Date        Author           Changes/Enhancements
*         1999.02.25  MB              Created
*         1999.03.25  MB              Updated to use ClientResourceToolkit and
*         SwingUtilities
*/
public class CFJButton
        extends JButton
        implements CFJItem {



    // The key used to look up specific resources
    private String resourceKey;
    private ClientResource clientResource = null;

    /**
     * Construct a CFJButton
     * Need a null constructor so we can use this class in a GUI builder
     */
    public CFJButton() {
        super();
    }


    /**
     * Construct a CFJButton using ClientResourceToolkit instance of
     * client resource
     *
     * @param resourceKey The key to be used in establishing resources
     */
    public CFJButton(String resourceKey) {
        this(ClientResourceToolkit.instance().getClientResource(), resourceKey);
    }


    /**
     * Construct a CFJButton
     *
     * @param resource    The resource to be used to establish basic attributes
     *                    of the JButton
     * @param resourceKey The key to be used in establishing resources
     */
    public CFJButton(ClientResource resource, String resourceKey) {
        super(resourceKey);

        Assert.pre(resource != null, "Parameter resource is not null");
        Assert.pre(resourceKey != null, "Parameter resourceKey is not null");

        this.resourceKey = resourceKey;
        update(resource);
        resource.addResourceChangeListener(this);
        setDefaultCapable(false);
    }


    /**
     * Get the resource key
     *
     * @return the resource key
     */
    public String getResourceKey() {
        return this.resourceKey;
    }

    /**
     * Set the resource key
     *
     * @param resourceKey the resource key
     */
    public void setResourceKey(String resourceKey) {
        Assert.pre(resourceKey != null, "Parameter resourceKey is not null");
        this.resourceKey = resourceKey;
    }

    /**
     * @see import com.nexirius.util.resource.ResourceChangeListener;
     */
    public void resourceChange(ResourceChangeEvent event) {
        update(event.getClientResource());
    }


    /**
     * Update properties based on client resource
     *
     * @param clientResource the resource to be used to establish properties
     */
    public void update(ClientResource clientResource) {
        if (resourceKey == null) {
            return;
        }

        if (clientResource == null) {
            clientResource = this.clientResource;
        } else if (this.clientResource != clientResource) {
            this.clientResource = clientResource;
        }

        final ClientResource _clientResource = clientResource;
//        SwingUtilities.invokeLater
//        (
//            new Runnable()
//            {
//                public void run()
//                {

        String text = _clientResource.getButtonText(resourceKey);

        if (text == null) {
            text = resourceKey;
        }

        setText(text);

        final int mnemo = _clientResource.getMnemonic(resourceKey, ".buttonText");
        if (text != null && mnemo != -1) {
            setMnemonic(text.charAt(mnemo));
        }

        final Icon icon = _clientResource.getIcon(resourceKey);
        if (icon != null) {
            setIcon(icon);

            if (text.length() == 0) {
                setMargin(new Insets(0, 0, 0, 0));
            }
        }

        final String tip = _clientResource.getToolTipText(resourceKey);
        if (tip != null) {
            setToolTipText(tip);
        }
/* FIX there is no setAccelerator method defines in JButton (use a menu status instead!
          final String accel = _clientResource.getKeyAcceleratorText(resourceKey);
          if (accel!=null)
          {
            int mask = Util.getMask(accel);
            int vKey = Util.getVirtualKey(accel);
            if (vKey != 0)
            {
              setAccelerator(KeyStroke.getKeyStroke(vKey, mask, false));
            }
          }
*/
        invalidate();
        validate();
        //repaint();
//        }
//      }
//    );
    }
}
TOP

Related Classes of com.nexirius.framework.swing.CFJButton

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.