1   package org.wcb.gui.forms;
2   
3   import org.wcb.gui.component.JXTitlePanel;
4   import org.wcb.gui.component.WaveButton;
5   import org.wcb.gui.component.JMessageTextPane;
6   import org.wcb.gui.LogbookFrame;
7   import org.wcb.resources.MessageResourceRegister;
8   import org.wcb.resources.MessageKey;
9   import org.wcb.model.vo.hibernate.Logbook;
10  import org.wcb.model.bd.LogbookDelegate;
11  import org.jdesktop.swingx.border.DropShadowBorder;
12  
13  import javax.swing.border.Border;
14  import javax.swing.border.CompoundBorder;
15  import javax.swing.*;
16  import java.awt.*;
17  import java.awt.event.ActionListener;
18  import java.awt.event.ActionEvent;
19  
20  /**
21   * <small>
22   * <p/>
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   * <p/>
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   * <p/>
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: Nov 13, 2006 12:25:19 PM $ <br>
40   * </small>
41   *
42   * @author wbogaardt
43   *         Used to display a logbook entry.
44   */
45  
46  public class LogbookEntryDisplay extends JXTitlePanel implements ActionListener {
47  
48      private LogbookEntryForm logEntryForm;
49      private JButton jButtonClear;
50      private JButton jButtonSave;
51      private LogbookFrame frameRef;
52      private LogbookDelegate delegate;
53  
54      public LogbookEntryDisplay(LogbookFrame parentFrame) {
55          super("Logbook New Entry", new Color(0x3779ff));
56          Border shadow = new DropShadowBorder(Color.BLACK, 2, 5, .3f, 10, false, true, true, true);
57          this.setBorder(new CompoundBorder(shadow, this.getBorder()));
58          this.delegate = new LogbookDelegate();
59          this.frameRef = parentFrame;
60          initComponents();
61      }
62  
63      private void initComponents() {
64          setLayout(new BorderLayout());
65  
66          logEntryForm = new LogbookEntryForm();
67  
68          JScrollPane scroller = new JScrollPane(logEntryForm);
69          scroller.setViewportView(logEntryForm);
70          scroller.setPreferredSize(new Dimension(250,400));
71          scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
72  
73          jButtonClear = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_CLEAR));
74          jButtonClear.addActionListener(this);
75  
76          jButtonSave = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_SAVE));
77          jButtonSave.addActionListener(this);
78  
79          JPanel topPanel = new JPanel(new GridLayout(1,2));
80          topPanel.add(scroller);
81          topPanel.add(createTextPane());
82  
83          JPanel buttonPanel = new JPanel(new GridBagLayout());
84          GridBagConstraints gridBagConstraints;
85  
86          gridBagConstraints = new GridBagConstraints();
87          gridBagConstraints.gridx = 0;
88          gridBagConstraints.gridy = 1;
89          gridBagConstraints.anchor = GridBagConstraints.WEST;
90          buttonPanel.add(jButtonClear, gridBagConstraints);
91  
92          gridBagConstraints = new GridBagConstraints();
93          gridBagConstraints.gridx = 1;
94          gridBagConstraints.gridy = 1;
95          gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
96          gridBagConstraints.anchor = GridBagConstraints.WEST;
97          buttonPanel.add(jButtonSave, gridBagConstraints);
98  
99  
100         add(topPanel, BorderLayout.CENTER);
101 
102         add(buttonPanel, BorderLayout.SOUTH);
103     }
104 
105     public Logbook getLogbookValue() {
106         return logEntryForm.getLogEntry();
107     }
108 
109     public void setLogbookValue(Logbook entryValue) {
110         logEntryForm.setLogEntry(entryValue);
111     }
112 
113     private JTextPane createTextPane() {
114         String[] initString =
115                 { " " ,
116                         "The idea is to make pilot logbook entry easy. ",
117                         "We've designed the entry form to be easy to work",
118                         "with, and assist in entering redundant information." ,
119                         "This logbook software is only as good as the data",
120                         "entered into it. It is your responsibility to ",
121                         "ensure accuracy, and compliance with the FAR rules.",
122                         " ",
123                         "The hope is that this logbook software makes it",
124                         "easier to track pilot time, and report that",
125                         "information acurately. Though reports, and some",
126                         "basic analysis of FAR regulations. Note: that",
127                         "regulations change and as such it is important",
128                         "to keep up with them."
129                 };
130 
131         JMessageTextPane messagePane = new JMessageTextPane(initString);
132         messagePane.setBackground(new Color(0x96bfdd));
133         return messagePane.getTextPane();
134     }
135 
136     public void actionPerformed(ActionEvent event) {
137         Object source = event.getSource();
138         if (source.equals(jButtonSave))
139         {
140             if (delegate.validate(logEntryForm.getLogEntry()))
141             {
142                 if((logEntryForm.getCrossCountry() > 0 && logEntryForm.isValidCrossCountry()) || logEntryForm.getCrossCountry() == 0) {
143                     delegate.saveLogEntry(logEntryForm.getLogEntry());
144                     //If we have reference to the display form then refresh the model.
145                     this.frameRef.refreshLogbookView();
146                     CardLayout cl = (CardLayout) frameRef.getCardPanel().getLayout();
147                     cl.show(frameRef.getCardPanel(), LogbookFrame.LOGBOOK_VIEW);
148                 } else {
149                     JOptionPane.showMessageDialog(this, "There is a validation problem with cross country time.", "Validation Error", JOptionPane.ERROR_MESSAGE);   
150                 }
151             }
152             else
153             {
154                 JOptionPane.showMessageDialog(this, "There is a possible validation problem", "Validation Error", JOptionPane.ERROR_MESSAGE);
155             }
156         }
157         if (source.equals(jButtonClear))
158         {
159             logEntryForm.reset();
160         }
161     }
162 
163 }