~nvervelle/jmol/trunk

« back to all changes in this revision

Viewing changes to srcjsv/jspecview/export/Exporter.java

  • Committer: hansonr
  • Date: 2017-07-20 11:42:01 UTC
  • Revision ID: svn-v4:62be27d7-c70d-0410-b753-bb59b6ba1b27:trunk/Jmol:21665
Jmol.___JmolVersion="14.20.3" // 2017.07.20

bug fix: CIPChirality tests for CIP1966#31,#32 douple spirans and C3 compounds
bug fix: CIPChirality for Rule 2 using atomic masses and not for duplicate atoms
bug fix: MOL file reading where isotope is indicated in atom line and V item
bug fix: MOL reader fails for H1, D, T isotopes indicated as symbols along with isotope difference

Show diffs side-by-side

added added

removed removed

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