⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelloader.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package jmt.gui.common.xml;

import jmt.gui.common.definitions.CommonModel;
import jmt.gui.common.definitions.ModelConverter;
import jmt.gui.common.definitions.StoredResultsModel;
import jmt.gui.exact.ExactModel;
import jmt.gui.exact.xml.XMLUtils;
import jmt.gui.jaba.JabaModel;
import org.w3c.dom.Document;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.File;
import java.util.Vector;

/**
 * <p>Title: Model Loader</p>
 * <p>Description: This class provides unified load/save functionality
 * for every JMT application</p>
 *
 * @author Bertoli Marco
 *         Date: 15-feb-2006
 *         Time: 15.36.51
 */
public class ModelLoader {
    /**
     * Filters for input files
     */
    public static final JmtFileFilter JMODEL = new JmtFileFilter(".jsimg;.jmodel","JSIMgraph data file");
    public static final JmtFileFilter JSIM = new JmtFileFilter(".jsimw;.jsim","JSIMwiz data file");
    public static final JmtFileFilter JMVA = new JmtFileFilter(".jmva","JMVA data file");
    public static final JmtFileFilter JABA = new JmtFileFilter(".jaba","JABA data file");
    public static final JmtFileFilter XML = new JmtFileFilter(".xml","Engine XML i/o file");
    public static final JmtFileFilter ALL = new JmtFileFilter(".jsimg;.jsimw;.jmva;.jaba;.xml;.jsim;.jmodel","All JMT data files");
    

    /**
     * Constants used for output
     */
    public static final int SUCCESS = 0;
    public static final int CANCELLED = 1;
    public static final int FAILURE = 2;
    public static final int WARNING = 3;

    /**
     * Constants to define xml types
     */
    protected static final int XML_SIM = 0;
    protected static final int XML_ARCHIVE = 1;
    protected static final int XML_MVA = 2;
    protected static final int XML_JABA = 5;
    protected static final int XML_RES_SIM = 3;
    protected static final int XML_RES_GUI = 4;
    protected static final int FILE_UNKNOWN = 255;

    /**
     * Failure motivations
     */
    protected static final String FAIL_UNKNOWN = "Unknown input file format";
    protected static final String FAIL_CONVERSION = "Input file is recognized but cannot be converted " +
            "to work within this application. Please open it with ";


    // Better to move this element elsewere...
    protected static final String XML_MVA_ROOT = "model";

    protected JmtFileChooser dialog;

    protected JmtFileFilter defaultFilter;

    // Motivation of last failure
    protected String failureMotivation;

    // Warnings found during last conversion
    protected Vector warnings = new Vector();

    protected XMLUtils xmlutils = new XMLUtils();

    /**
     * Initialize a new ModelLoader with specified default file filter (chosen between
     * constrants in this class)
     * @param defaultFilter default file filter for current application (used to detect application type)
     */
    public ModelLoader(JmtFileFilter defaultFilter) {
        this.defaultFilter = defaultFilter;
        // Initialize filechooser dialog
        dialog = new JmtFileChooser(defaultFilter);
    }

    /**
     * Gets the motivation of last failure
     * @return the motivation of last failure
     */
    public String getFailureMotivation() {
        return failureMotivation;
    }

