View Javadoc

    1   /*
    2    * @(#) EditorToolbar.java 20-jul-2004 Copyright (c) 2002-2005 Delft University
    3    * of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights
    4    * reserved. This software is proprietary information of Delft University of
    5    * Technology The code is published under the Lesser General Public License
    6    */
    7   package nl.tudelft.simulation.dsol.gui.editor2D;
    8
    9   import java.rmi.RemoteException;
    10  import java.util.Iterator;
    11  import java.util.Map;
    12  import java.util.Set;
    13
    14  import javax.swing.ImageIcon;
    15  import javax.swing.JMenuItem;
    16  import javax.swing.JPopupMenu;
    17  import javax.swing.JToggleButton;
    18  import javax.swing.JToolBar;
    19  import javax.swing.SwingConstants;
    20
    21  import nl.tudelft.simulation.dsol.animation.Editable;
    22  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
    23  import nl.tudelft.simulation.dsol.gui.editor2D.actions.EditorToolbarActions;
    24  import nl.tudelft.simulation.event.EventInterface;
    25  import nl.tudelft.simulation.event.EventListenerInterface;
    26  import nl.tudelft.simulation.language.io.URLResource;
    27  import nl.tudelft.simulation.logger.Logger;
    28
    29  /***
    30   * EditorToolbar.java <br>
    31   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
    32   * University of Technology </a>, the Netherlands. <br>
    33   * See for project information <a
    34   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    35   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
    36   * General Public License (LGPL) </a>, no warranty.
    37   *
    38   * @version $Revision$ $Date$
    39   * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
    40   *         </a>
    41   */
    42  public class EditorToolbar extends JToolBar implements EventListenerInterface
    43  {
    44
    45  	/*** The application */
    46  	private DSOLApplicationInterface application = null;
    47
    48  	/*** Select button */
    49  	private JToggleButton selectObject = null;
    50
    51  	/*** New button */
    52  	private JToggleButton newObject = null;
    53
    54  	/*** Edit button */
    55  	private JToggleButton editObject = null;
    56
    57  	/*** Delete button */
    58  	private JToggleButton deleteObject = null;
    59
    60  	/***
    61  	 * Popup menu with items that can be created by pressing the new-button.
    62  	 */
    63  	private JPopupMenu items = null;
    64
    65  	/*** Actions */
    66  	private EditorToolbarActions actions = null;
    67
    68  	/***
    69  	 * Constructor
    70  	 *
    71  	 * @param application The application
    72  	 * @param panel The panel
    73  	 */
    74  	public EditorToolbar(final DSOLApplicationInterface application,
    75  			final Editor2DPanel panel)
    76  	{
    77  		super();
    78  		this.application = application;
    79  		// Listen to the experiment changed event to be
    80  		// able to update the popup
    81  		try
    82  		{
    83  			this.application.addListener(this,
    84  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    85  		} catch (RemoteException exception)
    86  		{
    87  			Logger.warning(this, "EditorToolbar", exception);
    88  		}
    89
    90  		this.setFloatable(true);
    91  		this.setOrientation(SwingConstants.VERTICAL);
    92  		this.setRollover(true);
    93
    94  		// Initialize actions
    95  		this.actions = new EditorToolbarActions(this, panel);
    96
    97  		// Buttons
    98  		this.selectObject = createButton("", "Select an object",
    99  				"/editorToolbarButtons/select.gif");
    100 		this.newObject = createButton("", "Create a new object",
    101 				"/editorToolbarButtons/new.gif");
    102 		this.editObject = createButton("", "Edit the selected object",
    103 				"/editorToolbarButtons/edit.gif");
    104 		this.deleteObject = createButton("", "Delete the selected object",
    105 				"/editorToolbarButtons/delete.gif");
    106
    107 		// Popup menu
    108 		this.items = createMenu();
    109 	}
    110
    111 	/***
    112 	 * Create a toolbar button
    113 	 *
    114 	 * @param text Text on the button
    115 	 * @param tooltip Tooltip text
    116 	 * @param icon Location of icon
    117 	 * @return Button
    118 	 */
    119 	private JToggleButton createButton(final String text, final String tooltip,
    120 			final String icon)
    121 	{
    122 		JToggleButton button = new JToggleButton(text);
    123 		if (!icon.equals(""))
    124 		{
    125 			button.setIcon(new ImageIcon(URLResource.getResource(icon)));
    126 		}
    127 		button.setToolTipText(tooltip);
    128 		button.addActionListener(this.actions);
    129 		this.add(button);
    130 		return button;
    131 	}
    132
    133 	/***
    134 	 * Create a combobox with items to create
    135 	 *
    136 	 * @return JComboBox
    137 	 */
    138 	private JPopupMenu createMenu()
    139 	{
    140 		JPopupMenu popup = new JPopupMenu();
    141
    142 		JMenuItem menuItem = null;
    143 		popup.removeAll();
    144
    145 		// Fill the popup
    146 		try
    147 		{
    148 			Map editables = Editable.listEditables();
    149 			Set keys = editables.keySet();
    150 			// Loop over the keys to find the classes in the Map.
    151 			for (Iterator i = keys.iterator(); i.hasNext();)
    152 			{
    153 				Object key = i.next();
    154
    155 				menuItem = new JMenuItem(key.toString());
    156 				menuItem.addActionListener(this.actions);
    157 				popup.add(menuItem);
    158 			}
    159 		} catch (Exception exception)
    160 		{
    161 			Logger.warning(this, "actionPerformed", exception);
    162 		}
    163
    164 		return popup;
    165 	}
    166
    167 	/***
    168 	 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
    169 	 */
    170 	public void notify(final EventInterface event)
    171 	{
    172 		// Recreate the popup menu
    173 		this.items = this.createMenu();
    174 	}
    175
    176 	/***
    177 	 * @return Returns the deleteObject.
    178 	 */
    179 	public JToggleButton getDeleteObject()
    180 	{
    181 		return this.deleteObject;
    182 	}
    183
    184 	/***
    185 	 * @return Returns the editObject.
    186 	 */
    187 	public JToggleButton getEditObject()
    188 	{
    189 		return this.editObject;
    190 	}
    191
    192 	/***
    193 	 * @return Returns the newObject.
    194 	 */
    195 	public JToggleButton getNewObject()
    196 	{
    197 		return this.newObject;
    198 	}
    199
    200 	/***
    201 	 * @return Returns the selectObject.
    202 	 */
    203 	public JToggleButton getSelectObject()
    204 	{
    205 		return this.selectObject;
    206 	}
    207
    208 	/***
    209 	 * @return Returns the items.
    210 	 */
    211 	public JPopupMenu getItems()
    212 	{
    213 		return this.items;
    214 	}
    215 }