001 /** 002 * Java Gui Builder - A library to build GUIs using an XML file. 003 * Copyright 2002, 2003 (C) François Beausoleil 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package jgb.handlers.swing; 021 022 import jgb.builder.utils.KeyStrokeParser; 023 import org.xml.sax.SAXException; 024 025 import javax.swing.*; 026 import java.util.Map; 027 028 /** 029 * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a> 030 */ 031 public class ItemTagHandler extends AbstractControlTagHandler { 032 /** 033 * The prefix to add to the group name for searching in the context's 034 * {@link jgb.builder.TagHandler#OBJECTS_MAP_KEY OBJECTS_MAP_KEY}. 035 * <p>When adding menu radio buttons in groups, </p> 036 */ 037 public static final String GROUP_NAME_PREFIX = "items.groups."; 038 039 /** 040 * The value of the type attribute which instantiates a "normal" menu item. 041 */ 042 public static final String TYPE_NORMAL = "normal"; 043 044 /** 045 * The value of the type attribute which instantiates a checkbox menu item. 046 */ 047 public static final String TYPE_CHECK = "check"; 048 049 protected void enterElement(Map atts) throws SAXException { 050 JMenuItem item = null; 051 if (TYPE_CHECK.equals(atts.get(ATTR_TYPE))) { 052 item = new JCheckBoxMenuItem(); 053 } else if (TYPE_RADIO.equals(atts.get(ATTR_TYPE))) { 054 item = new JRadioButtonMenuItem(); 055 } else { 056 item = new JMenuItem(); 057 } 058 059 if (TYPE_NORMAL.equals(atts.get(ATTR_TYPE)) == false 060 && atts.containsKey(ATTR_SELECTED)) { 061 if ("true".equals(atts.get(ATTR_SELECTED))) { 062 item.setSelected(true); 063 } 064 } 065 066 if (atts.containsKey(ATTR_TEXT)) { 067 item.setText((String)atts.get(ATTR_TEXT)); 068 } 069 070 if (atts.containsKey(ATTR_MNEMONIC)) { 071 item.setMnemonic(((String)atts.get(ATTR_MNEMONIC)).charAt(0)); 072 } 073 074 if (atts.containsKey(ATTR_ACCELERATOR)) { 075 item.setAccelerator( 076 KeyStrokeParser.getKeyStroke( 077 (String)atts.get(ATTR_ACCELERATOR)) 078 ); 079 } 080 081 if (atts.containsKey(ATTR_GROUP)) { 082 String groupName = (String)atts.get(ATTR_GROUP); 083 ButtonGroup group = (ButtonGroup)getObject(GROUP_NAME_PREFIX + groupName); 084 if (group == null) { 085 group = new ButtonGroup(); 086 putObject(GROUP_NAME_PREFIX + groupName, group); 087 } 088 089 group.add(item); 090 } 091 092 if (atts.containsKey(ATTR_ID)) { 093 putObject((String)atts.get(ATTR_ID), item); 094 } 095 096 JMenu menu = (JMenu)getCurrentControl(); 097 menu.add(item); 098 099 pushCurrentObject((String)atts.get(ATTR_ID), item); 100 } 101 102 protected void exitElement() throws SAXException { 103 popCurrentObject(); 104 } 105 106 protected String getDefaultPackagePrefix() { 107 return null; 108 } 109 }