Package com.nexirius.framework.swing

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

//{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 com.nexirius.util.resource.Util;

import javax.swing.*;

/**
* Property name           Component Property
* -------------           ------------------
* resourceKey.menuText    text
* resourceKey.icon        icon
* resourceKey.accelerator accelerator
* resourceKey.tip         tooltiptext
* <p/>
*
* @author Marcel Baumann
*         <p/>
*         Date        Author           Changes/Enhancements
*         1999.02.25  MB              Created
*         1999.03.25  MB              Updated to use ClientResourceToolkit and
*         SwingUtilities
*/

public class CFJMenuItem extends JMenuItem
        implements CFJItem {



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

    /**
     * Construct a CFJMenuItem
     */
    public CFJMenuItem() {
        super("MenuItem");
    }


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


    /**
     * Construct a CFJMenuItem
     *
     * @param resource    The resource to be used to establish basic attributes
     *                    of the JMenuItem
     * @param resourceKey The key to be used in establishing resources
     */
    public CFJMenuItem(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);
    }


    /**
     * Get the resource key
     *
     * @return the resource key
     */
    public String getResourceKey() {
        return 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.getMenuText(resourceKey);
                        if (text != null) {
                            setText(text);
                        } else {
                            setText(resourceKey);
                        }

                        String accel = _clientResource.getKeyAcceleratorText(resourceKey);
                        if (text != null && accel != null) {
                            int mask = Util.getMask(accel);
                            int vKey = Util.getVirtualKey(accel);
                            if (vKey != 0) {
                                setAccelerator(KeyStroke.getKeyStroke(vKey, mask, false));
                            }
                        }

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

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

                        String tip = _clientResource.getToolTipText(resourceKey);
                        if (tip != null) {
                            setToolTipText(tip);
                        }

                        invalidate();
                        validate();
                        // repaint();
                    }
                });
    }
}
TOP

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

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.