1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.dialogs;
17
18 import java.awt.Frame;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import javax.swing.JButton;
25 import javax.swing.JDialog;
26 import javax.swing.JPanel;
27
28 import org.jamesii.core.data.DBConnectionData;
29 import org.jamesii.core.util.misc.Pair;
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
41
42
43
44
45 public class PreferencesDialog extends JDialog {
46
47
48 private static final long serialVersionUID = 3246519924977747183L;
49
50
51 public static final int DIALOG_WIDTH = 850;
52
53
54 public static final int DIALOG_HEIGHT = 150;
55
56
57 private static final int FORM_KEY_WIDTH = 200;
58
59
60 private final JPanel contentPanel;
61
62
63 private final DatabaseType dbType;
64
65
66 private final P3JConfigFile configFile;
67
68
69
70
71
72
73
74
75
76
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
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 }