1   package org.wcb.gui;
2   
3   import org.wcb.gui.forms.tabbed.LogbookTableDisplayForm;
4   import org.wcb.gui.forms.report.TotalFlightTimeByMonthReportForm;
5   import org.wcb.gui.forms.LogbookEntryDisplay;
6   import org.wcb.gui.component.*;
7   import org.wcb.gui.component.border.BookBorder;
8   import org.wcb.gui.component.border.LeftPageBookBorder;
9   import org.wcb.gui.event.ApplicationCloseListener;
10  import org.wcb.gui.event.FrameDialogListener;
11  import org.wcb.gui.util.UIHelper;
12  import org.wcb.gui.util.WindowStateSaver;
13  import org.wcb.gui.util.ApplicationPreferences;
14  import org.wcb.gui.dialog.JLoginDialog;
15  import org.wcb.resources.HSQLDatabaseInitializer;
16  import org.wcb.model.vo.hibernate.Logbook;
17  import org.jdesktop.swingx.border.DropShadowBorder;
18  import org.jdesktop.swingx.VerticalLayout;
19  //used for recency experience display
20  import org.wcb.model.service.impl.RecencyExperianceValidationService;
21  import org.wcb.model.service.IServicesConstants;
22  import org.wcb.model.util.SpringUtil;
23  import org.wcb.model.bd.LogbookDelegate;
24  import org.wcb.gui.util.IPilotsLogApplicationPreferenceKeys;
25  
26  import javax.swing.*;
27  import javax.swing.border.CompoundBorder;
28  import javax.swing.border.Border;
29  import java.awt.*;
30  
31  import com.jgoodies.plaf.plastic.Plastic3DLookAndFeel;
32  import com.jgoodies.plaf.plastic.theme.SkyBlue;
33  
34  /**
35   * <small>
36   * This library is free software; you can redistribute it and/or
37   * modify it under the terms of the GNU Lesser General Public
38   * License as published by the Free Software Foundation; either
39   * version 2.1 of the License, or (at your option) any later version.
40   *
41   * This library is distributed in the hope that it will be useful,
42   * but WITHOUT ANY WARRANTY; without even the implied warranty of
43   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
44   * Lesser General Public License for more details.
45   *
46   * You should have received a copy of the GNU Lesser General Public
47   * License along with this library; if not, write to the Free Software
48   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
49   * <p/>
50   * $File:  $ <br>
51   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 30, 2006 5:05:39 PM $ <br>
52   * </small>
53   *
54   * @author wbogaardt
55   * @version 1
56   *          Date: Mar 30, 2006
57   *          Time: 5:05:39 PM
58   */
59  
60  public class LogbookFrame extends JFrame {
61      private JPanel glass;
62      private JComponent sheet;
63      private JPanel eastPanel;
64      private TotalFlightTimeByMonthReportForm summaryReport;
65      private LogbookEntryDisplay logEntryform;
66      private LogbookTableDisplayForm logbookTableView;
67      public static final String LOGBOOK_VIEW = "Logbook";
68      public static final String LOGBOOK_ENTRY_VIEW = "Entry";
69      public static final String SUMMARY_REPORT_VIEW = "Summary";
70  
71      public LogbookFrame() {
72          glass = (JPanel) getGlassPane();
73          setTitle("Pilots Log");
74          super.setIconImage(UIHelper.getIcon("org/wcb/resources/gui/wings.png").getImage());
75          this.showSplashScreen();
76  		if (ApplicationPreferences.getInstance().getBoolean(IPilotsLogApplicationPreferenceKeys.SHOW_WARN_ON_START)) {
77  			RecencyExperianceValidationService recencyValidator = (RecencyExperianceValidationService) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_RECENCY_EXPERIANCE);
78  			LogbookDelegate delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
79              recencyValidator.setObject(delegate.getAllLogbookEntries());
80              if(!recencyValidator.validate()) {
81  			String messages = recencyValidator.getMessage();
82              JOptionPane.showMessageDialog(this, messages, "Warning" , JOptionPane.WARNING_MESSAGE);
83  			}
84  		}
85  		
86      }
87  
88      public JPanel getCardPanel() {
89          return eastPanel;
90      }
91  
92      private void setApplicationLookAndFeel() {
93          // PlasticLookAndFeel.setMyCurrentTheme(new SkyBlue());
94          try
95          {
96              UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
97              Plastic3DLookAndFeel.setMyCurrentTheme(new SkyBlue());
98              com.jgoodies.plaf.Options.setPopupDropShadowEnabled(true);
99          }
100         catch (Exception e)
101         {
102             e.printStackTrace();
103         }
104     }
105 
106     private void showSplashScreen() {
107         SplashScreen splash = new SplashScreen(UIHelper.getIcon("org/wcb/resources/gui/safelog_cover_black_sm.jpg"));
108         try
109         {
110             splash.showStatus("JPilot Logbook V 1.0");
111             new HSQLDatabaseInitializer();
112             splash.showStatus("created by");
113             Thread.sleep(100);
114             this.initComponents();
115             this.setApplicationLookAndFeel();
116             pack();
117             splash.showStatus("Walter Bogaardt");
118             Thread.sleep(500);
119             setSize(800, 620);
120             Toolkit tk = Toolkit.getDefaultToolkit();
121             tk.addAWTEventListener(WindowStateSaver.getInstance(), AWTEvent.WINDOW_EVENT_MASK);
122             loginUser();
123             addWindowListener(new ApplicationCloseListener(this));
124             splash.close();
125         }
126         catch (InterruptedException err)
127         {
128             err.printStackTrace();
129         }
130     }
131 
132     /**
133      * This method does calls to see if the user has configured the application to have
134      * user authentication. In the case of first time running this application this is turned
135      * off. The user has to enable user authentication and then it gets turned on everytime until
136      * they disable this feature.
137      */
138     private void loginUser() {
139         //test if the user elected to use login authentication if they did then display the login dialog.
140         if (ApplicationPreferences.getInstance().getBoolean(ApplicationPreferences.LOGON_ENABLE)) {
141             JLoginDialog dialog = new JLoginDialog(this, "JPilot Login", true);
142             dialog.setVisible(true);
143         }
144         else {
145             //show this frame because the user elected for no login authentication
146              setVisible(true);
147         }
148     }
149 
150     public void refreshSummaryReport() {
151         summaryReport.refresh();
152     }
153 
154     public void setLogEntryForm(Logbook oValue) {
155         logEntryform.setLogbookValue(oValue);
156     }
157 
158     public Logbook getLogEntryForm() {
159         return logEntryform.getLogbookValue();
160     }
161 
162     /**
163      * Allows refreshing the table view of the logbook. This
164      * in turn calls the table view's model to refresh from
165      * the database, this is because a new entry has been made.
166      */
167     public void refreshLogbookView() {
168         logbookTableView.refresh();
169     }
170 
171     private void initComponents() {
172         setLayout(new BorderLayout());
173         setJMenuBar(new JPilotMenu(this));
174         ImageIcon titleIcon = UIHelper.getIcon("org/wcb/resources/gui/log_title.png");
175         JLabel titleLabel = new JLabel(titleIcon);
176         logbookTableView = new LogbookTableDisplayForm();
177         //CoolTitlePanel titlePanel = new CoolTitlePanel();
178         JPanel titlePanel = new JPanel();
179         titlePanel.setBackground(new Color(0x8a84ab)); //new Color(0xa4b8d5)
180         titlePanel.setLayout(new BorderLayout());
181 
182         //The right side panel, which is the logbook entries
183         eastPanel = new JPanel(new CardLayout());
184         eastPanel.setBorder(new CompoundBorder(new BookBorder(), eastPanel.getBorder()));
185         eastPanel.add(logbookTableView, LOGBOOK_VIEW);
186 
187         JXTitlePanel summaryPanel = new JXTitlePanel("Summary of all Flights", new Color(0x3779ff));
188         Border shadow = new DropShadowBorder(Color.BLACK, 2, 5, .3f, 10, false, true, true, true);
189         summaryPanel.setBorder(new CompoundBorder(shadow, summaryPanel.getBorder()));
190         summaryReport = new TotalFlightTimeByMonthReportForm();
191         summaryPanel.add(summaryReport);
192         eastPanel.add(summaryPanel, SUMMARY_REPORT_VIEW);
193 
194         logEntryform = new LogbookEntryDisplay(this);
195         eastPanel.add(logEntryform, LOGBOOK_ENTRY_VIEW);
196         //The button panel on the left side
197         JPanel westPanel = new JPanel();
198         westPanel.setLayout(new VerticalLayout(2));
199         westPanel.setBorder(new CompoundBorder(new LeftPageBookBorder(), westPanel.getBorder()));
200         JButton logbook = new WaveButton("Logbook");
201         JButton summary = new WaveButton("Summary");
202         JButton airport = new WaveButton("Airports");
203         JButton aircraft = new WaveButton("Aircraft");
204         logbook.addActionListener(new FrameDialogListener(this));
205         summary.addActionListener(new FrameDialogListener(this));
206         airport.addActionListener(new FrameDialogListener(this));
207         aircraft.addActionListener(new FrameDialogListener(this));
208         westPanel.add(logbook);
209         westPanel.add(aircraft);
210         westPanel.add(airport);
211         westPanel.add(summary);
212 
213         //put the left side and the right side together to make it look like an open book
214         JPanel mainPanel = new JPanel(new BorderLayout());
215         mainPanel.add(westPanel, BorderLayout.WEST);
216         mainPanel.add(eastPanel, BorderLayout.CENTER);
217 
218         //titlePanel.add(new JPilotIconMenu(logbookForm), BorderLayout.NORTH);
219         titlePanel.add(new JPilotIconMenu(this), BorderLayout.NORTH);
220         titlePanel.add(titleLabel, BorderLayout.WEST);
221         add(titlePanel, BorderLayout.NORTH);
222         add(mainPanel, BorderLayout.CENTER);
223         add(new JStatusBar(), BorderLayout.SOUTH);
224     }
225 
226     public JComponent showJDialogAsSheet(JDialog dialog) {
227         sheet = (JComponent) dialog.getContentPane();
228         glass.setLayout(new GridBagLayout());
229         //sheet.setBorder(new LineBorder(Color.black, 1));
230         glass.removeAll();
231         GridBagConstraints gbc = new GridBagConstraints();
232         gbc.anchor = GridBagConstraints.NORTH;
233         glass.add(sheet, gbc);
234         gbc.gridy = 1;
235         gbc.weighty = Integer.MAX_VALUE;
236         glass.add(Box.createGlue(), gbc);
237         glass.setVisible(true);
238         return sheet;
239     }
240 
241     public void hideSheet() {
242         glass.setVisible(false);
243     }
244 
245     public static void main(String[] args) {
246         new LogbookFrame();
247     }
248 }