1   package org.wcb.gui.dialog.del;
2   
3   import org.wcb.gui.forms.AircraftEntryForm;
4   import org.wcb.gui.forms.AircraftTableDisplayForm;
5   import org.wcb.gui.component.VectorButton;
6   import org.wcb.resources.MessageResourceRegister;
7   import org.wcb.model.vo.hibernate.AircraftBO;
8   import org.wcb.model.bd.AircraftDelegate;
9   import org.wcb.model.util.SpringUtil;
10  import org.wcb.model.service.IServicesConstants;
11  import org.wcb.resources.MessageKey;
12  
13  import javax.swing.*;
14  import java.awt.*;
15  import java.awt.event.ActionListener;
16  import java.awt.event.ActionEvent;
17  
18  /**
19   * <small>
20   * Copyright (c)  2006  wbogaardt.
21   * This library is free software; you can redistribute it and/or
22   * modify it under the terms of the GNU Lesser General Public
23   * License as published by the Free Software Foundation; either
24   * version 2.1 of the License, or (at your option) any later version.
25   *
26   * This library is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29   * Lesser General Public License for more details.
30   *
31   * You should have received a copy of the GNU Lesser General Public
32   * License along with this library; if not, write to the Free Software
33   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34   * <p/>
35   * $File:  $ <br>
36   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 29, 2006 3:35:03 PM $ <br>
37   * </small>
38   *
39   * @author wbogaardt
40   * @version 1
41   *          Date: Mar 29, 2006
42   *          Time: 3:35:03 PM
43   */
44  
45  public class AircraftEntryDialog extends JDialog implements ActionListener {
46  
47      private AircraftEntryForm form;
48      private AircraftBO oAircraft;
49      private AircraftDelegate delegate;
50      private JButton clearButton;
51      private JButton saveButton;
52      private JComponent parentFrame;
53  
54      public AircraftEntryDialog(JComponent parent) {
55          setTitle("Enter New AircraftBO");
56          delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
57          setModal(true);
58          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
59          this.parentFrame = parent;
60          initComponents();
61      }
62  
63      public void setAircraft(AircraftBO oValue) {
64          this.oAircraft = oValue;
65          form.setAircraft(this.oAircraft);
66      }
67  
68      public AircraftBO getAircraft() {
69          this.oAircraft = form.getAircraft();
70          return this.oAircraft;
71      }
72  
73      /**
74       * Init the componets in the dialog
75       */
76      private void initComponents() {
77          form = new AircraftEntryForm();
78          saveButton = new VectorButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_SAVE));
79          saveButton.setBorderPainted(false);
80          saveButton.setForeground(new Color(50,255,0));
81          saveButton.addActionListener(this);
82          clearButton = new VectorButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CANCEL));
83          clearButton.setBorderPainted(false);
84          clearButton.setForeground(new Color(50,50,255));
85          clearButton.addActionListener(this);
86          getContentPane().setLayout(new BorderLayout());
87  
88          JPanel buttonPanel = new JPanel();
89          buttonPanel.setLayout(new GridBagLayout());
90          GridBagConstraints gridBagConstraints;
91          gridBagConstraints = new GridBagConstraints();
92          gridBagConstraints.gridx = 0;
93          gridBagConstraints.gridy = 1;
94          gridBagConstraints.anchor = GridBagConstraints.WEST;
95          buttonPanel.add(clearButton, gridBagConstraints);
96  
97          gridBagConstraints = new GridBagConstraints();
98          gridBagConstraints.gridx = 1;
99          gridBagConstraints.gridy = 1;
100         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
101         gridBagConstraints.anchor = GridBagConstraints.WEST;
102         buttonPanel.add(saveButton, gridBagConstraints);
103 
104         getContentPane().add(form, BorderLayout.CENTER);
105         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
106         pack();
107     }
108 
109     public void actionPerformed(ActionEvent event) {
110         Object source = event.getSource();
111         if (source.equals(saveButton))
112         {
113             this.save();
114             this.dispose();
115         }
116         if (source.equals(clearButton))
117         {
118             form.reset();
119             this.dispose();
120         }
121     }
122 
123     private void save() {
124         boolean success = delegate.saveAircraft(this.getAircraft());
125         if (success)
126         {
127             if(this.parentFrame instanceof AircraftTableDisplayForm) {
128                 ((AircraftTableDisplayForm) parentFrame).refresh();
129             }
130             JOptionPane.showMessageDialog(null, "Successfully saved!", "Save AircraftBO", JOptionPane.INFORMATION_MESSAGE);
131         }
132         else
133         {
134             JOptionPane.showMessageDialog(null, "Could not save data.", "Save AircraftBO", JOptionPane.ERROR_MESSAGE);
135         }
136     }
137 
138 }
139