📄 filemodel.java
字号:
String doubleIndent = " "; // 8 spaces buffer.append(doubleIndent); buffer.append("try\n"); buffer.append(doubleIndent); buffer.append("{\n"); String tripleIndent = " "; // now become 12 spaces buffer.append(tripleIndent); buffer.append("Calendar calendar = Calendar.getInstance();\n"); buffer.append(tripleIndent); buffer.append("Random random;\n"); buffer.append(tripleIndent); buffer.append("long seed = 11L*13*17*19*23+1;\n"); buffer.append(tripleIndent); buffer.append("seed = seed * 97 + 1;\n\n"); buffer.append(tripleIndent); buffer.append("String[] eff = {\"\"};\n"); buffer.append(tripleIndent); buffer.append("String[] efp = {\"\"};\n"); buffer.append(tripleIndent); buffer.append("String[] category = { \"*.USER.GridletCompletion\",\n"); buffer.append(indent); buffer.append(tripleIndent); buffer.append("\"*.USER.TimeUtilization\", "); buffer.append("\"*.USER.BudgetUtilization\" };\n\n"); buffer.append(tripleIndent); buffer.append("String ReportWriterName = \"ReportWriter"); buffer.append(className); buffer.append("\";\n\n"); // writes gridsim initialization int userNum = user_.getTotalUser(); int resNum = res_.getTotalResource(); buffer.append(tripleIndent); buffer.append("// Initializing GridSim for "); buffer.append(userNum); buffer.append(" grid users and "); buffer.append(resNum); buffer.append(" grid resources\n"); buffer.append(tripleIndent); buffer.append("GridSim.init("); buffer.append(userNum); buffer.append(", calendar, true, eff, efp, ReportWriterName);\n\n"); buffer.append(tripleIndent); buffer.append("// Creates Report Writer entity\n"); buffer.append(tripleIndent); buffer.append("new ReportWriter(ReportWriterName, "); buffer.append(userNum); buffer.append(", "); buffer.append(resNum); buffer.append(", \"reportFile"); buffer.append(className); buffer.append("\",\n"); buffer.append(indent); buffer.append(tripleIndent); buffer.append("category, false, null, true, 10000);\n\n"); // writes grid resources code buffer.append( res_.generateCode(tripleIndent) ); // writes grid users code buffer.append( user_.generateCode(tripleIndent) ); buffer.append("\n"); buffer.append(tripleIndent); buffer.append("// Starts grid simulation\n"); buffer.append(tripleIndent); buffer.append("GridSim.startGridSimulation();\n"); // writes catch clause buffer.append(doubleIndent); buffer.append("}\n"); buffer.append(doubleIndent); buffer.append("catch (Exception e) {\n"); buffer.append(tripleIndent); buffer.append("e.printStackTrace();\n"); buffer.append(doubleIndent); buffer.append("}\n } \n} \n\n"); // end of main and class try { FileOutputStream out = new FileOutputStream(name); BufferedOutputStream outBuffer = new BufferedOutputStream(out); outBuffer.write( buffer.toString().getBytes() ); outBuffer.close(); } catch(Exception e) { toolkit_.beep(); JOptionPane.showMessageDialog(frame_, "Sorry, cannot generate Java file.", "Generate GridSim code", JOptionPane.ERROR_MESSAGE); } toolkit_.beep(); JOptionPane.showMessageDialog(frame_, "Finish generating Java code.", "Generate GridSim code", JOptionPane.INFORMATION_MESSAGE); } /////////////////////////////// PRIVATE METHODS //////////////////////// /** * @param msg A message * @param type type of action * @return <tt>true</tt> if user wants to save, <tt>false</tt> otherwise * @pre msg != null * @pre type != null * @post $none */ private boolean confirmAction(String msg, String type) { optionPane_.setMessage(msg); JDialog dialog = optionPane_.createDialog(frame_, type); toolkit_.beep(); dialog.show(); // put try and catch, just in-case the something wrong with casting try { int value = ( (Integer) optionPane_.getValue() ).intValue(); if (value == JOptionPane.YES_OPTION) { return true; } } catch (Exception e) { System.out.println("FileModel.confirmAction() : " + e.getMessage()); } return false; } /** * A method that displays the open dialog * @pre $none * @post $none */ private void displayOpenDialog() { fc_.setDialogTitle("Open"); fc_.setFileFilter(fileFilter_); int returnVal = fc_.showOpenDialog(frame_); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc_.getSelectedFile(); // clear everything user_.newValue(); res_.newValue(); fileName_ = file.getAbsolutePath(); // if the filename has no extension, then attach String ext = fileFilter_.getExtension(file); if (ext == null) { fileName_ += ".xml"; } loadXmlFile(fileName_); } } /** * A method that reads the given XML (eXtensible Markup Language) file * @param fileName A file name * @pre fileName != null * @post $none */ private void loadXmlFile(String fileName) { // Create an instance of the DocumentBuilderFactory DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance(); try { //Get the DocumentBuilder from the factory that we just got above. DocumentBuilder docBuilder = docFac.newDocumentBuilder(); // turn it into an in-memory object Document doc = docBuilder.parse(fileName); // root element, i.e <gridsimProject> Element root = doc.getDocumentElement(); NodeList nodeList = root.getChildNodes(); Node node; String name; int length = nodeList.getLength(); for (int i = 0; i < length; i++) { node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { name = node.getNodeName(); NodeList childList = node.getChildNodes(); if (name.equals("gridUser") == true) { user_.loadXml(childList); } else if (name.equals("gridResource") == true) { res_.loadXml(childList); } } } } catch (Exception ex) { System.out.println("FileModel.loadXMLFile() : Error - unable to " + "open and/or read \"" + fileName + "\""); } } /** * A method that displays a save dialog * @pre $none * @post $none */ private void displaySaveDialog() { File file = fc_.getSelectedFile(); // this will get the absolute directory path + file name fileName_ = file.getAbsolutePath(); // if the filename has no extension, then attach String ext = fileFilter_.getExtension(file); if (ext == null) { fileName_ += ".xml"; } // if a file already exists, then double check with user File newFile = new File(fileName_); if (newFile.exists() == true) { StringBuffer msg = new StringBuffer(); msg.append(fileName_); msg.append(" already exists.\n"); msg.append("Do you want to replace it?"); if (confirmAction(msg.toString(), "Save File") == false) { fileName_ = null; return; } } save(); } /** * Saves the Visual Modeler project file into XML format * @pre $none * @post $none */ private void save() { String indent = " "; // 4 spaces StringBuffer xml = new StringBuffer(1000); xml.append("<?xml version=\"1.0\" standalone=\"yes\"?>\n\n"); xml.append("<!-- This file is auto-generated by VisualModeler.\n"); xml.append(" Created on "); Date date = Calendar.getInstance().getTime(); xml.append( date.toString() ); xml.append("\n-->\n\n"); xml.append("\n<gridsimProject>"); // saving grid user xml.append( user_.saveFile(indent) ); // saving grid resource xml.append( res_.saveFile(indent) ); xml.append("\n</gridsimProject>\n"); try { FileOutputStream out = new FileOutputStream(fileName_); out.write( xml.toString().getBytes() ); out.close(); hasSaved_ = true; setChanged(); notifyObservers("save"); } catch(Exception e) { toolkit_.beep(); JOptionPane.showMessageDialog(frame_, "Sorry, cannot save this file.", "Saving error", JOptionPane.ERROR_MESSAGE); } toolkit_.beep(); JOptionPane.showMessageDialog(frame_, "Finish saving file.", "Saving file success", JOptionPane.INFORMATION_MESSAGE); }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -