001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ExcelExportDialog.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.xls;
030:
031: import java.awt.Dialog;
032: import java.awt.Frame;
033: import java.awt.GridBagConstraints;
034: import java.awt.GridBagLayout;
035: import java.awt.Insets;
036: import java.awt.event.ActionEvent;
037: import java.io.File;
038: import java.text.MessageFormat;
039: import java.util.ResourceBundle;
040: import javax.swing.AbstractAction;
041: import javax.swing.Action;
042: import javax.swing.BorderFactory;
043: import javax.swing.JButton;
044: import javax.swing.JCheckBox;
045: import javax.swing.JFileChooser;
046: import javax.swing.JLabel;
047: import javax.swing.JOptionPane;
048: import javax.swing.JPanel;
049: import javax.swing.JTextField;
050:
051: import org.jfree.report.modules.gui.commonswing.AbstractExportDialog;
052: import org.jfree.report.modules.gui.commonswing.JStatusBar;
053: import org.jfree.report.modules.gui.commonswing.StatusType;
054: import org.jfree.ui.FilesystemFilter;
055: import org.jfree.ui.action.ActionButton;
056: import org.jfree.util.Configuration;
057: import org.jfree.util.DefaultConfiguration;
058: import org.jfree.util.StringUtils;
059:
060: /**
061: * A dialog that is used to prepare the printing of a report into an Excel
062: * file.
063: * <p/>
064: * The main method to call the dialog is
065: * {@link ExcelExportDialog#performQueryForExport(org.jfree.report.JFreeReport,
066: org.jfree.report.modules.gui.commonswing.SwingGuiContext)}.
067: * Given a report, the dialog is shown and if the user approved the dialog, the
068: * excel file is saved using the settings made in the dialog.
069: *
070: * @author Heiko Evermann
071: */
072: public class ExcelExportDialog extends AbstractExportDialog {
073: /** Internal action class to select a target file. */
074: private class ActionSelectFile extends AbstractAction {
075: /** Default constructor. */
076: protected ActionSelectFile(final ResourceBundle resources) {
077: putValue(Action.NAME, resources
078: .getString("excelexportdialog.selectFile")); //$NON-NLS-1$
079: }
080:
081: /**
082: * Receives notification that the action has occurred.
083: *
084: * @param e the action event.
085: */
086: public void actionPerformed(final ActionEvent e) {
087: performSelectFile();
088: }
089: }
090:
091: /** Select file action. */
092: private Action actionSelectFile;
093:
094: /** Filename text field. */
095: private JTextField txFilename;
096:
097: /** The strict layout check-box. */
098: private JCheckBox cbStrictLayout;
099:
100: private JStatusBar statusBar;
101:
102: /** A file chooser. */
103: private JFileChooser fileChooser;
104: private static final String XLS_FILE_EXTENSION = ".xls"; //$NON-NLS-1$
105:
106: /**
107: * Creates a new Excel save dialog.
108: *
109: * @param owner the dialog owner.
110: */
111: public ExcelExportDialog(final Frame owner) {
112: super (owner);
113: initConstructor();
114: }
115:
116: /**
117: * Creates a new Excel dialog.
118: *
119: * @param owner the dialog owner.
120: */
121: public ExcelExportDialog(final Dialog owner) {
122: super (owner);
123: initConstructor();
124: }
125:
126: /** Creates a new Excel save dialog. The created dialog is modal. */
127: public ExcelExportDialog() {
128: initConstructor();
129: }
130:
131: protected String getConfigurationSuffix() {
132: return "_xlsexport"; //$NON-NLS-1$
133: }
134:
135: /** Initialisation. */
136: private void initConstructor() {
137: actionSelectFile = new ActionSelectFile(getResources());
138: statusBar = new JStatusBar();
139: setTitle(getResources().getString(
140: "excelexportdialog.dialogtitle")); //$NON-NLS-1$
141: initialize();
142: clear();
143: }
144:
145: public JStatusBar getStatusBar() {
146: return statusBar;
147: }
148:
149: /**
150: * Returns a single instance of the file selection action.
151: *
152: * @return the action.
153: */
154: private Action getActionSelectFile() {
155: return actionSelectFile;
156: }
157:
158: /** Initializes the Swing components of this dialog. */
159: private void initialize() {
160: final JPanel contentPane = new JPanel();
161: contentPane.setLayout(new GridBagLayout());
162: contentPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3,
163: 3));
164:
165: final JLabel lblFileName = new JLabel(getResources().getString(
166: "excelexportdialog.filename")); //$NON-NLS-1$
167: final JButton btnSelect = new ActionButton(
168: getActionSelectFile());
169:
170: txFilename = new JTextField();
171: cbStrictLayout = new JCheckBox(getResources().getString(
172: "excelexportdialog.strict-layout")); //$NON-NLS-1$
173:
174: getFormValidator().registerButton(cbStrictLayout);
175: getFormValidator().registerTextField(txFilename);
176:
177: GridBagConstraints gbc = new GridBagConstraints();
178: gbc.gridx = 0;
179: gbc.gridy = 0;
180: gbc.anchor = GridBagConstraints.WEST;
181: gbc.insets = new Insets(3, 1, 1, 5);
182: contentPane.add(lblFileName, gbc);
183:
184: gbc = new GridBagConstraints();
185: gbc.fill = GridBagConstraints.HORIZONTAL;
186: gbc.weightx = 1;
187: gbc.gridx = 1;
188: gbc.gridy = 0;
189: gbc.ipadx = 120;
190: gbc.insets = new Insets(3, 1, 1, 1);
191: contentPane.add(txFilename, gbc);
192:
193: gbc = new GridBagConstraints();
194: gbc.fill = GridBagConstraints.HORIZONTAL;
195: gbc.weightx = 1;
196: gbc.gridx = 1;
197: gbc.gridy = 1;
198: gbc.ipadx = 120;
199: gbc.insets = new Insets(1, 1, 1, 1);
200: contentPane.add(cbStrictLayout, gbc);
201:
202: gbc = new GridBagConstraints();
203: gbc.anchor = GridBagConstraints.NORTHWEST;
204: gbc.gridx = 2;
205: gbc.gridy = 0;
206: gbc.gridheight = 2;
207: contentPane.add(btnSelect, gbc);
208:
209: gbc = new GridBagConstraints();
210: gbc.gridx = 0;
211: gbc.gridy = 2;
212: gbc.gridwidth = 2;
213: gbc.weightx = 1;
214: gbc.weighty = 1;
215: gbc.fill = GridBagConstraints.REMAINDER;
216: gbc.insets = new Insets(10, 1, 1, 1);
217: contentPane.add(new JPanel(), gbc);
218:
219: setContentPane(createContentPane(contentPane));
220: }
221:
222: /**
223: * Returns the filename of the excel file.
224: *
225: * @return the name of the file where to save the excel file.
226: */
227: public String getFilename() {
228: return txFilename.getText();
229: }
230:
231: /**
232: * Defines the filename of the excel file.
233: *
234: * @param filename the filename of the excel file
235: */
236: public void setFilename(final String filename) {
237: this .txFilename.setText(filename);
238: }
239:
240: /**
241: * Returns the setting of the 'strict layout' check-box.
242: *
243: * @return A boolean.
244: */
245: public boolean isStrictLayout() {
246: return cbStrictLayout.isSelected();
247: }
248:
249: /**
250: * Sets the 'strict-layout' check-box.
251: *
252: * @param strictLayout the new setting.
253: */
254: public void setStrictLayout(final boolean strictLayout) {
255: cbStrictLayout.setSelected(strictLayout);
256: }
257:
258: /** Clears all selections and input fields. */
259: public void clear() {
260: txFilename.setText(""); //$NON-NLS-1$
261: cbStrictLayout.setSelected(false);
262: }
263:
264: /** Selects a file to use as target for the report processing. */
265: protected void performSelectFile() {
266: if (fileChooser == null) {
267: fileChooser = new JFileChooser();
268: final FilesystemFilter filter = new FilesystemFilter(
269: XLS_FILE_EXTENSION, getResources().getString(
270: "excelexportdialog.excel-file-description")); //$NON-NLS-1$
271: fileChooser.addChoosableFileFilter(filter);
272: fileChooser.setMultiSelectionEnabled(false);
273: }
274:
275: final File file = new File(getFilename());
276: fileChooser.setCurrentDirectory(file);
277: fileChooser.setSelectedFile(file);
278: final int option = fileChooser.showSaveDialog(this );
279: if (option == JFileChooser.APPROVE_OPTION) {
280: final File selFile = fileChooser.getSelectedFile();
281: String selFileName = selFile.getAbsolutePath();
282:
283: // Test if ends on xls
284: if (StringUtils.endsWithIgnoreCase(selFileName,
285: XLS_FILE_EXTENSION) == false) {
286: selFileName = selFileName + XLS_FILE_EXTENSION;
287: }
288: setFilename(selFileName);
289: }
290: }
291:
292: /**
293: * Validates the contents of the dialog's input fields. If the selected file
294: * exists, it is also checked for validity.
295: *
296: * @return true, if the input is valid, false otherwise
297: */
298: public boolean performValidate() {
299: getStatusBar().clear();
300:
301: final String filename = getFilename();
302: if (filename.trim().length() == 0) {
303: getStatusBar().setStatus(
304: StatusType.ERROR,
305: getResources().getString(
306: "excelexportdialog.targetIsEmpty")); //$NON-NLS-1$
307: return false;
308: }
309: final File f = new File(filename);
310: if (f.exists()) {
311: if (f.isFile() == false) {
312: getStatusBar().setStatus(
313: StatusType.ERROR,
314: getResources().getString(
315: "excelexportdialog.targetIsNoFile")); //$NON-NLS-1$
316: return false;
317: }
318: if (f.canWrite() == false) {
319: getStatusBar()
320: .setStatus(
321: StatusType.ERROR,
322: getResources()
323: .getString(
324: "excelexportdialog.targetIsNotWritable")); //$NON-NLS-1$
325: return false;
326: }
327: final String message = MessageFormat.format(
328: getResources().getString(
329: "excelexportdialog.targetExistsWarning"), //$NON-NLS-1$
330: new Object[] { filename });
331: getStatusBar().setStatus(StatusType.WARNING, message);
332: }
333: return true;
334: }
335:
336: protected boolean performConfirm() {
337: final String filename = getFilename();
338: final File f = new File(filename);
339: if (f.exists()) {
340: final String key1 = "excelexportdialog.targetOverwriteConfirmation"; //$NON-NLS-1$
341: final String key2 = "excelexportdialog.targetOverwriteTitle"; //$NON-NLS-1$
342: if (JOptionPane.showConfirmDialog(this , MessageFormat
343: .format(getResources().getString(key1),
344: new Object[] { getFilename() }),
345: getResources().getString(key2),
346: JOptionPane.YES_NO_OPTION,
347: JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
348: return false;
349: }
350: }
351: return true;
352: }
353:
354: /**
355: * Initialises the Excel export dialog from the settings in the report
356: * configuration.
357: *
358: * @param config the report configuration.
359: */
360: protected void setDialogContents(final Configuration config) {
361: final String strict = config
362: .getConfigProperty("org.jfree.report.modules.output.table.xls.StrictLayout"); //$NON-NLS-1$
363: setStrictLayout("true".equals(strict)); //$NON-NLS-1$
364:
365: final String defaultFileName = config
366: .getConfigProperty("org.jfree.report.modules.gui.xls.FileName"); //$NON-NLS-1$
367: if (defaultFileName != null) {
368: setFilename(resolvePath(defaultFileName).getAbsolutePath());
369: }
370: }
371:
372: /**
373: * Returns a new (and not connected to the default config from the job)
374: * configuration containing all properties from the dialog.
375: *
376: * @param full
377: * @return
378: */
379: protected Configuration grabDialogContents(final boolean full) {
380: final DefaultConfiguration p = new DefaultConfiguration();
381:
382: if (full) {
383: p
384: .setProperty(
385: "org.jfree.report.modules.gui.xls.FileName", getFilename()); //$NON-NLS-1$
386: }
387: p
388: .setConfigProperty(
389: "org.jfree.report.modules.output.table.xls.StrictLayout", String.valueOf(isStrictLayout())); //$NON-NLS-1$
390: return p;
391: }
392:
393: protected String getResourceBaseName() {
394: return ExcelExportPlugin.BASE_RESOURCE_CLASS;
395: }
396:
397: protected String getConfigurationPrefix() {
398: return "org.jfree.report.modules.gui.xls."; //$NON-NLS-1$
399: }
400: }
|