summaryrefslogtreecommitdiff
path: root/pdf-over-gui/src/main/java/at
diff options
context:
space:
mode:
authortkellner <tkellner@174cde9d-5d70-4d2a-aa98-46368bc2aaf7>2013-04-10 19:22:50 +0000
committertkellner <tkellner@174cde9d-5d70-4d2a-aa98-46368bc2aaf7>2013-04-10 19:22:50 +0000
commita1208626fad77d9e09f88f7b9fdaae13d1adf0f2 (patch)
treefec7a981e3a71856f553744c124b6f8f269e7b60 /pdf-over-gui/src/main/java/at
parent504c988fae03e0a1d05fa37cb9feaa491bfdc722 (diff)
downloadpdf-over-a1208626fad77d9e09f88f7b9fdaae13d1adf0f2.tar.gz
pdf-over-a1208626fad77d9e09f88f7b9fdaae13d1adf0f2.tar.bz2
pdf-over-a1208626fad77d9e09f88f7b9fdaae13d1adf0f2.zip
Completely rework Output(State/Composite)
Only save once Allow saving to input doc directory Ask whether to overwrite existing file git-svn-id: https://joinup.ec.europa.eu/svn/pdf-over/trunk@289 174cde9d-5d70-4d2a-aa98-46368bc2aaf7
Diffstat (limited to 'pdf-over-gui/src/main/java/at')
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/OutputComposite.java404
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OutputState.java67
2 files changed, 231 insertions, 240 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/OutputComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/OutputComposite.java
index 855b3f65..d2ef6a54 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/OutputComposite.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/OutputComposite.java
@@ -41,6 +41,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import at.asit.pdfover.gui.Constants;
+import at.asit.pdfover.gui.controls.Dialog;
+import at.asit.pdfover.gui.controls.Dialog.BUTTONS;
+import at.asit.pdfover.gui.controls.Dialog.ICON;
import at.asit.pdfover.gui.utils.Messages;
import at.asit.pdfover.gui.workflow.states.State;
import at.asit.pdfover.signator.DocumentSource;
@@ -55,17 +58,81 @@ public class OutputComposite extends StateComposite {
**/
static final Logger log = LoggerFactory.getLogger(OutputComposite.class);
+ private Link lnk_saved_file;
+
+ private Button btn_save;
+
+ private Label lbl_success_message;
+
+ private DocumentSource signedDocument;
+
private File inputFile;
+ String outputDir = null;
+
+ String tempDirectory = null;
+
File outputFile = null;
- String outputDir = null;
+ private boolean saveFailed = false;
/**
- * @return the outputDir
+ * Create the composite.
+ *
+ * @param parent
+ * The parent composite
+ * @param style
+ * The swt style
+ * @param state
+ * The owning state
*/
- public String getOutputDir() {
- return this.outputDir;
+ public OutputComposite(Composite parent, int style, State state) {
+ super(parent, style, state);
+
+ this.setLayout(new FormLayout());
+
+ this.lbl_success_message = new Label(this, SWT.NATIVE | SWT.RESIZE);
+ FormData fd_lbl_success_message = new FormData();
+ fd_lbl_success_message.top = new FormAttachment(40, 0);
+ fd_lbl_success_message.left = new FormAttachment(0);
+ fd_lbl_success_message.right = new FormAttachment(100);
+ this.lbl_success_message.setLayoutData(fd_lbl_success_message);
+ this.lbl_success_message.setAlignment(SWT.CENTER);
+
+ FontData[] fD1 = this.lbl_success_message.getFont().getFontData();
+ fD1[0].setHeight(Constants.TEXT_SIZE_BIG);
+ this.lbl_success_message.setFont(new Font(Display.getCurrent(), fD1[0]));
+
+ this.lnk_saved_file = new Link(this, SWT.NATIVE | SWT.RESIZE);
+ FormData fd_lnk_saved_file = new FormData();
+ fd_lnk_saved_file.top = new FormAttachment(this.lbl_success_message, 10);
+ fd_lnk_saved_file.left = new FormAttachment(this.lbl_success_message, 0,
+ SWT.CENTER);
+ // fd_lnk_saved_file.right = new FormAttachment(100);
+ this.lnk_saved_file.setLayoutData(fd_lnk_saved_file);
+
+ this.lnk_saved_file.addSelectionListener(new OpenSelectionListener());
+
+ FontData[] fD2 = this.lnk_saved_file.getFont().getFontData();
+ fD2[0].setHeight(Constants.TEXT_SIZE_NORMAL);
+ this.lnk_saved_file.setFont(new Font(Display.getCurrent(), fD2[0]));
+
+ this.btn_save = new Button(this, SWT.NATIVE | SWT.RESIZE);
+
+ FontData[] fD_btn_save = this.btn_save.getFont().getFontData();
+ fD_btn_save[0].setHeight(Constants.TEXT_SIZE_BUTTON);
+ this.btn_save.setFont(new Font(Display.getCurrent(), fD_btn_save[0]));
+
+ FormData fd_btn_save = new FormData();
+ fd_btn_save.top = new FormAttachment(this.lnk_saved_file, 10);
+ fd_btn_save.left = new FormAttachment(this.lnk_saved_file, 0,
+ SWT.CENTER);
+ this.btn_save.setLayoutData(fd_btn_save);
+
+ this.btn_save.addSelectionListener(new SaveSelectionListener());
+ enableSaveButton(false);
+
+ reloadResources();
}
/**
@@ -77,6 +144,13 @@ public class OutputComposite extends StateComposite {
}
/**
+ * @return the outputDir
+ */
+ public String getOutputDir() {
+ return this.outputDir;
+ }
+
+ /**
* Sets the input file
*
* @param inputFile
@@ -96,85 +170,140 @@ public class OutputComposite extends StateComposite {
}
/**
- * Saves the file
- *
- * @throws IOException
+ * @param tempDirectory
*/
- void saveFile() throws IOException {
- FileDialog save = new FileDialog(OutputComposite.this.getShell(),
- SWT.SAVE | SWT.NATIVE);
- save.setFilterExtensions(new String[] { "*.pdf", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
- save.setFilterNames(new String[] {
- Messages.getString("common.PDFExtension_Description"), //$NON-NLS-1$
- Messages.getString("common.AllExtension_Description")}); //$NON-NLS-1$
-
- String proposed = OutputComposite.this.getInputFile().getAbsolutePath();
-
- String path = this.getOutputDir();
-
- if (path == null || path.equals("")) { //$NON-NLS-1$
- path = FilenameUtils.getFullPath(proposed);
- }
-
- if(!path.endsWith(File.separator)) {
- path += File.separator;
- }
-
- String name = FilenameUtils.getName(proposed);
+ public void setTempDir(String tempDirectory) {
+ this.tempDirectory = tempDirectory;
+ }
- String extension = FilenameUtils.getExtension(proposed);
+ /**
+ * Gets the signed document
+ *
+ * @return the signed document
+ */
+ public DocumentSource getSignedDocument() {
+ return this.signedDocument;
+ }
- name = FilenameUtils.removeExtension(name);
+ /**
+ * Sets the signed document
+ *
+ * @param signedDocument
+ * the signed document
+ */
+ public void setSignedDocument(final DocumentSource signedDocument) {
+ this.signedDocument = signedDocument;
+ }
- proposed = path + name + Constants.SIGNED_SUFFIX + "." + extension; //$NON-NLS-1$
+ private void enableSaveButton(boolean doEnable)
+ {
+ this.btn_save.setEnabled(doEnable);
+ this.btn_save.setVisible(doEnable);
+ }
- if (this.getOutputDir() != null && !this.getOutputDir().equals("")) //$NON-NLS-1$
- {
- // Output directory is set save there without asking user...
-
- saveResultAsFile(proposed);
+ /**
+ * Saves the signed document.
+ *
+ * If user has a default output directory set, try to save there.
+ * If not (or if directory unavailable), ask user for location.
+ */
+ public void saveDocument() {
+ File inputFolder = getInputFile().getParentFile();
+ String fileName = getInputFile().getName();
+ String proposedName = getSignedFileName(fileName);
+ String outputFileName;
+
+ String outputFolder = getOutputDir();
+ if (!this.saveFailed && outputFolder != null && !outputFolder.trim().equals("")) { //$NON-NLS-1$
+ // Output folder configured, try to save there
+
+ if(!outputFolder.endsWith(File.separator)) {
+ outputFolder += File.separator;
+ }
+ outputFileName = outputFolder + proposedName;
} else {
+ // Ask user where to save
+
+ FileDialog save = new FileDialog(this.getShell(),
+ SWT.SAVE | SWT.NATIVE);
+ save.setFilterExtensions(new String[] { "*.pdf", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
+ save.setFilterNames(new String[] {
+ Messages.getString("common.PDFExtension_Description"), //$NON-NLS-1$
+ Messages.getString("common.AllExtension_Description")}); //$NON-NLS-1$
+ save.setFilterPath(inputFolder.getAbsolutePath());
+ save.setFileName(proposedName);
+
+ outputFileName = save.open();
+ inputFolder = null;
+ }
+ log.debug("Trying to save to '" + outputFileName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
- save.setFileName(proposed);
-
- String target = save.open();
+ this.saveFailed = !saveResultAsFile(inputFolder, outputFileName);
- if (target != null) {
- saveResultAsFile(target);
- } else {
- // Show save message ...
- this.lnk_saved_file.setText(Messages
- .getString("output.link_save_message")); //$NON-NLS-1$
- this.btn_save.setVisible(true);
- }
+ if (!this.saveFailed) {
+ // Save successful
+ this.outputFile = new File(inputFolder, outputFileName);
}
+
+ // If saving failed, enable save button
+ enableSaveButton(this.saveFailed);
+ reloadResources();
+ layout(true);
}
/**
- * @param target
+ * Save the signed document under the given filename
+ * @param inputFolder the Folder the original document is located at
+ * @param target the filename to save the document as
+ *
+ * @return whether save was successful
* @throws FileNotFoundException
* @throws IOException
*/
- private void saveResultAsFile(String target) throws FileNotFoundException,
- IOException {
- File targetFile = new File(target);
+ private boolean saveResultAsFile(File inputFolder, String target) {
+ if (target == null)
+ return false;
+
+ File targetFile = new File(inputFolder, target);
+
+ if (targetFile.exists()) {
+ Dialog dialog = new Dialog(this.getShell(),
+ String.format(Messages.getString("output.file_ask_overwrite"), targetFile.getName()), //$NON-NLS-1$
+ BUTTONS.OK_CANCEL, ICON.QUESTION);
+ if (dialog.open() == SWT.CANCEL)
+ {
+ return false;
+ }
+ }
- DocumentSource source = OutputComposite.this
- .getSignedDocument();
+ DocumentSource source = this.getSignedDocument();
- FileOutputStream outstream = new FileOutputStream(targetFile);
- outstream.write(source.getByteArray(), 0,
- source.getByteArray().length);
- outstream.close();
+ try {
+ FileOutputStream outstream = new FileOutputStream(targetFile);
+ outstream.write(source.getByteArray(), 0,
+ source.getByteArray().length);
+ outstream.close();
+ } catch (FileNotFoundException e) {
+ log.debug("File not found", e); //$NON-NLS-1$
+ return false;
+ } catch (IOException e) {
+ log.debug("IO Error", e); //$NON-NLS-1$
+ return false;
+ }
- OutputComposite.this.savedFile = targetFile;
+ return targetFile.exists();
+ }
- this.outputFile = targetFile;
- // Show open message ...
- this.lnk_saved_file.setText(Messages
- .getString("output.link_open_message")); //$NON-NLS-1$
- this.layout(true);
- this.btn_save.setVisible(false);
+ /**
+ * Get the proposed filename of a signed document for a given input filename
+ * @param name input filename
+ * @return proposed output filename
+ */
+ private static String getSignedFileName(String name) {
+ name = FilenameUtils.getName(name);
+ String extension = FilenameUtils.getExtension(name);
+ name = FilenameUtils.removeExtension(name);
+ return name + Constants.SIGNED_SUFFIX + "." + extension; //$NON-NLS-1$
}
/**
@@ -190,7 +319,7 @@ public class OutputComposite extends StateComposite {
@Override
public void widgetSelected(SelectionEvent e) {
try {
- OutputComposite.this.saveFile();
+ OutputComposite.this.saveDocument();
} catch (Exception ex) {
log.error("SaveSelectionListener: ", ex); //$NON-NLS-1$
}
@@ -210,19 +339,18 @@ public class OutputComposite extends StateComposite {
@Override
public void widgetSelected(SelectionEvent e) {
try {
-
- if (OutputComposite.this.outputFile != null) {
-
- if (OutputComposite.this.outputFile.exists()) {
- // Desktop supported check already done in constructor
- if (Desktop.isDesktopSupported()) {
- Desktop.getDesktop().open(
- OutputComposite.this.outputFile);
- } else {
- log.error("SWT Desktop is not supported on this platform!"); //$NON-NLS-1$
- }
- return;
- }
+ if (OutputComposite.this.outputFile == null)
+ return;
+
+ if (!OutputComposite.this.outputFile.exists())
+ return;
+
+ // Desktop supported check already done in constructor
+ if (Desktop.isDesktopSupported()) {
+ Desktop.getDesktop().open(
+ OutputComposite.this.outputFile);
+ } else {
+ log.error("SWT Desktop is not supported on this platform!"); //$NON-NLS-1$
}
} catch (Exception ex) {
log.error("OpenSelectionListener: ", ex); //$NON-NLS-1$
@@ -230,114 +358,11 @@ public class OutputComposite extends StateComposite {
}
}
- File savedFile = null;
-
- private DocumentSource signedDocument;
-
- /**
- * Create the composite.
- *
- * @param parent
- * The parent composite
- * @param style
- * The swt style
- * @param state
- * The owning state
- */
- public OutputComposite(Composite parent, int style, State state) {
- super(parent, style, state);
-
- this.setLayout(new FormLayout());
-
- this.lbl_success_message = new Label(this, SWT.NATIVE | SWT.RESIZE);
- FormData fd_lbl_success_message = new FormData();
- fd_lbl_success_message.top = new FormAttachment(40, 0);
- fd_lbl_success_message.left = new FormAttachment(0);
- fd_lbl_success_message.right = new FormAttachment(100);
- this.lbl_success_message.setLayoutData(fd_lbl_success_message);
- this.lbl_success_message.setAlignment(SWT.CENTER);
- this.lbl_success_message.setText(Messages
- .getString("output.success_message")); //$NON-NLS-1$
-
- FontData[] fD1 = this.lbl_success_message.getFont().getFontData();
- fD1[0].setHeight(Constants.TEXT_SIZE_BIG);
- this.lbl_success_message.setFont(new Font(Display.getCurrent(), fD1[0]));
-
- this.lnk_saved_file = new Link(this, SWT.NATIVE | SWT.RESIZE);
- this.lnk_saved_file.setText(Messages
- .getString("output.link_save_message")); //$NON-NLS-1$
- FormData fd_lnk_saved_file = new FormData();
- fd_lnk_saved_file.top = new FormAttachment(this.lbl_success_message, 10);
- fd_lnk_saved_file.left = new FormAttachment(this.lbl_success_message, 0,
- SWT.CENTER);
- // fd_lnk_saved_file.right = new FormAttachment(100);
- this.lnk_saved_file.setLayoutData(fd_lnk_saved_file);
-
- this.lnk_saved_file.addSelectionListener(new OpenSelectionListener());
-
- FontData[] fD2 = this.lnk_saved_file.getFont().getFontData();
- fD2[0].setHeight(Constants.TEXT_SIZE_NORMAL);
- this.lnk_saved_file.setFont(new Font(Display.getCurrent(), fD2[0]));
-
- this.btn_save = new Button(this, SWT.NATIVE | SWT.RESIZE);
- this.btn_save.setText(Messages.getString("common.Save")); //$NON-NLS-1$
-
- FontData[] fD_btn_save = this.btn_save.getFont().getFontData();
- fD_btn_save[0].setHeight(Constants.TEXT_SIZE_BUTTON);
- this.btn_save.setFont(new Font(Display.getCurrent(), fD_btn_save[0]));
-
- FormData fd_btn_save = new FormData();
- fd_btn_save.top = new FormAttachment(this.lnk_saved_file, 10);
- fd_btn_save.left = new FormAttachment(this.lnk_saved_file, 0,
- SWT.CENTER);
- this.btn_save.setLayoutData(fd_btn_save);
-
- this.btn_save.setVisible(false);
- this.btn_save.addSelectionListener(new SaveSelectionListener());
-
- }
-
- String tempDirectory;
-
- /**
- * @param tempDirectory
- */
- public void setTempDirectory(String tempDirectory) {
- this.tempDirectory = tempDirectory;
- }
-
- /**
- * Gets the signed document
- *
- * @return the signed document
- */
- public DocumentSource getSignedDocument() {
- return this.signedDocument;
- }
-
- /**
- * Sets the signed document
- *
- * @param signedDocument
- * the signed document
- */
- public void setSignedDocument(final DocumentSource signedDocument) {
- this.signedDocument = signedDocument;
- }
-
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
- private boolean save_showed = false;
-
- private Link lnk_saved_file;
-
- private Button btn_save;
-
- private Label lbl_success_message;
-
/*
* (non-Javadoc)
*
@@ -347,14 +372,6 @@ public class OutputComposite extends StateComposite {
public void doLayout() {
// Nothing to do
this.layout(true);
- try {
- if (!this.save_showed) {
- OutputComposite.this.saveFile();
- this.save_showed = true;
- }
- } catch (Exception ex) {
- log.error("SaveSelectionListener: ", ex); //$NON-NLS-1$
- }
}
/* (non-Javadoc)
@@ -364,6 +381,13 @@ public class OutputComposite extends StateComposite {
public void reloadResources() {
this.lbl_success_message.setText(Messages
.getString("output.success_message")); //$NON-NLS-1$
+ if (this.outputFile == null) {
+ this.lnk_saved_file.setText(Messages
+ .getString("output.link_save_message")); //$NON-NLS-1$
+ } else {
+ this.lnk_saved_file.setText(Messages
+ .getString("output.link_open_message")); //$NON-NLS-1$
+ }
+ this.btn_save.setText(Messages.getString("common.Save")); //$NON-NLS-1$
}
-
}
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OutputState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OutputState.java
index cdf8694a..64df41f6 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OutputState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OutputState.java
@@ -17,30 +17,22 @@ package at.asit.pdfover.gui.workflow.states;
//Imports
import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
import org.eclipse.swt.SWT;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import at.asit.pdfover.gui.MainWindow.Buttons;
import at.asit.pdfover.gui.MainWindowBehavior;
import at.asit.pdfover.gui.composites.OutputComposite;
-import at.asit.pdfover.gui.controls.Dialog;
-import at.asit.pdfover.gui.controls.ErrorDialog;
-import at.asit.pdfover.gui.controls.Dialog.BUTTONS;
-import at.asit.pdfover.gui.controls.Dialog.ICON;
-import at.asit.pdfover.gui.utils.Messages;
import at.asit.pdfover.gui.workflow.StateMachine;
import at.asit.pdfover.gui.workflow.Status;
-import at.asit.pdfover.signator.DocumentSource;
/**
* Procduces the output of the signature process. (save file, open file)
*/
public class OutputState extends State {
+ private OutputComposite outputComposite = null;
+
/**
* @param stateMachine
*/
@@ -48,15 +40,7 @@ public class OutputState extends State {
super(stateMachine);
}
- /**
- * SFL4J Logger instance
- **/
- private static final Logger log = LoggerFactory
- .getLogger(OutputState.class);
-
- private OutputComposite outputComposite = null;
-
- private OutputComposite getSelectionComposite() {
+ private OutputComposite getOutputComposite() {
if (this.outputComposite == null) {
this.outputComposite = this.stateMachine.getGUIProvider()
.createComposite(OutputComposite.class, SWT.RESIZE, this);
@@ -68,48 +52,31 @@ public class OutputState extends State {
}
this.outputComposite.setOutputDir(this.stateMachine.getConfigProvider().getDefaultOutputFolder());
- this.outputComposite.setTempDirectory(tmpDir.getAbsolutePath());
+ this.outputComposite.setTempDir(tmpDir.getAbsolutePath());
this.outputComposite.setInputFile(this.stateMachine.getStatus().getDocument());
}
return this.outputComposite;
}
- private boolean saved = false;
-
@Override
public void run() {
Status status = this.stateMachine.getStatus();
- if (status.getSignResult() != null) {
- OutputComposite outputComposite = this.getSelectionComposite();
- outputComposite.setSignedDocument(status.getSignResult()
- .getSignedDocument());
- this.stateMachine.getGUIProvider().display(outputComposite);
-
- if (!this.saved) {
- this.saved = true;
- String outputFolder = this.stateMachine.getConfigProvider()
- .getDefaultOutputFolder();
- String fileName = status.getDocument().getName();
- if (outputFolder != null && !outputFolder.trim().equals("")) { //$NON-NLS-1$
- DocumentSource signedDocument = status.getSignResult().getSignedDocument();
- FileOutputStream output;
- try {
- output = new FileOutputStream(new File(outputFolder + "/" + fileName + "_signed.pdf")); //$NON-NLS-1$ //$NON-NLS-2$
- output.write(signedDocument.getByteArray(), 0,
- signedDocument.getByteArray().length);
- output.close();
- } catch (IOException e) {
- log.error("Failed to save signed document to configured output folder.", e); //$NON-NLS-1$
- ErrorDialog dialog = new ErrorDialog(outputComposite.getShell(),
- Messages.getString("error.SaveOutputFolder"), BUTTONS.OK); //$NON-NLS-1$
- dialog.open();
- }
- }
- }
-
+ if (status.getSignResult() == null) {
+ // TODO
+ return;
}
+
+ OutputComposite outputComposite = this.getOutputComposite();
+ outputComposite.setSignedDocument(status.getSignResult()
+ .getSignedDocument());
+
+ // Save signed document
+ outputComposite.saveDocument();
+
+ // Display dialog
+ this.stateMachine.getGUIProvider().display(outputComposite);
}
/*