| 1 | package event.editor; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.*; |
|---|
| 4 | import java.util.Observable; |
|---|
| 5 | import java.util.List; |
|---|
| 6 | import java.util.ArrayList; |
|---|
| 7 | import javax.swing.DefaultComboBoxModel; |
|---|
| 8 | import javax.swing.ComboBoxModel; |
|---|
| 9 | import scriptbuilder.structures.events.I_ScriptEvent; |
|---|
| 10 | import scriptbuilder.structures.units.Unit; |
|---|
| 11 | import scriptbuilder.structures.events.UnitEvent; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * |
|---|
| 15 | * @author nathaniellehrer |
|---|
| 16 | * |
|---|
| 17 | * |
|---|
| 18 | */ |
|---|
| 19 | public class UnitPanel extends javax.swing.JPanel implements I_ScriptEventEditorPanel |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | private ActionListener removeListener; |
|---|
| 23 | private UnitEvent event; |
|---|
| 24 | private List<Unit> units; |
|---|
| 25 | |
|---|
| 26 | List<String> names = new ArrayList<String>(); |
|---|
| 27 | /** |
|---|
| 28 | * Creates new form UnitPanel |
|---|
| 29 | */ |
|---|
| 30 | public UnitPanel() |
|---|
| 31 | { |
|---|
| 32 | initComponents(); |
|---|
| 33 | |
|---|
| 34 | //units = event.slice.getIncident().script.units; |
|---|
| 35 | |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Loads the event information into the event editor window. |
|---|
| 40 | * @param sei Scriptevent that is being loaded in |
|---|
| 41 | */ |
|---|
| 42 | @Override |
|---|
| 43 | public void getEventObject(I_ScriptEvent sei) |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | event = (UnitEvent) sei; |
|---|
| 47 | //loads the current unit list into the combobox on re-opening of unit event |
|---|
| 48 | loadSelector(); |
|---|
| 49 | for (int i = 0; i < ActiveDropdown.getItemCount(); i++) |
|---|
| 50 | { |
|---|
| 51 | if (event.unitActive.equalsIgnoreCase((String) ActiveDropdown.getItemAt(i))) |
|---|
| 52 | { |
|---|
| 53 | ActiveDropdown.setSelectedIndex(i); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | //for each name in the names list |
|---|
| 58 | for (int i = 0; i < names.size(); i++) |
|---|
| 59 | { |
|---|
| 60 | //which event equals which index in the names list? |
|---|
| 61 | if (event.unitNum.equalsIgnoreCase((String) names.get(i))) |
|---|
| 62 | { |
|---|
| 63 | |
|---|
| 64 | //set the unit selector to the data model value |
|---|
| 65 | unitSelector.setSelectedIndex(i); |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | for (int i = 0; i < PrimaryDropdown.getItemCount(); i++) |
|---|
| 71 | { |
|---|
| 72 | if (event.unitPrimary.equalsIgnoreCase((String) PrimaryDropdown.getItemAt(i))) |
|---|
| 73 | { |
|---|
| 74 | PrimaryDropdown.setSelectedIndex(i); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | boolean containsItem = false; |
|---|
| 79 | for (int i = 0; i < StatusDropdown.getItemCount() && !containsItem; i++) |
|---|
| 80 | { |
|---|
| 81 | if (event.unitStatus.equalsIgnoreCase((String) StatusDropdown.getItemAt(i))) |
|---|
| 82 | { |
|---|
| 83 | StatusDropdown.setSelectedIndex(i); |
|---|
| 84 | containsItem = true; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | if (!containsItem) |
|---|
| 88 | { |
|---|
| 89 | StatusDropdown.addItem(event.unitStatus); |
|---|
| 90 | StatusDropdown.setSelectedItem(StatusDropdown.getItemCount() - 1); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | @Override |
|---|
| 96 | public void update(Observable o, Object arg) |
|---|
| 97 | { |
|---|
| 98 | //todo: make supported |
|---|
| 99 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | @Override |
|---|
| 103 | public boolean removeAssociatedEvent() |
|---|
| 104 | { |
|---|
| 105 | event.removeThis(); |
|---|
| 106 | event = null; |
|---|
| 107 | return true; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | private void loadSelector(){ |
|---|
| 112 | //checks if the event is exist |
|---|
| 113 | if(event != null) |
|---|
| 114 | { |
|---|
| 115 | //loads the names list from the top data model |
|---|
| 116 | loadNames(); |
|---|
| 117 | //asks if the unitselector already has some units added into it |
|---|
| 118 | |
|---|
| 119 | if(unitSelector.getModel().equals(new DefaultComboBoxModel(names.toArray())) || (unitSelector.getModel().getSize() == 0)) |
|---|
| 120 | { |
|---|
| 121 | //if combobox empty, load combobox |
|---|
| 122 | unitSelector.setModel(new DefaultComboBoxModel(names.toArray())); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Loads the unit names from the unit list in SimulationScript |
|---|
| 133 | */ |
|---|
| 134 | private void loadNames(){ |
|---|
| 135 | //checks if the event is exist |
|---|
| 136 | if(event!=null) |
|---|
| 137 | { |
|---|
| 138 | //this sucks! try to change it sometime |
|---|
| 139 | //grabs the unit list from the top of the SimulationScript and |
|---|
| 140 | //appends them to the names list |
|---|
| 141 | units = event.slice.getIncident().script.units; |
|---|
| 142 | |
|---|
| 143 | for(Unit u : units) |
|---|
| 144 | { |
|---|
| 145 | names.add(u.UnitNum); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | @Override |
|---|
| 151 | /** |
|---|
| 152 | * when window is closed, these values are set. |
|---|
| 153 | */ |
|---|
| 154 | public void uponClose() |
|---|
| 155 | { |
|---|
| 156 | |
|---|
| 157 | if (event != null && unitSelector.getItemCount() != 0) |
|---|
| 158 | { |
|---|
| 159 | event.unitNum = unitSelector.getSelectedItem().toString(); |
|---|
| 160 | event.unitActive = ActiveDropdown.getSelectedItem().toString(); |
|---|
| 161 | event.unitPrimary = PrimaryDropdown.getSelectedItem().toString(); |
|---|
| 162 | event.unitStatus = StatusDropdown.getSelectedItem().toString(); |
|---|
| 163 | |
|---|
| 164 | //old logic for creating dummy unit |
|---|
| 165 | /*if(event.slice.getIncident().script.hasUnit(txtUnitNumber.getText())){ |
|---|
| 166 | //checks to see if there is already a unit selected there |
|---|
| 167 | //event.unitNum = (txtUnitNumber.getText()); |
|---|
| 168 | |
|---|
| 169 | }else{ |
|---|
| 170 | //Creates a dummy unit here |
|---|
| 171 | event.slice.getIncident().script.addDummyUnit(txtUnitNumber.getText()); |
|---|
| 172 | //event.unitNum = (txtUnitNumber.getText()); |
|---|
| 173 | event.unitActive = ActiveDropdown.getSelectedItem().toString(); |
|---|
| 174 | event.unitPrimary = PrimaryDropdown.getSelectedItem().toString(); |
|---|
| 175 | event.unitStatus = StatusDropdown.getSelectedItem().toString(); |
|---|
| 176 | //System.out.println("invalid unit number"); |
|---|
| 177 | }*/ |
|---|
| 178 | |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * This method is called from within the constructor to initialize the form. |
|---|
| 184 | * WARNING: Do NOT modify this code. The content of this method is always |
|---|
| 185 | * regenerated by the Form Editor. |
|---|
| 186 | */ |
|---|
| 187 | @SuppressWarnings("unchecked") |
|---|
| 188 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
|---|
| 189 | private void initComponents() { |
|---|
| 190 | |
|---|
| 191 | jLabel2 = new javax.swing.JLabel(); |
|---|
| 192 | StatusDropdown = new javax.swing.JComboBox(); |
|---|
| 193 | jLabel3 = new javax.swing.JLabel(); |
|---|
| 194 | PrimaryDropdown = new javax.swing.JComboBox(); |
|---|
| 195 | jLabel4 = new javax.swing.JLabel(); |
|---|
| 196 | ActiveDropdown = new javax.swing.JComboBox(); |
|---|
| 197 | help = new javax.swing.JLabel(); |
|---|
| 198 | jLabel5 = new javax.swing.JLabel(); |
|---|
| 199 | unitSelector = new javax.swing.JComboBox<>(); |
|---|
| 200 | |
|---|
| 201 | jLabel2.setText("Status"); |
|---|
| 202 | |
|---|
| 203 | StatusDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1098", "1097", "ENRT" })); |
|---|
| 204 | |
|---|
| 205 | jLabel3.setText("Primary"); |
|---|
| 206 | |
|---|
| 207 | PrimaryDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "True", "False" })); |
|---|
| 208 | |
|---|
| 209 | jLabel4.setText("Active"); |
|---|
| 210 | |
|---|
| 211 | ActiveDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "True", "False" })); |
|---|
| 212 | |
|---|
| 213 | jLabel5.setText("Unit"); |
|---|
| 214 | |
|---|
| 215 | unitSelector.addAncestorListener(new javax.swing.event.AncestorListener() { |
|---|
| 216 | public void ancestorMoved(javax.swing.event.AncestorEvent evt) { |
|---|
| 217 | } |
|---|
| 218 | public void ancestorAdded(javax.swing.event.AncestorEvent evt) { |
|---|
| 219 | unitSelectorAncestorAdded(evt); |
|---|
| 220 | } |
|---|
| 221 | public void ancestorRemoved(javax.swing.event.AncestorEvent evt) { |
|---|
| 222 | } |
|---|
| 223 | }); |
|---|
| 224 | unitSelector.addMouseListener(new java.awt.event.MouseAdapter() { |
|---|
| 225 | public void mouseEntered(java.awt.event.MouseEvent evt) { |
|---|
| 226 | unitSelectorMouseEntered(evt); |
|---|
| 227 | } |
|---|
| 228 | }); |
|---|
| 229 | unitSelector.addActionListener(new java.awt.event.ActionListener() { |
|---|
| 230 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
|---|
| 231 | unitSelectorActionPerformed(evt); |
|---|
| 232 | } |
|---|
| 233 | }); |
|---|
| 234 | |
|---|
| 235 | org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); |
|---|
| 236 | this.setLayout(layout); |
|---|
| 237 | layout.setHorizontalGroup( |
|---|
| 238 | layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 239 | .add(layout.createSequentialGroup() |
|---|
| 240 | .addContainerGap() |
|---|
| 241 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 242 | .add(org.jdesktop.layout.GroupLayout.TRAILING, help, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE) |
|---|
| 243 | .add(layout.createSequentialGroup() |
|---|
| 244 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 245 | .add(jLabel3) |
|---|
| 246 | .add(jLabel4)) |
|---|
| 247 | .add(43, 43, 43) |
|---|
| 248 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 249 | .add(ActiveDropdown, 0, 475, Short.MAX_VALUE) |
|---|
| 250 | .add(PrimaryDropdown, 0, 475, Short.MAX_VALUE))) |
|---|
| 251 | .add(layout.createSequentialGroup() |
|---|
| 252 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 253 | .add(jLabel2) |
|---|
| 254 | .add(jLabel5)) |
|---|
| 255 | .add(54, 54, 54) |
|---|
| 256 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 257 | .add(layout.createSequentialGroup() |
|---|
| 258 | .add(StatusDropdown, 0, 419, Short.MAX_VALUE) |
|---|
| 259 | .add(57, 57, 57)) |
|---|
| 260 | .add(layout.createSequentialGroup() |
|---|
| 261 | .add(unitSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) |
|---|
| 262 | .add(0, 0, Short.MAX_VALUE))))) |
|---|
| 263 | .addContainerGap()) |
|---|
| 264 | ); |
|---|
| 265 | layout.setVerticalGroup( |
|---|
| 266 | layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
|---|
| 267 | .add(layout.createSequentialGroup() |
|---|
| 268 | .add(82, 82, 82) |
|---|
| 269 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
|---|
| 270 | .add(jLabel5) |
|---|
| 271 | .add(unitSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
|---|
| 272 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
|---|
| 273 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
|---|
| 274 | .add(jLabel2) |
|---|
| 275 | .add(StatusDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
|---|
| 276 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
|---|
| 277 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
|---|
| 278 | .add(jLabel3) |
|---|
| 279 | .add(PrimaryDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
|---|
| 280 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
|---|
| 281 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
|---|
| 282 | .add(jLabel4) |
|---|
| 283 | .add(ActiveDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
|---|
| 284 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 100, Short.MAX_VALUE) |
|---|
| 285 | .add(help, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) |
|---|
| 286 | .addContainerGap()) |
|---|
| 287 | ); |
|---|
| 288 | }// </editor-fold>//GEN-END:initComponents |
|---|
| 289 | |
|---|
| 290 | private void unitSelectorAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_unitSelectorAncestorAdded |
|---|
| 291 | //loads the unitSelector on the initialization of the window |
|---|
| 292 | loadSelector(); |
|---|
| 293 | }//GEN-LAST:event_unitSelectorAncestorAdded |
|---|
| 294 | |
|---|
| 295 | private void unitSelectorMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_unitSelectorMouseEntered |
|---|
| 296 | |
|---|
| 297 | }//GEN-LAST:event_unitSelectorMouseEntered |
|---|
| 298 | |
|---|
| 299 | private void unitSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unitSelectorActionPerformed |
|---|
| 300 | // TODO add your handling code here: |
|---|
| 301 | }//GEN-LAST:event_unitSelectorActionPerformed |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | // Variables declaration - do not modify//GEN-BEGIN:variables |
|---|
| 305 | private javax.swing.JComboBox ActiveDropdown; |
|---|
| 306 | private javax.swing.JComboBox PrimaryDropdown; |
|---|
| 307 | private javax.swing.JComboBox StatusDropdown; |
|---|
| 308 | private javax.swing.JLabel help; |
|---|
| 309 | private javax.swing.JLabel jLabel2; |
|---|
| 310 | private javax.swing.JLabel jLabel3; |
|---|
| 311 | private javax.swing.JLabel jLabel4; |
|---|
| 312 | private javax.swing.JLabel jLabel5; |
|---|
| 313 | private javax.swing.JComboBox<String> unitSelector; |
|---|
| 314 | // End of variables declaration//GEN-END:variables |
|---|
| 315 | |
|---|
| 316 | } |
|---|