source: branches/MetisMQI/src/main/java/weka/gui/experiment/AlgorithmListPanel.java

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

Taggata versione per la demo e aggiunto branch.

File size: 20.7 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 *    AlgorithmListPanel.java
19 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.experiment;
24
25import weka.classifiers.Classifier;
26import weka.classifiers.AbstractClassifier;
27import weka.classifiers.xml.XMLClassifier;
28import weka.core.OptionHandler;
29import weka.core.SerializedObject;
30import weka.core.Utils;
31import weka.experiment.Experiment;
32import weka.gui.ExtensionFileFilter;
33import weka.gui.GenericObjectEditor;
34import weka.gui.JListHelper;
35import weka.gui.PropertyDialog;
36
37import java.awt.BorderLayout;
38import java.awt.Component;
39import java.awt.GridBagConstraints;
40import java.awt.GridBagLayout;
41import java.awt.Insets;
42import java.awt.Toolkit;
43import java.awt.datatransfer.Clipboard;
44import java.awt.datatransfer.StringSelection;
45import java.awt.event.ActionEvent;
46import java.awt.event.ActionListener;
47import java.awt.event.MouseAdapter;
48import java.awt.event.MouseEvent;
49import java.awt.event.MouseListener;
50import java.awt.event.WindowAdapter;
51import java.awt.event.WindowEvent;
52import java.beans.PropertyChangeEvent;
53import java.beans.PropertyChangeListener;
54import java.io.File;
55
56import javax.swing.BorderFactory;
57import javax.swing.DefaultListCellRenderer;
58import javax.swing.DefaultListModel;
59import javax.swing.JButton;
60import javax.swing.JFileChooser;
61import javax.swing.JFrame;
62import javax.swing.JList;
63import javax.swing.JMenuItem;
64import javax.swing.JOptionPane;
65import javax.swing.JPanel;
66import javax.swing.JPopupMenu;
67import javax.swing.JScrollPane;
68import javax.swing.event.ListSelectionEvent;
69import javax.swing.event.ListSelectionListener;
70import javax.swing.filechooser.FileFilter;
71
72/**
73 * This panel controls setting a list of algorithms for an experiment to
74 * iterate over.
75 *
76 * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
77 * @version $Revision: 5928 $
78 */
79public class AlgorithmListPanel
80  extends JPanel
81  implements ActionListener {
82
83  /** for serialization */
84  private static final long serialVersionUID = -7204528834764898671L;
85
86  /**
87   * Class required to show the Classifiers nicely in the list
88   */
89  public class ObjectCellRenderer
90    extends DefaultListCellRenderer {
91
92    /** for serialization */
93    private static final long serialVersionUID = -5067138526587433808L;
94   
95    /**
96     * Return a component that has been configured to display the specified
97     * value. That component's paint method is then called to "render" the
98     * cell. If it is necessary to compute the dimensions of a list because
99     * the list cells do not have a fixed size, this method is called to
100     * generate a component on which getPreferredSize can be invoked.
101     *
102     * @param list              The JList we're painting.
103     * @param value             The value returned by
104     *                          list.getModel().getElementAt(index).
105     * @param index             The cells index.
106     * @param isSelected        True if the specified cell was selected.
107     * @param cellHasFocus      True if the specified cell has the focus.
108     * @return                  A component whose paint() method will render
109     *                          the specified value.
110     */
111    public Component getListCellRendererComponent(JList list,
112                                                  Object value,
113                                                  int index,
114                                                  boolean isSelected,
115                                                  boolean cellHasFocus) {
116
117      Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
118      String rep = value.getClass().getName();
119      int dotPos = rep.lastIndexOf('.');
120      if (dotPos != -1) {
121        rep = rep.substring(dotPos + 1);
122      }
123      if (value instanceof OptionHandler) {
124        rep += " " + Utils.joinOptions(((OptionHandler)value)
125                                       .getOptions());
126      }
127      setText(rep);
128      return c;
129    }
130  }
131 
132  /** The experiment to set the algorithm list of */
133  protected Experiment m_Exp;
134
135  /** The component displaying the algorithm list */
136  protected JList m_List;
137
138  /** Click to add an algorithm */
139  protected JButton m_AddBut = new JButton("Add new...");
140 
141  /** Click to edit the selected algorithm */
142  protected JButton m_EditBut = new JButton("Edit selected...");
143
144  /** Click to remove the selected dataset from the list */
145  protected JButton m_DeleteBut = new JButton("Delete selected");
146 
147  /** Click to edit the load the options for athe selected algorithm */
148  protected JButton m_LoadOptionsBut = new JButton("Load options...");
149 
150  /** Click to edit the save the options from selected algorithm */
151  protected JButton m_SaveOptionsBut = new JButton("Save options...");
152 
153  /** Click to move the selected algorithm(s) one up */
154  protected JButton m_UpBut = new JButton("Up");
155 
156  /** Click to move the selected algorithm(s) one down */
157  protected JButton m_DownBut = new JButton("Down");
158 
159  /** The file chooser for selecting experiments */
160  protected JFileChooser m_FileChooser =
161    new JFileChooser(new File(System.getProperty("user.dir")));
162
163  /** A filter to ensure only experiment (in XML format) files get shown in the chooser */
164  protected FileFilter m_XMLFilter = 
165    new ExtensionFileFilter(".xml", 
166                            "Classifier options (*.xml)");
167
168  /** Whether an algorithm is added or only edited  */
169  protected boolean m_Editing = false;
170 
171  /** Lets the user configure the classifier */
172  protected GenericObjectEditor m_ClassifierEditor =
173    new GenericObjectEditor(true);
174
175  /** The currently displayed property dialog, if any */
176  protected PropertyDialog m_PD;
177
178  /** The list model used */
179  protected DefaultListModel m_AlgorithmListModel = new DefaultListModel();
180
181  /* Register the property editors we need */
182  static {
183     GenericObjectEditor.registerEditors();
184  }
185
186  /**
187   * Creates the algorithm list panel with the given experiment.
188   *
189   * @param exp a value of type 'Experiment'
190   */
191  public AlgorithmListPanel(Experiment exp) {
192
193    this();
194    setExperiment(exp);
195  }
196
197  /**
198   * Create the algorithm list panel initially disabled.
199   */
200  public AlgorithmListPanel() {
201    final AlgorithmListPanel self = this;
202    m_List = new JList();
203    MouseListener mouseListener = new MouseAdapter() {
204      public void mouseClicked(MouseEvent e) {
205        final int index = m_List.locationToIndex(e.getPoint());
206
207        if ((e.getClickCount() == 2) && (e.getButton() == MouseEvent.BUTTON1)) {
208          // unfortunately, locationToIndex only returns the nearest entry
209          // and not the exact one, i.e. if there's one item in the list and
210          // one doublelclicks somewhere in the list, this index will be
211          // returned
212          if (index > -1)
213            actionPerformed(new ActionEvent(m_EditBut, 0, ""));
214        }
215        else if (e.getClickCount() == 1) {
216          if (    (e.getButton() == MouseEvent.BUTTON3) 
217              || ((e.getButton() == MouseEvent.BUTTON1) && e.isAltDown() && e.isShiftDown()) ) {
218            JPopupMenu menu = new JPopupMenu();
219            JMenuItem item;
220
221            item = new JMenuItem("Add configuration...");
222            item.addActionListener(new ActionListener() {
223              public void actionPerformed(ActionEvent e) {
224                String str = JOptionPane.showInputDialog(
225                    self, 
226                    "Configuration (<classname> [<options>])");
227                if (str != null) {
228                  try {
229                    String[] options = Utils.splitOptions(str);
230                    String classname = options[0];
231                    options[0] = "";
232                    Object obj = Utils.forName(Object.class, classname, options);
233                    m_AlgorithmListModel.addElement(obj);
234                    updateExperiment();
235                  }
236                  catch (Exception ex) {
237                    ex.printStackTrace();
238                    JOptionPane.showMessageDialog(
239                        self, 
240                        "Error parsing commandline:\n" + ex, 
241                        "Error...",
242                        JOptionPane.ERROR_MESSAGE);
243                  }
244                }
245              }
246            });
247            menu.add(item);
248
249            if (m_List.getSelectedValue() != null) {
250              menu.addSeparator();
251
252              item = new JMenuItem("Show properties...");
253              item.addActionListener(new ActionListener() {
254                public void actionPerformed(ActionEvent e) {
255                  self.actionPerformed(new ActionEvent(m_EditBut, 0, ""));
256                }
257              });
258              menu.add(item);
259
260              item = new JMenuItem("Copy configuration to clipboard");
261              item.addActionListener(new ActionListener() {
262                public void actionPerformed(ActionEvent e) {
263                  String str = m_List.getSelectedValue().getClass().getName();
264                  if (m_List.getSelectedValue() instanceof OptionHandler)
265                    str += " " + Utils.joinOptions(((OptionHandler) m_List.getSelectedValue()).getOptions());
266                  StringSelection selection = new StringSelection(str.trim());
267                  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
268                  clipboard.setContents(selection, selection);
269                }
270              });
271              menu.add(item);
272
273              item = new JMenuItem("Enter configuration...");
274              item.addActionListener(new ActionListener() {
275                public void actionPerformed(ActionEvent e) {
276                  String str = JOptionPane.showInputDialog(
277                      self, 
278                      "Configuration (<classname> [<options>])");
279                  if (str != null) {
280                    try {
281                      String[] options = Utils.splitOptions(str);
282                      String classname = options[0];
283                      options[0] = "";
284                      Object obj = Utils.forName(Object.class, classname, options);
285                      m_AlgorithmListModel.setElementAt(obj, index);
286                      updateExperiment();
287                    }
288                    catch (Exception ex) {
289                      ex.printStackTrace();
290                      JOptionPane.showMessageDialog(
291                          self, 
292                          "Error parsing commandline:\n" + ex, 
293                          "Error...",
294                          JOptionPane.ERROR_MESSAGE);
295                    }
296                  }
297                }
298              });
299              menu.add(item);
300            }
301
302            menu.show(m_List, e.getX(), e.getY());
303          }
304        }
305      }
306    };
307    m_List.addMouseListener(mouseListener);
308 
309    m_ClassifierEditor.setClassType(Classifier.class);
310    m_ClassifierEditor.setValue(new weka.classifiers.rules.ZeroR());
311    m_ClassifierEditor.addPropertyChangeListener(new PropertyChangeListener() {
312        public void propertyChange(PropertyChangeEvent e) {
313          repaint();
314        }
315      });
316    ((GenericObjectEditor.GOEPanel) m_ClassifierEditor.getCustomEditor()).addOkListener(new ActionListener() {
317        public void actionPerformed(ActionEvent e) {
318          Classifier newCopy =
319            (Classifier) copyObject(m_ClassifierEditor.getValue());
320          addNewAlgorithm(newCopy);
321        }
322      });
323   
324    m_DeleteBut.setEnabled(false);
325    m_DeleteBut.addActionListener(this);
326    m_AddBut.setEnabled(false);
327    m_AddBut.addActionListener(this);
328    m_EditBut.setEnabled(false);
329    m_EditBut.addActionListener(this);
330    m_LoadOptionsBut.setEnabled(false);
331    m_LoadOptionsBut.addActionListener(this);
332    m_SaveOptionsBut.setEnabled(false);
333    m_SaveOptionsBut.addActionListener(this);
334    m_UpBut.setEnabled(false);
335    m_UpBut.addActionListener(this);
336    m_DownBut.setEnabled(false);
337    m_DownBut.addActionListener(this);
338   
339    m_List.addListSelectionListener(new ListSelectionListener() {
340        public void valueChanged(ListSelectionEvent e) {
341          setButtons(e);
342        }
343      });
344   
345    m_FileChooser.addChoosableFileFilter(m_XMLFilter);
346    m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
347
348    setLayout(new BorderLayout());
349    setBorder(BorderFactory.createTitledBorder("Algorithms"));
350    JPanel topLab = new JPanel();
351    GridBagLayout gb = new GridBagLayout();
352    GridBagConstraints constraints = new GridBagConstraints();
353    topLab.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
354    topLab.setLayout(gb);
355   
356    constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
357    constraints.fill = GridBagConstraints.HORIZONTAL;
358    constraints.gridwidth=1;constraints.gridheight=1;
359    constraints.insets = new Insets(0,2,0,2);
360    topLab.add(m_AddBut,constraints);
361    constraints.gridx=1;constraints.gridy=0;constraints.weightx=5;
362    constraints.gridwidth=1;constraints.gridheight=1;
363    topLab.add(m_EditBut,constraints);
364    constraints.gridx=2;constraints.gridy=0;constraints.weightx=5;
365    constraints.gridwidth=1;constraints.gridheight=1;
366    topLab.add(m_DeleteBut,constraints);
367
368    JPanel bottomLab = new JPanel();
369    gb = new GridBagLayout();
370    constraints = new GridBagConstraints();
371    bottomLab.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
372    bottomLab.setLayout(gb);
373   
374    constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
375    constraints.fill = GridBagConstraints.HORIZONTAL;
376    constraints.gridwidth=1;constraints.gridheight=1;
377    constraints.insets = new Insets(0,2,0,2);
378    bottomLab.add(m_LoadOptionsBut,constraints);
379    constraints.gridx=1;constraints.gridy=0;constraints.weightx=5;
380    constraints.gridwidth=1;constraints.gridheight=1;
381    bottomLab.add(m_SaveOptionsBut,constraints);
382    constraints.gridx=2;constraints.gridy=0;constraints.weightx=5;
383    constraints.gridwidth=1;constraints.gridheight=1;
384    bottomLab.add(m_UpBut,constraints);
385    constraints.gridx=3;constraints.gridy=0;constraints.weightx=5;
386    constraints.gridwidth=1;constraints.gridheight=1;
387    bottomLab.add(m_DownBut,constraints);
388
389    add(topLab, BorderLayout.NORTH);
390    add(new JScrollPane(m_List), BorderLayout.CENTER);
391    add(bottomLab, BorderLayout.SOUTH);
392  }
393
394  /**
395   * Tells the panel to act on a new experiment.
396   *
397   * @param exp a value of type 'Experiment'
398   */
399  public void setExperiment(Experiment exp) {
400
401    m_Exp = exp;
402    m_AddBut.setEnabled(true);
403    m_List.setModel(m_AlgorithmListModel);
404    m_List.setCellRenderer(new ObjectCellRenderer());
405    m_AlgorithmListModel.removeAllElements();
406    if (m_Exp.getPropertyArray() instanceof Classifier[]) {
407      Classifier[] algorithms = (Classifier[]) m_Exp.getPropertyArray();
408      for (int i=0; i<algorithms.length; i++) {
409        m_AlgorithmListModel.addElement(algorithms[i]);
410      }
411    }
412    m_EditBut.setEnabled((m_AlgorithmListModel.size() > 0));
413    m_DeleteBut.setEnabled((m_AlgorithmListModel.size() > 0));
414    m_LoadOptionsBut.setEnabled((m_AlgorithmListModel.size() > 0));
415    m_SaveOptionsBut.setEnabled((m_AlgorithmListModel.size() > 0));
416    m_UpBut.setEnabled(JListHelper.canMoveUp(m_List));
417    m_DownBut.setEnabled(JListHelper.canMoveDown(m_List));
418  }
419
420  /**
421   * Add a new algorithm to the list.
422   *
423   * @param newScheme   the new scheme to add
424   */
425  private void addNewAlgorithm(Classifier newScheme) {
426    if (!m_Editing)
427      m_AlgorithmListModel.addElement(newScheme);
428    else
429      m_AlgorithmListModel.setElementAt(newScheme, m_List.getSelectedIndex());
430   
431    updateExperiment();
432   
433    m_Editing = false;
434  }
435 
436  /**
437   * updates the classifiers in the experiment
438   */
439  private void updateExperiment() {
440    Classifier[] cArray = new Classifier[m_AlgorithmListModel.size()];
441    for (int i=0; i<cArray.length; i++) {
442      cArray[i] = (Classifier) m_AlgorithmListModel.elementAt(i);
443    }
444    m_Exp.setPropertyArray(cArray); 
445  }
446 
447  /**
448   * sets the state of the buttons according to the selection state of the
449   * JList
450   *
451   * @param e           the event
452   */
453  private void setButtons(ListSelectionEvent e) {
454    if (e.getSource() == m_List) {
455      m_DeleteBut.setEnabled(m_List.getSelectedIndex() > -1);
456      m_AddBut.setEnabled(true);
457      m_EditBut.setEnabled(m_List.getSelectedIndices().length == 1);
458      m_LoadOptionsBut.setEnabled(m_List.getSelectedIndices().length == 1);
459      m_SaveOptionsBut.setEnabled(m_List.getSelectedIndices().length == 1);
460      m_UpBut.setEnabled(JListHelper.canMoveUp(m_List));
461      m_DownBut.setEnabled(JListHelper.canMoveDown(m_List));
462    }
463  }
464
465  /**
466   * Handle actions when buttons get pressed.
467   *
468   * @param e a value of type 'ActionEvent'
469   */
470  public void actionPerformed(ActionEvent e) {
471
472    if (e.getSource() == m_AddBut) {
473      m_Editing = false;
474      if (m_PD == null) {
475        int x = getLocationOnScreen().x;
476        int y = getLocationOnScreen().y;
477        if (PropertyDialog.getParentDialog(this) != null)
478          m_PD = new PropertyDialog(
479              PropertyDialog.getParentDialog(this), 
480              m_ClassifierEditor, x, y);
481        else
482          m_PD = new PropertyDialog(
483              PropertyDialog.getParentFrame(this), 
484              m_ClassifierEditor, x, y);
485        m_PD.setVisible(true);
486      } else {
487        m_PD.setVisible(true);
488      }
489     
490    } else if (e.getSource() == m_EditBut) {
491      if (m_List.getSelectedValue() != null) {
492        m_ClassifierEditor.setClassType(weka.classifiers.Classifier.class);
493        // m_PD.getEditor().setValue(m_List.getSelectedValue());
494        m_ClassifierEditor.setValue(m_List.getSelectedValue());
495         m_Editing = true;
496         if (m_PD == null) {
497            int x = getLocationOnScreen().x;
498            int y = getLocationOnScreen().y;
499            if (PropertyDialog.getParentDialog(this) != null)
500              m_PD = new PropertyDialog(
501                  PropertyDialog.getParentDialog(this), 
502                  m_ClassifierEditor, x, y);
503            else
504              m_PD = new PropertyDialog(
505                  PropertyDialog.getParentFrame(this), 
506                  m_ClassifierEditor, x, y);
507            m_PD.setVisible(true);
508         } else {
509            m_PD.setVisible(true);
510         }
511      }
512
513    } else if (e.getSource() == m_DeleteBut) {
514
515      int [] selected = m_List.getSelectedIndices();
516      if (selected != null) {
517        for (int i = selected.length - 1; i >= 0; i--) {
518          int current = selected[i];
519          m_AlgorithmListModel.removeElementAt(current);
520          if (m_Exp.getDatasets().size() > current) {
521            m_List.setSelectedIndex(current);
522          } else {
523            m_List.setSelectedIndex(current - 1);
524          }
525        }
526      }
527      if (m_List.getSelectedIndex() == -1) {
528        m_EditBut.setEnabled(false);
529        m_DeleteBut.setEnabled(false);
530        m_LoadOptionsBut.setEnabled(false);
531        m_SaveOptionsBut.setEnabled(false);
532        m_UpBut.setEnabled(false);
533        m_DownBut.setEnabled(false);
534      }
535
536      updateExperiment();
537    } else if (e.getSource() == m_LoadOptionsBut) {
538      if (m_List.getSelectedValue() != null) {
539        int returnVal = m_FileChooser.showOpenDialog(this);
540        if (returnVal == JFileChooser.APPROVE_OPTION) {
541          try {
542            File file = m_FileChooser.getSelectedFile();
543            if (!file.getAbsolutePath().toLowerCase().endsWith(".xml"))
544              file = new File(file.getAbsolutePath() + ".xml");
545            XMLClassifier xmlcls = new XMLClassifier();
546            Classifier c = (Classifier) xmlcls.read(file);
547            m_AlgorithmListModel.setElementAt(c, m_List.getSelectedIndex());
548            updateExperiment();
549          }
550          catch (Exception ex) {
551            ex.printStackTrace();
552          }
553        }
554      }
555   } else if (e.getSource() == m_SaveOptionsBut) {
556      if (m_List.getSelectedValue() != null) {
557        int returnVal = m_FileChooser.showSaveDialog(this);
558        if (returnVal == JFileChooser.APPROVE_OPTION) {
559          try {
560            File file = m_FileChooser.getSelectedFile();
561            if (!file.getAbsolutePath().toLowerCase().endsWith(".xml"))
562              file = new File(file.getAbsolutePath() + ".xml");
563            XMLClassifier xmlcls = new XMLClassifier();
564            xmlcls.write(file, m_List.getSelectedValue());
565          }
566          catch (Exception ex) {
567            ex.printStackTrace();
568          }
569        }
570      }
571    } 
572    else if (e.getSource() == m_UpBut) {
573      JListHelper.moveUp(m_List);
574      updateExperiment();
575    }
576    else if (e.getSource() == m_DownBut) {
577      JListHelper.moveDown(m_List);
578      updateExperiment();
579    }
580  }
581
582  /**
583   * Makes a copy of an object using serialization
584   * @param source the object to copy
585   * @return a copy of the source object
586   */
587  protected Object copyObject(Object source) {
588   
589    Object result = null;
590    try {
591      SerializedObject so = new SerializedObject(source);
592      result = so.getObject();
593    } catch (Exception ex) {
594      System.err.println("AlgorithmListPanel: Problem copying object");
595      System.err.println(ex);
596    }
597    return result;
598  }
599 
600  /**
601   * Tests out the algorithm list panel from the command line.
602   *
603   * @param args ignored
604   */
605  public static void main(String [] args) {
606
607    try {
608      final JFrame jf = new JFrame("Algorithm List Editor");
609      jf.getContentPane().setLayout(new BorderLayout());
610      AlgorithmListPanel dp = new AlgorithmListPanel();
611      jf.getContentPane().add(dp,
612                              BorderLayout.CENTER);
613      jf.addWindowListener(new WindowAdapter() {
614        public void windowClosing(WindowEvent e) {
615          jf.dispose();
616          System.exit(0);
617        }
618      });
619      jf.pack();
620      jf.setVisible(true);
621      System.err.println("Short nap");
622      Thread.currentThread().sleep(3000);
623      System.err.println("Done");
624      dp.setExperiment(new Experiment());
625    } catch (Exception ex) {
626      ex.printStackTrace();
627      System.err.println(ex.getMessage());
628    }
629  }
630}
Note: See TracBrowser for help on using the repository browser.