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: * RTFExportDialog.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.rtf;
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 RTF file.
062: * <p/>
063: * The main method to call the dialog is {@link RTFExportDialog#performQueryForExport(org.jfree.report.JFreeReport,
064: *org.jfree.report.modules.gui.commonswing.SwingGuiContext)}. Given a report, the dialog is shown and if the user
065: * approved the dialog, the RTF file is saved using the settings made in the dialog.
066: *
067: * @author Heiko Evermann
068: */
069: public class RTFExportDialog extends AbstractExportDialog {
070: /**
071: * Internal action class to select a target file.
072: */
073: private class ActionSelectFile extends AbstractAction {
074: /**
075: * Default constructor.
076: */
077: protected ActionSelectFile(final ResourceBundle resources) {
078: putValue(Action.NAME, resources
079: .getString("rtf-exportdialog.selectFile")); //$NON-NLS-1$
080: }
081:
082: /**
083: * Receives notification that the action has occurred.
084: *
085: * @param e the action event.
086: */
087: public void actionPerformed(final ActionEvent e) {
088: performSelectFile();
089: }
090: }
091:
092: /**
093: * Select file action.
094: */
095: private Action actionSelectFile;
096:
097: /**
098: * Filename text field.
099: */
100: private JTextField txFilename;
101:
102: /**
103: * The strict layout check-box.
104: */
105: private JCheckBox cbStrictLayout;
106:
107: /**
108: * A file chooser.
109: */
110: private JFileChooser fileChooser;
111: private static final String RTF_FILE_EXTENSION = ".rtf"; //$NON-NLS-1$
112: private JStatusBar statusBar;
113:
114: /**
115: * Creates a new Excel save dialog.
116: *
117: * @param owner the dialog owner.
118: */
119: public RTFExportDialog(final Frame owner) {
120: super (owner);
121: initConstructor();
122: }
123:
124: /**
125: * Creates a new Excel dialog.
126: *
127: * @param owner the dialog owner.
128: */
129: public RTFExportDialog(final Dialog owner) {
130: super (owner);
131: initConstructor();
132: }
133:
134: /**
135: * Creates a new Excel save dialog. The created dialog is modal.
136: */
137: public RTFExportDialog() {
138: initConstructor();
139: }
140:
141: protected String getConfigurationSuffix() {
142: return "_rtfexport"; //$NON-NLS-1$
143: }
144:
145: /**
146: * Initialisation.
147: */
148: private void initConstructor() {
149: actionSelectFile = new ActionSelectFile(getResources());
150: statusBar = new JStatusBar();
151: setTitle(getResources().getString(
152: "rtf-exportdialog.dialogtitle")); //$NON-NLS-1$
153: initialize();
154: clear();
155: }
156:
157: /**
158: * Returns a single instance of the file selection action.
159: *
160: * @return the action.
161: */
162: private Action getActionSelectFile() {
163: return actionSelectFile;
164: }
165:
166: public JStatusBar getStatusBar() {
167: return statusBar;
168: }
169:
170: /**
171: * Initializes the Swing components of this dialog.
172: */
173: private void initialize() {
174: final JPanel contentPane = new JPanel();
175: contentPane.setLayout(new GridBagLayout());
176: contentPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3,
177: 3));
178:
179: final JLabel lblFileName = new JLabel(getResources().getString(
180: "rtf-exportdialog.filename")); //$NON-NLS-1$
181: final JButton btnSelect = new ActionButton(
182: getActionSelectFile());
183:
184: txFilename = new JTextField();
185: cbStrictLayout = new JCheckBox(getResources().getString(
186: "rtf-exportdialog.strict-layout")); //$NON-NLS-1$
187:
188: getFormValidator().registerButton(cbStrictLayout);
189: getFormValidator().registerTextField(txFilename);
190:
191: GridBagConstraints gbc = new GridBagConstraints();
192: gbc.gridx = 0;
193: gbc.gridy = 0;
194: gbc.anchor = GridBagConstraints.WEST;
195: gbc.insets = new Insets(3, 1, 1, 5);
196: contentPane.add(lblFileName, gbc);
197:
198: gbc = new GridBagConstraints();
199: gbc.fill = GridBagConstraints.HORIZONTAL;
200: gbc.weightx = 1;
201: gbc.gridx = 1;
202: gbc.gridy = 0;
203: gbc.ipadx = 120;
204: gbc.insets = new Insets(3, 1, 1, 1);
205: contentPane.add(txFilename, gbc);
206:
207: gbc = new GridBagConstraints();
208: gbc.fill = GridBagConstraints.HORIZONTAL;
209: gbc.weightx = 1;
210: gbc.gridx = 1;
211: gbc.gridy = 1;
212: gbc.ipadx = 120;
213: gbc.insets = new Insets(1, 1, 1, 1);
214: contentPane.add(cbStrictLayout, gbc);
215:
216: gbc = new GridBagConstraints();
217: gbc.anchor = GridBagConstraints.NORTHWEST;
218: gbc.gridx = 2;
219: gbc.gridy = 0;
220: gbc.gridheight = 2;
221: contentPane.add(btnSelect, gbc);
222:
223: gbc = new GridBagConstraints();
224: gbc.gridx = 0;
225: gbc.gridy = 2;
226: gbc.gridwidth = 2;
227: gbc.weightx = 1;
228: gbc.weighty = 1;
229: gbc.fill = GridBagConstraints.REMAINDER;
230: gbc.insets = new Insets(10, 1, 1, 1);
231: contentPane.add(new JPanel(), gbc);
232:
233: setContentPane(createContentPane(contentPane));
234: }
235:
236: /**
237: * Returns the filename of the RTF file.
238: *
239: * @return the name of the file where to save the RTF file.
240: */
241: public String getFilename() {
242: return txFilename.getText();
243: }
244:
245: /**
246: * Defines the filename of the RTF file.
247: *
248: * @param filename the filename of the RTF file
249: */
250: public void setFilename(final String filename) {
251: this .txFilename.setText(filename);
252: }
253:
254: /**
255: * Returns the setting of the 'strict layout' check-box.
256: *
257: * @return A boolean.
258: */
259: public boolean isStrictLayout() {
260: return cbStrictLayout.isSelected();
261: }
262:
263: /**
264: * Sets the 'strict-layout' check-box.
265: *
266: * @param strictLayout the new setting.
267: */
268: public void setStrictLayout(final boolean strictLayout) {
269: cbStrictLayout.setSelected(strictLayout);
270: }
271:
272: /**
273: * Clears all selections and input fields.
274: */
275: public void clear() {
276: txFilename.setText(""); //$NON-NLS-1$
277: cbStrictLayout.setSelected(false);
278: }
279:
280: /**
281: * Returns a new (and not connected to the default config from the job) configuration containing all properties from
282: * the dialog.
283: *
284: * @param full
285: * @return
286: */
287: protected Configuration grabDialogContents(final boolean full) {
288: final DefaultConfiguration p = new DefaultConfiguration();
289: if (full) {
290: p
291: .setProperty(
292: "org.jfree.report.modules.gui.rtf.FileName", getFilename()); //$NON-NLS-1$
293: }
294: p
295: .setProperty(
296: "org.jfree.report.modules.output.table.rtf.StrictLayout", String.valueOf(isStrictLayout())); //$NON-NLS-1$
297: return p;
298: }
299:
300: /**
301: * Initialises the Excel export dialog from the settings in the report configuration.
302: *
303: * @param config the report configuration.
304: */
305: protected void setDialogContents(final Configuration config) {
306: final String strict = config
307: .getConfigProperty("org.jfree.report.modules.output.table.rtf.StrictLayout"); //$NON-NLS-1$
308: setStrictLayout("true".equals(strict)); //$NON-NLS-1$
309:
310: final String defaultFileName = config
311: .getConfigProperty("org.jfree.report.modules.gui.rtf.FileName"); //$NON-NLS-1$
312: if (defaultFileName != null) {
313: setFilename(resolvePath(defaultFileName).getAbsolutePath());
314: }
315: }
316:
317: /**
318: * Selects a file to use as target for the report processing.
319: */
320: protected void performSelectFile() {
321: if (fileChooser == null) {
322: fileChooser = new JFileChooser();
323: final FilesystemFilter filter = new FilesystemFilter(
324: RTF_FILE_EXTENSION, getResources().getString(
325: "rtf-exportdialog.rtf-file-description")); //$NON-NLS-1$
326: fileChooser.addChoosableFileFilter(filter);
327: fileChooser.setMultiSelectionEnabled(false);
328: }
329:
330: final File file = new File(getFilename());
331: fileChooser.setCurrentDirectory(file);
332: fileChooser.setSelectedFile(file);
333: final int option = fileChooser.showSaveDialog(this );
334: if (option == JFileChooser.APPROVE_OPTION) {
335: final File selFile = fileChooser.getSelectedFile();
336: String selFileName = selFile.getAbsolutePath();
337:
338: // Test if ends on xls
339: if (StringUtils.endsWithIgnoreCase(selFileName,
340: RTF_FILE_EXTENSION) == false) {
341: selFileName = selFileName + RTF_FILE_EXTENSION;
342: }
343: setFilename(selFileName);
344: }
345: }
346:
347: /**
348: * Validates the contents of the dialog's input fields. If the selected file exists, it is also checked for validity.
349: *
350: * @return true, if the input is valid, false otherwise
351: */
352: public boolean performValidate() {
353: getStatusBar().clear();
354:
355: final String filename = getFilename();
356: if (filename.trim().length() == 0) {
357: getStatusBar().setStatus(
358: StatusType.ERROR,
359: getResources().getString(
360: "rtf-exportdialog.targetIsEmpty")); //$NON-NLS-1$
361: return false;
362: }
363: final File f = new File(filename);
364: if (f.exists()) {
365: if (f.isFile() == false) {
366: getStatusBar().setStatus(
367: StatusType.ERROR,
368: getResources().getString(
369: "rtf-exportdialog.targetIsNoFile")); //$NON-NLS-1$
370: return false;
371: }
372: if (f.canWrite() == false) {
373: getStatusBar()
374: .setStatus(
375: StatusType.ERROR,
376: getResources()
377: .getString(
378: "rtf-exportdialog.targetIsNotWritable")); //$NON-NLS-1$
379: return false;
380: }
381: final String message = MessageFormat.format(getResources()
382: .getString("rtf-exportdialog.targetExistsWarning"), //$NON-NLS-1$
383: new Object[] { filename });
384: getStatusBar().setStatus(StatusType.WARNING, message);
385: }
386: return true;
387: }
388:
389: protected boolean performConfirm() {
390: final String filename = getFilename();
391: final File f = new File(filename);
392: if (f.exists()) {
393: final String key1 = "rtf-exportdialog.targetOverwriteConfirmation"; //$NON-NLS-1$
394: final String key2 = "rtf-exportdialog.targetOverwriteTitle"; //$NON-NLS-1$
395: if (JOptionPane.showConfirmDialog(this , MessageFormat
396: .format(getResources().getString(key1),
397: new Object[] { getFilename() }),
398: getResources().getString(key2),
399: JOptionPane.YES_NO_OPTION,
400: JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
401: return false;
402: }
403: }
404: return true;
405: }
406:
407: protected String getResourceBaseName() {
408: return RTFExportPlugin.BASE_RESOURCE_CLASS;
409: }
410:
411: protected String getConfigurationPrefix() {
412: return "org.jfree.report.modules.gui.rtf."; //$NON-NLS-1$
413: }
414: }
|