View Javadoc

1   /*
2    * @(#) EditorToolbarActions.java 20-jul-2004 Copyright (c) 2002-2005 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.dsol.gui.editor2D.actions;
8   
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.util.Iterator;
12  import java.util.Map;
13  import java.util.Set;
14  
15  import javax.swing.JMenuItem;
16  
17  import nl.tudelft.simulation.dsol.animation.Editable;
18  import nl.tudelft.simulation.dsol.gui.editor2D.Editor2DPanel;
19  import nl.tudelft.simulation.dsol.gui.editor2D.EditorToolbar;
20  import nl.tudelft.simulation.logger.Logger;
21  
22  /***
23   * EditorToolbarActions.java <br>
24   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
25   * University of Technology </a>, the Netherlands. <br>
26   * See for project information <a
27   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
28   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
29   * General Public License (LGPL) </a>, no warranty.
30   * 
31   * @version $Revision$ $Date$
32   * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
33   *         </a>
34   */
35  public class EditorToolbarActions implements ActionListener
36  {
37  	/*** Parent */
38  	private EditorToolbar source = null;
39  
40  	/*** The panel */
41  	private Editor2DPanel panel = null;
42  
43  	/***
44  	 * Constructor
45  	 * 
46  	 * @param source The EditorToolbar
47  	 * @param panel The panel
48  	 */
49  	public EditorToolbarActions(final EditorToolbar source,
50  			final Editor2DPanel panel)
51  	{
52  		super();
53  		this.source = source;
54  		this.panel = panel;
55  	}
56  
57  	/***
58  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
59  	 */
60  	public void actionPerformed(final ActionEvent event)
61  	{
62  		Object src = event.getSource();
63  
64  		// De-select all toogle buttons
65  		this.source.getSelectObject().setSelected(false);
66  		this.source.getNewObject().setSelected(false);
67  		this.source.getEditObject().setSelected(false);
68  		this.source.getDeleteObject().setSelected(false);
69  		this.panel.setSelectedMode(Editor2DPanel.MODE_IDLE);
70  
71  		// Determine action and do it
72  		if (src.equals(this.source.getSelectObject()))
73  		{
74  			this.source.getSelectObject().setSelected(true);
75  			this.panel.setSelectedMode(Editor2DPanel.MODE_SELECT);
76  		} else if (src.equals(this.source.getNewObject()))
77  		{
78  			this.source.getNewObject().setSelected(true);
79  			this.panel.setSelectedMode(Editor2DPanel.MODE_NEW);
80  
81  			this.source.getItems().show(this.source.getNewObject(), 0, 0);
82  
83  		} else if (src.equals(this.source.getEditObject()))
84  		{
85  			this.source.getEditObject().setSelected(true);
86  			this.panel.setSelectedMode(Editor2DPanel.MODE_EDIT);
87  		} else if (src.equals(this.source.getDeleteObject()))
88  		{
89  			EditorUtilities.deleteEditable(this.panel
90  					.getSelectedEditableRenderable(), this.panel);
91  			// Object deleted: switch to select mode
92  			this.panel.setSelectedMode(Editor2DPanel.MODE_SELECT);
93  			this.source.getSelectObject().setSelected(true);
94  			this.source.getSelectObject().requestFocus();
95  		} else if (src instanceof JMenuItem)
96  		{
97  			// Determination of what object to create
98  			String text = ((JMenuItem) src).getText();
99  			Class editableClass = null;
100 			try
101 			{
102 				Map editables = Editable.listEditables();
103 				Set keys = editables.keySet();
104 				// Loop over the keys to find the classes in the Map.
105 				Iterator i = keys.iterator();
106 				boolean found = false;
107 				while ((i.hasNext()) && (!found))
108 				{
109 					String key = (String) i.next();
110 					if (key.equals(text))
111 					{
112 						String className = (String) editables.get(key);
113 						editableClass = Class.forName(className);
114 						found = true;
115 					}
116 				}
117 			} catch (Exception exception)
118 			{
119 				Logger.warning(this, "actionPerformed", exception);
120 			}
121 
122 			// Set the creation of the object if editableClass is not null
123 			if (editableClass != null)
124 			{
125 				this.panel.setSelectedRenderableClass(editableClass);
126 			}
127 
128 			// Make sure the new-button is pressed
129 			this.source.getNewObject().setSelected(true);
130 			this.panel.setSelectedMode(Editor2DPanel.MODE_NEW);
131 		}
132 
133 	}
134 
135 }