Saves the text in this TextPanel to a file. Set 'path' to "" to display a "save as" dialog. Returns 'false' if the user cancels the dialog.
(String path)
| 1059 | * the dialog. |
| 1060 | */ |
| 1061 | @AstroImageJ(reason = "Add check for AIJ window; Save table with 16 decimal places, not 6", modified = true) |
| 1062 | public boolean saveAs(String path) { |
| 1063 | boolean isResults = IJ.isResultsWindow() && IJ.getTextPanel()==this; |
| 1064 | boolean summarized = false; |
| 1065 | if (isResults) { |
| 1066 | String lastLine = iRowCount>=2?getLine(iRowCount-2):null; |
| 1067 | summarized = lastLine!=null && lastLine.startsWith("Max"); |
| 1068 | } |
| 1069 | String fileName = null; |
| 1070 | if (rt!=null && rt.size()>0 && !summarized) { |
| 1071 | if (path==null || path.equals("")) { |
| 1072 | IJ.wait(10); |
| 1073 | String name = isResults?"Results":title.replace("Measurements in ", ""); |
| 1074 | SaveDialog sd = new SaveDialog("Save Table", name, Prefs.defaultResultsExtension()); |
| 1075 | fileName = sd.getFileName(); |
| 1076 | if (fileName==null) |
| 1077 | return false; |
| 1078 | path = sd.getDirectory() + fileName; |
| 1079 | } |
| 1080 | var oldPrecision = rt.getPrecision(); |
| 1081 | rt.setPrecision(16); |
| 1082 | rt.saveAndRename(path); |
| 1083 | rt.setPrecision(oldPrecision); |
| 1084 | TextWindow tw = getTextWindow(); |
| 1085 | String title2 = rt.getTitle(); |
| 1086 | if (tw!=null && !"Results".equals(title)) { |
| 1087 | tw.setTitle(title2); |
| 1088 | Menus.updateWindowMenuItem(title, title2); |
| 1089 | title = title2; |
| 1090 | } |
| 1091 | } else { |
| 1092 | if (path.equals("")) { |
| 1093 | IJ.wait(10); |
| 1094 | boolean hasHeadings = !getColumnHeadings().equals(""); |
| 1095 | String ext = isResults||hasHeadings?Prefs.defaultResultsExtension():".txt"; |
| 1096 | SaveDialog sd = new SaveDialog("Save as Text", title, ext); |
| 1097 | String file = sd.getFileName(); |
| 1098 | if (file==null) |
| 1099 | return false; |
| 1100 | path = sd.getDirectory() + file; |
| 1101 | } |
| 1102 | PrintWriter pw = null; |
| 1103 | try { |
| 1104 | FileOutputStream fos = new FileOutputStream(path); |
| 1105 | BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1106 | pw = new PrintWriter(bos); |
| 1107 | } |
| 1108 | catch (IOException e) { |
| 1109 | IJ.error("Save As>Text", e.getMessage()); |
| 1110 | return true; |
| 1111 | } |
| 1112 | saveAsCSV = path.endsWith(".csv"); |
| 1113 | save(pw); |
| 1114 | saveAsCSV = false; |
| 1115 | pw.close(); |
| 1116 | } |
| 1117 | if (isResults) { |
| 1118 | Analyzer.setUnsavedMeasurements(false); |
no test coverage detected