1 8 package com.mountainminds.eclemma.internal.ui.wizards; 9 10 import java.io.File ; 11 import java.lang.reflect.InvocationTargetException ; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.jface.dialogs.ErrorDialog; 17 import org.eclipse.jface.dialogs.IDialogSettings; 18 import org.eclipse.jface.operation.IRunnableWithProgress; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.wizard.Wizard; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.ui.IEditorDescriptor; 23 import org.eclipse.ui.IEditorRegistry; 24 import org.eclipse.ui.IExportWizard; 25 import org.eclipse.ui.IWorkbench; 26 import org.eclipse.ui.IWorkbenchPage; 27 import org.eclipse.ui.PartInitException; 28 import org.eclipse.ui.ide.IDE; 29 30 import com.mountainminds.eclemma.core.CoverageTools; 31 import com.mountainminds.eclemma.core.ICoverageSession; 32 import com.mountainminds.eclemma.core.ISessionExporter; 33 import com.mountainminds.eclemma.internal.ui.EclEmmaUIPlugin; 34 import com.mountainminds.eclemma.internal.ui.UIMessages; 35 36 42 public class SessionExportWizard extends Wizard implements IExportWizard { 43 44 private static final String SETTINGSID = "SessionExportWizard"; 46 private IWorkbench workbench; 47 48 private SessionExportPage1 page1; 49 50 public SessionExportWizard() { 51 IDialogSettings pluginsettings = EclEmmaUIPlugin.getInstance() 52 .getDialogSettings(); 53 IDialogSettings wizardsettings = pluginsettings.getSection(SETTINGSID); 54 if (wizardsettings == null) { 55 wizardsettings = pluginsettings.addNewSection(SETTINGSID); 56 } 57 setDialogSettings(wizardsettings); 58 setWindowTitle(UIMessages.ExportReport_title); 59 setDefaultPageImageDescriptor( 60 EclEmmaUIPlugin.getImageDescriptor(EclEmmaUIPlugin.WIZBAN_EXPORT_SESSION)); 61 setNeedsProgressMonitor(true); 62 } 63 64 public void init(IWorkbench workbench, IStructuredSelection selection) { 65 this.workbench = workbench; 66 } 67 68 public void addPages() { 69 addPage(page1 = new SessionExportPage1()); 70 } 71 72 public boolean performFinish() { 73 page1.saveWidgetValues(); 74 boolean result = createReport(); 75 if (result && page1.getOpenReport()) { 76 openReport(); 77 } 78 return result; 79 } 80 81 private boolean createReport() { 82 ICoverageSession session = page1.getSelectedSession(); 83 final ISessionExporter exporter = CoverageTools.getExporter(session); 84 exporter.setFormat(page1.getReportFormat()); 85 exporter.setDestination(page1.getDestination()); 86 IRunnableWithProgress op = new IRunnableWithProgress() { 87 public void run(IProgressMonitor monitor) 88 throws InvocationTargetException , InterruptedException { 89 try { 90 exporter.export(monitor); 91 } catch (Exception e) { 92 throw new InvocationTargetException (e); 93 } 94 } 95 }; 96 try { 97 getContainer().run(true, true, op); 98 } catch (InterruptedException e) { 99 return false; 100 } catch (InvocationTargetException ite) { 101 Throwable ex = ite.getTargetException(); 102 EclEmmaUIPlugin.log(ex); 103 String title = UIMessages.ExportReportErrorDialog_title; 104 String msg = UIMessages.ExportReportErrorDialog_message; 105 msg = NLS.bind(msg, session.getDescription()); 106 IStatus status; 107 if (ex instanceof CoreException) { 108 status = ((CoreException) ex).getStatus(); 109 } else { 110 status = EclEmmaUIPlugin.errorStatus(String.valueOf(ex.getMessage()), ex); 111 } 112 ErrorDialog.openError(getShell(), title, msg, status); 113 return false; 114 } 115 return true; 116 } 117 118 private void openReport() { 119 IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage(); 120 File f = new File (page1.getDestination()); 121 String editorid = getEditorId(f); 122 if (editorid != null) { 123 try { 124 IDE.openEditor(page, new ExternalFileEditorInput(f), editorid); 125 } catch (PartInitException e) { 126 EclEmmaUIPlugin.log(e); 127 } 128 } 129 } 130 131 private String getEditorId(File file) { 132 IEditorRegistry editorRegistry= workbench.getEditorRegistry(); 133 IEditorDescriptor descriptor = editorRegistry.getDefaultEditor(file.getName()); 134 return descriptor == null ? null : descriptor.getId(); 135 } 136 137 } 138 | Popular Tags |