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

org.wings.SMenuItem Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2000,2005 wingS development team.
 *
 * This file is part of wingS (http://wingsframework.org).
 *
 * wingS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * Please see COPYING for the complete licence.
 */
package org.wings;

import org.wings.plaf.MenuBarCG;
import javax.swing.*;

/**
 * A chooseable item in a {@link SMenuBar} arranged inside a main {@link SMenu} topic.
 *
 * @author Andre Lison
 * @author Armin Haaf
 */
public class SMenuItem extends SButton {
    protected SComponent menuParent;
    private KeyStroke accelerator;
    private double acceleratorWidthScaleFactor = 0.4f;

    public SMenuItem() {
        init();
    }

    public SMenuItem(Action action) {
        super(action);
        init();
    }

    public SMenuItem(String text) {
        super(text);
        init();
    }

    public SMenuItem(SIcon icon) {
        super(icon);
        init();
    }

    public SMenuItem(String text, SIcon icon) {
        super(text, icon);
        init();
    }
    
    private void init() {
        setShowAsFormComponent(false);
        putClientProperty("drm:realParentComponent", "drm:null");
    }

    final void setParentMenu(SComponent menuParent) {
        SComponent oldVal = this.menuParent;
        this.menuParent = menuParent;
        propertyChangeSupport.firePropertyChange("parentMenu", oldVal, this.menuParent);
        setParentFrame(menuParent != null ? menuParent.getParentFrame() : null);
    }

    public SComponent getParentMenu() {
        return this.menuParent;
    }

    public void setCG(MenuBarCG cg) {
        super.setCG(cg);
    }

    public void setAccelerator(KeyStroke keyStroke) {
        KeyStroke oldVal = this.accelerator;
        accelerator = keyStroke;
        propertyChangeSupport.firePropertyChange("accelerator", oldVal, this.accelerator);
        if (accelerator != null) {
            getInputMap(WHEN_IN_FOCUSED_FRAME).put(accelerator, "item_accelerator");
            getActionMap().put("item_accelerator", getAction());
        }
    }

    public KeyStroke getAccelerator() {
        return accelerator;
    }

    @Override
    public boolean isRecursivelyVisible() {
        return visible && (menuParent != null ? menuParent.isRecursivelyVisible() : super.isRecursivelyVisible());
    }

    @Override
    public boolean getResidesInForm() {
        return true;
    }

    /**
     * This factor will be used to layout the menu. When we compute the menu width the
     * accelerator's part will be acceleratorWidthScaleFactor * accelerators text 
     * width in em measures. Default is 0.4 .    
     *   
     * @return
     */
    public double getAcceleratorWidthScaleFactor() {
      return acceleratorWidthScaleFactor;
    }

    /**
     * This factor will be used to layout the menu. When we compute the menu width, the
     * accelerator's part will be acceleratorWidthScaleFactor * accelerators text 
     * width in em measures. Default is 0.4 .    
     *   
     */
    public void setAcceleratorWidthScaleFactor(double acceleratorWidthScaleFactor) {
      this.acceleratorWidthScaleFactor = acceleratorWidthScaleFactor;
    }

}