1   package org.wcb.gui.dialog;
2   
3   import org.wcb.gui.forms.AircraftEntryForm;
4   import org.wcb.gui.forms.AircraftTableDisplayForm;
5   import org.wcb.gui.component.WaveButton;
6   import org.wcb.gui.util.UIHelper;
7   import org.wcb.resources.MessageResourceRegister;
8   import org.wcb.model.vo.hibernate.AircraftBO;
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 10:35:19 AM $ <br>
38   * </small>
39   *
40   * @author wbogaardt
41   * @version 1
42   *          Date: Mar 30, 2006
43   *          Time: 10:35:19 AM
44   */
45  
46  public class AircraftEditDialog extends JDialog implements ActionListener {
47  
48      private AircraftEntryForm form;
49      private JButton saveButton;
50      private JButton cancelButton;
51      private AircraftBO oAircraft;
52      private AircraftDelegate delegate;
53      private JComponent parentFrame;
54  
55      public AircraftEditDialog(AircraftBO craft) {
56          setTitle("Pilot's Log - Edit Aircraft");
57          setModal(true);
58          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
59          initComponents();
60          this.setAircraft(craft);
61      }
62  
63       public AircraftEditDialog(JComponent parent) {
64          setTitle("Pilot's Log - Enter New Aircraft");
65          setModal(true);
66          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
67          initComponents();
68          parentFrame = parent;
69      }
70  
71      /**
72       * Seter to set the delegate
73       * @param delegator AircraftDelegate from spring
74       */
75      public void setAircraftDelegate(AircraftDelegate delegator) {
76          this.delegate = delegator;
77      }
78  
79      /**
80       * Gets the delegate from spring.
81        * @return  The aircraft delegator
82       */
83      public AircraftDelegate getAircraftDelegate() {
84          if (this.delegate == null) {
85              this.delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
86          }
87          return this.delegate;
88      }
89  
90      public void setAircraft(AircraftBO oCraft) {
91          this.oAircraft = oCraft;
92          form.setAircraft(this.oAircraft);
93      }
94  
95      public AircraftBO getAircraft() {
96          this.oAircraft = form.getAircraft();
97          return this.oAircraft;
98      }
99  
100     /**
101      * Init the componets in the dialog
102      */
103     private void initComponents() {
104         form = new AircraftEntryForm();
105         saveButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_UPDATE));
106         saveButton.addActionListener(this);
107         cancelButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CANCEL));
108         cancelButton.addActionListener(this);
109         getContentPane().setLayout(new BorderLayout());
110 
111         JPanel buttonPanel = new JPanel();
112         buttonPanel.setLayout(new GridBagLayout());
113         GridBagConstraints gridBagConstraints;
114 
115         gridBagConstraints = new GridBagConstraints();
116         gridBagConstraints.gridx = 0;
117         gridBagConstraints.gridy = 1;
118         gridBagConstraints.anchor = GridBagConstraints.WEST;
119         buttonPanel.add(cancelButton, gridBagConstraints);
120 
121         gridBagConstraints = new GridBagConstraints();
122         gridBagConstraints.gridx = 1;
123         gridBagConstraints.gridy = 1;
124         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
125         gridBagConstraints.anchor = GridBagConstraints.WEST;
126         buttonPanel.add(saveButton, gridBagConstraints);
127 
128         getContentPane().add(form, BorderLayout.CENTER);
129         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
130         pack();
131         UIHelper.centerDialogToScreen(this);
132     }
133 
134     public void actionPerformed(ActionEvent event) {
135         Object source = event.getSource();
136         if (source.equals(saveButton))
137         {
138             this.save();
139         }
140         if (source.equals(cancelButton))
141         {
142             this.dispose();
143         }
144     }
145 
146      /**
147      * Save the form data into the database.
148      * A success value will show a success message dialog a failure to save
149      * will show an error message dialog.
150      */
151     private void save() {
152         boolean success = getAircraftDelegate().saveAircraft(this.getAircraft());
153         if (success)
154         {
155             if(this.parentFrame instanceof AircraftTableDisplayForm) {
156                 ((AircraftTableDisplayForm) parentFrame).refresh();
157             }
158             JOptionPane.showMessageDialog(null, "Successfully saved!", "Save Aircraft", JOptionPane.INFORMATION_MESSAGE);
159             dispose();
160         }
161         else
162         {
163             JOptionPane.showMessageDialog(null, "Could not save data.", "Save Aircraft", JOptionPane.ERROR_MESSAGE);
164         }
165     }
166 }