(XMLControl xml)
| 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(); |
no test coverage detected