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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
73
74
75 public void setAircraftDelegate(AircraftDelegate delegator) {
76 this.delegate = delegator;
77 }
78
79
80
81
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
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
148
149
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 }