KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > ui > wizards > SessionExportWizard


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: SessionExportWizard.java 86 2006-09-18 03:20:28Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.ui.wizards;
9
10 import java.io.File JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
37  * The export wizard for coverage sessions.
38  *
39  * @author Marc R. Hoffmann
40  * @version $Revision: 86 $
41  */

42 public class SessionExportWizard extends Wizard implements IExportWizard {
43
44   private static final String JavaDoc SETTINGSID = "SessionExportWizard"; //$NON-NLS-1$
45

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 JavaDoc, InterruptedException JavaDoc {
89         try {
90           exporter.export(monitor);
91         } catch (Exception JavaDoc e) {
92           throw new InvocationTargetException JavaDoc(e);
93         }
94       }
95     };
96     try {
97       getContainer().run(true, true, op);
98     } catch (InterruptedException JavaDoc e) {
99       return false;
100     } catch (InvocationTargetException JavaDoc ite) {
101       Throwable JavaDoc ex = ite.getTargetException();
102       EclEmmaUIPlugin.log(ex);
103       String JavaDoc title = UIMessages.ExportReportErrorDialog_title;
104       String JavaDoc 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 JavaDoc f = new File JavaDoc(page1.getDestination());
121     String JavaDoc 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 JavaDoc getEditorId(File JavaDoc 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