source: branches/MetisMQI/src/main/java/weka/gui/beans/LoaderCustomizer.java @ 38

Last change on this file since 38 was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 19.6 KB
Line 
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 *    LoaderCustomizer.java
19 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import weka.core.Environment;
26import weka.core.EnvironmentHandler;
27import weka.core.converters.DatabaseConverter;
28import weka.core.converters.DatabaseLoader;
29import weka.core.converters.FileSourcedConverter;
30import weka.gui.ExtensionFileFilter;
31import weka.gui.GenericObjectEditor;
32import weka.gui.PropertySheetPanel;
33
34import java.awt.BorderLayout;
35import java.awt.Dimension;
36import java.awt.FlowLayout;
37import java.awt.Font;
38import java.awt.GridBagConstraints;
39import java.awt.GridBagLayout;
40import java.awt.GridLayout;
41import java.awt.event.ActionEvent;
42import java.awt.event.ActionListener;
43import java.beans.Customizer;
44import java.beans.PropertyChangeEvent;
45import java.beans.PropertyChangeListener;
46import java.beans.PropertyChangeSupport;
47import java.io.File;
48import java.util.ArrayList;
49
50import javax.swing.BorderFactory;
51import javax.swing.JButton;
52import javax.swing.JFileChooser;
53import javax.swing.JFrame;
54import javax.swing.JLabel;
55import javax.swing.JPanel;
56import javax.swing.JPasswordField;
57import javax.swing.JTextField;
58import javax.swing.SwingConstants;
59import javax.swing.JCheckBox;
60
61/**
62 * GUI Customizer for the loader bean
63 *
64 * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
65 * @version $Revision: 5406 $
66 */
67public class LoaderCustomizer
68  extends JPanel
69  implements Customizer, CustomizerCloseRequester, EnvironmentHandler {
70
71  /** for serialization */
72  private static final long serialVersionUID = 6990446313118930298L;
73
74  static {
75     GenericObjectEditor.registerEditors();
76  }
77
78  private PropertyChangeSupport m_pcSupport = 
79    new PropertyChangeSupport(this);
80
81  private weka.gui.beans.Loader m_dsLoader;
82
83  private PropertySheetPanel m_LoaderEditor = 
84    new PropertySheetPanel();
85
86  private JFileChooser m_fileChooser
87    = new JFileChooser(new File(System.getProperty("user.dir")));
88  /*  private JDialog m_chooserDialog =
89    new JDialog((JFrame)getTopLevelAncestor(),
90    true); */
91
92  private JFrame m_parentFrame;
93  private JFrame m_fileChooserFrame;
94 
95  private EnvironmentField m_dbaseURLText;
96 
97  private EnvironmentField m_userNameText;
98 
99  private EnvironmentField m_queryText;
100   
101  private EnvironmentField m_keyText;
102 
103  private JPasswordField m_passwordText;
104
105  private JCheckBox m_relativeFilePath;
106 
107  private EnvironmentField m_fileText;
108 
109  private Environment m_env = Environment.getSystemWide();
110
111  public LoaderCustomizer() {
112    /*    m_fileEditor.addPropertyChangeListener(new PropertyChangeListener() {
113        public void propertyChange(PropertyChangeEvent e) {
114          if (m_dsLoader != null) {
115            m_dsLoader.setDataSetFile((File)m_fileEditor.getValue());
116          }
117        }
118        }); */
119
120    try {
121      /*      m_LoaderEditor.setClassType(weka.core.converters.Loader.class);
122              m_LoaderEditor.setValue(new weka.core.converters.ArffLoader()); */
123      m_LoaderEditor.addPropertyChangeListener(
124          new PropertyChangeListener() {
125              public void propertyChange(PropertyChangeEvent e) {
126                repaint();
127                if (m_dsLoader != null) {
128                  System.err.println("Property change!!");
129                  m_dsLoader.setLoader(m_dsLoader.getLoader());
130                }
131              }
132            });
133      repaint();
134    } catch (Exception ex) {
135      ex.printStackTrace();
136    }
137
138    setLayout(new BorderLayout());
139    //    add(m_fileEditor.getCustomEditor(), BorderLayout.CENTER);
140    //    add(m_LoaderEditor, BorderLayout.CENTER);
141    m_fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
142    m_fileChooser.addActionListener(new ActionListener() {
143        public void actionPerformed(ActionEvent e) {
144          if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
145            try {
146              File selectedFile = m_fileChooser.getSelectedFile();
147/*              EnvironmentField ef = m_environmentFields.get(0);
148              ef.setText(selectedFile.toString()); */
149              m_fileText.setText(selectedFile.toString());
150             
151/*            ((FileSourcedConverter)m_dsLoader.getLoader()).
152                setFile(selectedFile);
153              // tell the loader that a new file has been selected so
154              // that it can attempt to load the header
155              //m_dsLoader.setLoader(m_dsLoader.getLoader());
156              m_dsLoader.newFileSelected(); */
157            } catch (Exception ex) {
158              ex.printStackTrace();
159            }
160          }
161          // closing
162          if (m_fileChooserFrame != null) {
163            // m_parentFrame.dispose();
164            m_fileChooserFrame.dispose();
165          }
166        }
167      });   
168  }
169
170  public void setParentFrame(JFrame parent) {
171    m_parentFrame = parent;
172  }
173 
174  private void setUpOther() {
175    removeAll();
176    add(m_LoaderEditor, BorderLayout.CENTER);
177    validate();
178    repaint();
179  }
180 
181 
182  /** Sets up a customizer window for a Database Connection*/
183  private void setUpDatabase() {
184
185    removeAll();
186
187    JPanel db = new JPanel();
188    GridBagLayout gbLayout = new GridBagLayout();
189    //db.setLayout(new GridLayout(6, 1));
190    db.setLayout(gbLayout);
191
192    JLabel urlLab = new JLabel("Database URL", SwingConstants.RIGHT);
193    urlLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
194    GridBagConstraints gbConstraints = new GridBagConstraints();
195    gbConstraints.anchor = GridBagConstraints.EAST;
196    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
197    gbConstraints.gridy = 0; gbConstraints.gridx = 0;
198    gbLayout.setConstraints(urlLab, gbConstraints);
199    db.add(urlLab);
200
201    m_dbaseURLText = new EnvironmentField();
202    m_dbaseURLText.setEnvironment(m_env);
203/*    int width = m_dbaseURLText.getPreferredSize().width;
204    int height = m_dbaseURLText.getPreferredSize().height;
205    m_dbaseURLText.setMinimumSize(new Dimension(width * 2, height));
206    m_dbaseURLText.setPreferredSize(new Dimension(width * 2, height)); */
207    m_dbaseURLText.setText(((DatabaseConverter)m_dsLoader.getLoader()).getUrl());
208    gbConstraints = new GridBagConstraints();
209    gbConstraints.anchor = GridBagConstraints.EAST;
210    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
211    gbConstraints.gridy = 0; gbConstraints.gridx = 1;
212    gbConstraints.weightx = 5;
213    gbLayout.setConstraints(m_dbaseURLText, gbConstraints);
214    db.add(m_dbaseURLText);
215   
216    JLabel userLab = new JLabel("Username", SwingConstants.RIGHT);
217    userLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
218    gbConstraints = new GridBagConstraints();
219    gbConstraints.anchor = GridBagConstraints.EAST;
220    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
221    gbConstraints.gridy = 1; gbConstraints.gridx = 0;
222    gbLayout.setConstraints(userLab, gbConstraints);
223    db.add(userLab);
224   
225    m_userNameText = new EnvironmentField();
226    m_userNameText.setEnvironment(m_env);
227/*    m_userNameText.setMinimumSize(new Dimension(width * 2, height));
228    m_userNameText.setPreferredSize(new Dimension(width * 2, height)); */
229    m_userNameText.setText(((DatabaseConverter)m_dsLoader.getLoader()).getUser()); 
230    gbConstraints = new GridBagConstraints();
231    gbConstraints.anchor = GridBagConstraints.EAST;
232    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
233    gbConstraints.gridy = 1; gbConstraints.gridx = 1;
234    gbLayout.setConstraints(m_userNameText, gbConstraints);
235    db.add(m_userNameText);
236
237    JLabel passwordLab = new JLabel("Password ", SwingConstants.RIGHT);
238    passwordLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
239    gbConstraints = new GridBagConstraints();
240    gbConstraints.anchor = GridBagConstraints.EAST;
241    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
242    gbConstraints.gridy = 2; gbConstraints.gridx = 0;
243    gbLayout.setConstraints(passwordLab, gbConstraints);
244    db.add(passwordLab);
245   
246    m_passwordText = new JPasswordField();     
247    JPanel passwordHolder = new JPanel();
248    passwordHolder.setLayout(new BorderLayout());
249    passwordHolder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
250//    passwordHolder.add(passwordLab, BorderLayout.WEST);
251    passwordHolder.add(m_passwordText, BorderLayout.CENTER);
252/*    passwordHolder.setMinimumSize(new Dimension(width * 2, height));
253    passwordHolder.setPreferredSize(new Dimension(width * 2, height)); */
254    gbConstraints = new GridBagConstraints();
255    gbConstraints.anchor = GridBagConstraints.EAST;
256    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
257    gbConstraints.gridy = 2; gbConstraints.gridx = 1;
258    gbLayout.setConstraints(passwordHolder, gbConstraints);
259    db.add(passwordHolder);
260
261    JLabel queryLab = new JLabel("Query", SwingConstants.RIGHT);
262    queryLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
263    gbConstraints = new GridBagConstraints();
264    gbConstraints.anchor = GridBagConstraints.EAST;
265    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
266    gbConstraints.gridy = 3; gbConstraints.gridx = 0;
267    gbLayout.setConstraints(queryLab, gbConstraints);
268    db.add(queryLab);
269   
270    m_queryText = new EnvironmentField();
271    m_queryText.setEnvironment(m_env);
272/*    m_queryText.setMinimumSize(new Dimension(width * 2, height));
273    m_queryText.setPreferredSize(new Dimension(width * 2, height)); */
274    m_queryText.setText(((DatabaseLoader)m_dsLoader.getLoader()).getQuery());
275    gbConstraints = new GridBagConstraints();
276    gbConstraints.anchor = GridBagConstraints.EAST;
277    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
278    gbConstraints.gridy = 3; gbConstraints.gridx = 1;
279    gbLayout.setConstraints(m_queryText, gbConstraints);
280    db.add(m_queryText);
281   
282    JLabel keyLab = new JLabel("Key columns", SwingConstants.RIGHT);
283    keyLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
284    gbConstraints = new GridBagConstraints();
285    gbConstraints.anchor = GridBagConstraints.EAST;
286    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
287    gbConstraints.gridy = 4; gbConstraints.gridx = 0;
288    gbLayout.setConstraints(keyLab, gbConstraints);
289    db.add(keyLab);
290   
291    m_keyText = new EnvironmentField();
292    m_keyText.setEnvironment(m_env);
293    /*m_keyText.setMinimumSize(new Dimension(width * 2, height));
294    m_keyText.setPreferredSize(new Dimension(width * 2, height)); */
295    m_keyText.setText(((DatabaseLoader)m_dsLoader.getLoader()).getKeys());
296    gbConstraints = new GridBagConstraints();
297    gbConstraints.anchor = GridBagConstraints.EAST;
298    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
299    gbConstraints.gridy = 4; gbConstraints.gridx = 1;
300    gbLayout.setConstraints(m_keyText, gbConstraints);
301    db.add(m_keyText);
302
303    JPanel buttonsP = new JPanel();
304    buttonsP.setLayout(new FlowLayout());
305    JButton ok,cancel;
306    buttonsP.add(ok = new JButton("OK"));
307    buttonsP.add(cancel=new JButton("Cancel"));
308    ok.addActionListener(new ActionListener(){
309      public void actionPerformed(ActionEvent evt){
310        ((DatabaseLoader)m_dsLoader.getLoader()).resetStructure(); 
311        ((DatabaseConverter)m_dsLoader.getLoader()).setUrl(m_dbaseURLText.getText());
312        ((DatabaseConverter)m_dsLoader.getLoader()).setUser(m_userNameText.getText());
313        ((DatabaseConverter)m_dsLoader.getLoader()).setPassword(new String(m_passwordText.getPassword()));
314        ((DatabaseLoader)m_dsLoader.getLoader()).setQuery(m_queryText.getText());
315        ((DatabaseLoader)m_dsLoader.getLoader()).setKeys(m_keyText.getText());
316        try{
317          m_dsLoader.notifyStructureAvailable(((DatabaseLoader)m_dsLoader.getLoader()).getStructure());
318          //database connection has been configured
319          m_dsLoader.setDB(true);
320        }catch (Exception ex){
321        }
322        if (m_parentFrame != null) {
323          m_parentFrame.dispose();
324        }
325      }
326    });
327    cancel.addActionListener(new ActionListener(){
328      public void actionPerformed(ActionEvent evt){
329        if (m_parentFrame != null) {
330          m_parentFrame.dispose();
331        }
332      }
333    });
334   
335    JPanel holderP = new JPanel();
336    holderP.setLayout(new BorderLayout());
337    holderP.add(db, BorderLayout.NORTH);
338    holderP.add(buttonsP, BorderLayout.SOUTH);
339   
340    //db.add(buttonsP);
341    JPanel about = m_LoaderEditor.getAboutPanel();
342    if (about != null) {
343      add(about, BorderLayout.NORTH);
344    }
345    add(holderP, BorderLayout.SOUTH);
346  }
347
348  public void setUpFile() {
349    removeAll();
350
351    boolean currentFileIsDir = false;
352    File tmp = ((FileSourcedConverter)m_dsLoader.getLoader()).retrieveFile();
353    String tmpString = tmp.toString();
354    if (Environment.containsEnvVariables(tmpString)) {
355      try {
356        tmpString = m_env.substitute(tmpString);
357      } catch (Exception ex) {
358        // ignore
359      }
360    }
361    File tmp2 = new File((new File(tmpString)).getAbsolutePath());
362
363    if (tmp2.isDirectory()) {
364      m_fileChooser.setCurrentDirectory(tmp2);
365      currentFileIsDir = true;
366    } else {
367      m_fileChooser.setSelectedFile(tmp2);
368    }
369   
370    FileSourcedConverter loader = (FileSourcedConverter) m_dsLoader.getLoader();
371    String[] ext = loader.getFileExtensions();
372    ExtensionFileFilter firstFilter = null;
373    for (int i = 0; i < ext.length; i++) {
374      ExtensionFileFilter ff =
375        new ExtensionFileFilter(
376            ext[i], loader.getFileDescription() + " (*" + ext[i] + ")");
377      if (i == 0)
378        firstFilter = ff;
379      m_fileChooser.addChoosableFileFilter(ff);
380    }
381    if (firstFilter != null)
382      m_fileChooser.setFileFilter(firstFilter);
383    JPanel about = m_LoaderEditor.getAboutPanel();
384    JPanel northPanel = new JPanel();
385    northPanel.setLayout(new BorderLayout());
386    if (about != null) {
387      northPanel.add(about, BorderLayout.NORTH);
388    }
389    add(northPanel, BorderLayout.NORTH);
390    final EnvironmentField ef = new EnvironmentField();
391    JPanel efHolder = new JPanel();
392    efHolder.setLayout(new BorderLayout());
393
394    ef.setEnvironment(m_env);
395    /*int width = ef.getPreferredSize().width;
396    int height = ef.getPreferredSize().height;
397//    ef.setMinimumSize(new Dimension(width * 2, height));
398    ef.setPreferredSize(new Dimension(width * 2, height)); */
399    m_fileText = ef;
400   
401    // only set the text on the EnvironmentField if the current file is not a directory
402    if (!currentFileIsDir) {
403      ef.setText(tmp.toString());
404    }
405   
406    efHolder.add(ef, BorderLayout.CENTER);
407    JButton browseBut = new JButton("Browse...");
408    browseBut.addActionListener(new ActionListener() {
409      public void actionPerformed(ActionEvent e) {
410        try {
411          final JFrame jf = new JFrame("Choose file");
412          jf.getContentPane().setLayout(new BorderLayout());
413          jf.getContentPane().add(m_fileChooser, BorderLayout.CENTER);
414          jf.pack();
415          jf.setVisible(true);
416          m_fileChooserFrame = jf;
417        } catch (Exception ex) {
418          ex.printStackTrace();
419        }
420      }
421    });
422   
423    JPanel bP = new JPanel(); bP.setLayout(new BorderLayout());
424    bP.setBorder(BorderFactory.createEmptyBorder(5,0,5,5));
425    bP.add(browseBut, BorderLayout.CENTER);
426    efHolder.add(bP, BorderLayout.EAST);
427    JPanel alignedP = new JPanel();
428    GridBagLayout gbLayout = new GridBagLayout();
429    alignedP.setLayout(gbLayout);
430    JLabel efLab = new JLabel("Filename", SwingConstants.RIGHT);
431    efLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
432    GridBagConstraints gbConstraints = new GridBagConstraints();
433    gbConstraints.anchor = GridBagConstraints.EAST;
434    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
435    gbConstraints.gridy = 0; gbConstraints.gridx = 0;
436    gbLayout.setConstraints(efLab, gbConstraints);   
437    alignedP.add(efLab);
438   
439    gbConstraints = new GridBagConstraints();
440    gbConstraints.anchor = GridBagConstraints.EAST;
441    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
442    gbConstraints.gridy = 0; gbConstraints.gridx = 1;
443    gbConstraints.weightx = 5;
444    gbLayout.setConstraints(efHolder, gbConstraints);
445    alignedP.add(efHolder);
446   
447   
448    northPanel.add(alignedP, BorderLayout.SOUTH);
449   
450    // add(m_fileChooser, BorderLayout.CENTER);
451
452    // m_relativeFilePath = new JCheckBox("Use relative file paths");
453    m_relativeFilePath = new JCheckBox();
454    m_relativeFilePath.
455      setSelected(((FileSourcedConverter)m_dsLoader.getLoader()).getUseRelativePath());
456
457    m_relativeFilePath.addActionListener(new ActionListener() {
458        public void actionPerformed(ActionEvent e) {
459          ((FileSourcedConverter)m_dsLoader.getLoader()).
460            setUseRelativePath(m_relativeFilePath.isSelected());
461        }
462      });
463   
464    JLabel relativeLab = new JLabel("Use relative file paths", SwingConstants.RIGHT);
465    relativeLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
466    gbConstraints = new GridBagConstraints();
467    gbConstraints.anchor = GridBagConstraints.EAST;
468    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
469    gbConstraints.gridy = 1; gbConstraints.gridx = 0;
470    gbLayout.setConstraints(relativeLab, gbConstraints);
471    alignedP.add(relativeLab);
472   
473   
474    gbConstraints = new GridBagConstraints();
475    gbConstraints.anchor = GridBagConstraints.EAST;
476    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
477    gbConstraints.gridy = 1; gbConstraints.gridx = 1;
478    gbLayout.setConstraints(m_relativeFilePath, gbConstraints);
479    alignedP.add(m_relativeFilePath);
480       
481    JPanel butHolder = new JPanel();
482    //butHolder.setLayout(new GridLayout(1,2));
483    butHolder.setLayout(new FlowLayout());
484    JButton OKBut = new JButton("OK");
485    OKBut.addActionListener(new ActionListener() {
486      public void actionPerformed(ActionEvent e) {
487        try {
488          ((FileSourcedConverter)m_dsLoader.getLoader()).
489          setFile(new File(ef.getText()));
490          // tell the loader that a new file has been selected so
491          // that it can attempt to load the header
492          m_dsLoader.setLoader(m_dsLoader.getLoader());
493          m_dsLoader.newFileSelected();
494        } catch (Exception ex) {
495          ex.printStackTrace();
496        }
497       
498        m_parentFrame.dispose();
499      }
500    });
501
502    JButton CancelBut = new JButton("Cancel");
503    CancelBut.addActionListener(new ActionListener() {
504      public void actionPerformed(ActionEvent e) {
505        m_parentFrame.dispose();
506      }
507    });
508   
509    butHolder.add(OKBut);
510    butHolder.add(CancelBut);
511   
512    add(butHolder, BorderLayout.SOUTH);
513  }
514
515  /**
516   * Set the loader to be customized
517   *
518   * @param object a weka.gui.beans.Loader
519   */
520  public void setObject(Object object) {
521    m_dsLoader = (weka.gui.beans.Loader)object;
522    m_LoaderEditor.setTarget(m_dsLoader.getLoader());
523    //    m_fileEditor.setValue(m_dsLoader.getDataSetFile());
524    if (m_dsLoader.getLoader() instanceof FileSourcedConverter) {
525      setUpFile();
526    } else{ 
527        if(m_dsLoader.getLoader() instanceof DatabaseConverter) {
528            setUpDatabase();
529        }
530        else
531      setUpOther();
532    }
533  }
534 
535  public void setEnvironment(Environment env) {
536    m_env = env;
537  }
538
539  /**
540   * Add a property change listener
541   *
542   * @param pcl a <code>PropertyChangeListener</code> value
543   */
544  public void addPropertyChangeListener(PropertyChangeListener pcl) {
545    m_pcSupport.addPropertyChangeListener(pcl);
546  }
547
548  /**
549   * Remove a property change listener
550   *
551   * @param pcl a <code>PropertyChangeListener</code> value
552   */
553  public void removePropertyChangeListener(PropertyChangeListener pcl) {
554    m_pcSupport.removePropertyChangeListener(pcl);
555  }
556}
Note: See TracBrowser for help on using the repository browser.