1   package org.wcb.gui.dialog;
2   
3   import org.wcb.gui.forms.AirportEntryForm;
4   import org.wcb.gui.forms.AirportTableDisplayForm;
5   import org.wcb.gui.forms.AirportAdvancedEntryForm;
6   import org.wcb.gui.component.WaveButton;
7   import org.wcb.gui.component.JMessageTextPane;
8   import org.wcb.gui.util.UIHelper;
9   import org.wcb.resources.MessageResourceRegister;
10  import org.wcb.model.vo.hibernate.AirportBO;
11  import org.wcb.model.bd.AirportDelegate;
12  import org.wcb.model.util.SpringUtil;
13  import org.wcb.model.service.IServicesConstants;
14  import org.wcb.resources.MessageKey;
15  
16  import javax.swing.*;
17  import java.awt.event.ActionListener;
18  import java.awt.event.ActionEvent;
19  import java.awt.*;
20  
21  /**
22   * <small>
23   * Copyright (c)  2006  wbogaardt.
24   * This library is free software; you can redistribute it and/or
25   * modify it under the terms of the GNU Lesser General Public
26   * License as published by the Free Software Foundation; either
27   * version 2.1 of the License, or (at your option) any later version.
28   *
29   * This library is distributed in the hope that it will be useful,
30   * but WITHOUT ANY WARRANTY; without even the implied warranty of
31   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32   * Lesser General Public License for more details.
33   *
34   * You should have received a copy of the GNU Lesser General Public
35   * License along with this library; if not, write to the Free Software
36   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
37   * <p/>
38   * $File:  $ <br>
39   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 29, 2006 3:35:03 PM $ <br>
40   * </small>
41   *
42   * @author wbogaardt
43   * @version 1
44   *          Date: Mar 29, 2006
45   *          Time: 3:35:03 PM
46   */
47  
48  public class AirportEntryDialog extends JDialog implements ActionListener {
49  
50      private AirportEntryForm form;
51      private AirportAdvancedEntryForm formAdvance;
52      private JButton cancelButton;
53      private JButton saveButton;
54      private AirportBO airport;
55      private JComponent parentComponent;
56  
57      public AirportEntryDialog() {
58          setTitle("Pilot's Log - Update Airport");
59          setModal(true);
60          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
61          initComponents();
62      }
63  
64      public AirportEntryDialog(JComponent form) {
65          parentComponent = form;
66          setTitle("Pilot's Log - Enter New Airport");
67          setModal(true);
68          setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
69          initComponents();
70      }
71  
72      public void setAirport(AirportBO oValue) {
73          this.airport = oValue;
74          form.setAirport(this.airport);
75          formAdvance.setAirport(this.airport);
76      }
77  
78      public AirportBO getAirport() {
79          this.airport = form.getAirport();
80          this.airport.setAltitude(formAdvance.getAltitude());
81          this.airport.setLatitude(formAdvance.getLatitude());
82          this.airport.setLongitude(formAdvance.getLongitude());
83          return this.airport;
84      }
85  
86      /**
87       * Init the componets in the dialog
88       */
89      private void initComponents() {
90          form = new AirportEntryForm();
91          formAdvance = new AirportAdvancedEntryForm();
92          saveButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_SAVE));
93          saveButton.addActionListener(this);
94  
95          cancelButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CANCEL));
96  
97          cancelButton.addActionListener(this);
98          getContentPane().setLayout(new BorderLayout());
99  
100         JPanel buttonPanel = new JPanel();
101         buttonPanel.setLayout(new GridBagLayout());
102         GridBagConstraints gridBagConstraints;
103         gridBagConstraints = new GridBagConstraints();
104         gridBagConstraints.gridx = 0;
105         gridBagConstraints.gridy = 1;
106         gridBagConstraints.anchor = GridBagConstraints.WEST;
107         buttonPanel.add(cancelButton, gridBagConstraints);
108 
109         gridBagConstraints = new GridBagConstraints();
110         gridBagConstraints.gridx = 1;
111         gridBagConstraints.gridy = 1;
112         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
113         gridBagConstraints.anchor = GridBagConstraints.WEST;
114         buttonPanel.add(saveButton, gridBagConstraints);
115 
116         JTabbedPane tabbed = new JTabbedPane();
117         tabbed.add("Basic", form);
118         tabbed.add("Advanced", formAdvance);
119         tabbed.add("Instrument", new JPanel());
120 
121         getContentPane().add(createTextPane(),BorderLayout.NORTH);
122         getContentPane().add(tabbed, BorderLayout.CENTER);
123         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
124         pack();
125         UIHelper.centerDialogToScreen(this);
126     }
127 
128     public void actionPerformed(ActionEvent event) {
129         Object source = event.getSource();
130         if (source.equals(saveButton))
131         {
132             AirportDelegate delegate = (AirportDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRPORT_DELEGATE);
133             delegate.saveAirport(this.getAirport());
134             this.dispose();
135         }
136         if (source.equals(cancelButton))
137         {
138             form.setAirport(new AirportBO());
139             this.dispose();
140         }
141     }
142 
143     private JTextPane createTextPane() {
144         String[] initString =
145                 { " " ,
146                   "This screen is to add/edit airports in your personal airport list.",
147                   " " ,
148                   "You can simply type in the relavant data below this information can",
149                   "be obtained either from AirNav.com or purchasing the CD from the FAA.",
150                   "There are other sources that you can use to get this airport data,",
151                   "including the airport facility directory, and the Bureau of ",
152                   "Transportation Statistics.",
153                   " ",
154                  };
155 
156        JMessageTextPane messagePane = new JMessageTextPane(initString);
157        return messagePane.getTextPane();
158     }
159 
160     /**
161      * override normal dispose because we want to update the calling component
162      * or table so it get's its display updated.
163      */
164      public void dispose() {
165         if (parentComponent instanceof AirportTableDisplayForm)
166         {
167             ((AirportTableDisplayForm) parentComponent).refresh();
168         }
169         super.dispose();
170     }
171 }