1   package org.wcb.gui.dialog;
2   
3   import org.wcb.gui.component.WaveButton;
4   import org.wcb.gui.component.JXTitlePanel;
5   import org.wcb.gui.forms.ConfigureAppForm;
6   import org.wcb.gui.util.ApplicationPreferences;
7   import org.wcb.gui.util.UIHelper;
8   import org.wcb.gui.util.IPilotsLogApplicationPreferenceKeys;
9   import org.wcb.resources.MessageResourceRegister;
10  import org.wcb.resources.MessageKey;
11  import org.jdesktop.swingx.border.DropShadowBorder;
12  
13  import javax.swing.*;
14  import javax.swing.border.Border;
15  import javax.swing.border.CompoundBorder;
16  import java.awt.event.ActionListener;
17  import java.awt.event.ActionEvent;
18  import java.awt.*;
19  import java.text.ParseException;
20  
21  /**
22   * <small>
23   * <p/>
24   * Copyright (C)  2006  wbogaardt.
25   * This library is free software; you can redistribute it and/or
26   * modify it under the terms of the GNU Lesser General Public
27   * License as published by the Free Software Foundation; either
28   * version 2.1 of the License, or (at your option) any later version.
29   * <p/>
30   * This library is distributed in the hope that it will be useful,
31   * but WITHOUT ANY WARRANTY; without even the implied warranty of
32   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33   * Lesser General Public License for more details.
34   * <p/>
35   * You should have received a copy of the GNU Lesser General Public
36   * License along with this library; if not, write to the Free Software
37   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
38   * <p/>
39   * $File:  $ <br>
40   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Oct 24, 2006 1:42:00 PM $ <br>
41   * </small>
42   *
43   * @author wbogaardt
44   *         The configuration dialog.
45   */
46  
47  public class ConfigureDialog extends JDialog implements ActionListener {
48  
49      private JButton okButton;
50      private ConfigureAppForm form;
51  
52      public ConfigureDialog() {
53          setTitle("Pilot's Log Configuration");
54          setModal(true);
55          this.initComponents();
56          this.refresh();
57      }
58  
59      private void initComponents() {
60          okButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_OK));
61          okButton.addActionListener(this);
62          JPanel mainPanel = new JXTitlePanel("Configuration", new Color(0x3779ff));
63          Border shadow = new DropShadowBorder(Color.BLACK, 2, 5, .3f, 10, false, true, true, true);
64          mainPanel.setLayout(new GridBagLayout());
65          mainPanel.setBorder(new CompoundBorder(shadow, mainPanel.getBorder()));
66  
67          form = new ConfigureAppForm();
68          GridBagConstraints gridBagConstraints = new GridBagConstraints();
69          gridBagConstraints.gridx = 0;
70          gridBagConstraints.gridy = 0;
71          gridBagConstraints.insets = new Insets(10,10,10,10);
72          gridBagConstraints.anchor = GridBagConstraints.WEST;
73          mainPanel.add(form, gridBagConstraints);
74  
75          gridBagConstraints = new GridBagConstraints();
76          gridBagConstraints.gridx = 0;
77          gridBagConstraints.gridy = 1;
78          gridBagConstraints.insets = new Insets(10,10,10,10);
79          gridBagConstraints.anchor = GridBagConstraints.EAST;
80          mainPanel.add(okButton, gridBagConstraints);
81  
82  
83          getContentPane().setLayout(new BorderLayout());
84          getContentPane().add(mainPanel, BorderLayout.CENTER);
85          pack();
86      }
87  
88      private void refresh() {
89          form.setEnableLogin(ApplicationPreferences.getInstance().getBoolean(ApplicationPreferences.LOGON_ENABLE));
90          form.setNumberRows(ApplicationPreferences.getInstance().getInt(IPilotsLogApplicationPreferenceKeys.LOG_ENTRIES_PER_PAGE, 15));
91          form.setEnableEffect(ApplicationPreferences.getInstance().getBoolean(IPilotsLogApplicationPreferenceKeys.ENABLE_EFFECTS_ON_CLOSING));
92          form.setWarningDays(ApplicationPreferences.getInstance().getInt(IPilotsLogApplicationPreferenceKeys.DAYS_WARNING_CARRY_PASSENGERS, 90));
93          form.setWarningNight(ApplicationPreferences.getInstance().getInt(IPilotsLogApplicationPreferenceKeys.DAYS_WARNING_CARRY_NIGHT, 90));
94          form.setWarningIfr(ApplicationPreferences.getInstance().getInt(IPilotsLogApplicationPreferenceKeys.DAYS_WARNING_IFR, 180));
95          form.setEnableRecencyWarning(ApplicationPreferences.getInstance().getBoolean(IPilotsLogApplicationPreferenceKeys.SHOW_WARN_ON_START));
96  		form.setEnableIfr(ApplicationPreferences.getInstance().getBoolean(IPilotsLogApplicationPreferenceKeys.CHECK_IFR));
97          UIHelper.centerDialogToScreen(this);
98      }
99  
100     public void actionPerformed(ActionEvent evt) {
101         Object src = evt.getSource();
102         if (src.equals(okButton))
103         {
104             try {
105                 ApplicationPreferences.getInstance().setBoolean(ApplicationPreferences.LOGON_ENABLE, form.isEnableLogin());
106                 ApplicationPreferences.getInstance().putInt(IPilotsLogApplicationPreferenceKeys.LOG_ENTRIES_PER_PAGE, form.getNumberRows());
107                 ApplicationPreferences.getInstance().setBoolean(IPilotsLogApplicationPreferenceKeys.ENABLE_EFFECTS_ON_CLOSING, form.isEnableEffect());
108                 ApplicationPreferences.getInstance().putInt(IPilotsLogApplicationPreferenceKeys.DAYS_WARNING_CARRY_PASSENGERS, form.getWarningDays());
109                 ApplicationPreferences.getInstance().putInt(IPilotsLogApplicationPreferenceKeys.DAYS_WARNING_CARRY_NIGHT, form.getWarningNight());
110                 ApplicationPreferences.getInstance().setBoolean(IPilotsLogApplicationPreferenceKeys.SHOW_WARN_ON_START, form.isEnableRecencyWarning());
111                 ApplicationPreferences.getInstance().setBoolean(IPilotsLogApplicationPreferenceKeys.CHECK_IFR, form.isIfr());
112 				/* IFR flight checking doesn't happen with all pilots so lets not save this information if not needed */
113                 if (form.isIfr()) {
114                     ApplicationPreferences.getInstance().putInt(IPilotsLogApplicationPreferenceKeys.DAYS_WARNING_IFR, form.getWarningIfr());
115                 }
116                 dispose();
117             } catch (ParseException pe) {
118                 JOptionPane.showMessageDialog(this, "The warning fields must be in # of days.", "Form Entry Error", JOptionPane.ERROR_MESSAGE);
119             }
120         }
121     }
122 }