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