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.database;
17  
18  import james.core.data.DBConnectionData;
19  import james.core.util.misc.Pair;
20  
21  import javax.swing.JPasswordField;
22  import javax.swing.JTextField;
23  
24  import p3j.gui.panels.PropertiesShowPanelFactory;
25  import p3j.misc.Misc;
26  
27  /**
28   * GUI to configure database connection to MySQL server.
29   * 
30   * CAn be easily re-used for other (non-file-based) DBMS.
31   * 
32   * Created on 28.10.2012
33   * 
34   * @author Christina Bohk
35   * @author Roland Ewald
36   */
37  public class MySQLPreferencesUIProvider implements IPreferencesUIProvider {
38  
39    /** The url. */
40    final JTextField dbURL = new JTextField();
41  
42    /** The user name. */
43    final JTextField dbUserName = new JTextField();
44  
45    /** The password. */
46    final JTextField dbPassword = new JPasswordField();
47  
48    /** The database type. */
49    private static final DatabaseType DB_TYPE = DatabaseType.MYSQL;
50  
51    @Override
52    public int getHeight() {
53      return 70;
54    }
55  
56    @Override
57    public void addUIElements(PropertiesShowPanelFactory pspf,
58        Pair<DBConnectionData, String> connData) {
59      setDBPreferences(connData);
60      pspf.app(Misc.PREF_DB_URL + ": " + Misc.MYSQL_URL_PREFIX, dbURL);
61      pspf.app(Misc.PREF_DB_USER + ":", dbUserName);
62      pspf.app(Misc.PREF_DB_PWD + ":", dbPassword);
63    }
64  
65    @Override
66    public Pair<DBConnectionData, String> getDBPreferences() {
67      return new Pair<>(new DBConnectionData(Misc.MYSQL_URL_PREFIX
68          + dbURL.getText(), dbUserName.getText(), dbPassword.getText(),
69          Misc.JDBC_DRIVERS.get(DB_TYPE)), Misc.HIBERNATE_DIALECTS.get(DB_TYPE));
70    }
71  
72    @Override
73    public void setDBPreferences(Pair<DBConnectionData, String> connData) {
74      dbURL.setText(connData.getFirstValue().getUrl()
75          .substring(Misc.MYSQL_URL_PREFIX.length()));
76      dbUserName.setText(connData.getFirstValue().getUser());
77      dbPassword.setText(connData.getFirstValue().getPassword());
78    }
79  
80  }