package org.eparapher.rcp.tools; import org.apache.log4j.Logger; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.window.Window; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.statushandlers.StatusManager; import org.eparapher.core.crypto.EPKeystoreManager; import org.eparapher.core.interfaces.UserInterface; import org.eparapher.rcp.Application; import org.eparapher.rcp.EPReferences; import org.eparapher.rcp.preferences.CMSSignaturePreferencePage; import org.eparapher.rcp.preferences.EParapherPreferencePage; import org.eparapher.rcp.preferences.KeystorePreferencePage; import org.eparapher.rcp.preferences.OpenOfficeEditorPreferences; import org.eparapher.rcp.preferences.PDFSignaturePreferencePage; import org.eparapher.rcp.preferences.SecretPolicyPreferencePage; import org.eparapher.rcp.preferences.SignaturePreferencePage; import org.eparapher.rcp.preferences.XMLSignaturePreferencePage; import org.eparapher.rcp.wizards.NewCertificateWizard; import org.eparapher.rcp.wizards.PinOrPassphraseWizard; public class RCPGUI implements UserInterface { private static Logger log = Logger.getLogger(RCPGUI.class); public boolean askUserText(String message) { Shell sh = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); return MessageDialog.openQuestion(sh, "eParapher question", message); } public boolean askUserYesNo(String msg) { return askUserYesNo(msg, SWT.ICON_WARNING); } public void errorMessage(String msg) { IStatus status = new Status(IStatus.ERROR,Application.PLUGIN_ID,msg); StatusManager.getManager().handle(status, StatusManager.BLOCK); log.error(msg); } public void errorMessage(String message, Throwable t) { IStatus status = new Status(IStatus.ERROR,Application.PLUGIN_ID,message,t); StatusManager.getManager().handle(status, StatusManager.BLOCK); log.error(message,t); } public void infoMessage(String msg) { infoMessage("eParapher information", msg); } public static void infoMessage(String title, String message) { IStatus status = new Status(IStatus.INFO,Application.PLUGIN_ID,message); StatusManager.getManager().handle(status, StatusManager.SHOW); log.info(message); } public void warnMessage(String msg) { warnMessage("eParapher Warning", msg); } public static void warnMessage(String title, String msg) { IStatus status = new Status(IStatus.WARNING,Application.PLUGIN_ID,msg); StatusManager.getManager().handle(status, StatusManager.SHOW); log.warn(msg); } public void debugMessage(String msg) { debugMessage("eParapher Debug", msg); } public static void debugMessage(String title, String msg) { IStatus status = new Status(IStatus.WARNING,Application.PLUGIN_ID,msg); StatusManager.getManager().handle(status, StatusManager.BLOCK); log.error(msg); } public String askUserKeystoreSecret(boolean isPrivateKey,boolean isNewSecret, String alias) { String current_default_alias = EPKeystoreManager.getInstance().getUserkeystore().getDefaultAlias(); EPKeystoreManager.getInstance().getUserkeystore().setDefaultAlias(alias); final PinOrPassphraseWizard passphraseWizard = new PinOrPassphraseWizard(isPrivateKey, isNewSecret, alias ); try { Display.getDefault().syncExec( new Runnable() { public void run() { passphraseWizard.askForSecret(); } } ); } catch (Exception e) { errorMessage(e.getLocalizedMessage(), e); log.error(e.getLocalizedMessage(),e); } if (current_default_alias!=null) EPKeystoreManager.getInstance().getUserkeystore().setDefaultAlias(current_default_alias); return passphraseWizard.getSecret(); } public boolean askUserNewCertificate() { NewCertificateWizard newCertWizard = new NewCertificateWizard(); WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), newCertWizard); if ( dialog.open() != Window.OK ) { log.info("New certificate wizard cancelled"); return false; } else { if (EPReferences.getInstance().getUsercertview()!=null) EPReferences.getInstance().getUsercertview().refreshView(); } return true; } /** * Ask the user for an answer. * @param message The message for the User * @param swt_icon_type Dialog icon. must be SWT.ICON_ERROR SWT.ICON_WARNING SWT.ICON_WORKING SWT.ICON_INFORMATION SWT.ICON_QUESTION * @return true if the user click OK */ public static boolean askUserYesNo(String message, int swt_icon_type) { AskYesNo popup = new AskYesNo(message, swt_icon_type); Display.getDefault().syncExec(popup); return popup.getAnswer(); } public void showKeystoreSettings() { eParapherTools.showPrefGUI(KeystorePreferencePage.ID, new String[] { KeystorePreferencePage.ID, SecretPolicyPreferencePage.ID, EParapherPreferencePage.ID}); } public void showSignatureSettings() { eParapherTools.showPrefGUI(SignaturePreferencePage.ID, new String[] { PDFSignaturePreferencePage.ID, XMLSignaturePreferencePage.ID, CMSSignaturePreferencePage.ID}); } public void showOpenOfficeSettings() { eParapherTools.showPrefGUI(OpenOfficeEditorPreferences.ID, new String[] { }); } public void showeParapherSettings() { String[] listsPrefs = { CMSSignaturePreferencePage.ID, PDFSignaturePreferencePage.ID, OpenOfficeEditorPreferences.ID, KeystorePreferencePage.ID, SecretPolicyPreferencePage.ID, EParapherPreferencePage.ID, SignaturePreferencePage.ID, XMLSignaturePreferencePage.ID }; eParapherTools.showPrefGUI(EParapherPreferencePage.ID, listsPrefs ); } public void showAllSettings() { eParapherTools.showPrefGUI(KeystorePreferencePage.ID, new String[] { KeystorePreferencePage.ID, SecretPolicyPreferencePage.ID, EParapherPreferencePage.ID}); } public void refreshCertificateList() { Display.getDefault().asyncExec(new Runnable() { public void run() { EPReferences.getInstance().getUsercertview().refreshView(); } }); } } class AskYesNo implements Runnable { private int swt_icon; private String msg; private boolean answer; public AskYesNo(String m_msg, int m_swt_icon) { this.msg = m_msg; this.swt_icon= m_swt_icon; if ( (m_swt_icon != SWT.ICON_ERROR) && (m_swt_icon!= SWT.ICON_WARNING) && (m_swt_icon!= SWT.ICON_WORKING) && (m_swt_icon!= SWT.ICON_INFORMATION) && (m_swt_icon!= SWT.ICON_QUESTION) ) this.swt_icon= SWT.ICON_QUESTION; this.answer = false; } public void run() { Shell sh; if ( Display.getCurrent() != null ) sh = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); else sh = new Shell(); MessageBox messageBox = new MessageBox(sh, SWT.OK | SWT.CANCEL | this.swt_icon); messageBox.setMessage(this.msg); this.answer = messageBox.open() == SWT.OK ? true : false; } public boolean getAnswer() { return this.answer; } }