Changeset 145 in tmcsimulator-scriptbuilder for trunk/src/event/editor/UnitPanel.java


Ignore:
Timestamp:
11/04/2019 08:14:32 AM (7 years ago)
Author:
sdanthin
Message:

Move from Git to Svn (LARGE COMMIT)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/event/editor/UnitPanel.java

    r140 r145  
    33import java.awt.event.*; 
    44import java.util.Observable; 
     5import java.util.List; 
     6import java.util.ArrayList; 
     7import javax.swing.DefaultComboBoxModel; 
     8import javax.swing.ComboBoxModel; 
    59import scriptbuilder.structures.events.I_ScriptEvent; 
     10import scriptbuilder.structures.units.Unit; 
    611import scriptbuilder.structures.events.UnitEvent; 
    712 
     
    914 * 
    1015 * @author nathaniellehrer 
     16 *  
     17 *  
    1118 */ 
    1219public class UnitPanel extends javax.swing.JPanel implements I_ScriptEventEditorPanel 
     
    1522    private ActionListener removeListener; 
    1623    private UnitEvent event; 
    17  
     24    private List<Unit> units; 
     25     
     26    List<String> names = new ArrayList<String>(); 
    1827    /** 
    1928     * Creates new form UnitPanel 
     
    2231    { 
    2332        initComponents(); 
    24  
    25         txtUnitNumber.addFocusListener(new FocusListener() 
    26         { 
    27  
    28             public void focusGained(FocusEvent e) 
    29             { 
    30                 help.setText("Unit number is formatted as #-#\t\tExample: 5-5"); 
    31             } 
    32  
    33             public void focusLost(FocusEvent e) 
    34             { 
    35                 help.setText(""); 
    36             } 
    37  
    38         }); 
    39     } 
    40  
     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     */ 
    4142    @Override 
    4243    public void getEventObject(I_ScriptEvent sei) 
    4344    { 
     45         
    4446        event = (UnitEvent) sei; 
    45         txtUnitNumber.setText(event.unitNum); 
     47        //loads the current unit list into the combobox on re-opening of unit event 
     48        loadSelector(); 
    4649        for (int i = 0; i < ActiveDropdown.getItemCount(); i++) 
    4750        { 
     
    4952            { 
    5053                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                 
    5167            } 
    5268        } 
     
    8096    public void update(Observable o, Object arg) 
    8197    { 
     98        //todo: make supported 
    8299        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    83100    } 
     
    90107        return true; 
    91108    } 
     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    } 
    92149 
    93150    @Override 
     151    /** 
     152     * when window is closed, these values are set. 
     153     */ 
    94154    public void uponClose() 
    95155    { 
    96         if (event != null) 
    97         { 
    98             event.unitNum = txtUnitNumber.getText(); 
     156         
     157        if (event != null && unitSelector.getItemCount() != 0) 
     158        { 
     159            event.unitNum = unitSelector.getSelectedItem().toString(); 
    99160            event.unitActive = ActiveDropdown.getSelectedItem().toString(); 
    100161            event.unitPrimary = PrimaryDropdown.getSelectedItem().toString(); 
    101162            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             
    102179        } 
    103180    } 
     
    110187    @SuppressWarnings("unchecked") 
    111188    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 
    112     private void initComponents() 
    113     { 
    114  
    115         jLabel1 = new javax.swing.JLabel(); 
    116         txtUnitNumber = new javax.swing.JFormattedTextField(); 
     189    private void initComponents() { 
     190 
    117191        jLabel2 = new javax.swing.JLabel(); 
    118192        StatusDropdown = new javax.swing.JComboBox(); 
     
    122196        ActiveDropdown = new javax.swing.JComboBox(); 
    123197        help = new javax.swing.JLabel(); 
    124  
    125         jLabel1.setText("Unit Number"); 
    126  
    127         txtUnitNumber.setText("  -  "); 
    128         txtUnitNumber.setToolTipText(""); 
     198        jLabel5 = new javax.swing.JLabel(); 
     199        unitSelector = new javax.swing.JComboBox<>(); 
    129200 
    130201        jLabel2.setText("Status"); 
     
    139210 
    140211        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        }); 
    141234 
    142235        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 
     
    150243                    .add(layout.createSequentialGroup() 
    151244                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
    152                             .add(jLabel1) 
    153                             .add(jLabel2)) 
    154                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
    155                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
    156                             .add(StatusDropdown, 0, 475, Short.MAX_VALUE) 
    157                             .add(txtUnitNumber, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE))) 
    158                     .add(layout.createSequentialGroup() 
    159                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
    160245                            .add(jLabel3) 
    161246                            .add(jLabel4)) 
     
    163248                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
    164249                            .add(ActiveDropdown, 0, 475, Short.MAX_VALUE) 
    165                             .add(PrimaryDropdown, 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))))) 
    166263                .addContainerGap()) 
    167264        ); 
     
    169266            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
    170267            .add(layout.createSequentialGroup() 
    171                 .add(47, 47, 47) 
     268                .add(82, 82, 82) 
    172269                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
    173                     .add(jLabel1) 
    174                     .add(txtUnitNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
    175                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) 
     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) 
    176273                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
    177274                    .add(jLabel2) 
     
    185282                    .add(jLabel4) 
    186283                    .add(ActiveDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
    187                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 128, Short.MAX_VALUE) 
     284                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 100, Short.MAX_VALUE) 
    188285                .add(help, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
    189286                .addContainerGap()) 
    190287        ); 
    191288    }// </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 
    192302 
    193303 
     
    197307    private javax.swing.JComboBox StatusDropdown; 
    198308    private javax.swing.JLabel help; 
    199     private javax.swing.JLabel jLabel1; 
    200309    private javax.swing.JLabel jLabel2; 
    201310    private javax.swing.JLabel jLabel3; 
    202311    private javax.swing.JLabel jLabel4; 
    203     private javax.swing.JFormattedTextField txtUnitNumber; 
     312    private javax.swing.JLabel jLabel5; 
     313    private javax.swing.JComboBox<String> unitSelector; 
    204314    // End of variables declaration//GEN-END:variables 
    205315 
Note: See TracChangeset for help on using the changeset viewer.