1   package org.wcb.gui.forms;
2   
3   import org.wcb.model.bd.AircraftDelegate;
4   import org.wcb.model.vo.hibernate.AircraftTypeBO;
5   import org.wcb.model.vo.hibernate.AircraftBO;
6   import org.wcb.model.util.SpringUtil;
7   import org.wcb.model.service.IServicesConstants;
8   import org.wcb.gui.dialog.AircraftTypeTableDisplayDialog;
9   import org.wcb.gui.component.JXTitlePanel;
10  import org.wcb.resources.MessageResourceRegister;
11  import org.wcb.resources.MessageKey;
12  import org.jdesktop.swingx.border.DropShadowBorder;
13  
14  import javax.swing.*;
15  import javax.swing.border.CompoundBorder;
16  import java.awt.*;
17  import java.awt.event.ActionListener;
18  import java.awt.event.ActionEvent;
19  
20  /**
21   * <small>
22   * Copyright (c)  2006  wbogaardt.
23   * This library is free software; you can redistribute it and/or
24   * modify it under the terms of the GNU Lesser General Public
25   * License as published by the Free Software Foundation; either
26   * version 2.1 of the License, or (at your option) any later version.
27   *
28   * This library is distributed in the hope that it will be useful,
29   * but WITHOUT ANY WARRANTY; without even the implied warranty of
30   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31   * Lesser General Public License for more details.
32   *
33   * You should have received a copy of the GNU Lesser General Public
34   * License along with this library; if not, write to the Free Software
35   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
36   * <p/>
37   * $File:  $ <br>
38   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 27, 2006 11:59:17 AM $ <br>
39   * </small>
40   *
41   * @author wbogaardt
42   * @version 1
43   *          Date: Mar 27, 2006
44   *          Time: 11:59:17 AM
45   */
46  
47  public class AircraftEntryForm extends JXTitlePanel implements ActionListener {
48  
49      private JTextField jTextFieldRegistration;
50      private JButton selectAircraftTypeButton;
51      private JLabel aircraftTypeLabel;
52      private AircraftDelegate delegate;
53      private AircraftBO oAircraft;
54      private AircraftTypeBO oAircraftType;
55  
56  
57      public AircraftEntryForm() {
58          super("AircraftBO", new Color(0x522aec));
59          this.setBorder(new CompoundBorder(new DropShadowBorder(Color.BLACK, 0, 5, .5f, 12, true, true, true, true), this.getBorder()));
60          delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
61          initComponents();
62      }
63  
64      /**
65       * Sets up the componets on this panel to be properly layed out.
66       */
67      private void initComponents() {
68          jTextFieldRegistration = new JTextField(15);
69          selectAircraftTypeButton = new JButton("Select");
70          selectAircraftTypeButton.setToolTipText("Select an aircraft type");
71          selectAircraftTypeButton.addActionListener(this);
72          aircraftTypeLabel = new JLabel("Cessna 172M  t");
73  
74  
75          setLayout(new GridBagLayout());
76  
77          GridBagConstraints gridBagConstraints = new GridBagConstraints();
78          gridBagConstraints.anchor = GridBagConstraints.WEST;
79          gridBagConstraints.insets = new Insets(0, 5, 0, 0);
80          add(new JLabel(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_REGISTRATION)), gridBagConstraints);
81  
82          gridBagConstraints = new GridBagConstraints();
83          gridBagConstraints.gridwidth = 2;
84          gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
85          gridBagConstraints.insets = new Insets(0, 5, 0, 5);
86          add(jTextFieldRegistration, gridBagConstraints);
87  
88          gridBagConstraints = new GridBagConstraints();
89          gridBagConstraints.gridx = 0;
90          gridBagConstraints.gridy = 1;
91          gridBagConstraints.anchor = GridBagConstraints.WEST;
92          gridBagConstraints.insets = new Insets(0, 5, 0, 0);
93          add(new JLabel(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_AIRCRAFT_TYPE)), gridBagConstraints);
94  
95          gridBagConstraints = new GridBagConstraints();
96          gridBagConstraints.gridx = 1;
97          gridBagConstraints.gridy = 1;
98          gridBagConstraints.insets = new Insets(0, 5, 0, 5);
99          add(selectAircraftTypeButton, gridBagConstraints);
100 
101         gridBagConstraints = new GridBagConstraints();
102         gridBagConstraints.gridx = 2;
103         gridBagConstraints.gridy = 1;
104         gridBagConstraints.insets = new Insets(0, 0, 0, 5);
105         add(aircraftTypeLabel, gridBagConstraints);
106     }
107 
108     /**
109      * Resets the form values to blank.
110      */
111     public void reset() {
112         this.setRegistration("");
113         this.aircraftTypeLabel.setText("");
114         oAircraftType = new AircraftTypeBO();
115     }
116 
117     /**
118      * Marshall the form information into a value object
119      * @return the value object from the form.
120      */
121     private AircraftBO marshalFromForm(AircraftBO oCraft) {
122         oCraft.setRegistrationNumber(this.getRegistration());
123         oCraft.setTypeId(this.getAircraftType().getId());
124         return oCraft;
125     }
126 
127     private void marshalToForm(AircraftBO oCraft) {
128         this.setRegistration(oCraft.getRegistrationNumber());
129         this.setAircraftType(delegate.getAircraftType(oCraft.getTypeId()));
130         this.aircraftTypeLabel.setText(this.getAircraftType().getManufacturer() + " " + this.getAircraftType().getModel());
131     }
132 
133     public AircraftBO getAircraft() {
134         if (oAircraft == null)
135         {
136             oAircraft = new AircraftBO();
137         }
138         return this.marshalFromForm(oAircraft);
139     }
140 
141     public void setAircraft(AircraftBO oValue) {
142         this.oAircraft = oValue;
143         marshalToForm(oAircraft);
144     }
145 
146     public void setRegistration(String sVal) {
147         jTextFieldRegistration.setText(sVal);
148     }
149 
150     public String getRegistration() {
151         return jTextFieldRegistration.getText();
152     }
153 
154     public void setAircraftType(AircraftTypeBO val) {
155         oAircraftType = val;
156         this.aircraftTypeLabel.setText(this.getAircraftType().getManufacturer() + " " + this.getAircraftType().getModel());
157     }
158 
159     public AircraftTypeBO getAircraftType() {
160         return oAircraftType;
161     }
162 
163     public void actionPerformed(ActionEvent evt) {
164         AircraftTypeTableDisplayDialog dialog = new AircraftTypeTableDisplayDialog(this);
165         dialog.setVisible(true);
166     }
167 }