    /**
     * Gets a vector containing warnings of last performed operation
     * @return a Vector of String with every found warning
     */
    public Vector getLastWarnings() {
        return warnings;
    }

// --- Methods used to load models ------------------------------------------------------------
    /**
     * Shows a Load window and loads chosen model inside specified model data file
     * @param modelData data file where information should be stored. Note that <b>its type
     * must be compatible with defaultFilter chosen in the constructor</b>, otherwise a
     * ClassCastException will be thrown
     * @param parent parent component of loading window
     * @return SUCCESS on success, CANCELLED if loading is cancelled,
     * FAILURE if an error occurs and WARNING is one or more warnings are generated due to
     * format conversion
     * @throws ClassCastException if modelData is not of instance of the correct class
     * @see #getFailureMotivation getFailureMotivation()
     */
    public int loadModel(Object modelData, Component parent) {
        warnings.clear();
        int status;
        status = this.showOpenDialog(parent);
        if (status == JmtFileChooser.CANCEL_OPTION)
            return CANCELLED;
        else if (status == JmtFileChooser.ERROR_OPTION) {
            failureMotivation = "Error selecting input file";
            return FAILURE;
        }

        File file = dialog.getSelectedFile();

        try {
            if (defaultFilter == JMODEL || defaultFilter == JSIM) {
                StoredResultsModel srm;
                // Handles loading of JSIM/JMODEL models
                switch (getXmlFileType(file.getAbsolutePath())) {
                    case XML_SIM:
                        XMLReader.loadModel(file, (CommonModel) modelData);
                        break;
                    case XML_ARCHIVE:
                        XMLArchiver.loadModel((CommonModel) modelData, file);
                        break;
                    case XML_MVA:
                        ExactModel tmp = new ExactModel();
                        tmp.loadDocument(xmlutils.loadXML(file));
                        warnings.addAll(ModelConverter.convertJMVAtoJSIM(tmp, (CommonModel)modelData));
                        break;
                    case XML_JABA:
                        //TODO implement bridge JABA --> JSIM
                        failureMotivation = FAIL_CONVERSION + "JABA.";
                        return FAILURE;
                    case XML_RES_SIM:
                        srm = new StoredResultsModel();
                        XMLResultsReader.loadModel(srm, file);
                        ((CommonModel)modelData).setSimulationResults(srm);
                        warnings.add("Loaded file contained simulation results only. Associated queuing network model is not available. " +
                                "Results can be shown by selecting \"Show Results\" icon.");
                        break;
                    case XML_RES_GUI:
                        srm = new StoredResultsModel();
                        XMLResultsReader.loadGuiModel(srm, file);
                        ((CommonModel)modelData).setSimulationResults(srm);
                        warnings.add("Loaded file contained simulation results only. Associated queuing network model is not available. " +
                                "Results can be shown by selecting \"Show Results\" icon.");
                        break;
                    default:
                        failureMotivation = FAIL_UNKNOWN;
                        return FAILURE;
                }
            }
            else if (defaultFilter == JMVA) {
                // Handles loading of JMVA models
                CommonModel tmp = new CommonModel();
                switch (getXmlFileType(file.getAbsolutePath())) {
                    case XML_SIM:
                        XMLReader.loadModel(file, tmp);
                        warnings.addAll(ModelConverter.convertJSIMtoJMVA(tmp, (ExactModel)modelData));
                        break;
                    case XML_ARCHIVE:
                        XMLArchiver.loadModel(tmp, file);
                        warnings.addAll(ModelConverter.convertJSIMtoJMVA(tmp, (ExactModel)modelData));
                        break;
                    case XML_JABA:
                        //TODO implement bridge JABA --> JMVA
                        failureMotivation = FAIL_CONVERSION + "JABA.";
                        return FAILURE;
                    case XML_MVA:
                        ((ExactModel)modelData).loadDocument(xmlutils.loadXML(file));
                        break;
                    case XML_RES_SIM:
                    case XML_RES_GUI:
                        // This is silly to be opened in JMVA...
                        failureMotivation = FAIL_CONVERSION + "JSIM or JMODEL.";
                        return FAILURE;
                    default:
                        failureMotivation = FAIL_UNKNOWN;
                        return FAILURE;
                }
            }
            else if (defaultFilter == JABA) {
                // Handles loading of JABA models
                switch (getXmlFileType(file.getAbsolutePath())) {
                    case XML_SIM:
                        //TODO implement bridge JSIM --> JABA
                    case XML_ARCHIVE:
                        //TODO implement bridge JSIM --> JABA
                        failureMotivation = FAIL_CONVERSION + "JSIM or JMODEL.";
                        return FAILURE;
                    case XML_MVA:
                        //TODO implement bridge JMVA --> JABA
                        //failureMotivation = FAIL_CONVERSION + "JMVA.";
                        //return FAILURE;
                    case XML_JABA:
                        ((JabaModel)modelData).loadDocument(xmlutils.loadXML(file));
                        break;
                    case XML_RES_SIM:
                    case XML_RES_GUI:
                        // This is silly to be opened in JABA...
                        failureMotivation = FAIL_CONVERSION + "JSIM or JMODEL.";
                        return FAILURE;
                    default:
                        failureMotivation = FAIL_UNKNOWN;
                        return FAILURE;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            failureMotivation = e.getClass().getName() + ": " + e.getMessage();
            return FAILURE;
        }
        // If no warnings were found, report success
        if (warnings.size() > 0)
            return WARNING;
        else
            return SUCCESS;
    }

    /**
     * Returns name of selected file for i/o operations
     * @return name of selected file for open or save
     */
    public File getSelectedFile() {
        return dialog.getSelectedFile();
    }

// --------------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -