📄 modelloader.java
字号:
// --- Methods used to save models ------------------------------------------------------------
/**
* Saves specified model into specified file or shows save as window if file is null
* @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 window that will own the save as dialog
* @param file location where pecified model must be saved or null if save as must be shown
* @return SUCCESS on success, CANCELLED if loading is cancelled,
* FAILURE if an error occurs
* @throws ClassCastException if modelData is not of instance of the correct class
* @see #getFailureMotivation getFailureMotivation()
*/
public int saveModel (Object modelData, Component parent, File file) {
if (file == null) {
// Shows save as window
int status;
status = this.showSaveDialog(parent);
if (status == JmtFileChooser.CANCEL_OPTION)
return CANCELLED;
else if (status == JmtFileChooser.ERROR_OPTION) {
failureMotivation = "Error selecting output file";
return FAILURE;
}
file = dialog.getSelectedFile();
}
else
// Check extension to avoid saving over a converted file
if (!file.getName().endsWith(defaultFilter.getFirstExtension())) {
int resultValue = JOptionPane.showConfirmDialog(parent,
"<html>File <font color=#0000ff>" + file.getName() + "</font> does not have \"" + defaultFilter.getFirstExtension() + "\" extension.<br>Do you want to replace it anyway?</html>",
"JMT - Warning",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (resultValue != JOptionPane.OK_OPTION)
return CANCELLED;
}
// Now checks to save correct type of model
try {
if (defaultFilter == JMODEL || defaultFilter == JSIM) {
XMLArchiver.saveModel(file, (CommonModel) modelData);
}
else if (defaultFilter == JMVA) {
xmlutils.saveXML(((ExactModel)modelData).createDocument(), file);
}
else if (defaultFilter == JABA) {
xmlutils.saveXML(((JabaModel)modelData).createDocument(), file);
}
} catch (Exception e) {
failureMotivation = e.getClass().getName() + ": " + e.getMessage();
return FAILURE;
}
return SUCCESS;
}
// --------------------------------------------------------------------------------------------
// --- Methods to open and parse files --------------------------------------------------------
protected int getXmlFileType(String fileName) {
// Opens without validating (as we don't know document type)
try {
Document doc = XMLReader.loadXML(fileName);
String root = doc.getDocumentElement().getNodeName();
// Uses root name to determine document type
if (root.equals(XMLConstantNames.XML_DOCUMENT_ROOT))
return XML_SIM;
else if (root.equals(GuiXMLConstants.XML_ARCHIVE_DOCUMENT_ROOT))
return XML_ARCHIVE;
else if (root.equals(ModelLoader.XML_MVA_ROOT)) {
// Finds if this is JMVA or JABA model
String jaba = doc.getDocumentElement().getAttribute("jaba");
if (jaba != null && jaba.equals("true"))
return XML_JABA;
else
return XML_MVA;
}
else if (root.equals(XMLResultsConstants.XML_DOCUMENT_O_ROOT))
return XML_RES_SIM;
else if (root.equals(XMLResultsConstants.XML_DOCUMENT_ROOT))
return XML_RES_GUI;
else
return FILE_UNKNOWN;
} catch (Exception e) {
// If an exception is thrown, reports that format is unknown
return FILE_UNKNOWN;
}
}
// --------------------------------------------------------------------------------------------
// --- Methods to show dialogs ----------------------------------------------------------------
/**
* Adds all filters to current dialog
*/
protected void addAllFilters() {
dialog.setAcceptAllFileFilterUsed(true);
dialog.addChoosableFileFilter(ALL);
dialog.addChoosableFileFilter(JMODEL);
dialog.addChoosableFileFilter(JSIM);
dialog.addChoosableFileFilter(JMVA);
dialog.addChoosableFileFilter(JABA);
dialog.addChoosableFileFilter(XML);
//dialog.setFileFilter(defaultFilter);
dialog.setFileFilter(ALL);
}
/**
* Shows open file dialog
* @param parent parent component for this dialog
* @return the return state of the file chooser on popdown:
* <ul>
* <li>JFileChooser.CANCEL_OPTION
* <li>JFileChooser.APPROVE_OPTION
* <li>JFileCHooser.ERROR_OPTION if an error occurs or the
* dialog is dismissed
* </ul>
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
*/
protected int showOpenDialog(Component parent) {
addAllFilters();
return dialog.showOpenDialog(parent);
}
/**
* Shows save file dialog
* @param parent the parent component of the dialog,
* can be <code>null</code>;
* see <code>showDialog</code> for details
* @return the return state of the file chooser on popdown:
* <ul>
* <li>JFileChooser.CANCEL_OPTION
* <li>JFileChooser.APPROVE_OPTION
* <li>JFileCHooser.ERROR_OPTION if an error occurs or the
* dialog is dismissed
* </ul>
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
*/
protected int showSaveDialog(Component parent) {
dialog.resetChoosableFileFilters();
dialog.addChoosableFileFilter(defaultFilter);
dialog.setFileFilter(defaultFilter);
return dialog.showSaveDialog(parent);
}
// --------------------------------------------------------------------------------------------
// --- Inner classes --------------------------------------------------------------------------
/**
* Custom file chooser class
*/
protected static class JmtFileChooser extends JFileChooser {
protected JmtFileFilter defaultFilter;
/**
* Creates a File chooser in the appropriate directory user deafault.
* @param defaultFilter default file filter
*/
public JmtFileChooser(JmtFileFilter defaultFilter) {
super(new File(System.getProperty("user.dir")));
this.defaultFilter = defaultFilter;
}
/**
* Overrides default method to provide a warning if saving over an existing file
*/
public void approveSelection() {
// Gets the choosed file name
String name = getSelectedFile().getName();
String parent = getSelectedFile().getParent();
if (getDialogType() == OPEN_DIALOG) {
super.approveSelection();
}
if (getDialogType() == SAVE_DIALOG) {
if (!name.toLowerCase().endsWith(defaultFilter.getFirstExtension())) {
name = name + defaultFilter.getFirstExtension();
setSelectedFile(new File(parent, name));
}
if (getSelectedFile().exists()) {
int resultValue = JOptionPane.showConfirmDialog(this,
"<html>File <font color=#0000ff>" + name + "</font> already exists in this folder.<br>Do you want to replace it?</html>",
"JMT - Warning",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (resultValue == JOptionPane.OK_OPTION) {
getSelectedFile().delete();
super.approveSelection();
}
} else {
super.approveSelection();
}
}
}
}
/**
* Inner class used to create simple file filters with only extension check
*/
protected static class JmtFileFilter extends FileFilter {
public static final String SEP = ";";
private String description;
private String[] extensions;
/**
* Creates a new filefilter with specified dot and comma separated list of
* extensions and specified description
* @param extensionList comma separated list of extensions
* of this filter (for example ".jmt;.jmodel")
* @param description description of this filter
*/
public JmtFileFilter(String extensionList, String description) {
this.extensions = extensionList.split(SEP);
this.description = createDescription(description, extensions);
}
/**
* Whether the given file is accepted by this filter.
*/
public boolean accept(File f) {
String name = f.getName().toLowerCase();
if (f.isDirectory())
return true;
for (int i=0;i<extensions.length;i++)
if (name.endsWith(extensions[i]))
return true;
return false;
}
/**
* Creates a suitable description for this filter
* @param text text description of the filter
* @param extensions extensions to be matched
* @return created description
*/
private String createDescription(String text, String[] extensions) {
StringBuffer sb = new StringBuffer();
sb.append(text);
sb.append(" (*");
for (int i=0; i<extensions.length - 1; i++) {
sb.append(extensions[i]);
sb.append("; *");
}
sb.append(extensions[extensions.length - 1]);
sb.append(")");
return sb.toString();
}
/**
* The description of this filter
* @see javax.swing.filechooser.FileView#getName
*/
public String getDescription() {
return description;
}
/**
* Gets the first extension of this filter
* @return extension of this filter
*/
public String getFirstExtension() {
return extensions[0];
}
}
// --------------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -