001 /* PAVLOV -- Multiple Choice Study System 002 * Copyright (C) 2000 - 2004 T.J. Willis 003 * 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License 006 * as published by the Free Software Foundation; either version 2 007 * of the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. 017 * 018 * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/swing/HashtableJRadioButtonMenu.java,v 1.3 2004/05/10 15:02:41 tj_willis Exp $ 019 */ 020 package net.sourceforge.pavlov.swing; 021 import javax.swing.*; 022 import java.util.Hashtable; 023 import java.awt.event.*; 024 025 // note.. hashtable will only be populated 026 // by calls to methods overriden in this class. 027 // a complete implementation of this class 028 // would override all such methods 029 030 // also, class is schizo as to whether it deals with JMenuItem or JRadio...MenuItem 031 /** 032 * This is a _partial_ implementation of a JMenu that creates 033 * JRadioButtonMenuItems for all the keys in a given hashtable. 034 * This class needs some serious polishing. 035 * @author <a href="mailto:tj_willis@users.sourceforge.net"></a> 036 * @version 1.0 037 */ 038 public class HashtableJRadioButtonMenu extends JMenu { 039 // FIXED: reduce access 040 private Hashtable<String,JMenuItem> _items; 041 042 /** 043 * Creates a new <code>HashtableJRadioButtonMenu</code> instance. 044 * 045 */ 046 public HashtableJRadioButtonMenu() { 047 super(); 048 _items = new Hashtable<String,JMenuItem>(); 049 } 050 051 /** 052 * Creates a new <code>HashtableJRadioButtonMenu</code> instance. 053 * 054 * @param a an <code>Action</code> value 055 */ 056 public HashtableJRadioButtonMenu(Action a) { 057 super(a); 058 _items = new Hashtable<String,JMenuItem>(); 059 } 060 061 /** 062 * Creates a new <code>HashtableJRadioButtonMenu</code> instance. 063 * 064 * @param s a <code>String</code> value 065 */ 066 public HashtableJRadioButtonMenu(String s) { 067 super(s); 068 _items = new Hashtable<String,JMenuItem>(); 069 } 070 071 /** 072 * Creates a new <code>HashtableJRadioButtonMenu</code> instance. 073 * 074 * @param s a <code>String</code> value 075 * @param b a <code>boolean</code> value 076 */ 077 public HashtableJRadioButtonMenu(String s, boolean b) { 078 super(s, b); 079 _items = new Hashtable<String,JMenuItem>(); 080 } 081 082 /** 083 * Adds the given menuitem, placing a reference in the hashtable. 084 * 085 * @param menuItem a <code>JMenuItem</code> value 086 * @return a <code>JMenuItem</code> value 087 */ 088 public JMenuItem add(JMenuItem menuItem) { 089 _items.put(menuItem.getText(), menuItem); 090 JMenuItem x = super.add(menuItem); 091 092 return x; 093 } 094 095 /** 096 * Adds a JMenuItem with the given string. Adds the string as its 097 * hashtable key. 098 * 099 * @param s a <code>String</code> value 100 * @return a <code>JMenuItem</code> value 101 */ 102 public JMenuItem add(String s) { 103 JMenuItem x = super.add(s); 104 105 _items.put(s, x); 106 return x; 107 } 108 109 // get-by-name is the whole motivation for this class 110 /** 111 * Returns the JMenuItem corresponding to the given key string. 112 * 113 * @param s a <code>String</code> value 114 * @return a <code>JMenuItem</code> value 115 */ 116 public JMenuItem get(String s) { 117 return _items.get(s); 118 } 119 120 // add a bunch of names with a single actionlistener 121 /** 122 * Describe <code>addAllAsJRadioButtonMenuItems</code> method here. 123 * 124 * @param names a <code>String[]</code> value 125 * @param lis a <code>ActionListener</code> value 126 */ 127 public void addAllAsJRadioButtonMenuItems(String[] names, ActionListener lis) { 128 int sz = java.lang.reflect.Array.getLength(names); 129 130 for (int i = 0; i < sz; i++) { 131 JRadioButtonMenuItem dir1 = new JRadioButtonMenuItem(names[i]); 132 133 dir1.addActionListener(lis); 134 add(dir1); 135 } 136 } 137 138 //set a particular item to selected by name 139 /** 140 * Sets the named JRadioButtonMenuItem to selected. 141 * 142 * @param _name a <code>String</code> value 143 * @param tog a <code>boolean</code> value 144 */ 145 public void setSelected(String _name, boolean tog) { 146 // FIXME: check for classcast exception 147 JRadioButtonMenuItem dir1 = (JRadioButtonMenuItem) get(_name); 148 149 if (dir1 == null) return; 150 dir1.setSelected(tog); 151 } 152 153 }