View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.dialogs;
17  
18  import james.core.data.DBConnectionData;
19  import james.core.util.misc.Pair;
20  
21  import java.awt.Frame;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.swing.JButton;
28  import javax.swing.JDialog;
29  import javax.swing.JPanel;
30  
31  import p3j.database.DatabaseType;
32  import p3j.database.IPreferencesUIProvider;
33  import p3j.database.hibernate.P3MDatabase;
34  import p3j.gui.misc.P3JConfigFile;
35  import p3j.gui.panels.PropertiesShowPanelFactory;
36  import p3j.misc.Misc;
37  import p3j.misc.gui.GUI;
38  
39  /**
40   * Dialog to edit preferences.
41   * 
42   * @author Christina Bohk
43   * @author Roland Ewald
44   */
45  public class PreferencesDialog extends JDialog {
46  
47    /** The Constant serialVersionUID. */
48    private static final long serialVersionUID = 3246519924977747183L;
49  
50    /** Width of the dialog. */
51    public static final int DIALOG_WIDTH = 850;
52  
53    /** Height of the dialog. */
54    public static final int DIALOG_HEIGHT = 150;
55  
56    /** The width of the key column in the form. */
57    private static final int FORM_KEY_WIDTH = 200;
58  
59    /** The content panel. */
60    private final JPanel contentPanel;
61  
62    /** The chosen database type. */
63    private final DatabaseType dbType;
64  
65    /** The configuration file. */
66    private final P3JConfigFile configFile;
67  
68    /**
69     * Instantiates a new preferences dialog.
70     * 
71     * @param owner
72     *          the owner
73     * @param p3jConfiguration
74     *          the p3j configuration
75     * @param databaseType
76     *          the chosen database type
77     */
78    public PreferencesDialog(Frame owner, final P3JConfigFile p3jConfiguration,
79        DatabaseType databaseType) {
80      super(owner, "Edit Preferences", true);
81      dbType = databaseType;
82      configFile = p3jConfiguration;
83  
84      // Create new assignment
85      JButton apply = new JButton("Apply");
86      JButton cancel = new JButton("Cancel");
87      JButton resetToDefaults = new JButton("Reset Defaults");
88      JButton testDBConnection = new JButton("Test DB Connection");
89  
90      List<JButton> buttons = new ArrayList<JButton>();
91      buttons.add(testDBConnection);
92      buttons.add(resetToDefaults);
93      buttons.add(cancel);
94      buttons.add(apply);
95  
96      final PreferencesDialog thisDialog = this;
97  
98      PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
99          FORM_KEY_WIDTH, buttons, 2);
100 
101     pspf.sep("Database Connection");
102 
103     final IPreferencesUIProvider uiProvider = dbType.getPreferencesUIProvider();
104     setSize(DIALOG_WIDTH, DIALOG_HEIGHT + uiProvider.getHeight());
105     GUI.centerOnScreen(this);
106 
107     uiProvider.addUIElements(pspf, dbType.readPreferences(p3jConfiguration));
108 
109     apply.addActionListener(new ActionListener() {
110       @Override
111       public void actionPerformed(ActionEvent e) {
112         Pair<DBConnectionData, String> connData = uiProvider.getDBPreferences();
113         Exception ex = P3MDatabase.testConnection(connData.getFirstValue()
114             .getUrl(), connData.getFirstValue().getUser(), connData
115             .getFirstValue().getPassword());
116         if (ex == null) {
117           dbType.writePreferences(configFile, connData);
118           configFile.put(Misc.PREF_DB_TYPE, dbType);
119         } else {
120           GUI.printErrorMessage(
121               thisDialog,
122               "DB Connection Failed",
123               "Old database configuration retained - the new connection data caused an error:"
124                   + ex.getMessage(), ex);
125         }
126         setVisible(false);
127       }
128     });
129 
130     cancel.addActionListener(new ActionListener() {
131       @Override
132       public void actionPerformed(ActionEvent e) {
133         setVisible(false);
134       }
135     });
136 
137     testDBConnection.addActionListener(new ActionListener() {
138       @Override
139       public void actionPerformed(ActionEvent e) {
140         Pair<DBConnectionData, String> connData = uiProvider.getDBPreferences();
141         Exception ex = P3MDatabase.testConnection(connData.getFirstValue()
142             .getUrl(), connData.getFirstValue().getUser(), connData
143             .getFirstValue().getPassword());
144         if (ex != null) {
145           GUI.printErrorMessage(
146               thisDialog,
147               "DB Connection Failed",
148               "The connection to the database could not be established:"
149                   + ex.getMessage(), ex);
150         } else {
151           GUI.printMessage(thisDialog, "DB Connection Test",
152               "A connection was established successfully.");
153         }
154       }
155     });
156 
157     resetToDefaults.addActionListener(new ActionListener() {
158       @Override
159       public void actionPerformed(ActionEvent e) {
160         uiProvider.setDBPreferences(dbType.getDefaults());
161         contentPanel.repaint();
162       }
163     });
164 
165     contentPanel = pspf.constructPanel();
166     this.getContentPane().add(contentPanel);
167   }
168 }