MCPcopy Create free account
hub / github.com/OpenSourcePhysics/osp / saveXML

Method saveXML

src/demoJS/SaveXMLFile.java:22–71  ·  view source on GitHub ↗
(XMLControl xml)

Source from the content-addressed store, hash-verified

20 }
21
22 public void saveXML(XMLControl xml){
23 //String ext=".txt";
24 String ext=".xml";
25 JFileChooser chooser = OSPRuntime.getChooser();
26 //JFileChooser chooser = new JFileChooser();
27 String oldTitle = chooser.getDialogTitle();
28 chooser.setDialogTitle("Save XML File");
29 chooser.setCurrentDirectory(currentLocation);
30 int result = -1;
31 try {
32 // This works in JavaScript and Java, because both are modal for a save request
33 result = chooser.showSaveDialog(null);
34 } catch (Throwable e) {
35 e.printStackTrace();
36 }
37 chooser.setDialogTitle(oldTitle);
38 if(result==JFileChooser.APPROVE_OPTION) {
39 File file = chooser.getSelectedFile();
40 // check to see if file already exists
41
42 // BH It is not possible to see if a file that is going to be saved exists -- or, more
43 // Specifically, not relevant, since the browser will always add "(1)" or "(2)", etc. to
44 // a filename if one exists already. Also, the web page has no way of knowing what directory
45 // the file will be saved in, and it certainly cannot search it, even if it did know.
46
47 org.opensourcephysics.display.OSPRuntime.chooserDir = chooser.getCurrentDirectory().toString();
48 String fileName = file.getAbsolutePath();
49 if((fileName==null)||fileName.trim().equals("")) {
50 return;
51 }
52 int i = fileName.toLowerCase().lastIndexOf(ext);
53 if(i!=fileName.length()-4) {
54 fileName += ext;
55 file = new File(fileName);
56 }
57
58 // BH bypass, since it is not a relevant question in JavaScript
59 if(/** @j2sNative false && */file.exists()) {
60
61 // Again, this would not block in JavaScript. "selected" will be NaN.
62 int selected = JOptionPane.showConfirmDialog(null, "Replace existing "+file.getName()+"?", "Replace File",
63 JOptionPane.YES_NO_CANCEL_OPTION);
64 if(selected!=JOptionPane.YES_OPTION) {
65 return;
66 }
67 }
68 xml.write(fileName);
69 }
70
71}
72
73 public static void main(String[] args) {
74 XMLControlElement control= new XMLControlElement();

Callers 1

SaveXMLFileMethod · 0.95

Calls 11

getChooserMethod · 0.95
lengthMethod · 0.80
existsMethod · 0.80
showConfirmDialogMethod · 0.80
equalsMethod · 0.65
getNameMethod · 0.65
writeMethod · 0.65
showSaveDialogMethod · 0.45
toStringMethod · 0.45
getAbsolutePathMethod · 0.45
trimMethod · 0.45

Tested by

no test coverage detected