package org.eparapher.rcp.actions; import java.io.File; import org.apache.log4j.Logger; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.window.Window; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.SWT; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eparapher.core.signature.CMSSignatureParameters; import org.eparapher.core.signature.CMSSigner; import org.eparapher.core.signature.DefaultSignatureParameters; import org.eparapher.core.signature.GenericSigner; import org.eparapher.rcp.tools.GUIIcons; import org.eparapher.rcp.wizards.CMSSigningWizard; public class CMSSignDocumentAction extends GenericSignatureAction { private static Logger log = Logger.getLogger(CMSSignDocumentAction.class); public CMSSignDocumentAction() { super(null, true, "CMS signing"); } public CMSSignDocumentAction(TreeViewer mviewer, boolean museWizard) { super(mviewer, museWizard, "CMS signing"); } public CMSSignDocumentAction(IWorkbenchWindow mwindow) { super(mwindow, "CMS signing"); } public CMSSignDocumentAction(String[] files, boolean museWizard) { super(null, museWizard, "CMS signing"); filesForAction = files; } public void buildAction() { this.setId("CMSSignDocumentAction"); this.setImageDescriptor(GUIIcons.SIGNATURE_ICON); if (!usewizard) { this.setText("CMS Digital Signature"); this.setToolTipText("Sign your document(s) in CMS format with default parameters"); this.setAccelerator(SWT.CTRL | 'R'); } else { this.setText("CMS Digital Signature (wizard)"); this.setToolTipText("Sign your document(s) in CMS format with a wizard"); this.setAccelerator(SWT.CTRL | 'T'); } } protected DefaultSignatureParameters getParameters() { CMSSignatureParameters cmssignparams = new CMSSignatureParameters(); cmssignparams.setCMSSignatureParamsFromPreferences(); if (viewer != null) cmssignparams.setFileSelection(getFileSelectionFromView()); if (filesForAction != null) cmssignparams.setFileSelection(getFileSelectionFromParameters()); if (usewizard) { //Launch Wizard to get CMS Signature Parameters CMSSigningWizard cmsWizard = new CMSSigningWizard(cmssignparams); if (window==null) window=PlatformUI.getWorkbench().getActiveWorkbenchWindow(); WizardDialog dialog = new WizardDialog(window.getShell(), cmsWizard); try { if ( dialog.open() != Window.OK ) { log.warn("CMS signing wizard cancelled"); return null; } } catch (Exception e) { log.debug("Problem while invoking the CMS Settings Signature Wizard",e); } //recover params from wizard cmssignparams = cmsWizard.getCMSSignatureParams(); } return cmssignparams; } @Override GenericSigner getSigner() { return new CMSSigner(); } @Override boolean isFileWithCorrectFormat(File file2verify) { // Always true as CMS can sign any bit return true; } @Override void presignature(IProgressMonitor monitor, GenericSigner signer, DefaultSignatureParameters signparams) { // Nothing to do here } @Override void signFile(IProgressMonitor monitor, File file, GenericSigner signer, DefaultSignatureParameters cmssignparams) { monitor.subTask("Signing "+file.getName()); signer.sign( file.getAbsolutePath(), cmssignparams); monitor.worked(1); } @Override void postsignature(IProgressMonitor monitor, GenericSigner signer, DefaultSignatureParameters signparams) { // Nothing to do here } }