Java tutorial
/* * 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 3 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, see <http://www.gnu.org/licenses/>. */ /** * OpenTestData.java * Copyright (C) 2016 University of Waikato, Hamilton, NZ */ package meka.gui.explorer.classify; import meka.gui.core.GUIHelper; import meka.gui.explorer.ClassifyTab; import weka.core.Instances; import weka.core.converters.AbstractFileLoader; import weka.gui.ConverterFileChooser; import weka.gui.ExtensionFileFilter; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Menu item for loading a model from disk. * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class OpenTestData extends AbstractClassifyTabMenuItem { /** the session key for the file filechooser. */ public final static String SESSION_KEY_FILECHOOSER = "filechooser"; /** * Returns the group of the plugin. Used for the grouping the menu items. * * @return the group */ public String getGroup() { return "Test data"; } /** * Returns the name of the plugin. Used for the menu item text. * * @return the name */ public String getName() { return "Open test data..."; } /** * Returns the name of the icon to use. * * @return the name of the icon, null if none to use */ public String getIcon() { return "open.gif"; } /** * Returns the action lister to use in the menu. * * @return the listener */ public ActionListener getActionListener(final ClassifyTab owner) { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ConverterFileChooser filechooser; if (!hasSessionValue(owner, SESSION_KEY_FILECHOOSER)) { filechooser = GUIHelper.newConverterFileChooser(); ExtensionFileFilter filter = new ExtensionFileFilter(".model", "Model files (*.model)"); filechooser.addChoosableFileFilter(filter); filechooser.setFileFilter(filter); filechooser.setAcceptAllFileFilterUsed(true); setSessionValue(owner, SESSION_KEY_FILECHOOSER, filechooser); } else { filechooser = (ConverterFileChooser) getSessionValue(owner, SESSION_KEY_FILECHOOSER); } if (filechooser.showOpenDialog(owner) != ConverterFileChooser.APPROVE_OPTION) return; AbstractFileLoader loader = filechooser.getLoader(); try { Instances data = loader.getDataSet(); if (data != null) owner.setTestData(data); } catch (Exception ex) { String msg = "Failed to load file: " + filechooser.getSelectedFile(); System.err.println(msg); ex.printStackTrace(); JOptionPane.showMessageDialog(owner, msg + "\n" + ex, "Error", JOptionPane.ERROR_MESSAGE); } } }; } /** * Updates the menu item using the current state of the tab. * * @param owner the tab that the menu item belongs to * @param menuitem the menu item to update (was generated by this class) */ @Override public void update(ClassifyTab owner, JMenuItem menuitem) { } }