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 import javax.swing.JPasswordField;
28 import javax.swing.JTextField;
29
30 import p3j.database.hibernate.P3MDatabase;
31 import p3j.gui.misc.P3JConfigFile;
32 import p3j.gui.panels.PropertiesShowPanelFactory;
33 import p3j.misc.Misc;
34 import p3j.misc.gui.GUI;
35
36
37
38
39
40
41
42 public class PreferencesDialog extends JDialog {
43
44
45 private static final long serialVersionUID = 3246519924977747183L;
46
47
48 public static final int DIALOG_WIDTH = 850;
49
50
51 public static final int DIALOG_HEIGHT = 220;
52
53
54 private static final int FORM_KEY_WIDTH = 200;
55
56
57 private final JPanel contentPanel;
58
59
60
61
62
63
64
65
66
67 public PreferencesDialog(Frame owner, final P3JConfigFile p3jConfiguration) {
68 super(owner, "Edit Preferences", true);
69 setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
70 GUI.centerOnScreen(this);
71
72
73 JButton apply = new JButton("Apply");
74 JButton cancel = new JButton("Cancel");
75 JButton resetToDefaults = new JButton("Reset Defaults");
76 JButton testDBConnection = new JButton("Test DB Connection");
77
78 List<JButton> buttons = new ArrayList<JButton>();
79 buttons.add(testDBConnection);
80 buttons.add(resetToDefaults);
81 buttons.add(cancel);
82 buttons.add(apply);
83
84 final PreferencesDialog thisDialog = this;
85
86 PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
87 FORM_KEY_WIDTH, buttons, 2);
88
89 pspf.sep("Database Connection");
90 final JTextField dbURL = new JTextField(
91 (String) p3jConfiguration.get(Misc.PREF_DB_URL));
92 pspf.app(Misc.PREF_DB_URL + ":", dbURL);
93
94 final JTextField dbUserName = new JTextField(
95 (String) p3jConfiguration.get(Misc.PREF_DB_USER));
96 pspf.app(Misc.PREF_DB_USER + ":", dbUserName);
97
98 final JTextField dbPassword = new JPasswordField(
99 (String) p3jConfiguration.get(Misc.PREF_DB_PWD));
100 pspf.app(Misc.PREF_DB_PWD + ":", dbPassword);
101
102 apply.addActionListener(new ActionListener() {
103 @Override
104 public void actionPerformed(ActionEvent e) {
105 Exception ex = P3MDatabase.testConnection(dbURL.getText(),
106 dbUserName.getText(), dbPassword.getText());
107 if (ex == null) {
108 p3jConfiguration.put(Misc.PREF_DB_URL, dbURL.getText());
109 p3jConfiguration.put(Misc.PREF_DB_USER, dbUserName.getText());
110 p3jConfiguration.put(Misc.PREF_DB_PWD, dbPassword.getText());
111 } else {
112 GUI.printErrorMessage(
113 thisDialog,
114 "DB Connection Failed",
115 "Old database configuration retained - the new connection data caused an error:"
116 + ex.getMessage(), ex);
117 }
118 setVisible(false);
119 }
120 });
121
122 cancel.addActionListener(new ActionListener() {
123 @Override
124 public void actionPerformed(ActionEvent e) {
125 setVisible(false);
126 }
127 });
128
129 testDBConnection.addActionListener(new ActionListener() {
130 @Override
131 public void actionPerformed(ActionEvent e) {
132 Exception ex = P3MDatabase.testConnection(dbURL.getText(),
133 dbUserName.getText(), dbPassword.getText());
134
135 if (ex != null) {
136 GUI.printErrorMessage(
137 thisDialog,
138 "DB Connection Failed",
139 "The connection to the database could not be established:"
140 + ex.getMessage(), ex);
141 } else {
142 GUI.printMessage(thisDialog, "DB Connection Test",
143 "A connection was established successfully.");
144 }
145 }
146 });
147
148 resetToDefaults.addActionListener(new ActionListener() {
149 @Override
150 public void actionPerformed(ActionEvent e) {
151 dbURL.setText(Misc.DEFAULT_DB_URL);
152 dbUserName.setText(Misc.DEFAULT_DB_USER);
153 dbPassword.setText(Misc.DEFAULT_DB_PWD);
154 contentPanel.repaint();
155 }
156 });
157
158 contentPanel = pspf.constructPanel();
159 this.getContentPane().add(contentPanel);
160 }
161 }