/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library 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.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.ts;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;

/**
 * A PopupMenu especially designed to work with the
 * TSList component. It is invoked, when the RIGHT mouse button
 * is pressed over a time series list. The underlying TSList can
 * be referenced by getTSList after the popup was triggered. The
 * component also takes care, that the registered TSActionTypes
 * and TSMenuTypes get a reference to the underlying
 * TSList via the TSListAccessor interface.
 * 

* * The DefaultTSListPopup can be customized by adding/removing * TSActionTypes or TSMenuTypes. New * actions/menus can easily be created by subclassing TSActionTypes * or TSMenuTypes. This is the preferred and easiest way of * costumizing the DefaultTSListPopup, although subclassing or * using other methods provided by JPopupMenu are also possible. * * @author Markus Kraetzig */ public class DefaultTSListPopup extends JPopupMenu { /** * Default instance that can be shared between TSList instances. */ private static final DefaultTSListPopup defaultTSListPopup = new DefaultTSListPopup( "DEFAULT_TSList_PopupMenu"); /** * Reference to the underlying list. */ private TSList tSList = null; /** * Name of the popup. */ private final String name; private final List actionList = new ArrayList(); /** * Creates a DefaultTSListPopup with a descriptive name. Use * this constructor only, if you need a instance that should be different * from getSharedInstance. * * @param label * String */ public DefaultTSListPopup(String label) { super(label); this.name = label; addTSMenu(TSMenuTypes.SET_TSTYPE); addSeparator(); addTSAction(TSActionTypes.TRANSFORM); addTSAction(TSActionTypes.CREATE_DUMMY); addTSAction(TSActionTypes.EDIT_TS); addSeparator(); addTSAction(TSActionTypes.DELETE); addTSAction(TSActionTypes.RENAME); addSeparator(); addTSAction(TSActionTypes.SHOW_DESCRIPTION); addTSAction(TSActionTypes.SAVE_DAT); } /** * Adds an action to this popup menu. Nothing is done, if the same action * was already added. * * @param actionType * the action to add */ public final void addTSAction(TSActionTypes actionType) { if (!actionList.contains(actionType)) { actionList.add(actionType); add(actionType); } } /** * Adds an action to this popup menu at a given index. Nothing is done, if * the same action was already added. * * @param actionType * the action to add * @param pos * index of new position * @throws IllegalArgumentException * if pos not valid */ public final void addTSActionAt(TSActionTypes actionType, int pos) { if (!actionList.contains(actionType)) { if (pos < 0 || pos > getComponentCount()) throw new IllegalArgumentException("Position " + pos + " not in [0," + getComponentCount() + "]."); actionList.add(actionType); add(new JMenuItem(actionType), pos); } } /** * Adds a menu to this popup menu. Nothing is done, if the same menu was * already added. * * @param tsMenu * the menu to add */ public final void addTSMenu(TSMenuTypes tsMenu) { if (!actionList.contains(tsMenu)) { actionList.add(tsMenu); add(tsMenu); } } /** * Adds a menu to this popup menu at a given index. Nothing is done, if the * same menu was already added. * * @param tsMenu * the menu to add * @param pos * index of new position * @throws IllegalArgumentException * if pos not valid */ public final void addTSMenuAt(TSMenuTypes tsMenu, int pos) { if (!actionList.contains(tsMenu)) { if (pos < 0 || pos > getComponentCount()) throw new IllegalArgumentException("Position " + pos + " not in [0," + getComponentCount() + "]."); actionList.add(tsMenu); add(tsMenu, pos); } } /** * Returns an instance of the popup, that can be shared across all * TSList instances. It is also possible to create instances * with the constructor that can be individually configured and set to a * TSList that should have a different popup. * * @return an instance of this class that can be shared */ public static DefaultTSListPopup getSharedInstance() { return defaultTSListPopup; } /** * Gets the reference to the underlying TSList that this * popup is invoked on. * * @return the currently referenced TSList */ public final TSList getTSList() { return tSList; } /** * Triggers the popup menu if the invoker was a TSList. * * @param e * the mouse event * @param path * can be null in this context * @param manager * can be null in this context */ public void processMouseEvent(MouseEvent e, MenuElement[] path, MenuSelectionManager manager) { if (!SwingUtilities.isRightMouseButton(e)) return; Component c = e.getComponent(); /** * Show only for TSList. */ if (c instanceof TSList) { tSList = (TSList) c; // Enable/Disable items according to selection for (int i = 0; i < actionList.size(); i++) actionList.get(i).enableForTSList(tSList); pack(); show(tSList, e.getX(), e.getY()); } } /** * Removes actions from the popupmenu. Nothing is done, if the action does * not exist. * * @param actionType * the action to be removed */ public final void removeAction(TSActionTypes actionType) { if (actionList.contains(actionType)) { actionList.remove(actionType); removeAll(); for (int i = 0; i < actionList.size(); i++) add((AbstractAction) actionList.get(i)); } } /** * Sets the reference to the TSList that this popup is * invoked on. * * @param newTsList * the list to be referenced */ public final void setTSList(TSList newTsList) { tSList = newTsList; } /** * Returns a string representation of DefaultTSListPopup. * * @return string representation */ public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(getClass().getName() + "[name=" + name); for (int i = 0; i < getComponentCount(); i++) buffer.append("," + getComponent(i).getName()); buffer.append("]"); return buffer.toString(); } }

Ads help maintain this website.