001: package jimm.datavision.gui;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.layout.*;
005: import jimm.datavision.layout.pdf.PDFLE;
006: import jimm.datavision.layout.excel.ExcelLE;
007: import jimm.util.XMLWriter;
008: import jimm.util.I18N;
009: import java.io.PrintWriter;
010: import java.io.FileWriter;
011: import java.io.FileOutputStream;
012: import java.io.IOException;
013: import java.awt.Frame;
014: import java.awt.BorderLayout;
015: import java.awt.event.ActionListener;
016: import java.awt.event.ActionEvent;
017: import java.util.prefs.Preferences;
018: import javax.swing.*;
019:
020: /**
021: * This dialog lets the user export report results using one of the
022: * layout engines.
023: *
024: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
025: */
026: public class ExportWin extends JDialog implements ActionListener {
027:
028: protected static Preferences prefs = Preferences
029: .userNodeForPackage(ExportWin.class);
030:
031: protected Report report;
032: protected JComboBox combo;
033:
034: /**
035: * Constructor.
036: *
037: * @param owner the parent window to which this dialog belongs
038: * @param report the report
039: */
040: public ExportWin(Frame owner, Report report) {
041: super (owner, I18N.get("ExportWin.title"));
042: this .report = report;
043: buildWindow();
044: pack();
045: setVisible(true);
046: }
047:
048: /**
049: * Builds the window contents.
050: */
051: protected void buildWindow() {
052: // Panel containing list of layout engines
053: combo = new JComboBox(layoutNames());
054: String lastUsedEngine = prefs.get("last_used_engine", null);
055: if (lastUsedEngine != null)
056: combo.setSelectedItem(lastUsedEngine);
057:
058: JPanel comboPanel = new JPanel();
059: comboPanel.setBorder(BorderFactory.createEmptyBorder(20, 20,
060: 20, 20));
061: comboPanel.add(combo);
062:
063: // OK and Cancel Buttons
064: JPanel buttonPanel = buildButtonPanel();
065:
066: // Add list, cards, and buttons to window
067: getContentPane().setLayout(new BorderLayout());
068: getContentPane().add(comboPanel, BorderLayout.CENTER);
069: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
070: }
071:
072: /**
073: * Builds and returns a panel containing the OK and Cancel
074: *
075: * @return a panel
076: */
077: protected JPanel buildButtonPanel() {
078: JPanel buttonPanel = new JPanel();
079: JButton button;
080:
081: buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
082: button.addActionListener(this );
083: button.setDefaultCapable(true);
084:
085: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
086: button.addActionListener(this );
087:
088: return buttonPanel;
089: }
090:
091: /**
092: * Handles the OK and Cancel buttons.
093: *
094: * @param e action event
095: */
096: public void actionPerformed(ActionEvent e) {
097: String cmd = e.getActionCommand();
098: if (I18N.get("GUI.ok").equals(cmd)) {
099: String choice = (String) combo.getSelectedItem();
100: prefs.put("last_used_engine", choice);
101: dispose();
102: runReport(choice);
103: } else if (I18N.get("GUI.cancel").equals(cmd))
104: dispose();
105: }
106:
107: protected String[] layoutNames() {
108: String[] names = new String[9];
109: int i = 0;
110: names[i++] = I18N.get("ExportWin.le_comma");
111: names[i++] = I18N.get("ExportWin.le_tab");
112: names[i++] = I18N.get("ExportWin.le_docbook");
113: names[i++] = I18N.get("ExportWin.le_html");
114: names[i++] = I18N.get("ExportWin.le_latex");
115: names[i++] = I18N.get("ExportWin.le_pdf");
116: names[i++] = I18N.get("ExportWin.le_xml");
117: names[i++] = I18N.get("ExportWin.le_xls");
118: names[i++] = I18N.get("ExportWin.le_csshtml");
119: return names;
120: }
121:
122: /**
123: * Given the user's choice of layout engine, ask user for output file
124: * and run the report.
125: *
126: * @param choice the combo box string that the user selected
127: */
128: protected void runReport(String choice) {
129: String extension = null;
130: if (I18N.get("ExportWin.le_comma").equals(choice))
131: extension = ".csv";
132: else if (I18N.get("ExportWin.le_tab").equals(choice))
133: extension = ".tab";
134: else if (I18N.get("ExportWin.le_docbook").equals(choice))
135: extension = ".sgml";
136: else if (I18N.get("ExportWin.le_html").equals(choice))
137: extension = ".html";
138: else if (I18N.get("ExportWin.le_latex").equals(choice))
139: extension = ".tex";
140: else if (I18N.get("ExportWin.le_pdf").equals(choice))
141: extension = ".pdf";
142: else if (I18N.get("ExportWin.le_xml").equals(choice))
143: extension = ".xml";
144: else if (I18N.get("ExportWin.le_xls").equals(choice))
145: extension = ".xls";
146: else if (I18N.get("ExportWin.le_csshtml").equals(choice))
147: extension = ".html";
148:
149: String path = selectFile(extension);
150: if (path == null) // Cancelled by user
151: return;
152:
153: try {
154: LayoutEngine le = null;
155: if (I18N.get("ExportWin.le_comma").equals(choice))
156: le = new CharSepLE(
157: new PrintWriter(new FileWriter(path)), ',');
158: else if (I18N.get("ExportWin.le_tab").equals(choice))
159: le = new CharSepLE(
160: new PrintWriter(new FileWriter(path)), '\t');
161: else if (I18N.get("ExportWin.le_docbook").equals(choice))
162: le = new DocBookLE(
163: new PrintWriter(new FileWriter(path)));
164: else if (I18N.get("ExportWin.le_html").equals(choice))
165: le = new HTMLLE(new PrintWriter(new FileWriter(path)));
166: else if (I18N.get("ExportWin.le_latex").equals(choice))
167: le = new LaTeXLE(new PrintWriter(new FileWriter(path)));
168: else if (I18N.get("ExportWin.le_pdf").equals(choice))
169: le = new PDFLE(new FileOutputStream(path));
170: else if (I18N.get("ExportWin.le_xml").equals(choice))
171: le = new XMLLE(
172: new XMLWriter(new FileOutputStream(path)));
173: else if (I18N.get("ExportWin.le_xls").equals(choice))
174: le = new ExcelLE(new FileOutputStream(path), false);
175: else if (I18N.get("ExportWin.le_csshtml").equals(choice))
176: le = new CSSHTMLLE(
177: new PrintWriter(new FileWriter(path)));
178:
179: if (le != null) {
180: report.setLayoutEngine(le);
181: report.run();
182: }
183: } catch (IOException e) {
184: ErrorHandler.error(I18N.get("ExportWin.err_msg"), e, I18N
185: .get("ExportWin.err_title"));
186: }
187: }
188:
189: /**
190: * Lets user select output file. I'd like to be able to give a default
191: * file extension (thus the argument), but that will have to come later.
192: *
193: * @param extension default file name extension (unused)
194: * @return the path to the selected file
195: */
196: protected String selectFile(String extension) {
197: JFileChooser chooser = Designer.getChooser();
198: Designer.setPrefsDir(chooser, "outputDir");
199: if (chooser.showSaveDialog(this .getOwner()) == JFileChooser.APPROVE_OPTION) {
200: Designer.savePrefsDir(chooser, "outputDir");
201: //output should probably have it's own prefs
202: //Designer.prefsPutReportDir(chooser); // save report directory
203: return chooser.getSelectedFile().getPath();
204: } else
205: return null;
206: }
207:
208: }
|