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

org.wings.SPopupMenu 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.PopupMenuCG;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * A popup or context menu that can be attached to abitrary components.
 *
 * @author hengels
 */
public class SPopupMenu
        extends SComponent {
    protected final List menuItems = new ArrayList();
    private double widthScaleFactor = 0.7f;

    /**
     * Add a menu item to this menu.
     */
    public void add(SMenuItem menuItem) {
        menuItems.add(menuItem);
        menuItem.setParentMenu(this);
        menuItem.putClientProperty("drm:realParentComponent", this);
    }

    /**
     * Add a menu item to this menu.
     */
    public void add(SComponent menuItem) {
        menuItems.add(menuItem);
        menuItem.setParentFrame(getParentFrame());
        menuItem.putClientProperty("drm:realParentComponent", this);
    }
    
    /**
     * Add a separator to this menu.
     */
    public void addSeparator() {
       add(new SSeparator());
    }

    @Override
    public void setParentFrame(SFrame f) {
        if (getParentFrame() == null && f != null) {
            reload();
        }
        if (f != null || (f == null && !getSession().getMenuManager().isMenuLinked(this))) {
            super.setParentFrame(f);
            for (Object menuItem : menuItems) {
                ((SComponent) menuItem).setParentFrame(f);
            }
        }
    }

    /**
     * Add a menu item to this menu.
     */
    public void add(String menuitem) {
        this.add(new SMenuItem(menuitem));
    }

    public SComponent getMenuComponent(int pos) {
        return (SComponent) menuItems.get(pos);
    }

    /**
     * Return the number of items on the menu, including separators.
     */
    public int getMenuComponentCount() {
        return menuItems.size();
    }

    /**
     * Remove all {@link SMenuItem} from this menu.
     */
    public void removeAll() {
        while (menuItems.size() > 0) {
            remove(0);
        }
    }

    /**
     * Removes the menu item at specified index from the menu.
     */
    public void remove(int pos) {
        remove(getMenuComponent(pos));
    }

    /**
     * removes a specific menu item component.
     */
    public void remove(SComponent comp) {
        menuItems.remove(comp);
        comp.setParentFrame(null);
        comp.putClientProperty("drm:realParentComponent", "drm:null");
    }

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

    /**
     * Returns the scale factor for the width of the Menu components.
     * The length of the children texts is multiplied by this factor and set as
     * width (in em) for the children.
     *
     * @return Returns the widthScaleFactor.
     */
    public double getWidthScaleFactor() {
        return widthScaleFactor;
    }
    /**
     * Sets the scale factor for the width of the Menu components.
     * The length of the children texts is multiplied by this factor and set as
     * width (in em) for the children.
     *
     * Default value is 0.8.
     *
     * @param widthScaleFactor The widthScaleFactor to set.
     */
    public void setWidthScaleFactor(double widthScaleFactor) {
        double oldVal = this.widthScaleFactor;
        this.widthScaleFactor = widthScaleFactor;
        propertyChangeSupport.firePropertyChange("widthScaleFactor", oldVal, this.widthScaleFactor);
    }

    @Override
    public void setEnabled(boolean enabled) {
        if (enabled != isEnabled()) {
            Set menuLinks = getSession().getMenuManager().getMenueLinks(this);
            for (Object menuLink : menuLinks) {
                ((SComponent) menuLink).reload();
            }
        }
        super.setEnabled(enabled);
    }

    @Override
    public boolean isRecursivelyVisible() {
        return getSession().getMenuManager().isMenuLinked(this);
        //return super.isRecursivelyVisible();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy