| 1 | package event.editor.frame; |
|---|
| 2 | |
|---|
| 3 | import java.util.EnumMap; |
|---|
| 4 | import java.util.Observable; |
|---|
| 5 | import java.util.Vector; |
|---|
| 6 | import javax.swing.JPanel; |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * |
|---|
| 10 | * @author Bryan McGuffin |
|---|
| 11 | */ |
|---|
| 12 | public 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 removablePanel) |
|---|
| 93 | { |
|---|
| 94 | boolean result = false; |
|---|
| 95 | |
|---|
| 96 | if ((result = properties.remove(removablePanel))) |
|---|
| 97 | { |
|---|
| 98 | setChanged(); |
|---|
| 99 | notifyObservers(new PropertyUpdate(UpdateType.Remove, removablePanel)); |
|---|
| 100 | } |
|---|
| 101 | else |
|---|
| 102 | { |
|---|
| 103 | throw new RuntimeException("Attempted to remove panel that did not exist"); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if (removablePanel.getProperty().getType() == PropertyTypes.Multiple) |
|---|
| 107 | { |
|---|
| 108 | Vector<MultPropertyPanel> panelsOfSameType = new Vector<MultPropertyPanel>(); |
|---|
| 109 | Vector<MultPropertyPanel> panelsOfGreaterIndex = new Vector<MultPropertyPanel>(); |
|---|
| 110 | |
|---|
| 111 | propertyCounter.put(removablePanel.getProperty(), propertyCounter.get(removablePanel.getProperty()) - 1); |
|---|
| 112 | |
|---|
| 113 | // extract panels of the same property type |
|---|
| 114 | for (PropertyPanel pan : properties) |
|---|
| 115 | { |
|---|
| 116 | if (pan.getProperty() == removablePanel.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) removablePanel).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 (removablePanel.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 | * returns a corresponding PropertyPanel from a jPanel |
|---|
| 202 | * @param jPanel |
|---|
| 203 | * @return |
|---|
| 204 | */ |
|---|
| 205 | public PropertyPanel getProperty(JPanel jPanel) |
|---|
| 206 | { |
|---|
| 207 | PropertyPanel returnable = null; |
|---|
| 208 | |
|---|
| 209 | for (PropertyPanel panel : properties) |
|---|
| 210 | { |
|---|
| 211 | if (panel.getPanel() == jPanel) |
|---|
| 212 | { |
|---|
| 213 | returnable = panel; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | return returnable; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | public PropertyPanel removeProperty(String title) |
|---|
| 220 | { |
|---|
| 221 | PropertyPanel removed = null; |
|---|
| 222 | |
|---|
| 223 | for (PropertyPanel panel : properties) |
|---|
| 224 | { |
|---|
| 225 | if (panel.title().equals(title)) |
|---|
| 226 | { |
|---|
| 227 | if (removed == null) |
|---|
| 228 | { |
|---|
| 229 | removed = panel; |
|---|
| 230 | } |
|---|
| 231 | else |
|---|
| 232 | { |
|---|
| 233 | throw new RuntimeException("Attempted to remove a property by title but the title was not unique"); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | if (removed == null) |
|---|
| 239 | { |
|---|
| 240 | throw new RuntimeException("Did not find titel \"" + title + "\""); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | remove(removed); |
|---|
| 244 | |
|---|
| 245 | return removed; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | public boolean removeProperty(PropertyPanel panel) |
|---|
| 249 | { |
|---|
| 250 | return remove(panel); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | public boolean containsProperty(Properties property) |
|---|
| 254 | { |
|---|
| 255 | boolean contains = false; |
|---|
| 256 | |
|---|
| 257 | for (PropertyPanel panel : properties) |
|---|
| 258 | { |
|---|
| 259 | if (panel.getProperty() == property) |
|---|
| 260 | { |
|---|
| 261 | contains = true; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | return contains; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|