1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
39
40
41
42 public class JRSTViewHandler {
43
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
119
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 }