001/* A radio menu item factory that creates actions for firing actions. 002 003 Copyright (c) 2008-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.vergil.toolbox; 029 030import java.util.Enumeration; 031 032import javax.swing.AbstractButton; 033import javax.swing.Action; 034import javax.swing.ButtonGroup; 035import javax.swing.JMenu; 036import javax.swing.JMenuItem; 037import javax.swing.JRadioButtonMenuItem; 038 039import diva.gui.toolbox.JContextMenu; 040import ptolemy.kernel.util.NamedObj; 041 042////////////////////////////////////////////////////////////////////////// 043//// RadioMenuActionFactory 044 045/** 046 A factory that adds a given action or set of actions as radio selections 047 to a context menu. All the actions created by an instance of this factory are 048 in the same group. Selecting one item in a group causes the currently selected 049 item in the same group unselected, if it is not the same as the newly selected 050 one. If an array of actions is given to the constructor, then the actions will 051 be put in a submenu with the specified label. 052 053 @author Thomas Huining Feng 054 @version $Id$ 055 @since Ptolemy II 8.0 056 @Pt.ProposedRating Red (tfeng) 057 @Pt.AcceptedRating Red (tfeng) 058 */ 059public class RadioMenuActionFactory extends MenuActionFactory { 060 061 /** Construct a factory that adds a given action as a radio selection to a 062 * given context menu. 063 * @param action The action to be associated with the context menu. 064 */ 065 public RadioMenuActionFactory(Action action) { 066 super(action); 067 } 068 069 /** Construct a factory that adds a given group of actions as radio 070 * selections to a given context menu in a submenu with the specified 071 * label. 072 * @param actions The actions to be in the submenu. 073 * @param label The label for the submenu. 074 */ 075 public RadioMenuActionFactory(Action[] actions, String label) { 076 super(actions, label); 077 } 078 079 /** Add an item to the given context menu that will configure the 080 * parameters on the given target. 081 * @param menu The context menu to add to. 082 * @param object The object that the menu item command will operate on. 083 * @return A menu item, or null to decline to provide a menu item. 084 */ 085 @Override 086 public JMenuItem create(JContextMenu menu, NamedObj object) { 087 int selected = -1; 088 if (_group != null) { 089 Enumeration<AbstractButton> elements = _group.getElements(); 090 int i = 0; 091 while (elements.hasMoreElements()) { 092 AbstractButton button = elements.nextElement(); 093 if (button.isSelected()) { 094 selected = i; 095 } 096 i++; 097 } 098 } 099 _group = new ButtonGroup(); 100 JMenuItem item = super.create(menu, object); 101 102 Enumeration<AbstractButton> elements = _group.getElements(); 103 int i = 0; 104 while (elements.hasMoreElements()) { 105 AbstractButton button = elements.nextElement(); 106 if (i >= selected) { 107 button.setSelected(true); 108 break; 109 } 110 i++; 111 } 112 return item; 113 } 114 115 /** Add an action to the context menu. 116 * @param menu The context menu. 117 * @param action The action to be added to the context menu. 118 * @param tooltip The tooltip for the action. 119 * @return The added menu item. 120 */ 121 @Override 122 protected JMenuItem _add(JContextMenu menu, Action action, String tooltip) { 123 String label = (String) action.getValue(Action.NAME); 124 125 if (tooltip == null) { 126 tooltip = (String) action.getValue("tooltip"); 127 } 128 action.putValue("tooltip", tooltip); 129 130 JRadioButtonMenuItem item = new JRadioButtonMenuItem(action); 131 item.setText(label); 132 item.setToolTipText(tooltip); 133 action.putValue("menuItem", item); 134 135 _group.add(item); 136 menu.add(item); 137 return item; 138 } 139 140 /** Add an action to the submenu. 141 * @param submenu The submenu. 142 * @param action The action to be added to the submenu. 143 * @return The added menu item. 144 */ 145 @Override 146 protected JMenuItem _add(JMenu submenu, Action action) { 147 String label = (String) action.getValue(Action.NAME); 148 149 String tooltip = (String) action.getValue("tooltip"); 150 action.putValue("tooltip", tooltip); 151 152 JRadioButtonMenuItem item = new JRadioButtonMenuItem(action); 153 item.setText(label); 154 item.setToolTipText(tooltip); 155 action.putValue("menuItem", item); 156 157 _group.add(item); 158 submenu.add(item); 159 return item; 160 } 161 162 /** The group that contains all the actions for this factory. */ 163 private ButtonGroup _group; 164}