View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2012 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.jrst.ui;
23  
24  import com.google.common.io.Files;
25  import java.awt.Color;
26  import java.io.File;
27  import javax.swing.ButtonGroup;
28  import javax.swing.JOptionPane;
29  import javax.swing.JPanel;
30  import jaxx.runtime.context.JAXXInitialContext;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.nuiton.jrst.JRST;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * Created: 03/05/12
39   *
40   * @author jpages <j.pages@codelutin.com>
41   */
42  public class JRSTViewHandler {
43      /** to use log facility, just put in your code: log.info("..."); */
44      protected static Log log = LogFactory.getLog(JRSTViewHandler.class);
45  
46      protected JRSTView jrstView;
47  
48      public JRSTViewHandler(JRSTView jrstView) {
49          this.jrstView = jrstView;
50      }
51  
52      public void init() {
53          ButtonGroup group = new ButtonGroup();
54          group.add(jrstView.getXslRadio());
55          group.add(jrstView.getFormat());
56  
57          addXslLocation();
58      }
59  
60      public int askEcraser() {
61          return JOptionPane.showConfirmDialog(jrstView, t("overwriteGraph?"));
62      }
63  
64      public String[] getFormats() {
65          String[] formats = JRST.PATTERN_TYPE.split("\\|");
66          return formats;
67      }
68  
69      public void cancel() {
70          System.exit(0);
71      }
72  
73      public void addXslLocation() {
74          JAXXInitialContext context = new JAXXInitialContext();
75          context.add(jrstView);
76          JRSTCommandModel model = jrstView.getModel();
77          context.add(model);
78          XslPanel xslPanel = new XslPanel(context);
79          xslPanel.setModel(model);
80          xslPanel.setModel(model);
81          int panelNumber = model.getPanelNumber();
82          xslPanel.setPanelNumber(panelNumber);
83          JPanel xslListPanel = jrstView.getXslListPanel();
84          xslListPanel.add(xslPanel);
85          jrstView.pack();
86      }
87  
88      public void convert() {
89          if (jrstView.getOpenLocation() == null) {
90              jrstView.getErrorLbl().setText(t("openEmpty?"));
91              jrstView.getErrorLbl().setForeground(Color.RED);
92              jrstView.pack();
93          } else {
94              launchJRST();
95              jrstView.setVisible(false);
96          }
97      }
98  
99      public void launchJRST() {
100         JRSTCommandModel model = jrstView.getModel();
101         File fileIn = model.getOpenFile();
102 
103         if (fileIn != null) {
104             String xslListOrFormat;
105             if (model.isFormatEnabled())
106                 xslListOrFormat = model.getSelectedFormat();
107             else {
108                 xslListOrFormat = model.getXsls();
109             }
110             File outputFile = model.getSaveFile();
111             if (outputFile.exists()) {
112                 int choix = jrstView.getHandler().askEcraser();
113                 if (choix == JOptionPane.NO_OPTION) {
114                     return;
115                 }
116             }
117             if (model.getSaveFile() == null) {
118                 // If the user didn't give the output file, we take the input file name with the right extension
119                 // For example, nameExample.rst becomes nameExample.xml
120                 if (outputFile.isFile()) {
121                     String outputPath = fileIn.getAbsolutePath();
122                     String ext = Files.getFileExtension(outputPath);
123                     if (ext.isEmpty()) {
124                         outputPath = outputPath + "." + xslListOrFormat;
125                     } else {
126                         outputPath = outputPath.replace("." + ext, "." + xslListOrFormat);
127                     }
128                     outputFile = new File(outputPath);
129                 }
130             }
131 
132             try{
133                 JRST.generate(xslListOrFormat, fileIn, outputFile, JRST.Overwrite.ALLTIME, model.isSimpleMode());
134             } catch (Exception e) {
135                 log.error("Can't generate the document with this configuration", e);
136             } finally {
137                 jrstView.dispose();
138             }
139         }
140     }
141 }