source: tmcsimulator-scriptbuilder/trunk/src/event/editor/UnitPanel.java @ 130

Revision 130, 8.1 KB checked in by bmcguffin, 9 years ago (diff)

Changed implementation of save function for event editor panels. Instead of updating their model objects whenever the enter key is pressed, panels are now notified when the window is closed and update their information then.

Line 
1package event.editor;
2
3import java.awt.event.*;
4import java.util.Observable;
5import scriptbuilder.structures.events.I_ScriptEvent;
6import scriptbuilder.structures.events.UnitEvent;
7
8/**
9 *
10 * @author nathaniellehrer
11 */
12public class UnitPanel extends javax.swing.JPanel implements I_ScriptEventEditorPanel
13{
14
15    private ActionListener removeListener;
16    private UnitEvent event;
17
18    /**
19     * Creates new form UnitPanel
20     */
21    public UnitPanel()
22    {
23        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
41    @Override
42    public void getEventObject(I_ScriptEvent sei)
43    {
44        event = (UnitEvent) sei;
45        txtUnitNumber.setText(event.unitNum);
46        for (int i = 0; i < ActiveDropdown.getItemCount(); i++)
47        {
48            if (event.unitActive.equalsIgnoreCase((String) ActiveDropdown.getItemAt(i)))
49            {
50                ActiveDropdown.setSelectedIndex(i);
51            }
52        }
53
54        for (int i = 0; i < PrimaryDropdown.getItemCount(); i++)
55        {
56            if (event.unitPrimary.equalsIgnoreCase((String) PrimaryDropdown.getItemAt(i)))
57            {
58                PrimaryDropdown.setSelectedIndex(i);
59            }
60        }
61
62        boolean containsItem = false;
63        for (int i = 0; i < StatusDropdown.getItemCount() && !containsItem; i++)
64        {
65            if (event.unitStatus.equalsIgnoreCase((String) StatusDropdown.getItemAt(i)))
66            {
67                StatusDropdown.setSelectedIndex(i);
68                containsItem = true;
69            }
70        }
71        if (!containsItem)
72        {
73            StatusDropdown.addItem(event.unitStatus);
74            StatusDropdown.setSelectedItem(StatusDropdown.getItemCount() - 1);
75        }
76
77    }
78
79    @Override
80    public void update(Observable o, Object arg)
81    {
82        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
83    }
84
85    @Override
86    public boolean removeAssociatedEvent()
87    {
88        event.removeThis();
89        event = null;
90        return true;
91    }
92
93    @Override
94    public void uponClose()
95    {
96        event.unitNum = txtUnitNumber.getText();
97        event.unitActive = ActiveDropdown.getSelectedItem().toString();
98        event.unitPrimary = PrimaryDropdown.getSelectedItem().toString();
99        event.unitStatus = StatusDropdown.getSelectedItem().toString();
100    }
101
102    /**
103     * This method is called from within the constructor to initialize the form.
104     * WARNING: Do NOT modify this code. The content of this method is always
105     * regenerated by the Form Editor.
106     */
107    @SuppressWarnings("unchecked")
108    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
109    private void initComponents()
110    {
111
112        jLabel1 = new javax.swing.JLabel();
113        txtUnitNumber = new javax.swing.JFormattedTextField();
114        jLabel2 = new javax.swing.JLabel();
115        StatusDropdown = new javax.swing.JComboBox();
116        jLabel3 = new javax.swing.JLabel();
117        PrimaryDropdown = new javax.swing.JComboBox();
118        jLabel4 = new javax.swing.JLabel();
119        ActiveDropdown = new javax.swing.JComboBox();
120        help = new javax.swing.JLabel();
121
122        jLabel1.setText("Unit Number");
123
124        txtUnitNumber.setText("  -  ");
125        txtUnitNumber.setToolTipText("");
126
127        jLabel2.setText("Status");
128
129        StatusDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1098", "1097", "ENRT" }));
130
131        jLabel3.setText("Primary");
132
133        PrimaryDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "True", "False" }));
134
135        jLabel4.setText("Active");
136
137        ActiveDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "True", "False" }));
138
139        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
140        this.setLayout(layout);
141        layout.setHorizontalGroup(
142            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
143            .add(layout.createSequentialGroup()
144                .addContainerGap()
145                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
146                    .add(org.jdesktop.layout.GroupLayout.TRAILING, help, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
147                    .add(layout.createSequentialGroup()
148                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
149                            .add(jLabel1)
150                            .add(jLabel2))
151                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
152                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
153                            .add(StatusDropdown, 0, 475, Short.MAX_VALUE)
154                            .add(txtUnitNumber, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)))
155                    .add(layout.createSequentialGroup()
156                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
157                            .add(jLabel3)
158                            .add(jLabel4))
159                        .add(43, 43, 43)
160                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
161                            .add(ActiveDropdown, 0, 475, Short.MAX_VALUE)
162                            .add(PrimaryDropdown, 0, 475, Short.MAX_VALUE))))
163                .addContainerGap())
164        );
165        layout.setVerticalGroup(
166            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
167            .add(layout.createSequentialGroup()
168                .add(47, 47, 47)
169                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
170                    .add(jLabel1)
171                    .add(txtUnitNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
172                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
173                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
174                    .add(jLabel2)
175                    .add(StatusDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
176                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
177                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
178                    .add(jLabel3)
179                    .add(PrimaryDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
180                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
181                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
182                    .add(jLabel4)
183                    .add(ActiveDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
184                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 128, Short.MAX_VALUE)
185                .add(help, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
186                .addContainerGap())
187        );
188    }// </editor-fold>//GEN-END:initComponents
189
190
191    // Variables declaration - do not modify//GEN-BEGIN:variables
192    private javax.swing.JComboBox ActiveDropdown;
193    private javax.swing.JComboBox PrimaryDropdown;
194    private javax.swing.JComboBox StatusDropdown;
195    private javax.swing.JLabel help;
196    private javax.swing.JLabel jLabel1;
197    private javax.swing.JLabel jLabel2;
198    private javax.swing.JLabel jLabel3;
199    private javax.swing.JLabel jLabel4;
200    private javax.swing.JFormattedTextField txtUnitNumber;
201    // End of variables declaration//GEN-END:variables
202
203}
Note: See TracBrowser for help on using the repository browser.