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 java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  import java.io.File;
24  
25  import javax.swing.JButton;
26  import javax.swing.JFileChooser;
27  import javax.swing.JPanel;
28  import javax.swing.JTextField;
29  
30  import p3j.gui.P3J;
31  import p3j.gui.panels.PropertiesShowPanelFactory;
32  import p3j.misc.Misc;
33  
34  /**
35   * The user interface to configure HSQLDB database connections.
36   * 
37   * Created on 28.10.2012
38   * 
39   * @author Christina Bohk
40   * @author Roland Ewald
41   */
42  public class HSQLDBPreferencesUIProvider implements IPreferencesUIProvider {
43  
44    /** The width of the text field. */
45    private static final int TEXT_FIELD_WIDTH = 20;
46  
47    /** The text field containing the Database location. */
48    final JTextField dbLocation = new JTextField();
49  
50    /** The file chooser button. */
51    final JButton chooseFile = new JButton("...");
52    {
53      chooseFile.addActionListener(new ActionListener() {
54        @Override
55        public void actionPerformed(ActionEvent e) {
56          JFileChooser chooser = new JFileChooser(dbLocation.getText());
57          chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
58          if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(P3J
59              .getInstance())) {
60            dbLocation.setText(chooser.getSelectedFile().getAbsolutePath());
61          }
62        }
63      });
64    }
65  
66    /**
67     * Panel that contains the location text field and a button for the file
68     * chooser.
69     */
70    final JPanel dbLocationPanel = new JPanel();
71  
72    /**
73     * Instantiates a new UI provider for HSQLDB preferences.
74     */
75    public HSQLDBPreferencesUIProvider() {
76      dbLocation.setColumns(TEXT_FIELD_WIDTH);
77      dbLocationPanel.add(dbLocation);
78      dbLocationPanel.add(chooseFile);
79    }
80  
81    @Override
82    public int getHeight() {
83      return 30;
84    }
85  
86    @Override
87    public void addUIElements(PropertiesShowPanelFactory pspf,
88        Pair<DBConnectionData, String> connData) {
89      setDBPreferences(connData);
90      pspf.app(Misc.GUI_LABEL_DB_FILE_LOCATION, dbLocationPanel);
91    }
92  
93    @Override
94    public Pair<DBConnectionData, String> getDBPreferences() {
95      return new Pair<>(new DBConnectionData(Misc.HSQLDB_URL_PREFIX
96          + dbLocation.getText() + File.separator + Misc.HSQLDB_FILE_NAME,
97          Misc.DEFAULT_DB_USERS.get(DatabaseType.HSQLDB),
98          Misc.DEFAULT_DB_PWDS.get(DatabaseType.HSQLDB),
99          Misc.JDBC_DRIVERS.get(DatabaseType.HSQLDB)),
100         Misc.HIBERNATE_DIALECTS.get(DatabaseType.HSQLDB));
101   }
102 
103   @Override
104   public void setDBPreferences(Pair<DBConnectionData, String> connData) {
105     dbLocation.setText((new File(connData
106         .getFirstValue()
107         .getUrl()
108         .substring(
109             Misc.HSQLDB_URL_PREFIX.length(),
110             connData.getFirstValue().getUrl().length()
111                 - Misc.HSQLDB_FILE_NAME.length())).getAbsolutePath()));
112   }
113 
114 }