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.BorderLayout;
19  import java.awt.Frame;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.ActionListener;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.swing.BoxLayout;
26  import javax.swing.ButtonGroup;
27  import javax.swing.JButton;
28  import javax.swing.JDialog;
29  import javax.swing.JPanel;
30  import javax.swing.JRadioButton;
31  
32  import p3j.database.DatabaseType;
33  import p3j.gui.panels.PropertiesShowPanelFactory;
34  import p3j.misc.gui.GUI;
35  
36  /**
37   * Allows user to select a database type to use.
38   * 
39   * Created on 27.10.2012
40   * 
41   * @author Christina Bohk
42   * @author Roland Ewald
43   */
44  public class DatabaseTypeSelectionDialog extends JDialog {
45  
46    /** The Constant serialVersionUID. */
47    private static final long serialVersionUID = -6739792487369234085L;
48  
49    private static final int DIALOG_WIDTH = 500;
50  
51    private static final int DIALOG_HEIGHT = 200;
52  
53    private final JPanel contentPanel = new JPanel(GUI.getStdBorderLayout());
54  
55    private DatabaseType dbType = DatabaseType.HSQLDB;
56  
57    public DatabaseTypeSelectionDialog(Frame owner, DatabaseType currentDBType) {
58      super(owner, "Select Database Type", true);
59      setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
60      GUI.centerOnScreen(this);
61  
62      dbType = currentDBType;
63  
64      JButton next = new JButton("Next");
65      next.addActionListener(new ActionListener() {
66        @Override
67        public void actionPerformed(ActionEvent e) {
68          setVisible(false);
69        }
70      });
71  
72      JButton cancel = new JButton("Cancel");
73      cancel.addActionListener(new ActionListener() {
74        @Override
75        public void actionPerformed(ActionEvent e) {
76          dbType = null;
77          setVisible(false);
78        }
79      });
80  
81      List<JButton> buttons = new ArrayList<JButton>();
82      buttons.add(cancel);
83      buttons.add(next);
84  
85      PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(0,
86          buttons, 1);
87  
88      JPanel radioButtonPanel = createRadioButtons();
89  
90      contentPanel.add(radioButtonPanel, BorderLayout.CENTER);
91      contentPanel.add(pspf.constructPanel(), BorderLayout.SOUTH);
92      this.getContentPane().add(contentPanel);
93    }
94  
95    /**
96     * Creates the radio buttons.
97     * 
98     * @return the panel containing the radio buttons
99     */
100   private JPanel createRadioButtons() {
101     JPanel radioButtonPanel = new JPanel();
102     BoxLayout layout = new BoxLayout(radioButtonPanel, BoxLayout.Y_AXIS);
103     radioButtonPanel.setLayout(layout);
104     ButtonGroup dbTypesRadioButtons = new ButtonGroup();
105     for (final DatabaseType type : DatabaseType.values()) {
106       JRadioButton radioButton = new JRadioButton(type.toString(),
107           type == dbType);
108       radioButton.addActionListener(new ActionListener() {
109         @Override
110         public void actionPerformed(ActionEvent e) {
111           dbType = type;
112         }
113       });
114       dbTypesRadioButtons.add(radioButton);
115       radioButtonPanel.add(radioButton);
116     }
117 
118     JPanel resultPanel = new JPanel(GUI.getStdBorderLayout());
119     resultPanel.add(radioButtonPanel, BorderLayout.CENTER);
120     resultPanel.add(new JPanel(), BorderLayout.NORTH);
121     resultPanel.add(new JPanel(), BorderLayout.WEST);
122     return resultPanel;
123   }
124 
125   public DatabaseType getDBType() {
126     return dbType;
127   }
128 
129 }