source: tmcsimulator-scriptbuilder/trunk/src/event/editor/PropertyPanels.java @ 89

Revision 89, 6.6 KB checked in by bmcguffin, 9 years ago (diff)

Added dropdown menu item to ScriptBuilderFrame?: "Delete Incident". When clicked, user may select an existing incident to delete. Program will prompt user to confirm the deletion, then remove the incident from the script and refresh the display.

Added button to individual event editor window: "Remove this event". When clicked, the currently displayed event will be removed from the timeslice it is in. The display will be refreshed accordingly. NOTE: This still has some bugs, namely that the last remaining event in a timeslice fails to be deleted.

Restructured Interface ScriptEventEditorPanel? to include a removeAssociatedEvent method, which calls a new method in I_ScriptEvent called removeThis, which causes the event to be removed from its timeslice.

Editor.Java previously contained several classes and enums, none of which were set to private scope. Moved these extra classes to their own files to decrease clutter in Editor.java and increase readability of all files.

Line 
1package event.editor;
2
3import java.util.EnumMap;
4import java.util.Observable;
5import java.util.Vector;
6import javax.swing.JPanel;
7
8/**
9 *
10 * @author Bryan McGuffin
11 */
12public class PropertyPanels extends Observable
13{
14
15    private Vector<PropertyPanel> properties = new Vector<PropertyPanel>();
16
17    private EnumMap<Properties, Integer> propertyCounter
18            = new EnumMap<Properties, Integer>(Properties.class);
19
20    public PropertyPanels()
21    {
22        for (Properties property : Properties.values())
23        {
24            propertyCounter.put(property, 0);
25        }
26    }
27
28    private int nextIndex(Properties property)
29    {
30        propertyCounter.put(property, propertyCounter.get(property) + 1);
31
32        return propertyCounter.get(property);
33    }
34
35    /**
36     * public Vector<PropertyPanel> getProperties() { return
37     * (Vector<PropertyPanel>) properties.clone(); }
38     */
39    private int add(PropertyPanel panel)
40    {
41        int position = 0;
42
43        for (int i = 0; i < properties.size(); i++)
44        {
45            if (properties.get(i).title().compareTo(panel.title()) < 0)
46            {
47                position = i + 1;
48            }
49        }
50
51        properties.add(position, panel);
52
53        return position;
54    }
55
56    public PropertyPanel addPropertyPanel(Properties property, JPanel panel)
57    {
58        PropertyPanel propertyPanel = null;
59
60        if (property.getType() == PropertyTypes.Multiple)
61        {
62            propertyPanel = new MultPropertyPanel(panel, property, nextIndex(property));
63            //propertyPanel.addObserver(this);
64            int position = add(propertyPanel);
65            setChanged();
66            notifyObservers(new PropertyUpdate(UpdateType.Add, propertyPanel, position));
67
68        }
69        else if (property.getType() == PropertyTypes.Optional)
70        {
71            if (containsProperty(property))
72            {
73                throw new RuntimeException("Property \"" + property + "\" is already added");
74            }
75            else
76            {
77                propertyPanel = new PropertyPanel(panel, property);
78                int position = add(propertyPanel);
79                //propertyPanel.addObserver(this);
80                setChanged();
81                notifyObservers(new PropertyUpdate(UpdateType.Add, propertyPanel, position));
82            }
83        }
84        else
85        {
86            throw new RuntimeException("Property \"" + property + "\" not accounted for");
87        }
88
89        return propertyPanel;
90    }
91
92    private boolean remove(PropertyPanel panel)
93    {
94        boolean result = false;
95
96        if ((result = properties.remove(panel)))
97        {
98            setChanged();
99            notifyObservers(new PropertyUpdate(UpdateType.Remove, panel));
100        }
101        else
102        {
103            throw new RuntimeException("Attempted to remove panel that did not exist");
104        }
105
106        if (panel.getProperty().getType() == PropertyTypes.Multiple)
107        {
108            Vector<MultPropertyPanel> panelsOfSameType = new Vector<MultPropertyPanel>();
109            Vector<MultPropertyPanel> panelsOfGreaterIndex = new Vector<MultPropertyPanel>();
110
111            propertyCounter.put(panel.getProperty(), propertyCounter.get(panel.getProperty()) - 1);
112
113            // extract panels of the same property type
114            for (PropertyPanel pan : properties)
115            {
116                if (pan.getProperty() == panel.getProperty())
117                {
118                    panelsOfSameType.add((MultPropertyPanel) pan);
119                }
120            }
121
122            // extract panels whose index is greater than the index of the panel
123            // being removed
124            for (MultPropertyPanel pan : panelsOfSameType)
125            {
126                if (pan.getIndex() > ((MultPropertyPanel) panel).getIndex())
127                {
128                    panelsOfGreaterIndex.add(pan);
129                }
130            }
131
132            // decrement the index of each of the remaining panels by one
133            for (MultPropertyPanel pan : panelsOfGreaterIndex)
134            {
135                pan.setIndex(pan.getIndex() - 1);
136                setChanged();
137                notifyObservers(new PropertyUpdate(UpdateType.TitleChange, pan));
138            }
139        }
140        else if (panel.getProperty().getType() == PropertyTypes.Optional)
141        {
142            // do nothing
143        }
144        else
145        {
146            throw new RuntimeException("PropertyTypes not accounted for");
147        }
148
149        return result;
150    }
151
152    public PropertyPanel removeProperty(Properties property)
153    {
154        if (property.getType() == PropertyTypes.Multiple)
155        {
156            throw new RuntimeException("Attemping to remove a property of multiple type");
157        }
158
159        PropertyPanel removed = null;
160
161        for (PropertyPanel panel : properties)
162        {
163            if (panel.getProperty() == property)
164            {
165                removed = panel;
166            }
167        }
168
169        if (removed == null)
170        {
171            throw new RuntimeException("Could not find panel with property \"" + property + "\"");
172        }
173
174        remove(removed);
175
176        return removed;
177    }
178
179    public PropertyPanel removeProperty(JPanel jPanel)
180    {
181        PropertyPanel removed = null;
182
183        for (PropertyPanel panel : properties)
184        {
185            if (panel.getPanel() == jPanel)
186            {
187                removed = panel;
188            }
189        }
190
191        if (removed == null)
192        {
193            throw new RuntimeException("Could not find jPanel");
194        }
195
196        remove(removed);
197
198        return removed;
199    }
200
201    public PropertyPanel removeProperty(String title)
202    {
203        PropertyPanel removed = null;
204
205        for (PropertyPanel panel : properties)
206        {
207            if (panel.title().equals(title))
208            {
209                if (removed == null)
210                {
211                    removed = panel;
212                }
213                else
214                {
215                    throw new RuntimeException("Attempted to remove a property by title but the title was not unique");
216                }
217            }
218        }
219
220        if (removed == null)
221        {
222            throw new RuntimeException("Did not find titel \"" + title + "\"");
223        }
224
225        remove(removed);
226
227        return removed;
228    }
229
230    public boolean removeProperty(PropertyPanel panel)
231    {
232        return remove(panel);
233    }
234
235    public boolean containsProperty(Properties property)
236    {
237        boolean contains = false;
238
239        for (PropertyPanel panel : properties)
240        {
241            if (panel.getProperty() == property)
242            {
243                contains = true;
244            }
245        }
246
247        return contains;
248    }
249}
Note: See TracBrowser for help on using the repository browser.