📄 algorithmlistpanel.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * AlgorithmListPanel.java * Copyright (C) 2002 Richard Kirkby * */package weka.gui.experiment;import weka.classifiers.Classifier;import weka.classifiers.xml.XMLClassifier;import weka.core.OptionHandler;import weka.core.SerializedObject;import weka.core.Utils;import weka.experiment.Experiment;import weka.gui.ExtensionFileFilter;import weka.gui.GenericObjectEditor;import weka.gui.JListHelper;import weka.gui.PropertyDialog;import java.awt.BorderLayout;import java.awt.Component;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.Toolkit;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.StringSelection;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.File;import javax.swing.BorderFactory;import javax.swing.DefaultListCellRenderer;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.filechooser.FileFilter;/** * This panel controls setting a list of algorithms for an experiment to * iterate over. * * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) * @version $Revision: 1.18 $ */public class AlgorithmListPanel extends JPanel implements ActionListener { /** * Class required to show the Classifiers nicely in the list */ public class ObjectCellRenderer extends DefaultListCellRenderer { /** * Return a component that has been configured to display the specified * value. That component's paint method is then called to "render" the * cell. If it is necessary to compute the dimensions of a list because * the list cells do not have a fixed size, this method is called to * generate a component on which getPreferredSize can be invoked. * * @param list The JList we're painting. * @param value The value returned by * list.getModel().getElementAt(index). * @param index The cells index. * @param isSelected True if the specified cell was selected. * @param cellHasFocus True if the specified cell has the focus. * @return A component whose paint() method will render * the specified value. */ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); String rep = value.getClass().getName(); int dotPos = rep.lastIndexOf('.'); if (dotPos != -1) { rep = rep.substring(dotPos + 1); } if (value instanceof OptionHandler) { rep += " " + Utils.joinOptions(((OptionHandler)value) .getOptions()); } setText(rep); return c; } } /** The experiment to set the algorithm list of */ protected Experiment m_Exp; /** The component displaying the algorithm list */ protected JList m_List; /** Click to add an algorithm */ protected JButton m_AddBut = new JButton("Add new..."); /** Click to edit the selected algorithm */ protected JButton m_EditBut = new JButton("Edit selected..."); /** Click to remove the selected dataset from the list */ protected JButton m_DeleteBut = new JButton("Delete selected"); /** Click to edit the load the options for athe selected algorithm */ protected JButton m_LoadOptionsBut = new JButton("Load options..."); /** Click to edit the save the options from selected algorithm */ protected JButton m_SaveOptionsBut = new JButton("Save options..."); /** Click to move the selected algorithm(s) one up */ protected JButton m_UpBut = new JButton("Up"); /** Click to move the selected algorithm(s) one down */ protected JButton m_DownBut = new JButton("Down"); /** The file chooser for selecting experiments */ protected JFileChooser m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); /** A filter to ensure only experiment (in XML format) files get shown in the chooser */ protected FileFilter m_XMLFilter = new ExtensionFileFilter(".xml", "Classifier options (*.xml)"); /** Whether an algorithm is added or only edited */ protected boolean m_Editing = false; /** Lets the user configure the classifier */ protected GenericObjectEditor m_ClassifierEditor = new GenericObjectEditor(true); /** The currently displayed property dialog, if any */ protected PropertyDialog m_PD; /** The list model used */ protected DefaultListModel m_AlgorithmListModel = new DefaultListModel(); /* Register the property editors we need */ static { GenericObjectEditor.registerEditors(); } /** * Creates the algorithm list panel with the given experiment. * * @param exp a value of type 'Experiment' */ public AlgorithmListPanel(Experiment exp) { this(); setExperiment(exp); } /** * Create the algorithm list panel initially disabled. */ public AlgorithmListPanel() { final AlgorithmListPanel self = this; m_List = new JList(); MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { final int index = m_List.locationToIndex(e.getPoint()); if ((e.getClickCount() == 2) && (e.getButton() == MouseEvent.BUTTON1)) { // unfortunately, locationToIndex only returns the nearest entry // and not the exact one, i.e. if there's one item in the list and // one doublelclicks somewhere in the list, this index will be // returned if (index > -1) actionPerformed(new ActionEvent(m_EditBut, 0, "")); } else if (e.getClickCount() == 1) { if ( (e.getButton() == MouseEvent.BUTTON3) || ((e.getButton() == MouseEvent.BUTTON1) && e.isAltDown() && e.isShiftDown()) ) { JPopupMenu menu = new JPopupMenu(); JMenuItem item; item = new JMenuItem("Add configuration..."); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = JOptionPane.showInputDialog( self, "Configuration (<classname> [<options>])"); if (str != null) { try { String[] options = Utils.splitOptions(str); String classname = options[0]; options[0] = ""; DefaultListModel model = (DefaultListModel) m_List.getModel(); Object obj = Utils.forName(Object.class, classname, options); model.addElement(obj); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( self, "Error parsing commandline:\n" + ex, "Error...", JOptionPane.ERROR_MESSAGE); } } } }); menu.add(item); if (m_List.getSelectedValue() != null) { menu.addSeparator(); item = new JMenuItem("Show properties..."); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { self.actionPerformed(new ActionEvent(m_EditBut, 0, "")); } }); menu.add(item); item = new JMenuItem("Copy configuration to clipboard"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = m_List.getSelectedValue().getClass().getName(); if (m_List.getSelectedValue() instanceof OptionHandler) str += " " + Utils.joinOptions(((OptionHandler) m_List.getSelectedValue()).getOptions()); StringSelection selection = new StringSelection(str.trim()); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(selection, selection); } }); menu.add(item); item = new JMenuItem("Enter configuration..."); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = JOptionPane.showInputDialog( self, "Configuration (<classname> [<options>])"); if (str != null) { try { String[] options = Utils.splitOptions(str); String classname = options[0]; options[0] = ""; DefaultListModel model = (DefaultListModel) m_List.getModel(); Object obj = Utils.forName(Object.class, classname, options); model.setElementAt(obj, index); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( self, "Error parsing commandline:\n" + ex, "Error...", JOptionPane.ERROR_MESSAGE); } } } }); menu.add(item); } menu.show(m_List, e.getX(), e.getY()); } } } }; m_List.addMouseListener(mouseListener); m_ClassifierEditor.setClassType(Classifier.class); m_ClassifierEditor.setValue(new weka.classifiers.rules.ZeroR()); m_ClassifierEditor.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -