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 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   * Dialog to edit preferences.
38   * 
39   * @author Christina Bohk
40   * @author Roland Ewald
41   */
42  public class PreferencesDialog extends JDialog {
43  
44  	/** The Constant serialVersionUID. */
45  	private static final long serialVersionUID = 3246519924977747183L;
46  
47  	/** Width of the dialog. */
48  	public static final int DIALOG_WIDTH = 850;
49  
50  	/** Height of the dialog. */
51  	public static final int DIALOG_HEIGHT = 220;
52  
53  	/** The width of the key column in the form. */
54  	private static final int FORM_KEY_WIDTH = 200;
55  
56  	/** The content panel. */
57  	private final JPanel contentPanel;
58  
59  	/**
60  	 * Instantiates a new preferences dialog.
61  	 * 
62  	 * @param owner
63  	 *          the owner
64  	 * @param p3jConfiguration
65  	 *          the p3j configuration
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  		// Create new assignment
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 }