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   * <small>
18   * Copyright (c)  2006  wbogaardt.
19   * This library is free software; you can redistribute it and/or
20   * modify it under the terms of the GNU Lesser General Public
21   * License as published by the Free Software Foundation; either
22   * version 2.1 of the License, or (at your option) any later version.
23   *
24   * This library is distributed in the hope that it will be useful,
25   * but WITHOUT ANY WARRANTY; without even the implied warranty of
26   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27   * Lesser General Public License for more details.
28   *
29   * You should have received a copy of the GNU Lesser General Public
30   * License along with this library; if not, write to the Free Software
31   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
32   * <p/>
33   * $File:  $ <br>
34   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 30, 2006 10:35:19 AM $ <br>
35   * </small>
36   *
37   * @author wbogaardt
38   * @version 1
39   *          Date: Mar 30, 2006
40   *          Time: 10:35:19 AM
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       * Init the componets in the dialog.
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 }