1
2
3
4
5
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 2003 <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/gpl.html">General Public
36 * License (GPL) </a>, no warranty <br>
37 *
38 * @version 1.0 <br>
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
80
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
95 this.actions = new EditorToolbarActions(this, panel);
96
97
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
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
146 try
147 {
148 Map editables = Editable.listEditables();
149 Set keys = editables.keySet();
150
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) throws RemoteException
171 {
172
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 }