| [29] | 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 | * SaverCustomizer.java |
|---|
| 19 | * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.gui.beans; |
|---|
| 24 | |
|---|
| 25 | import java.awt.BorderLayout; |
|---|
| 26 | import java.awt.Dimension; |
|---|
| 27 | import java.awt.FlowLayout; |
|---|
| 28 | import java.awt.GridBagConstraints; |
|---|
| 29 | import java.awt.GridBagLayout; |
|---|
| 30 | import java.awt.event.ActionEvent; |
|---|
| 31 | import java.awt.event.ActionListener; |
|---|
| 32 | import java.beans.Customizer; |
|---|
| 33 | import java.beans.PropertyChangeEvent; |
|---|
| 34 | import java.beans.PropertyChangeListener; |
|---|
| 35 | import java.beans.PropertyChangeSupport; |
|---|
| 36 | import java.io.File; |
|---|
| 37 | import java.io.IOException; |
|---|
| 38 | |
|---|
| 39 | import javax.swing.BorderFactory; |
|---|
| 40 | import javax.swing.JButton; |
|---|
| 41 | import javax.swing.JCheckBox; |
|---|
| 42 | import javax.swing.JFileChooser; |
|---|
| 43 | import javax.swing.JFrame; |
|---|
| 44 | import javax.swing.JLabel; |
|---|
| 45 | import javax.swing.JPanel; |
|---|
| 46 | import javax.swing.JPasswordField; |
|---|
| 47 | import javax.swing.SwingConstants; |
|---|
| 48 | import javax.swing.filechooser.FileFilter; |
|---|
| 49 | |
|---|
| 50 | import weka.core.Environment; |
|---|
| 51 | import weka.core.EnvironmentHandler; |
|---|
| 52 | import weka.core.converters.DatabaseConverter; |
|---|
| 53 | import weka.core.converters.DatabaseSaver; |
|---|
| 54 | import weka.core.converters.FileSourcedConverter; |
|---|
| 55 | import weka.gui.GenericObjectEditor; |
|---|
| 56 | import weka.gui.PropertySheetPanel; |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * GUI Customizer for the saver bean |
|---|
| 60 | * |
|---|
| 61 | * @author <a href="mailto:mutter@cs.waikato.ac.nz">Stefan Mutter</a> |
|---|
| 62 | * @version $Revision: 5423 $ |
|---|
| 63 | */ |
|---|
| 64 | public class SaverCustomizer |
|---|
| 65 | extends JPanel |
|---|
| 66 | implements Customizer, CustomizerCloseRequester, EnvironmentHandler { |
|---|
| 67 | |
|---|
| 68 | /** for serialization */ |
|---|
| 69 | private static final long serialVersionUID = -4874208115942078471L; |
|---|
| 70 | |
|---|
| 71 | static { |
|---|
| 72 | GenericObjectEditor.registerEditors(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | private PropertyChangeSupport m_pcSupport = |
|---|
| 76 | new PropertyChangeSupport(this); |
|---|
| 77 | |
|---|
| 78 | private weka.gui.beans.Saver m_dsSaver; |
|---|
| 79 | |
|---|
| 80 | private PropertySheetPanel m_SaverEditor = |
|---|
| 81 | new PropertySheetPanel(); |
|---|
| 82 | |
|---|
| 83 | private JFileChooser m_fileChooser |
|---|
| 84 | = new JFileChooser(new File(System.getProperty("user.dir"))); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | private JFrame m_parentFrame; |
|---|
| 88 | |
|---|
| 89 | private JFrame m_fileChooserFrame; |
|---|
| 90 | |
|---|
| 91 | private EnvironmentField m_dbaseURLText; |
|---|
| 92 | |
|---|
| 93 | private EnvironmentField m_userNameText; |
|---|
| 94 | |
|---|
| 95 | private JPasswordField m_passwordText; |
|---|
| 96 | |
|---|
| 97 | private EnvironmentField m_tableText; |
|---|
| 98 | |
|---|
| 99 | private JCheckBox m_idBox; |
|---|
| 100 | |
|---|
| 101 | private JCheckBox m_tabBox; |
|---|
| 102 | |
|---|
| 103 | private EnvironmentField m_prefixText; |
|---|
| 104 | |
|---|
| 105 | private JCheckBox m_relativeFilePath; |
|---|
| 106 | |
|---|
| 107 | private JCheckBox m_relationNameForFilename; |
|---|
| 108 | |
|---|
| 109 | private Environment m_env = Environment.getSystemWide(); |
|---|
| 110 | |
|---|
| 111 | private EnvironmentField m_directoryText; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | /** Constructor */ |
|---|
| 115 | public SaverCustomizer() { |
|---|
| 116 | |
|---|
| 117 | try { |
|---|
| 118 | m_SaverEditor.addPropertyChangeListener( |
|---|
| 119 | new PropertyChangeListener() { |
|---|
| 120 | public void propertyChange(PropertyChangeEvent e) { |
|---|
| 121 | repaint(); |
|---|
| 122 | if (m_dsSaver != null) { |
|---|
| 123 | System.err.println("Property change!!"); |
|---|
| 124 | m_dsSaver.setSaver(m_dsSaver.getSaver()); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | }); |
|---|
| 128 | repaint(); |
|---|
| 129 | } catch (Exception ex) { |
|---|
| 130 | ex.printStackTrace(); |
|---|
| 131 | } |
|---|
| 132 | setLayout(new BorderLayout()); |
|---|
| 133 | m_fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); |
|---|
| 134 | m_fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
|---|
| 135 | m_fileChooser.setApproveButtonText("Select directory"); |
|---|
| 136 | m_fileChooser.addActionListener(new ActionListener() { |
|---|
| 137 | public void actionPerformed(ActionEvent e) { |
|---|
| 138 | if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) { |
|---|
| 139 | try { |
|---|
| 140 | File selectedFile = m_fileChooser.getSelectedFile(); |
|---|
| 141 | m_directoryText.setText(selectedFile.toString()); |
|---|
| 142 | |
|---|
| 143 | /* (m_dsSaver.getSaver()).setFilePrefix(m_prefixText.getText()); |
|---|
| 144 | (m_dsSaver.getSaver()).setDir(m_fileChooser.getSelectedFile().getPath()); |
|---|
| 145 | m_dsSaver. |
|---|
| 146 | setRelationNameForFilename(m_relationNameForFilename.isSelected()); */ |
|---|
| 147 | |
|---|
| 148 | } catch (Exception ex) { |
|---|
| 149 | ex.printStackTrace(); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | // closing |
|---|
| 153 | if (m_fileChooserFrame != null) { |
|---|
| 154 | m_fileChooserFrame.dispose(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | }); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | public void setParentFrame(JFrame parent) { |
|---|
| 161 | m_parentFrame = parent; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** Sets up dialog for saving instances in other data sinks then files |
|---|
| 165 | * To be extended. |
|---|
| 166 | */ |
|---|
| 167 | private void setUpOther() { |
|---|
| 168 | removeAll(); |
|---|
| 169 | add(m_SaverEditor, BorderLayout.CENTER); |
|---|
| 170 | validate(); |
|---|
| 171 | repaint(); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** Sets up the dialog for saving to a database*/ |
|---|
| 175 | private void setUpDatabase() { |
|---|
| 176 | |
|---|
| 177 | removeAll(); |
|---|
| 178 | JPanel db = new JPanel(); |
|---|
| 179 | GridBagLayout gbLayout = new GridBagLayout(); |
|---|
| 180 | db.setLayout(gbLayout); |
|---|
| 181 | |
|---|
| 182 | JLabel dbaseURLLab = new JLabel(" Database URL", SwingConstants.RIGHT); |
|---|
| 183 | dbaseURLLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 184 | GridBagConstraints gbConstraints = new GridBagConstraints(); |
|---|
| 185 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 186 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 187 | gbConstraints.gridy = 0; gbConstraints.gridx = 0; |
|---|
| 188 | gbLayout.setConstraints(dbaseURLLab, gbConstraints); |
|---|
| 189 | db.add(dbaseURLLab); |
|---|
| 190 | |
|---|
| 191 | m_dbaseURLText = new EnvironmentField(); |
|---|
| 192 | m_dbaseURLText.setEnvironment(m_env); |
|---|
| 193 | /* int width = m_dbaseURLText.getPreferredSize().width; |
|---|
| 194 | int height = m_dbaseURLText.getPreferredSize().height; |
|---|
| 195 | m_dbaseURLText.setMinimumSize(new Dimension(width * 2, height)); |
|---|
| 196 | m_dbaseURLText.setPreferredSize(new Dimension(width * 2, height)); */ |
|---|
| 197 | m_dbaseURLText.setText(((DatabaseConverter)m_dsSaver.getSaver()).getUrl()); |
|---|
| 198 | gbConstraints = new GridBagConstraints(); |
|---|
| 199 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 200 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 201 | gbConstraints.gridy = 0; gbConstraints.gridx = 1; |
|---|
| 202 | gbConstraints.weightx = 5; |
|---|
| 203 | gbLayout.setConstraints(m_dbaseURLText, gbConstraints); |
|---|
| 204 | db.add(m_dbaseURLText); |
|---|
| 205 | |
|---|
| 206 | JLabel userLab = new JLabel("Username", SwingConstants.RIGHT); |
|---|
| 207 | userLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 208 | gbConstraints = new GridBagConstraints(); |
|---|
| 209 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 210 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 211 | gbConstraints.gridy = 1; gbConstraints.gridx = 0; |
|---|
| 212 | gbLayout.setConstraints(userLab, gbConstraints); |
|---|
| 213 | db.add(userLab); |
|---|
| 214 | |
|---|
| 215 | m_userNameText = new EnvironmentField(); |
|---|
| 216 | m_userNameText.setEnvironment(m_env); |
|---|
| 217 | /* m_userNameText.setMinimumSize(new Dimension(width * 2, height)); |
|---|
| 218 | m_userNameText.setPreferredSize(new Dimension(width * 2, height)); */ |
|---|
| 219 | m_userNameText.setText(((DatabaseConverter)m_dsSaver.getSaver()).getUser()); |
|---|
| 220 | gbConstraints = new GridBagConstraints(); |
|---|
| 221 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 222 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 223 | gbConstraints.gridy = 1; gbConstraints.gridx = 1; |
|---|
| 224 | gbLayout.setConstraints(m_userNameText, gbConstraints); |
|---|
| 225 | db.add(m_userNameText); |
|---|
| 226 | |
|---|
| 227 | JLabel passwordLab = new JLabel("Password ", SwingConstants.RIGHT); |
|---|
| 228 | passwordLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 229 | gbConstraints = new GridBagConstraints(); |
|---|
| 230 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 231 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 232 | gbConstraints.gridy = 2; gbConstraints.gridx = 0; |
|---|
| 233 | gbLayout.setConstraints(passwordLab, gbConstraints); |
|---|
| 234 | db.add(passwordLab); |
|---|
| 235 | |
|---|
| 236 | m_passwordText = new JPasswordField(); |
|---|
| 237 | JPanel passwordHolder = new JPanel(); |
|---|
| 238 | passwordHolder.setLayout(new BorderLayout()); |
|---|
| 239 | passwordHolder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
|---|
| 240 | passwordHolder.add(m_passwordText, BorderLayout.CENTER); |
|---|
| 241 | /*passwordHolder.setMinimumSize(new Dimension(width * 2, height)); |
|---|
| 242 | passwordHolder.setPreferredSize(new Dimension(width * 2, height)); */ |
|---|
| 243 | gbConstraints = new GridBagConstraints(); |
|---|
| 244 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 245 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 246 | gbConstraints.gridy = 2; gbConstraints.gridx = 1; |
|---|
| 247 | gbLayout.setConstraints(passwordHolder, gbConstraints); |
|---|
| 248 | db.add(passwordHolder); |
|---|
| 249 | |
|---|
| 250 | JLabel tableLab = new JLabel("Table Name", SwingConstants.RIGHT); |
|---|
| 251 | tableLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 252 | gbConstraints = new GridBagConstraints(); |
|---|
| 253 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 254 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 255 | gbConstraints.gridy = 3; gbConstraints.gridx = 0; |
|---|
| 256 | gbLayout.setConstraints(tableLab, gbConstraints); |
|---|
| 257 | db.add(tableLab); |
|---|
| 258 | |
|---|
| 259 | m_tableText = new EnvironmentField(); |
|---|
| 260 | m_tableText.setEnvironment(m_env); |
|---|
| 261 | /* m_tableText.setMinimumSize(new Dimension(width * 2, height)); |
|---|
| 262 | m_tableText.setPreferredSize(new Dimension(width * 2, height)); */ |
|---|
| 263 | m_tableText.setEnabled(!((DatabaseSaver)m_dsSaver.getSaver()).getRelationForTableName()); |
|---|
| 264 | m_tableText.setText(((DatabaseSaver)m_dsSaver.getSaver()).getTableName()); |
|---|
| 265 | gbConstraints = new GridBagConstraints(); |
|---|
| 266 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 267 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 268 | gbConstraints.gridy = 3; gbConstraints.gridx = 1; |
|---|
| 269 | gbLayout.setConstraints(m_tableText, gbConstraints); |
|---|
| 270 | db.add(m_tableText); |
|---|
| 271 | |
|---|
| 272 | JLabel tabLab = new JLabel("Use relation name", SwingConstants.RIGHT); |
|---|
| 273 | tabLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 274 | gbConstraints = new GridBagConstraints(); |
|---|
| 275 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 276 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 277 | gbConstraints.gridy = 4; gbConstraints.gridx = 0; |
|---|
| 278 | gbLayout.setConstraints(tabLab, gbConstraints); |
|---|
| 279 | db.add(tabLab); |
|---|
| 280 | |
|---|
| 281 | m_tabBox = new JCheckBox(); |
|---|
| 282 | m_tabBox.setSelected(((DatabaseSaver)m_dsSaver.getSaver()).getRelationForTableName()); |
|---|
| 283 | m_tabBox.addActionListener(new ActionListener(){ |
|---|
| 284 | public void actionPerformed(ActionEvent e){ |
|---|
| 285 | m_tableText.setEnabled(!m_tabBox.isSelected()); |
|---|
| 286 | } |
|---|
| 287 | }); |
|---|
| 288 | gbConstraints = new GridBagConstraints(); |
|---|
| 289 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 290 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 291 | gbConstraints.gridy = 4; gbConstraints.gridx = 1; |
|---|
| 292 | gbLayout.setConstraints(m_tabBox, gbConstraints); |
|---|
| 293 | db.add(m_tabBox); |
|---|
| 294 | |
|---|
| 295 | JLabel idLab = new JLabel("Automatic primary key", SwingConstants.RIGHT); |
|---|
| 296 | idLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 297 | gbConstraints = new GridBagConstraints(); |
|---|
| 298 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 299 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 300 | gbConstraints.gridy = 5; gbConstraints.gridx = 0; |
|---|
| 301 | gbLayout.setConstraints(idLab, gbConstraints); |
|---|
| 302 | db.add(idLab); |
|---|
| 303 | |
|---|
| 304 | m_idBox = new JCheckBox(); |
|---|
| 305 | m_idBox.setSelected(((DatabaseSaver)m_dsSaver.getSaver()).getAutoKeyGeneration()); |
|---|
| 306 | gbConstraints = new GridBagConstraints(); |
|---|
| 307 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 308 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 309 | gbConstraints.gridy = 5; gbConstraints.gridx = 1; |
|---|
| 310 | gbLayout.setConstraints(m_idBox, gbConstraints); |
|---|
| 311 | db.add(m_idBox); |
|---|
| 312 | |
|---|
| 313 | JPanel buttonsP = new JPanel(); |
|---|
| 314 | buttonsP.setLayout(new FlowLayout()); |
|---|
| 315 | JButton ok,cancel; |
|---|
| 316 | buttonsP.add(ok = new JButton("OK")); |
|---|
| 317 | buttonsP.add(cancel=new JButton("Cancel")); |
|---|
| 318 | ok.addActionListener(new ActionListener(){ |
|---|
| 319 | public void actionPerformed(ActionEvent evt){ |
|---|
| 320 | ((DatabaseSaver)m_dsSaver.getSaver()).resetStructure(); |
|---|
| 321 | ((DatabaseConverter)m_dsSaver.getSaver()).setUrl(m_dbaseURLText.getText()); |
|---|
| 322 | ((DatabaseConverter)m_dsSaver.getSaver()).setUser(m_userNameText.getText()); |
|---|
| 323 | ((DatabaseConverter)m_dsSaver.getSaver()).setPassword(new String(m_passwordText.getPassword())); |
|---|
| 324 | if(!m_tabBox.isSelected()) { |
|---|
| 325 | ((DatabaseSaver)m_dsSaver.getSaver()).setTableName(m_tableText.getText()); |
|---|
| 326 | } |
|---|
| 327 | ((DatabaseSaver)m_dsSaver.getSaver()).setAutoKeyGeneration(m_idBox.isSelected()); |
|---|
| 328 | ((DatabaseSaver)m_dsSaver.getSaver()).setRelationForTableName(m_tabBox.isSelected()); |
|---|
| 329 | if (m_parentFrame != null) { |
|---|
| 330 | m_parentFrame.dispose(); |
|---|
| 331 | } |
|---|
| 332 | } |
|---|
| 333 | }); |
|---|
| 334 | cancel.addActionListener(new ActionListener(){ |
|---|
| 335 | public void actionPerformed(ActionEvent evt){ |
|---|
| 336 | if (m_parentFrame != null) { |
|---|
| 337 | m_parentFrame.dispose(); |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | }); |
|---|
| 341 | |
|---|
| 342 | JPanel holderP = new JPanel(); |
|---|
| 343 | holderP.setLayout(new BorderLayout()); |
|---|
| 344 | holderP.add(db, BorderLayout.NORTH); |
|---|
| 345 | holderP.add(buttonsP, BorderLayout.SOUTH); |
|---|
| 346 | |
|---|
| 347 | // db.add(buttonsP); |
|---|
| 348 | JPanel about = m_SaverEditor.getAboutPanel(); |
|---|
| 349 | if (about != null) { |
|---|
| 350 | add(about, BorderLayout.NORTH); |
|---|
| 351 | } |
|---|
| 352 | add(holderP,BorderLayout.SOUTH); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** Sets up dialog for saving instances in a file */ |
|---|
| 356 | public void setUpFile() { |
|---|
| 357 | removeAll(); |
|---|
| 358 | |
|---|
| 359 | m_fileChooser.setFileFilter(new FileFilter() { |
|---|
| 360 | public boolean accept(File f) { |
|---|
| 361 | return f.isDirectory(); |
|---|
| 362 | } |
|---|
| 363 | public String getDescription() { |
|---|
| 364 | return "Directory"; |
|---|
| 365 | } |
|---|
| 366 | }); |
|---|
| 367 | |
|---|
| 368 | m_fileChooser.setAcceptAllFileFilterUsed(false); |
|---|
| 369 | |
|---|
| 370 | try{ |
|---|
| 371 | if(!(((m_dsSaver.getSaver()).retrieveDir()).equals(""))) { |
|---|
| 372 | String dirStr = m_dsSaver.getSaver().retrieveDir(); |
|---|
| 373 | if (Environment.containsEnvVariables(dirStr)) { |
|---|
| 374 | try { |
|---|
| 375 | dirStr = m_env.substitute(dirStr); |
|---|
| 376 | } catch (Exception ex) { |
|---|
| 377 | // ignore |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | File tmp = new File(dirStr); |
|---|
| 381 | tmp = new File(tmp.getAbsolutePath()); |
|---|
| 382 | m_fileChooser.setCurrentDirectory(tmp); |
|---|
| 383 | } |
|---|
| 384 | } catch(Exception ex) { |
|---|
| 385 | System.out.println(ex); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | JPanel innerPanel = new JPanel(); |
|---|
| 389 | innerPanel.setLayout(new BorderLayout()); |
|---|
| 390 | |
|---|
| 391 | JPanel alignedP = new JPanel(); |
|---|
| 392 | GridBagLayout gbLayout = new GridBagLayout(); |
|---|
| 393 | alignedP.setLayout(gbLayout); |
|---|
| 394 | |
|---|
| 395 | JLabel prefixLab = new JLabel("Prefix for file name", SwingConstants.RIGHT); |
|---|
| 396 | prefixLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 397 | GridBagConstraints gbConstraints = new GridBagConstraints(); |
|---|
| 398 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 399 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 400 | gbConstraints.gridy = 0; gbConstraints.gridx = 0; |
|---|
| 401 | gbLayout.setConstraints(prefixLab, gbConstraints); |
|---|
| 402 | alignedP.add(prefixLab); |
|---|
| 403 | |
|---|
| 404 | m_prefixText = new EnvironmentField(); |
|---|
| 405 | m_prefixText.setEnvironment(m_env); |
|---|
| 406 | m_prefixText.setToolTipText("Prefix for file name " |
|---|
| 407 | + "(or filename itself if relation name is not used)"); |
|---|
| 408 | /* int width = m_prefixText.getPreferredSize().width; |
|---|
| 409 | int height = m_prefixText.getPreferredSize().height; |
|---|
| 410 | m_prefixText.setMinimumSize(new Dimension(width * 2, height)); |
|---|
| 411 | m_prefixText.setPreferredSize(new Dimension(width * 2, height)); */ |
|---|
| 412 | gbConstraints = new GridBagConstraints(); |
|---|
| 413 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 414 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 415 | gbConstraints.gridy = 0; gbConstraints.gridx = 1; |
|---|
| 416 | gbLayout.setConstraints(m_prefixText, gbConstraints); |
|---|
| 417 | alignedP.add(m_prefixText); |
|---|
| 418 | |
|---|
| 419 | try{ |
|---|
| 420 | // m_prefixText = new JTextField(m_dsSaver.getSaver().filePrefix(),25); |
|---|
| 421 | |
|---|
| 422 | m_prefixText.setText(m_dsSaver.getSaver().filePrefix()); |
|---|
| 423 | |
|---|
| 424 | /* final JLabel prefixLab = |
|---|
| 425 | new JLabel(" Prefix for file name:", SwingConstants.LEFT); */ |
|---|
| 426 | |
|---|
| 427 | JLabel relationLab = new JLabel("Relation name for filename", SwingConstants.RIGHT); |
|---|
| 428 | relationLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 429 | gbConstraints = new GridBagConstraints(); |
|---|
| 430 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 431 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 432 | gbConstraints.gridy = 1; gbConstraints.gridx = 0; |
|---|
| 433 | gbLayout.setConstraints(relationLab, gbConstraints); |
|---|
| 434 | alignedP.add(relationLab); |
|---|
| 435 | |
|---|
| 436 | m_relationNameForFilename = new JCheckBox(); |
|---|
| 437 | m_relationNameForFilename.setSelected(m_dsSaver.getRelationNameForFilename()); |
|---|
| 438 | m_relationNameForFilename.addActionListener(new ActionListener() { |
|---|
| 439 | public void actionPerformed(ActionEvent e) { |
|---|
| 440 | if (m_relationNameForFilename.isSelected()) { |
|---|
| 441 | m_prefixText.setLabel("Prefix for file name"); |
|---|
| 442 | m_fileChooser.setApproveButtonText("Select directory and prefix"); |
|---|
| 443 | } else { |
|---|
| 444 | m_prefixText.setLabel("File name"); |
|---|
| 445 | m_fileChooser.setApproveButtonText("Select directory and filename"); |
|---|
| 446 | } |
|---|
| 447 | } |
|---|
| 448 | }); |
|---|
| 449 | |
|---|
| 450 | gbConstraints = new GridBagConstraints(); |
|---|
| 451 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 452 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 453 | gbConstraints.gridy = 1; gbConstraints.gridx = 1; |
|---|
| 454 | gbConstraints.weightx = 5; |
|---|
| 455 | gbLayout.setConstraints(m_relationNameForFilename, gbConstraints); |
|---|
| 456 | alignedP.add(m_relationNameForFilename); |
|---|
| 457 | } catch(Exception ex){ |
|---|
| 458 | } |
|---|
| 459 | //innerPanel.add(m_SaverEditor, BorderLayout.SOUTH); |
|---|
| 460 | JPanel about = m_SaverEditor.getAboutPanel(); |
|---|
| 461 | if (about != null) { |
|---|
| 462 | innerPanel.add(about, BorderLayout.NORTH); |
|---|
| 463 | } |
|---|
| 464 | add(innerPanel, BorderLayout.NORTH); |
|---|
| 465 | // add(m_fileChooser, BorderLayout.CENTER); |
|---|
| 466 | |
|---|
| 467 | JLabel directoryLab = new JLabel("Directory", SwingConstants.RIGHT); |
|---|
| 468 | directoryLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 469 | gbConstraints = new GridBagConstraints(); |
|---|
| 470 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 471 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 472 | gbConstraints.gridy = 2; gbConstraints.gridx = 0; |
|---|
| 473 | gbLayout.setConstraints(directoryLab, gbConstraints); |
|---|
| 474 | alignedP.add(directoryLab); |
|---|
| 475 | |
|---|
| 476 | m_directoryText = new EnvironmentField(); |
|---|
| 477 | // m_directoryText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
|---|
| 478 | m_directoryText.setEnvironment(m_env); |
|---|
| 479 | /* width = m_directoryText.getPreferredSize().width; |
|---|
| 480 | height = m_directoryText.getPreferredSize().height; |
|---|
| 481 | m_directoryText.setMinimumSize(new Dimension(width * 2, height)); |
|---|
| 482 | m_directoryText.setPreferredSize(new Dimension(width * 2, height)); */ |
|---|
| 483 | |
|---|
| 484 | try { |
|---|
| 485 | m_directoryText.setText(m_dsSaver.getSaver().retrieveDir()); |
|---|
| 486 | } catch (IOException ex) { |
|---|
| 487 | // ignore |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | JButton browseBut = new JButton("Browse..."); |
|---|
| 491 | browseBut.addActionListener(new ActionListener() { |
|---|
| 492 | public void actionPerformed(ActionEvent e) { |
|---|
| 493 | try { |
|---|
| 494 | final JFrame jf = new JFrame("Choose directory"); |
|---|
| 495 | jf.getContentPane().setLayout(new BorderLayout()); |
|---|
| 496 | jf.getContentPane().add(m_fileChooser, BorderLayout.CENTER); |
|---|
| 497 | jf.pack(); |
|---|
| 498 | jf.setVisible(true); |
|---|
| 499 | m_fileChooserFrame = jf; |
|---|
| 500 | } catch (Exception ex) { |
|---|
| 501 | ex.printStackTrace(); |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | }); |
|---|
| 505 | |
|---|
| 506 | JPanel efHolder = new JPanel(); |
|---|
| 507 | efHolder.setLayout(new BorderLayout()); |
|---|
| 508 | JPanel bP = new JPanel(); bP.setLayout(new BorderLayout()); |
|---|
| 509 | bP.setBorder(BorderFactory.createEmptyBorder(5,0,5,5)); |
|---|
| 510 | bP.add(browseBut, BorderLayout.CENTER); |
|---|
| 511 | efHolder.add(m_directoryText, BorderLayout.CENTER); |
|---|
| 512 | efHolder.add(bP, BorderLayout.EAST); |
|---|
| 513 | //efHolder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
|---|
| 514 | gbConstraints = new GridBagConstraints(); |
|---|
| 515 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 516 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 517 | gbConstraints.gridy = 2; gbConstraints.gridx = 1; |
|---|
| 518 | gbLayout.setConstraints(efHolder, gbConstraints); |
|---|
| 519 | alignedP.add(efHolder); |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | JLabel relativeLab = new JLabel("Use relative file paths", SwingConstants.RIGHT); |
|---|
| 523 | relativeLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
|---|
| 524 | gbConstraints = new GridBagConstraints(); |
|---|
| 525 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 526 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 527 | gbConstraints.gridy = 3; gbConstraints.gridx = 0; |
|---|
| 528 | gbLayout.setConstraints(relativeLab, gbConstraints); |
|---|
| 529 | alignedP.add(relativeLab); |
|---|
| 530 | |
|---|
| 531 | m_relativeFilePath = new JCheckBox(); |
|---|
| 532 | m_relativeFilePath. |
|---|
| 533 | setSelected(((FileSourcedConverter)m_dsSaver.getSaver()).getUseRelativePath()); |
|---|
| 534 | |
|---|
| 535 | m_relativeFilePath.addActionListener(new ActionListener() { |
|---|
| 536 | public void actionPerformed(ActionEvent e) { |
|---|
| 537 | ((FileSourcedConverter)m_dsSaver.getSaver()). |
|---|
| 538 | setUseRelativePath(m_relativeFilePath.isSelected()); |
|---|
| 539 | } |
|---|
| 540 | }); |
|---|
| 541 | gbConstraints = new GridBagConstraints(); |
|---|
| 542 | gbConstraints.anchor = GridBagConstraints.EAST; |
|---|
| 543 | gbConstraints.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 544 | gbConstraints.gridy = 3; gbConstraints.gridx = 1; |
|---|
| 545 | gbLayout.setConstraints(m_relativeFilePath, gbConstraints); |
|---|
| 546 | alignedP.add(m_relativeFilePath); |
|---|
| 547 | |
|---|
| 548 | JButton OKBut = new JButton("OK"); |
|---|
| 549 | OKBut.addActionListener(new ActionListener() { |
|---|
| 550 | public void actionPerformed(ActionEvent e) { |
|---|
| 551 | try { |
|---|
| 552 | (m_dsSaver.getSaver()).setFilePrefix(m_prefixText.getText()); |
|---|
| 553 | (m_dsSaver.getSaver()).setDir(m_directoryText.getText()); |
|---|
| 554 | m_dsSaver. |
|---|
| 555 | setRelationNameForFilename(m_relationNameForFilename.isSelected()); |
|---|
| 556 | } catch (Exception ex) { |
|---|
| 557 | ex.printStackTrace(); |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | m_parentFrame.dispose(); |
|---|
| 561 | } |
|---|
| 562 | }); |
|---|
| 563 | |
|---|
| 564 | JButton CancelBut = new JButton("Cancel"); |
|---|
| 565 | CancelBut.addActionListener(new ActionListener() { |
|---|
| 566 | public void actionPerformed(ActionEvent e) { |
|---|
| 567 | m_parentFrame.dispose(); |
|---|
| 568 | } |
|---|
| 569 | }); |
|---|
| 570 | |
|---|
| 571 | JPanel butHolder = new JPanel(); |
|---|
| 572 | butHolder.setLayout(new FlowLayout()); |
|---|
| 573 | butHolder.add(OKBut); |
|---|
| 574 | butHolder.add(CancelBut); |
|---|
| 575 | JPanel holder2 = new JPanel(); |
|---|
| 576 | holder2.setLayout(new BorderLayout()); |
|---|
| 577 | holder2.add(alignedP, BorderLayout.NORTH); |
|---|
| 578 | holder2.add(butHolder, BorderLayout.SOUTH); |
|---|
| 579 | |
|---|
| 580 | add(holder2, BorderLayout.SOUTH); |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | /** |
|---|
| 584 | * Set the saver to be customized |
|---|
| 585 | * |
|---|
| 586 | * @param object a weka.gui.beans.Saver |
|---|
| 587 | */ |
|---|
| 588 | public void setObject(Object object) { |
|---|
| 589 | m_dsSaver = (weka.gui.beans.Saver)object; |
|---|
| 590 | m_SaverEditor.setTarget(m_dsSaver.getSaver()); |
|---|
| 591 | if(m_dsSaver.getSaver() instanceof DatabaseConverter){ |
|---|
| 592 | setUpDatabase(); |
|---|
| 593 | } |
|---|
| 594 | else{ |
|---|
| 595 | if (m_dsSaver.getSaver() instanceof FileSourcedConverter) { |
|---|
| 596 | setUpFile(); |
|---|
| 597 | } else { |
|---|
| 598 | setUpOther(); |
|---|
| 599 | } |
|---|
| 600 | } |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | /** |
|---|
| 604 | * Add a property change listener |
|---|
| 605 | * |
|---|
| 606 | * @param pcl a <code>PropertyChangeListener</code> value |
|---|
| 607 | */ |
|---|
| 608 | public void addPropertyChangeListener(PropertyChangeListener pcl) { |
|---|
| 609 | m_pcSupport.addPropertyChangeListener(pcl); |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | /** |
|---|
| 613 | * Remove a property change listener |
|---|
| 614 | * |
|---|
| 615 | * @param pcl a <code>PropertyChangeListener</code> value |
|---|
| 616 | */ |
|---|
| 617 | public void removePropertyChangeListener(PropertyChangeListener pcl) { |
|---|
| 618 | m_pcSupport.removePropertyChangeListener(pcl); |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | /* (non-Javadoc) |
|---|
| 622 | * @see weka.core.EnvironmentHandler#setEnvironment(weka.core.Environment) |
|---|
| 623 | */ |
|---|
| 624 | public void setEnvironment(Environment env) { |
|---|
| 625 | m_env = env; |
|---|
| 626 | } |
|---|
| 627 | } |
|---|