meka.gui.explorer.classify.OpenModel.java Source code

Java tutorial

Introduction

Here is the source code for meka.gui.explorer.classify.OpenModel.java

Source

/*
 *   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/>.
 */

/**
 * OpenModel.java
 * Copyright (C) 2016 University of Waikato, Hamilton, NZ
 */

package meka.gui.explorer.classify;

import meka.classifiers.multilabel.MultiLabelClassifier;
import meka.core.Result;
import meka.gui.choosers.MekaFileChooser;
import meka.gui.explorer.ClassifyTab;
import weka.core.Instances;
import weka.core.SerializationHelper;
import weka.gui.ExtensionFileFilter;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Menu item for loading a model from disk.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 * @version $Revision$
 */
public class OpenModel extends AbstractClassifyTabMenuItem {

    /** the session key for the model filechooser. */
    public final static String SESSION_KEY_MODELCHOOSER = "modelchooser";

    /**
     * Returns the group of the plugin. Used for the grouping the menu items.
     *
     * @return          the group
     */
    public String getGroup() {
        return "Model";
    }

    /**
     * Returns the name of the plugin. Used for the menu item text.
     *
     * @return          the name
     */
    public String getName() {
        return "Open model...";
    }

    /**
     * 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) {
                MekaFileChooser filechooser;
                if (!hasSessionValue(owner, SESSION_KEY_MODELCHOOSER)) {
                    filechooser = new MekaFileChooser();
                    ExtensionFileFilter filter = new ExtensionFileFilter(".model", "Model files (*.model)");
                    filechooser.addChoosableFileFilter(filter);
                    filechooser.setFileFilter(filter);
                    filechooser.setAcceptAllFileFilterUsed(true);
                    setSessionValue(owner, SESSION_KEY_MODELCHOOSER, filechooser);
                } else {
                    filechooser = (MekaFileChooser) getSessionValue(owner, SESSION_KEY_MODELCHOOSER);
                }

                int retVal = filechooser.showOpenDialog(owner.getOwner());
                if (retVal != MekaFileChooser.APPROVE_OPTION)
                    return;
                File model = filechooser.getSelectedFile();
                try {
                    Object[] objs = SerializationHelper.readAll(model.getAbsolutePath());
                    MultiLabelClassifier classifier = (MultiLabelClassifier) objs[0];
                    Instances data = null;
                    if (objs.length > 1)
                        data = (Instances) objs[1];
                    Result result = new Result();
                    owner.addResultToHistory(result, new Object[] { classifier, new Instances(data, 0) },
                            classifier.getClass().getName().replace("meka.classifiers.", ""));
                } catch (Exception ex) {
                    owner.handleException("Loading of model '" + model + "' failed:", ex);
                    JOptionPane.showMessageDialog(owner, "Loading of model '" + model + "' failed:\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) {

    }
}