source: branches/MetisMQI/src/main/java/weka/gui/beans/SerializedModelSaverCustomizer.java

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

Taggata versione per la demo e aggiunto branch.

File size: 14.9 KB
RevLine 
[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 *    SerializedModelSaverCustomizer.java
19 *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import weka.gui.GenericObjectEditor;
26import weka.gui.PropertySheetPanel;
27import weka.core.Environment;
28import weka.core.EnvironmentHandler;
29import weka.core.Tag;
30
31import java.awt.BorderLayout;
32import java.awt.Dimension;
33import java.awt.FlowLayout;
34import java.awt.Font;
35import java.awt.GridBagConstraints;
36import java.awt.GridBagLayout;
37import java.awt.GridLayout;
38import java.awt.event.ActionEvent;
39import java.awt.event.ActionListener;
40import java.awt.event.ItemEvent;
41import java.awt.event.ItemListener;
42import java.beans.Customizer;
43import java.beans.PropertyChangeEvent;
44import java.beans.PropertyChangeListener;
45import java.beans.PropertyChangeSupport;
46import java.io.File;
47
48import javax.swing.BoxLayout;
49import javax.swing.JButton;
50import javax.swing.JCheckBox;
51import javax.swing.JComboBox;
52import javax.swing.JFileChooser;
53import javax.swing.JFrame;
54import javax.swing.JLabel;
55import javax.swing.JPanel;
56import javax.swing.BorderFactory;
57
58import javax.swing.JTextField;
59import javax.swing.SwingConstants;
60import javax.swing.filechooser.FileFilter;
61
62/**
63 * GUI Customizer for the SerializedModelSaver bean
64 *
65 * @author Mark Hall (mhall{[at]}pentaho{[dot]}org
66 * @version $Revision: 5563 $
67 */
68public class SerializedModelSaverCustomizer
69  extends JPanel
70  implements Customizer, CustomizerCloseRequester, EnvironmentHandler {
71
72  /** for serialization */
73  private static final long serialVersionUID = -4874208115942078471L;
74
75  static {
76     GenericObjectEditor.registerEditors();
77  }
78
79  private PropertyChangeSupport m_pcSupport = 
80    new PropertyChangeSupport(this);
81
82  private weka.gui.beans.SerializedModelSaver m_smSaver;
83
84  private PropertySheetPanel m_SaverEditor = 
85    new PropertySheetPanel();
86
87  private JFileChooser m_fileChooser
88    = new JFileChooser(new File(System.getProperty("user.dir")));
89 
90
91  private JFrame m_parentFrame;
92 
93  private JFrame m_fileChooserFrame;
94 
95  //private JTextField m_prefixText;
96  private EnvironmentField m_prefixText;
97
98  private JComboBox m_fileFormatBox;
99
100  private JCheckBox m_relativeFilePath;
101 
102  private JCheckBox m_includeRelationName;
103 
104  private Environment m_env = Environment.getSystemWide();
105 
106  private EnvironmentField m_directoryText;
107 
108
109  /** Constructor */ 
110  public SerializedModelSaverCustomizer() {
111
112    try {
113      m_SaverEditor.addPropertyChangeListener(
114          new PropertyChangeListener() {
115              public void propertyChange(PropertyChangeEvent e) {
116                repaint();
117                if (m_smSaver != null) {
118                  System.err.println("Property change!!");
119                  //              m_smSaver.setSaver(m_smSaver.getSaver());
120                }
121              }
122            });
123      repaint();
124    } catch (Exception ex) {
125      ex.printStackTrace();
126    }
127    setLayout(new BorderLayout());
128
129    m_fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
130    m_fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
131    m_fileChooser.setApproveButtonText("Select directory and prefix");
132
133    m_fileChooser.addActionListener(new ActionListener() {
134        public void actionPerformed(ActionEvent e) {
135          if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
136            try {
137              m_smSaver.setPrefix(m_prefixText.getText());
138//              m_smSaver.setDirectory(m_fileChooser.getSelectedFile());
139             
140              File selectedFile = m_fileChooser.getSelectedFile();
141              m_directoryText.setText(selectedFile.toString());
142             
143            } catch (Exception ex) {
144              ex.printStackTrace();
145            }
146          }
147          // closing
148          if (m_parentFrame != null) {
149            m_fileChooserFrame.dispose();
150          }
151        }
152      });   
153  }
154
155  public void setParentFrame(JFrame parent) {
156    m_parentFrame = parent;
157  }
158 
159  private void setUpOther() {
160    removeAll();
161    add(m_SaverEditor, BorderLayout.CENTER);
162    validate();
163    repaint();
164  }
165 
166  /** Sets up dialog for saving models to a file */ 
167  public void setUpFile() {
168    removeAll();
169    m_fileChooser.setFileFilter(new FileFilter() { 
170      public boolean accept(File f) { 
171        return f.isDirectory();
172      }
173      public String getDescription() {
174        return "Directory";
175      }
176    });
177
178    m_fileChooser.setAcceptAllFileFilterUsed(false);
179
180    try{
181      if (!m_smSaver.getDirectory().getPath().equals("")) {
182       // File tmp = m_smSaver.getDirectory();
183        String dirStr = m_smSaver.getDirectory().toString();
184        if (Environment.containsEnvVariables(dirStr)) {
185          try {
186            dirStr = m_env.substitute(dirStr);
187          } catch (Exception ex) {
188            // ignore
189          }
190        }
191        File tmp = new File(dirStr);;
192        tmp = new File (tmp.getAbsolutePath());
193        m_fileChooser.setCurrentDirectory(tmp);
194      }
195    } catch(Exception ex) {
196      System.out.println(ex);
197    }
198
199    JPanel innerPanel = new JPanel();
200    innerPanel.setLayout(new BorderLayout());
201   
202    JPanel alignedP = new JPanel();
203    GridBagLayout gbLayout = new GridBagLayout();
204    alignedP.setLayout(gbLayout);
205   
206    JLabel prefixLab = new JLabel("Prefix for file name", SwingConstants.RIGHT);
207    prefixLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
208    GridBagConstraints gbConstraints = new GridBagConstraints();
209    gbConstraints.anchor = GridBagConstraints.EAST;
210    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
211    gbConstraints.gridy = 0; gbConstraints.gridx = 0;
212    gbLayout.setConstraints(prefixLab, gbConstraints);
213    alignedP.add(prefixLab);
214   
215//    m_prefixText = new JTextField(m_smSaver.getPrefix(), 25);
216    m_prefixText = new EnvironmentField();
217    m_prefixText.setEnvironment(m_env);
218/*    int width = m_prefixText.getPreferredSize().width;
219    int height = m_prefixText.getPreferredSize().height;
220    m_prefixText.setMinimumSize(new Dimension(width * 2, height));
221    m_prefixText.setPreferredSize(new Dimension(width * 2, height)); */
222    m_prefixText.setText(m_smSaver.getPrefix());
223    gbConstraints = new GridBagConstraints();
224    gbConstraints.anchor = GridBagConstraints.EAST;
225    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
226    gbConstraints.gridy = 0; gbConstraints.gridx = 1;
227    gbLayout.setConstraints(m_prefixText, gbConstraints);
228    alignedP.add(m_prefixText);
229   
230    JLabel ffLab = new JLabel("File format", SwingConstants.RIGHT);
231    ffLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
232    gbConstraints = new GridBagConstraints();
233    gbConstraints.anchor = GridBagConstraints.EAST;
234    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
235    gbConstraints.gridy = 1; gbConstraints.gridx = 0;
236    gbLayout.setConstraints(ffLab, gbConstraints);
237    alignedP.add(ffLab);
238   
239    setUpFileFormatComboBox();
240    m_fileFormatBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
241    gbConstraints = new GridBagConstraints();
242    gbConstraints.anchor = GridBagConstraints.EAST;
243    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
244    gbConstraints.gridy = 1; gbConstraints.gridx = 1;
245    gbLayout.setConstraints(m_fileFormatBox, gbConstraints);
246    alignedP.add(m_fileFormatBox);
247
248    JPanel about = m_SaverEditor.getAboutPanel();
249    if (about != null) {
250      innerPanel.add(about, BorderLayout.NORTH);
251    }
252    add(innerPanel, BorderLayout.NORTH);
253   
254    JLabel directoryLab = new JLabel("Directory", SwingConstants.RIGHT);
255    directoryLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
256    gbConstraints = new GridBagConstraints();
257    gbConstraints.anchor = GridBagConstraints.EAST;
258    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
259    gbConstraints.gridy = 2; gbConstraints.gridx = 0;
260    gbLayout.setConstraints(directoryLab, gbConstraints);
261    alignedP.add(directoryLab);
262   
263    m_directoryText = new EnvironmentField();
264    m_directoryText.setEnvironment(m_env); 
265/*    width = m_directoryText.getPreferredSize().width;
266    height = m_directoryText.getPreferredSize().height;
267    m_directoryText.setMinimumSize(new Dimension(width * 2, height));
268    m_directoryText.setPreferredSize(new Dimension(width * 2, height)); */
269   
270    m_directoryText.setText(m_smSaver.getDirectory().toString());
271   
272    JButton browseBut = new JButton("Browse...");
273    browseBut.addActionListener(new ActionListener() {
274      public void actionPerformed(ActionEvent e) {
275        try {
276          final JFrame jf = new JFrame("Choose directory");
277          jf.getContentPane().setLayout(new BorderLayout());
278          jf.getContentPane().add(m_fileChooser, BorderLayout.CENTER);
279          jf.pack();
280          jf.setVisible(true);
281          m_fileChooserFrame = jf;
282        } catch (Exception ex) {
283          ex.printStackTrace();
284        }
285      }
286    });
287   
288    JPanel efHolder = new JPanel();
289    efHolder.setLayout(new BorderLayout());
290    JPanel bP = new JPanel(); bP.setLayout(new BorderLayout());
291    bP.setBorder(BorderFactory.createEmptyBorder(5,0,5,5));
292    bP.add(browseBut, BorderLayout.CENTER);
293    efHolder.add(bP, BorderLayout.EAST);
294    efHolder.add(m_directoryText, BorderLayout.CENTER);
295    gbConstraints = new GridBagConstraints();
296    gbConstraints.anchor = GridBagConstraints.EAST;
297    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
298    gbConstraints.gridy = 2; gbConstraints.gridx = 1;
299    gbConstraints.weightx = 5; // make sure that extra horizontal space gets allocated to this column
300    gbLayout.setConstraints(efHolder, gbConstraints);
301    alignedP.add(efHolder);
302
303   
304    JLabel relativeLab = new JLabel("Use relative file paths", SwingConstants.RIGHT);
305    relativeLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
306    gbConstraints = new GridBagConstraints();
307    gbConstraints.anchor = GridBagConstraints.EAST;
308    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
309    gbConstraints.gridy = 3; gbConstraints.gridx = 0;
310    gbLayout.setConstraints(relativeLab, gbConstraints);
311    alignedP.add(relativeLab);
312   
313    m_relativeFilePath = new JCheckBox();
314    m_relativeFilePath.
315      setSelected(m_smSaver.getUseRelativePath());
316
317    m_relativeFilePath.addActionListener(new ActionListener() {
318        public void actionPerformed(ActionEvent e) {
319          m_smSaver.setUseRelativePath(m_relativeFilePath.isSelected());
320        }
321      });
322    gbConstraints = new GridBagConstraints();
323    gbConstraints.anchor = GridBagConstraints.EAST;
324    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
325    gbConstraints.gridy = 3; gbConstraints.gridx = 1;
326    gbLayout.setConstraints(m_relativeFilePath, gbConstraints);
327    alignedP.add(m_relativeFilePath);
328   
329   
330    JLabel relationLab = new JLabel("Include relation name in file name", SwingConstants.RIGHT);
331    relationLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
332    gbConstraints = new GridBagConstraints();
333    gbConstraints.anchor = GridBagConstraints.EAST;
334    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
335    gbConstraints.gridy = 4; gbConstraints.gridx = 0;
336    gbLayout.setConstraints(relationLab, gbConstraints);
337    alignedP.add(relationLab);
338   
339    m_includeRelationName = new JCheckBox();
340    m_includeRelationName.setToolTipText("Include the relation name of the training data used "
341        + "to create the model in the file name.");
342    m_includeRelationName.setSelected(m_smSaver.getIncludeRelationName());
343       
344    gbConstraints = new GridBagConstraints();
345    gbConstraints.anchor = GridBagConstraints.EAST;
346    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
347    gbConstraints.gridy = 4; gbConstraints.gridx = 1;
348    gbLayout.setConstraints(m_includeRelationName, gbConstraints);
349    alignedP.add(m_includeRelationName);
350   
351    JButton OKBut = new JButton("OK");
352    OKBut.addActionListener(new ActionListener() {
353      public void actionPerformed(ActionEvent e) {
354        try {         
355          m_smSaver.setPrefix(m_prefixText.getText());
356          m_smSaver.setDirectory(new File(m_directoryText.getText()));
357          m_smSaver.
358            setIncludeRelationName(m_includeRelationName.isSelected());
359        } catch (Exception ex) {
360          ex.printStackTrace();
361        }
362       
363        m_parentFrame.dispose();
364      }
365    });
366
367    JButton CancelBut = new JButton("Cancel");
368    CancelBut.addActionListener(new ActionListener() {
369      public void actionPerformed(ActionEvent e) {
370        m_parentFrame.dispose();
371      }
372    });
373   
374    JPanel butHolder = new JPanel();
375    butHolder.setLayout(new FlowLayout());
376    butHolder.add(OKBut);
377    butHolder.add(CancelBut);
378 
379    JPanel holderPanel = new JPanel();
380    holderPanel.setLayout(new BorderLayout());
381    holderPanel.add(alignedP, BorderLayout.NORTH);
382    holderPanel.add(butHolder, BorderLayout.SOUTH);
383    add(holderPanel, BorderLayout.SOUTH);
384  }
385
386  /**
387   * Set the model saver to be customized
388   *
389   * @param object a weka.gui.beans.SerializedModelSaver
390   */
391  public void setObject(Object object) {
392    m_smSaver = (weka.gui.beans.SerializedModelSaver)object;
393    m_SaverEditor.setTarget(m_smSaver);
394
395    setUpFile();   
396  }
397
398  private void setUpFileFormatComboBox() {
399    m_fileFormatBox = new JComboBox();
400    for (int i = 0; i < SerializedModelSaver.s_fileFormatsAvailable.size(); i++) {
401      Tag temp = SerializedModelSaver.s_fileFormatsAvailable.get(i);
402      m_fileFormatBox.addItem(temp);
403    }
404
405    Tag result = m_smSaver.validateFileFormat(m_smSaver.getFileFormat());
406    if (result == null) {
407      m_fileFormatBox.setSelectedIndex(0);
408    } else {
409      m_fileFormatBox.setSelectedItem(result);
410    }
411
412    m_fileFormatBox.addActionListener(new ActionListener() {
413        public void actionPerformed(ActionEvent e) {
414          Tag selected = (Tag)m_fileFormatBox.getSelectedItem();
415          if (selected != null) {
416            m_smSaver.setFileFormat(selected);
417          }
418        }
419      });
420  }
421
422  /**
423   * Add a property change listener
424   *
425   * @param pcl a <code>PropertyChangeListener</code> value
426   */
427  public void addPropertyChangeListener(PropertyChangeListener pcl) {
428    m_pcSupport.addPropertyChangeListener(pcl);
429  }
430
431  /**
432   * Remove a property change listener
433   *
434   * @param pcl a <code>PropertyChangeListener</code> value
435   */
436  public void removePropertyChangeListener(PropertyChangeListener pcl) {
437    m_pcSupport.removePropertyChangeListener(pcl);
438  }
439
440  /* (non-Javadoc)
441   * @see weka.core.EnvironmentHandler#setEnvironment(weka.core.Environment)
442   */
443  public void setEnvironment(Environment env) {
444    m_env = env;
445  }
446}
Note: See TracBrowser for help on using the repository browser.