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

📄 filemodel.java

📁 实现网格环境下资源调度和分配的仿真
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Title:        Visual Modeler for GridSim Toolkit * Description:  This Visual Modeler enables the user to quickly create *               experiments on different Grid testbeds and generate the *               default Grid Broker source codes (in Java). * * $Id: FileModel.java,v 1.6 2003/06/19 07:31:00 anthony Exp $ */package visualmodeler;import java.awt.Toolkit;import java.util.*;import java.io.*;import javax.swing.*;// to handle loading XML. These packages exist since java 1.4import org.w3c.dom.*;import javax.xml.parsers.*;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;/** * FileModel deals with the back-end operation of the VisualModeler system. * The operations handle by this class are: * <ul> * <li> saving new file in XML format * <li> opening new file in XML format * <li> generating Java code to be used in GridSim simulation * </ul> * * @author       Anthony Sulistio and Chee Shin Yeo * @version      1.1 * @invariant $none */public class FileModel extends Observable{    private DefaultFileFilter fileFilter_;    private JFrame frame_;    private UserModel user_;    private ResourceModel res_;    private final JFileChooser fc_;    private boolean hasSaved_;    private JOptionPane optionPane_;    private String fileName_;    private Toolkit toolkit_;    /**     * Allocates a new FileModel object     * @param frame  a JFrame object     * @param user   a UserModel object     * @param res    a ResourceModel object     * @pre frame != null     * @pre user != null     * @pre res != null     * @post $none     */    public FileModel(JFrame frame, UserModel user, ResourceModel res)    {        frame_ = frame;        user_ = user;        res_ = res;        fileName_ = null;        hasSaved_ = false;        toolkit_ = Toolkit.getDefaultToolkit();        fc_ = new JFileChooser();        fileFilter_ = new DefaultFileFilter("xml", "XML Files");        optionPane_ = new JOptionPane("Empty message",                                JOptionPane.QUESTION_MESSAGE,                                JOptionPane.YES_NO_OPTION);    }    /**     * Saves the Visual Modeler project file      * @pre $none     * @post $none     */    public void saveFile()    {        // if no changes to both user and resource model, then exit        if (user_.hasChanged() == false && res_.hasChanged() == false) {            return;        }        if (fileName_ != null)        {            save();            return;        }        fc_.setDialogTitle("Save");        fc_.setFileFilter(fileFilter_);        int returnVal = fc_.showSaveDialog(frame_);        if (returnVal == JFileChooser.APPROVE_OPTION) {            displaySaveDialog();        }    }    /**     * A method that asks the user to save the changes before exiting the     * program      * @pre $none     * @post $none     */    public void newFile()    {        // if the file has been saved before then open a new file and        // reset the flag        if (hasSaved_ == true)        {            hasSaved_ = false;            user_.newValue();            res_.newValue();            fileName_ = null;            return;        }        // if no changes to both user and resource model, then exit        if (user_.hasChanged() == false && res_.hasChanged() == false) {            return;        }        String msg="This file has not been saved yet.\nOpen a new file anyway?";        if (confirmAction(msg, "New File") == true)        {            user_.newValue();            res_.newValue();            hasSaved_ = false;            fileName_ = null;        }    }    /**     * A method that handles the dialog to open a new file      * @pre $none     * @post $none     */    public void openFile()    {        // if no changes to both user and resource model, then open        if (user_.hasChanged() == false && res_.hasChanged() == false)        {            displayOpenDialog();            return;        }        String msg="This file has not been saved yet.\nOpen a new file anyway?";        if (confirmAction(msg, "Open File") == true) {            displayOpenDialog();        }    }    /**     * A method that shows a close file dialog      * @pre $none     * @post $none     */    public void closeFile()    {        // if no changes to both user and resource model, then exit        if (user_.hasChanged() == false && res_.hasChanged() == false) {            return;        }        String msg = "The file has not been saved yet.\nClose anyway?";        if (confirmAction(msg, "Close File") == true)        {            user_.newValue();            res_.newValue();            fileName_ = null;        }    }    /**     * A method that shows a Save As dialog      * @pre $none     * @post $none     */    public void saveAsFile()    {        fc_.setDialogTitle("Save As");        fc_.setFileFilter(fileFilter_);        int returnVal = fc_.showSaveDialog(frame_);        if (returnVal == JFileChooser.APPROVE_OPTION) {            displaySaveDialog();        }    }    /**     * A method that asks the user whether to quit the program or not     * @return <tt>true</tt> if the user wants to quit, <tt>false</tt> otherwise     * @pre $none     * @post $none     */    public boolean quitProgram()    {        // if no changes to both user and resource model, then exit        if (user_.hasChanged() == false && res_.hasChanged() == false) {            return true;        }        String msg = "The file has not been saved yet.\nQuit anyway?";        if (confirmAction(msg, "Exit Program") == true)        {            user_.newValue();            res_.newValue();            return true;        }        return false;    }    /**     * Generates a Java source code      * @pre $none     * @post $none     */    public void generateCode()    {        // if no changes to both user and resource model, then exit        /******        if (user_.hasChanged() == false && res_.hasChanged() == false)        {            toolkit_.beep();            JOptionPane.showMessageDialog(frame_,                "Cannot generate GridSim code.\n" +                "No user or resource has been created.",                "Generate GridSim code",                JOptionPane.ERROR_MESSAGE);            return;        }        *************/        DefaultFileFilter filter = new DefaultFileFilter("java", "Java Files");        fc_.setDialogTitle("Generate GridSim code");        fc_.setFileFilter(filter);        int returnVal = fc_.showSaveDialog(frame_);        if (returnVal != JFileChooser.APPROVE_OPTION) {            return;        }        // get the name of the file and use it as a java class name        File file = fc_.getSelectedFile();        String className = file.getName();        className = fileFilter_.getFileNameOnly(className);        // Creates a string buffer with long chars to increase performance        StringBuffer buffer = new StringBuffer(1000);                 // this will get the absolute directory path + file name        buffer.append( file.getAbsolutePath() );        // if the filename has no extension, then attach        if (fileFilter_.getExtension(file) == null) {            buffer.append(".java");        }        // Copy the file name        String name = buffer.toString();        // if a file already exists, then double check with user        File newFile = new File(name);        if (newFile.exists() == true)        {            // reset the buffer            buffer.delete( 0, buffer.length() );            buffer.append(name);            buffer.append(" already exists.\nDo you want to replace it?");                        if ( confirmAction(buffer.toString(), "Generate GridSim code")                     == false )             {                return;            }        }        String indent = "    ";     // 4 spaces                // reset the buffer        buffer.delete( 0, buffer.length() );        // Writes the Java file header        buffer.append("// This file is auto-generated by VisualModeler.\n");        buffer.append("// Created on ");        Date date = Calendar.getInstance().getTime();        buffer.append( date.toString() );        buffer.append("\n\n");        buffer.append("import java.util.*;\n");        buffer.append("import gridsim.*;\n");        buffer.append("import gridbroker.*;\n");        // writes the class name and static method        buffer.append("\npublic class ");        buffer.append(className);        buffer.append(" \n{\n\n");        buffer.append(indent);        buffer.append("public static void main(String[] args)\n");        buffer.append(indent);        buffer.append("{\n\n");        // writes statements inside the main method

⌨️ 快捷键说明

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