1 package org.wcb.gui.dialog;
2
3 import org.wcb.gui.forms.LogbookEntryForm;
4 import org.wcb.gui.component.WaveButton;
5 import org.wcb.gui.util.UIHelper;
6 import org.wcb.resources.MessageResourceRegister;
7 import org.wcb.model.vo.hibernate.Logbook;
8 import org.wcb.model.bd.LogbookDelegate;
9 import org.wcb.resources.MessageKey;
10
11 import javax.swing.*;
12 import java.awt.event.ActionListener;
13 import java.awt.event.ActionEvent;
14 import java.awt.*;
15
16
17
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 public class LogbookEditDialog extends JDialog implements ActionListener {
43
44 private LogbookEntryForm form;
45 private JButton jButtonClear;
46 private JButton jButtonSave;
47 private JButton cancelButton;
48 private Logbook entryValue;
49 private LogbookDelegate delegate;
50
51 public LogbookEditDialog() {
52 this(null);
53 }
54
55 public LogbookEditDialog(Logbook entry) {
56 setTitle("Edit Logbook Entry");
57 setModal(true);
58 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
59 delegate = new LogbookDelegate();
60 this.entryValue = entry;
61 initComponents();
62 this.setLogbook(entry);
63 UIHelper.centerDialogToScreen(this);
64 }
65
66 public void setLogbook(Logbook entry) {
67 this.entryValue = entry;
68 form.setLogEntry(entry);
69 }
70
71 public Logbook getLogbook() {
72 this.entryValue = form.getLogEntry();
73 return this.entryValue;
74 }
75
76
77
78
79 private void initComponents() {
80 form = new LogbookEntryForm();
81 cancelButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CANCEL));
82 cancelButton.addActionListener(this);
83
84 jButtonClear = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CLEAR));
85 jButtonClear.addActionListener(this);
86
87 jButtonSave = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_UPDATE));
88 if (entryValue == null)
89 {
90 jButtonSave.setText(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_SAVE));
91 }
92 jButtonSave.addActionListener(this);
93 getContentPane().setLayout(new BorderLayout());
94
95 JPanel buttonPanel = new JPanel();
96 buttonPanel.setLayout(new GridBagLayout());
97 GridBagConstraints gridBagConstraints;
98
99 gridBagConstraints = new GridBagConstraints();
100 gridBagConstraints.gridx = 0;
101 gridBagConstraints.gridy = 1;
102 gridBagConstraints.anchor = GridBagConstraints.WEST;
103 buttonPanel.add(cancelButton, gridBagConstraints);
104
105 gridBagConstraints = new GridBagConstraints();
106 gridBagConstraints.gridx = 1;
107 gridBagConstraints.gridy = 1;
108 gridBagConstraints.anchor = GridBagConstraints.WEST;
109 buttonPanel.add(jButtonClear, gridBagConstraints);
110
111 gridBagConstraints = new GridBagConstraints();
112 gridBagConstraints.gridx = 2;
113 gridBagConstraints.gridy = 1;
114 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
115 gridBagConstraints.anchor = GridBagConstraints.WEST;
116 buttonPanel.add(jButtonSave, gridBagConstraints);
117
118 JScrollPane scroller = new JScrollPane(form);
119 scroller.setViewportView(form);
120 scroller.setPreferredSize(new Dimension(260, 400));
121 scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
122
123 getContentPane().add(scroller, BorderLayout.CENTER);
124 getContentPane().add(buttonPanel, BorderLayout.SOUTH);
125 pack();
126 }
127
128 public void actionPerformed(ActionEvent event) {
129 Object source = event.getSource();
130 if (source.equals(jButtonSave))
131 {
132 entryValue = form.getLogEntry();
133 if (delegate.validate(entryValue))
134 {
135 if((form.getCrossCountry() > 0 && form.isValidCrossCountry()) || form.getCrossCountry() == 0) {
136 delegate.saveLogEntry(entryValue);
137 this.dispose();
138 } else {
139 JOptionPane.showMessageDialog(this, "There is a validation problem with cross country time.", "Validation Error", JOptionPane.ERROR_MESSAGE);
140 }
141 }
142 else
143 {
144 JOptionPane.showMessageDialog(this, "There is a possible validation problem", "Validation Error", JOptionPane.ERROR_MESSAGE);
145 }
146 }
147 if (source.equals(jButtonClear))
148 {
149 form.reset();
150 }
151 if (source.equals(cancelButton))
152 {
153 entryValue = null;
154 this.dispose();
155 }
156 }
157
158 }