[4] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * DatabaseConnectionDialog.java |
---|
| 19 | * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui; |
---|
| 24 | |
---|
| 25 | import java.awt.BorderLayout; |
---|
| 26 | import java.awt.FlowLayout; |
---|
| 27 | import java.awt.Font; |
---|
| 28 | import java.awt.Frame; |
---|
| 29 | import java.awt.GridLayout; |
---|
| 30 | import java.awt.event.ActionEvent; |
---|
| 31 | import java.awt.event.ActionListener; |
---|
| 32 | |
---|
| 33 | import javax.swing.JButton; |
---|
| 34 | import javax.swing.JCheckBox; |
---|
| 35 | import javax.swing.JDialog; |
---|
| 36 | import javax.swing.JLabel; |
---|
| 37 | import javax.swing.JOptionPane; |
---|
| 38 | import javax.swing.JPanel; |
---|
| 39 | import javax.swing.JPasswordField; |
---|
| 40 | import javax.swing.JTextField; |
---|
| 41 | import javax.swing.SwingConstants; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * A dialog to enter URL, username and password for a database connection. |
---|
| 45 | * |
---|
| 46 | * @author Dale Fletcher (dale@cs.waikato.ac.nz) |
---|
| 47 | * @version $Revision: 1.8 $ |
---|
| 48 | */ |
---|
| 49 | public class DatabaseConnectionDialog |
---|
| 50 | extends JDialog { |
---|
| 51 | |
---|
| 52 | /** for serialization */ |
---|
| 53 | private static final long serialVersionUID = -1081946748666245054L; |
---|
| 54 | |
---|
| 55 | /* URL field and label */ |
---|
| 56 | protected JTextField m_DbaseURLText; |
---|
| 57 | protected JLabel m_DbaseURLLab; |
---|
| 58 | |
---|
| 59 | /* Username field and label */ |
---|
| 60 | protected JTextField m_UserNameText; |
---|
| 61 | protected JLabel m_UserNameLab; |
---|
| 62 | |
---|
| 63 | /* Password field and label */ |
---|
| 64 | protected JPasswordField m_PasswordText; |
---|
| 65 | protected JLabel m_PasswordLab; |
---|
| 66 | |
---|
| 67 | /* Debug checkbox and label */ |
---|
| 68 | protected JCheckBox m_DebugCheckBox; |
---|
| 69 | protected JLabel m_DebugLab; |
---|
| 70 | |
---|
| 71 | /* whether dialog was cancel'ed or OK'ed */ |
---|
| 72 | protected int m_returnValue; |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Create database connection dialog. |
---|
| 76 | * |
---|
| 77 | * @param parentFrame the parent frame of the dialog |
---|
| 78 | */ |
---|
| 79 | public DatabaseConnectionDialog(Frame parentFrame) { |
---|
| 80 | this(parentFrame, "", ""); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Create database connection dialog. |
---|
| 85 | * |
---|
| 86 | * @param parentFrame the parent frame of the dialog |
---|
| 87 | * @param url initial text for URL field |
---|
| 88 | * @param uname initial text for username field |
---|
| 89 | */ |
---|
| 90 | public DatabaseConnectionDialog(Frame parentFrame, String url, String uname) { |
---|
| 91 | this(parentFrame, url, uname, true); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Create database connection dialog. |
---|
| 96 | * |
---|
| 97 | * @param parentFrame the parent frame of the dialog |
---|
| 98 | * @param url initial text for URL field |
---|
| 99 | * @param uname initial text for username field |
---|
| 100 | * @param debug whether to display the debug checkbox |
---|
| 101 | */ |
---|
| 102 | public DatabaseConnectionDialog(Frame parentFrame, String url, String uname, boolean debug) { |
---|
| 103 | super(parentFrame,"Database Connection Parameters", true); |
---|
| 104 | DbConnectionDialog(url, uname, debug); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Returns URL from dialog |
---|
| 109 | * |
---|
| 110 | * @return URL string |
---|
| 111 | */ |
---|
| 112 | public String getURL(){ |
---|
| 113 | return(m_DbaseURLText.getText()); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Returns Username from dialog |
---|
| 118 | * |
---|
| 119 | * @return Username string |
---|
| 120 | */ |
---|
| 121 | public String getUsername(){ |
---|
| 122 | return(m_UserNameText.getText()); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Returns password from dialog |
---|
| 127 | * |
---|
| 128 | * @return Password string |
---|
| 129 | */ |
---|
| 130 | public String getPassword(){ |
---|
| 131 | return(new String(m_PasswordText.getPassword())); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * Returns the debug flag |
---|
| 136 | * |
---|
| 137 | * @return true if debugging should be enabled |
---|
| 138 | */ |
---|
| 139 | public boolean getDebug() { |
---|
| 140 | return m_DebugCheckBox.isSelected(); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Returns which of OK or cancel was clicked from dialog |
---|
| 145 | * |
---|
| 146 | * @return either JOptionPane.OK_OPTION or JOptionPane.CLOSED_OPTION |
---|
| 147 | */ |
---|
| 148 | public int getReturnValue(){ |
---|
| 149 | return(m_returnValue); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Display the database connection dialog |
---|
| 154 | * |
---|
| 155 | * @param url initial text for URL field |
---|
| 156 | * @param uname initial text for username field |
---|
| 157 | */ |
---|
| 158 | public void DbConnectionDialog(String url, String uname) { |
---|
| 159 | DbConnectionDialog(url, uname, true); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Display the database connection dialog |
---|
| 164 | * |
---|
| 165 | * @param url initial text for URL field |
---|
| 166 | * @param uname initial text for username field |
---|
| 167 | * @param debug whether to display the debug checkbox |
---|
| 168 | */ |
---|
| 169 | public void DbConnectionDialog(String url, String uname, boolean debug) { |
---|
| 170 | |
---|
| 171 | JPanel DbP = new JPanel(); |
---|
| 172 | if (debug) |
---|
| 173 | DbP.setLayout(new GridLayout(5, 1)); |
---|
| 174 | else |
---|
| 175 | DbP.setLayout(new GridLayout(4, 1)); |
---|
| 176 | |
---|
| 177 | m_DbaseURLText = new JTextField(url,50); |
---|
| 178 | m_DbaseURLLab = new JLabel(" Database URL", SwingConstants.LEFT); |
---|
| 179 | m_DbaseURLLab.setFont(new Font("Monospaced", Font.PLAIN, 12)); |
---|
| 180 | m_DbaseURLLab.setDisplayedMnemonic('D'); |
---|
| 181 | m_DbaseURLLab.setLabelFor(m_DbaseURLText); |
---|
| 182 | |
---|
| 183 | m_UserNameText = new JTextField(uname,25); |
---|
| 184 | m_UserNameLab = new JLabel(" Username ", SwingConstants.LEFT); |
---|
| 185 | m_UserNameLab.setFont(new Font("Monospaced", Font.PLAIN, 12)); |
---|
| 186 | m_UserNameLab.setDisplayedMnemonic('U'); |
---|
| 187 | m_UserNameLab.setLabelFor(m_UserNameText); |
---|
| 188 | |
---|
| 189 | m_PasswordText = new JPasswordField(25); |
---|
| 190 | m_PasswordLab = new JLabel(" Password ", SwingConstants.LEFT); |
---|
| 191 | m_PasswordLab.setFont(new Font("Monospaced", Font.PLAIN, 12)); |
---|
| 192 | m_PasswordLab.setDisplayedMnemonic('P'); |
---|
| 193 | m_PasswordLab.setLabelFor(m_PasswordText); |
---|
| 194 | |
---|
| 195 | m_DebugCheckBox = new JCheckBox(); |
---|
| 196 | m_DebugLab = new JLabel(" Debug ", SwingConstants.LEFT); |
---|
| 197 | m_DebugLab.setFont(new Font("Monospaced", Font.PLAIN, 12)); |
---|
| 198 | m_DebugLab.setDisplayedMnemonic('P'); |
---|
| 199 | m_DebugLab.setLabelFor(m_DebugCheckBox); |
---|
| 200 | |
---|
| 201 | JPanel urlP = new JPanel(); |
---|
| 202 | urlP.setLayout(new FlowLayout(FlowLayout.LEFT)); |
---|
| 203 | urlP.add(m_DbaseURLLab); |
---|
| 204 | urlP.add(m_DbaseURLText); |
---|
| 205 | DbP.add(urlP); |
---|
| 206 | |
---|
| 207 | JPanel usernameP = new JPanel(); |
---|
| 208 | usernameP.setLayout(new FlowLayout(FlowLayout.LEFT)); |
---|
| 209 | usernameP.add(m_UserNameLab); |
---|
| 210 | usernameP.add(m_UserNameText); |
---|
| 211 | DbP.add(usernameP); |
---|
| 212 | |
---|
| 213 | JPanel passwordP = new JPanel(); |
---|
| 214 | passwordP.setLayout(new FlowLayout(FlowLayout.LEFT)); |
---|
| 215 | passwordP.add(m_PasswordLab); |
---|
| 216 | passwordP.add(m_PasswordText); |
---|
| 217 | DbP.add(passwordP); |
---|
| 218 | |
---|
| 219 | if (debug) { |
---|
| 220 | JPanel debugP = new JPanel(); |
---|
| 221 | debugP.setLayout(new FlowLayout(FlowLayout.LEFT)); |
---|
| 222 | debugP.add(m_DebugLab); |
---|
| 223 | debugP.add(m_DebugCheckBox); |
---|
| 224 | DbP.add(debugP); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | JPanel buttonsP = new JPanel(); |
---|
| 228 | buttonsP.setLayout(new FlowLayout()); |
---|
| 229 | JButton ok,cancel; |
---|
| 230 | buttonsP.add(ok = new JButton("OK")); |
---|
| 231 | buttonsP.add(cancel=new JButton("Cancel")); |
---|
| 232 | ok.setMnemonic('O'); |
---|
| 233 | ok.addActionListener(new ActionListener(){ |
---|
| 234 | public void actionPerformed(ActionEvent evt){ |
---|
| 235 | m_returnValue=JOptionPane.OK_OPTION; |
---|
| 236 | DatabaseConnectionDialog.this.dispose(); |
---|
| 237 | } |
---|
| 238 | }); |
---|
| 239 | cancel.setMnemonic('C'); |
---|
| 240 | cancel.addActionListener(new ActionListener(){ |
---|
| 241 | public void actionPerformed(ActionEvent evt){ |
---|
| 242 | m_returnValue=JOptionPane.CLOSED_OPTION; |
---|
| 243 | DatabaseConnectionDialog.this.dispose(); |
---|
| 244 | } |
---|
| 245 | }); |
---|
| 246 | |
---|
| 247 | // Listen for window close events |
---|
| 248 | addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 249 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 250 | System.err.println("Cancelled!!"); |
---|
| 251 | m_returnValue = JOptionPane.CLOSED_OPTION; |
---|
| 252 | } |
---|
| 253 | }); |
---|
| 254 | |
---|
| 255 | DbP.add(buttonsP); |
---|
| 256 | this.getContentPane().add(DbP,BorderLayout.CENTER); |
---|
| 257 | this.pack(); |
---|
| 258 | getRootPane().setDefaultButton(ok); |
---|
| 259 | setResizable(false); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * for testing only |
---|
| 264 | */ |
---|
| 265 | public static void main(String[] args){ |
---|
| 266 | DatabaseConnectionDialog dbd= new DatabaseConnectionDialog(null,"URL","username"); |
---|
| 267 | dbd.setVisible(true); |
---|
| 268 | System.out.println(dbd.getReturnValue()+":"+dbd.getUsername()+":"+dbd.getPassword()+":"+dbd.getURL()); |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | |
---|