1 package jspecview.export; 2 3 import java.io.BufferedReader; 4 5 import org.jmol.api.GenericFileInterface; 6 7 import javajs.util.OC; 8 import javajs.util.Lst; 9 import javajs.util.PT; 10 11 12 import jspecview.api.ExportInterface; 13 import jspecview.api.JSVExporter; 14 import jspecview.api.JSVFileHelper; 15 import jspecview.api.JSVPanel; 16 import jspecview.common.ExportType; 17 import jspecview.common.Spectrum; 18 import jspecview.common.JSVFileManager; 19 import jspecview.common.JSViewer; 20 import jspecview.common.PanelData; 21 import jspecview.common.PrintLayout; 22 import jspecview.common.Annotation.AType; 23 24 public class Exporter implements ExportInterface { 25 26 static final String newLine = System.getProperty("line.separator"); 27 Exporter()28 public Exporter() { 29 // for reflection; called directly only from MainFrame 30 } 31 32 @Override write(JSViewer viewer, Lst<String> tokens, boolean forInkscape)33 public String write(JSViewer viewer, Lst<String> tokens, boolean forInkscape) { 34 // MainFrame or applet WRITE command 35 if (tokens == null) 36 return printPDF(viewer, null, false); 37 38 String type = null; 39 String fileName = null; 40 ExportType eType; 41 OC out; 42 JSVPanel jsvp = viewer.selectedPanel; 43 try { 44 switch (tokens.size()) { 45 default: 46 return "WRITE what?"; 47 case 1: 48 fileName = PT.trimQuotes(tokens.get(0)); 49 if (fileName.indexOf(".") >= 0) 50 type = "XY"; 51 if (jsvp == null) 52 return null; 53 eType = ExportType.getType(fileName); 54 switch (eType) { 55 case PDF: 56 case PNG: 57 case JPG: 58 return exportTheSpectrum(viewer, eType, null, null, -1, -1, null, false); 59 default: 60 // select a spectrum 61 viewer.fileHelper.setFileChooser(eType); 62 String[] items = getExportableItems(viewer, eType.equals(ExportType.SOURCE)); 63 int index = (items == null ? -1 : viewer.getOptionFromDialog(items, "Export", "Choose a spectrum to export")); 64 if (index == Integer.MIN_VALUE) 65 return null; 66 GenericFileInterface file = viewer.fileHelper.getFile(getSuggestedFileName(viewer, eType), jsvp, true); 67 if (file == null) 68 return null; 69 out = viewer.getOutputChannel(file.getFullPath(), false); 70 String msg = exportSpectrumOrImage(viewer, eType, index, out); 71 boolean isOK = msg.startsWith("OK"); 72 if (isOK) 73 viewer.si.siUpdateRecentMenus(file.getFullPath()); 74 out.closeChannel(); 75 return msg; 76 } 77 case 2: 78 type = tokens.get(0).toUpperCase(); 79 fileName = PT.trimQuotes(tokens.get(1)); 80 break; 81 } 82 String ext = fileName.substring(fileName.lastIndexOf(".") + 1) 83 .toUpperCase(); 84 if (ext.equals("BASE64")) { 85 fileName = ";base64,"; 86 } else if (ext.equals("JDX")) { 87 if (type == null) 88 type = "XY"; 89 } else if (ExportType.isExportMode(ext)) { 90 type = ext; 91 } else if (ExportType.isExportMode(type)) { 92 fileName += "." + type; 93 } 94 eType = ExportType.getType(type); 95 if (forInkscape && eType == ExportType.SVG) 96 eType = ExportType.SVGI; 97 98 out = viewer.getOutputChannel(fileName, false); 99 return exportSpectrumOrImage(viewer, eType, -1, out); 100 } catch (Exception e) { 101 System.out.println(e); 102 return null; 103 } 104 } 105 106 /** 107 * 108 * This export method will clip the data based on the current display 109 * 110 * @param viewer 111 * @param eType 112 * @param index 113 * @param out 114 * @return status line message 115 */ exportSpectrumOrImage(JSViewer viewer, ExportType eType, int index, OC out)116 private String exportSpectrumOrImage(JSViewer viewer, ExportType eType, 117 int index, OC out) { 118 Spectrum spec; 119 PanelData pd = viewer.pd(); 120 if (index < 0 && (index = pd.getCurrentSpectrumIndex()) < 0) 121 return "Error exporting spectrum: No spectrum selected"; 122 spec = pd.getSpectrumAt(index); 123 int startIndex = pd.getStartingPointIndex(index); 124 int endIndex = pd.getEndingPointIndex(index); 125 String msg = null; 126 try { 127 boolean asBase64 = out.isBase64(); 128 msg = exportTheSpectrum(viewer, eType, out, spec, startIndex, endIndex, pd, asBase64); 129 if (asBase64) 130 return msg; 131 if (msg.startsWith("OK")) 132 return "OK - Exported " + eType.name() + ": " + out.getFileName() + msg.substring(2); 133 } catch (Exception ioe) { 134 msg = ioe.toString(); 135 } 136 return "Error exporting " + out.getFileName() + ": " + msg; 137 } 138 139 @Override exportTheSpectrum(JSViewer viewer, ExportType mode, OC out, Spectrum spec, int startIndex, int endIndex, PanelData pd, boolean asBase64)140 public String exportTheSpectrum(JSViewer viewer, ExportType mode, 141 OC out, Spectrum spec, int startIndex, int endIndex, 142 PanelData pd, boolean asBase64) throws Exception { 143 JSVPanel jsvp = viewer.selectedPanel; 144 String type = mode.name(); 145 switch (mode) { 146 case AML: 147 case CML: 148 case SVG: 149 case SVGI: 150 break; 151 case DIF: 152 case DIFDUP: 153 case FIX: 154 case PAC: 155 case SQZ: 156 case XY: 157 type = "JDX"; 158 break; 159 case JPG: 160 case PNG: 161 if (jsvp == null) 162 return null; 163 viewer.fileHelper.setFileChooser(mode); 164 String name = getSuggestedFileName(viewer, mode); 165 GenericFileInterface file = viewer.fileHelper.getFile(name, jsvp, true); 166 if (file == null) 167 return null; 168 return jsvp.saveImage(type.toLowerCase(), file, out); 169 case PDF: 170 return printPDF(viewer, "PDF", asBase64); 171 case SOURCE: 172 if (jsvp == null) 173 return null; 174 String data = jsvp.getPanelData().getSpectrum().getInlineData(); 175 if (data != null) { 176 out.append(data); 177 out.closeChannel(); 178 return "OK " + out.getByteCount() + " bytes"; 179 } 180 String path = jsvp.getPanelData().getSpectrum().getFilePath(); 181 return fileCopy(path, out); 182 case UNK: 183 return null; 184 } 185 return ((JSVExporter) JSViewer.getInterface("jspecview.export." 186 + type.toUpperCase() + "Exporter")).exportTheSpectrum(viewer, mode, 187 out, spec, startIndex, endIndex, null, false); 188 } 189 190 @SuppressWarnings("resource") printPDF(JSViewer viewer, String pdfFileName, boolean isBase64)191 private String printPDF(JSViewer viewer, String pdfFileName, boolean isBase64) { 192 193 boolean isJob = (pdfFileName == null || pdfFileName.length() == 0); 194 if (!isBase64 && !viewer.si.isSigned()) 195 return "Error: Applet must be signed for the PRINT command."; 196 PanelData pd = viewer.pd(); 197 if (pd == null) 198 return null; 199 boolean useDialog = false; 200 PrintLayout pl; 201 /** 202 * @j2sNative 203 * 204 * useDialog = false; 205 * 206 */ 207 { 208 pd.closeAllDialogsExcept(AType.NONE); 209 useDialog = true; 210 } 211 pl = viewer.getDialogPrint(isJob); 212 if (pl == null) 213 return null; 214 if (!useDialog) 215 pl.asPDF = true; // JavaScript only 216 if (isJob && pl.asPDF) { 217 isJob = false; 218 pdfFileName = "PDF"; 219 } 220 JSVPanel jsvp = viewer.selectedPanel; 221 if (!isBase64 && !isJob) { 222 JSVFileHelper helper = viewer.fileHelper; 223 helper.setFileChooser(ExportType.PDF); 224 if (pdfFileName.equals("?") || pdfFileName.equalsIgnoreCase("PDF")) 225 pdfFileName = getSuggestedFileName(viewer, ExportType.PDF); 226 GenericFileInterface file = helper.getFile(pdfFileName, jsvp, true); 227 if (file == null) 228 return null; 229 if (!viewer.isJS) 230 viewer.setProperty("directoryLastExportedFile", 231 helper.setDirLastExported(file.getParentAsFile().getFullPath())); 232 pdfFileName = file.getFullPath(); 233 } 234 String s = null; 235 try { 236 OC out = (isJob ? null : 237 isBase64 ? new OC().setParams(null, ";base64,", false, null) 238 : viewer.getOutputChannel(pdfFileName, true)); 239 String printJobTitle = pd.getPrintJobTitle(true); 240 if (pl.showTitle) { 241 printJobTitle = jsvp.getInput("Title?", "Title for Printing", 242 printJobTitle); 243 if (printJobTitle == null) 244 return null; 245 } 246 jsvp.printPanel(pl, out, printJobTitle); 247 s = out.toString(); 248 } catch (Exception e) { 249 jsvp.showMessage(e.toString(), "File Error"); 250 } 251 return s; 252 } 253 getExportableItems(JSViewer viewer, boolean isSameType)254 private String[] getExportableItems(JSViewer viewer, 255 boolean isSameType) { 256 PanelData pd = viewer.pd(); 257 boolean isView = viewer.currentSource.isView; 258 // From popup menu click SaveAs or Export 259 // if JSVPanel has more than one spectrum...Choose which one to export 260 int nSpectra = pd.getNumberOfSpectraInCurrentSet(); 261 if (nSpectra == 1 || !isView && isSameType 262 || pd.getCurrentSpectrumIndex() >= 0) 263 return null; 264 String[] items = new String[nSpectra]; 265 for (int i = 0; i < nSpectra; i++) 266 items[i] = pd.getSpectrumAt(i).getTitle(); 267 return items; 268 } 269 getSuggestedFileName(JSViewer viewer, ExportType imode)270 private String getSuggestedFileName(JSViewer viewer, ExportType imode) { 271 PanelData pd = viewer.pd(); 272 String sourcePath = pd.getSpectrum().getFilePath(); 273 String newName = JSVFileManager.getTagName(sourcePath); 274 if (newName.startsWith("$")) 275 newName = newName.substring(1); 276 int pt = newName.lastIndexOf("."); 277 String name = (pt < 0 ? newName : newName.substring(0, pt)); 278 String ext = ".jdx"; 279 boolean isPrint = false; 280 switch (imode) { 281 case XY: 282 case FIX: 283 case PAC: 284 case SQZ: 285 case DIF: 286 case DIFDUP: 287 case SOURCE: 288 if (!(name.endsWith("_" + imode))) 289 name += "_" + imode; 290 ext = ".jdx"; 291 break; 292 case AML: 293 ext = ".xml"; 294 break; 295 case JPG: 296 case PNG: 297 case PDF: 298 isPrint = true; 299 //$FALL-THROUGH$ 300 default: 301 ext = "." + imode.toString().toLowerCase(); 302 } 303 if (viewer.currentSource.isView) 304 name = pd.getPrintJobTitle(isPrint); 305 name += ext; 306 return name; 307 } 308 fileCopy(String name, OC out)309 private static String fileCopy(String name, OC out) { 310 try { 311 BufferedReader br = JSVFileManager.getBufferedReaderFromName(name, 312 null); 313 String line = null; 314 while ((line = br.readLine()) != null) { 315 out.append(line); 316 out.append(newLine); 317 } 318 out.closeChannel(); 319 return "OK " + out.getByteCount() + " bytes"; 320 } catch (Exception e) { 321 return e.toString(); 322 } 323 } 324 325 326 } 327