1   package org.wcb.gui.dialog;
2   
3   import org.wcb.gui.forms.AircraftTypeEntryForm;
4   import org.wcb.gui.forms.AircraftTypeTableDisplayForm;
5   import org.wcb.gui.component.VectorButton;
6   import org.wcb.gui.util.UIHelper;
7   import org.wcb.resources.MessageResourceRegister;
8   import org.wcb.model.vo.hibernate.AircraftTypeBO;
9   import org.wcb.model.bd.AircraftDelegate;
10  import org.wcb.model.util.SpringUtil;
11  import org.wcb.model.service.IServicesConstants;
12  import org.wcb.resources.MessageKey;
13  
14  import javax.swing.*;
15  import java.awt.event.ActionListener;
16  import java.awt.event.ActionEvent;
17  import java.awt.*;
18  
19  /**
20   * <small>
21   * Copyright (c)  2006  wbogaardt.
22   * This library is free software; you can redistribute it and/or
23   * modify it under the terms of the GNU Lesser General Public
24   * License as published by the Free Software Foundation; either
25   * version 2.1 of the License, or (at your option) any later version.
26   *
27   * This library is distributed in the hope that it will be useful,
28   * but WITHOUT ANY WARRANTY; without even the implied warranty of
29   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30   * Lesser General Public License for more details.
31   *
32   * You should have received a copy of the GNU Lesser General Public
33   * License along with this library; if not, write to the Free Software
34   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
35   * <p/>
36   * $File:  $ <br>
37   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 30, 2006 12:36:17 PM $ <br>
38   * </small>
39   *
40   * @author wbogaardt
41   * @version 1
42   *          Date: Mar 30, 2006
43   *          Time: 12:36:17 PM
44   */
45  
46  public class AircraftTypeEntryDialog extends JDialog implements ActionListener {
47  
48      private AircraftTypeEntryForm form;
49      private AircraftTypeBO oAircraftType;
50      private AircraftDelegate delegate;
51      private JButton cancelButton;
52      private JButton saveButton;
53      private JComponent parent;
54  
55      /**
56       * References to the parent jcomponent.
57       * @param parent the parent comopnent.
58       */
59      public AircraftTypeEntryDialog(JComponent parent) {
60          this.parent = parent;
61          delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
62          setTitle("Pilot's Log - New Aircraft Type");
63          setModal(true);
64          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
65          initComponents();
66      }
67  
68      /**
69       * Call the dialog and set the aircraft type to marshall to the form.
70       * @param type To marshall to form.
71       */
72      public AircraftTypeEntryDialog(AircraftTypeBO type) {
73          delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
74          setTitle("Pilot's Log - Edit Aircraft Type");
75          setModal(true);
76          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
77          initComponents();
78          setAircraftType(type);
79      }
80  
81      /**
82       * Sets the aircraft type to the form.
83       * @param oValue the object to marshall to form.
84       */
85      public void setAircraftType(AircraftTypeBO oValue) {
86          this.oAircraftType = oValue;
87          form.setAircraftType(this.oAircraftType);
88      }
89  
90      /**
91       * Access to get aircraft type object.
92       * @return the aircraft type from the form.
93       */
94      public AircraftTypeBO getAircraftType() {
95          this.oAircraftType = form.getAircraftType();
96          return this.oAircraftType;
97      }
98  
99      /**
100      * Init the componets in the dialog.
101      */
102     private void initComponents() {
103         form = new AircraftTypeEntryForm();
104         saveButton = new VectorButton();
105         saveButton.setBorderPainted(false);
106         saveButton.setForeground(new Color(50, 255, 0));
107         saveButton.setText(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_SAVE));
108         saveButton.addActionListener(this);
109         cancelButton =  new VectorButton();
110         cancelButton.setBorderPainted(false);
111         cancelButton.setForeground(new Color(50, 50, 255));
112         cancelButton.setText(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CANCEL));
113         cancelButton.addActionListener(this);
114         getContentPane().setLayout(new BorderLayout());
115 
116         JPanel buttonPanel = new JPanel();
117         buttonPanel.setLayout(new GridBagLayout());
118         GridBagConstraints gridBagConstraints;
119         gridBagConstraints = new GridBagConstraints();
120         gridBagConstraints.gridx = 0;
121         gridBagConstraints.gridy = 1;
122         gridBagConstraints.anchor = GridBagConstraints.WEST;
123         buttonPanel.add(cancelButton, gridBagConstraints);
124 
125         gridBagConstraints = new GridBagConstraints();
126         gridBagConstraints.gridx = 1;
127         gridBagConstraints.gridy = 1;
128         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
129         gridBagConstraints.anchor = GridBagConstraints.WEST;
130         buttonPanel.add(saveButton, gridBagConstraints);
131 
132         getContentPane().add(form, BorderLayout.CENTER);
133         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
134         pack();
135         UIHelper.centerDialogToScreen(this);
136     }
137 
138     /**
139      * Action events are if the save button is clicked save the information.
140      * Cancel closes the dialog box.
141      * @param event The component calling
142      */
143     public void actionPerformed(ActionEvent event) {
144         Object source = event.getSource();
145         if (source.equals(saveButton))
146         {
147             this.save();
148             if (this.parent instanceof AircraftTypeTableDisplayForm)
149             {
150                 ((AircraftTypeTableDisplayForm) parent).refreshModel();
151             }
152             this.dispose();
153         }
154         if (source.equals(cancelButton))
155         {
156             form.reset();
157             this.dispose();
158         }
159     }
160 
161     private void save() {
162         boolean success = delegate.saveAircraftType(this.getAircraftType());
163         if (success)
164         {
165             JOptionPane.showMessageDialog(null,"Successfully saved!","Save Type of Aircraft", JOptionPane.INFORMATION_MESSAGE);
166         }
167         else
168         {
169             JOptionPane.showMessageDialog(null, "Could not save data.", "Save Type of Aircraft", JOptionPane.ERROR_MESSAGE);
170         }
171     }
172 }