KickJava   Java API By Example, From Geeks To Geeks.

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


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: SessionExportPage1.java 137 2006-10-18 09:29:27Z mtnminds $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.ui.wizards;
9
10 import java.io.File JavaDoc;
11
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.core.runtime.Path;
14 import org.eclipse.jface.dialogs.IDialogSettings;
15 import org.eclipse.jface.viewers.ArrayContentProvider;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.viewers.TableViewer;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.FileDialog;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.ui.model.WorkbenchLabelProvider;
35
36 import com.mountainminds.eclemma.core.CoverageTools;
37 import com.mountainminds.eclemma.core.ICoverageSession;
38 import com.mountainminds.eclemma.core.ISessionExporter;
39 import com.mountainminds.eclemma.internal.ui.UIMessages;
40
41 /**
42  * This wizard page allows selecting a coverage session, the output format and
43  * destination.
44  *
45  * @author Marc R. Hoffmann
46  * @version $Revision: 137 $
47  */

48 public class SessionExportPage1 extends WizardPage {
49   
50   private static final String JavaDoc ID = "SessionExportPage1"; //$NON-NLS-1$
51

52   private static final int LIST_HEIGHT = 120;
53   private static final int TEXT_FIELD_WIDTH = 250;
54   
55   private static final String JavaDoc STORE_PREFIX = ID + "."; //$NON-NLS-1$
56
private static final String JavaDoc STORE_FORMAT = STORE_PREFIX + "format"; //$NON-NLS-1$
57
private static final String JavaDoc STORE_DESTINATIONS = STORE_PREFIX + "destinations"; //$NON-NLS-1$
58
private static final String JavaDoc STORE_OPENREPORT = STORE_PREFIX + "openreport"; //$NON-NLS-1$
59

60   private TableViewer sessionstable;
61   private Combo formatcombo;
62   private Combo destinationcombo;
63   private Button opencheckbox;
64   
65   public SessionExportPage1() {
66     super(ID);
67     setTitle(UIMessages.ExportReportPage1_title);
68     setDescription(UIMessages.ExportReportPage1_description);
69   }
70
71   public void createControl(Composite parent) {
72     initializeDialogUnits(parent);
73     parent = new Composite(parent, SWT.NONE);
74     parent.setLayout(new GridLayout());
75     new Label(parent, SWT.NONE).setText(UIMessages.ExportReportPage1Sessions_label);
76     sessionstable = new TableViewer(parent, SWT.BORDER);
77     sessionstable.setLabelProvider(new WorkbenchLabelProvider());
78     sessionstable.setContentProvider(new ArrayContentProvider());
79     sessionstable.setInput(CoverageTools.getSessionManager().getSessions());
80     ICoverageSession active = CoverageTools.getSessionManager().getActiveSession();
81     if (active != null) {
82       sessionstable.setSelection(new StructuredSelection(active));
83     }
84     GridData gd = new GridData(GridData.FILL_BOTH);
85     gd.heightHint = LIST_HEIGHT;
86     sessionstable.getControl().setLayoutData(gd);
87     Group group = new Group(parent, SWT.NONE);
88     group.setText(UIMessages.ExportReportPage1DestinationGroup_label);
89     group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
90     createExportOptionsGroup(group);
91     opencheckbox = new Button(parent, SWT.CHECK);
92     opencheckbox.setText(UIMessages.ExportReportOpenReport_label);
93     setControl(parent);
94     restoreWidgetValues();
95   }
96   
97   private void createExportOptionsGroup(Composite parent) {
98     parent.setLayout(new GridLayout(3, false));
99     new Label(parent, SWT.NONE).setText(UIMessages.ExportReportPage1Format_label);
100     formatcombo = new Combo(parent, SWT.READ_ONLY);
101     formatcombo.add(UIMessages.ExportReportPage1HTMLFormat_value);
102     formatcombo.add(UIMessages.ExportReportPage1XMLFormat_value);
103     formatcombo.add(UIMessages.ExportReportPage1TextFormat_value);
104     formatcombo.add(UIMessages.ExportReportPage1EMMAFormat_value);
105     formatcombo.addSelectionListener(new SelectionAdapter() {
106       public void widgetSelected(SelectionEvent e) {
107         // Adjust the extension to the new format
108
IPath path = Path.fromOSString(destinationcombo.getText());
109         path = path.removeFileExtension();
110         String JavaDoc ext = ISessionExporter.DEFAULT_EXTENSIONS[formatcombo.getSelectionIndex()];
111         path = path.addFileExtension(ext);
112         destinationcombo.setText(path.toOSString());
113       }
114     });
115     GridData gd = new GridData(GridData.FILL_HORIZONTAL);
116     gd.horizontalSpan = 2;
117     formatcombo.setLayoutData(gd);
118     new Label(parent, SWT.NONE).setText(UIMessages.ExportReportPage1Destination_label);
119     destinationcombo = new Combo(parent, SWT.BORDER);
120     destinationcombo.addModifyListener(new ModifyListener() {
121       public void modifyText(ModifyEvent e) {
122         update();
123       }
124     });
125     gd = new GridData(GridData.FILL_HORIZONTAL);
126     gd.widthHint = TEXT_FIELD_WIDTH;
127     destinationcombo.setLayoutData(gd);
128     Button browsebutton = new Button(parent, SWT.NONE);
129     browsebutton.setText(UIMessages.BrowseAction_label);
130     setButtonLayoutData(browsebutton);
131     browsebutton.addSelectionListener(new SelectionAdapter() {
132       public void widgetSelected(SelectionEvent e) {
133         openBrowseDialog();
134       }
135     });
136     update();
137   }
138   
139   private void openBrowseDialog() {
140     FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
141     fd.setText(UIMessages.ExportReportPage1BrowseDialog_title);
142     fd.setFileName(destinationcombo.getText());
143     String JavaDoc ext = ISessionExporter.DEFAULT_EXTENSIONS[formatcombo.getSelectionIndex()];
144     fd.setFilterExtensions(new String JavaDoc[] { "*." + ext, "*.*"} ); //$NON-NLS-1$ //$NON-NLS-2$
145
String JavaDoc file = fd.open();
146     if (file != null) {
147       destinationcombo.setText(file);
148     }
149   }
150   
151   private void update() {
152     // make sure we have a session to export
153
if (getSelectedSession() == null) {
154       setErrorMessage(UIMessages.ExportReportPage1NoSession_message);
155       setPageComplete(false);
156       return;
157     }
158     // a destination file must be spezified
159
if (getDestination().length() == 0) {
160       setMessage(UIMessages.ExportReportPage1MissingDestination_message);
161       setPageComplete(false);
162       return;
163     }
164     // the destination must be a file and must be in a existing directory
165
File JavaDoc f = new File JavaDoc(getDestination());
166     File JavaDoc p = f.getParentFile();
167     if (f.isDirectory() || (p != null && !p.isDirectory())) {
168       setErrorMessage(UIMessages.ExportReportPage1InvalidDestination_message);
169       setPageComplete(false);
170       return;
171     }
172     // the extension should correspond to the report type
173
String JavaDoc exta = Path.fromOSString(getDestination()).getFileExtension();
174     String JavaDoc exte = ISessionExporter.DEFAULT_EXTENSIONS[getReportFormat()];
175     if (!exte.equalsIgnoreCase(exta)) {
176       setMessage(NLS.bind(UIMessages.ExportReportPage1WrongExtension_message, exte), WARNING);
177       setPageComplete(true);
178       return;
179     }
180     setErrorMessage(null);
181     setMessage(null);
182     setPageComplete(true);
183   }
184   
185   protected void restoreWidgetValues() {
186     IDialogSettings settings = getDialogSettings();
187     try {
188       formatcombo.select(settings.getInt(STORE_FORMAT));
189     } catch (NumberFormatException JavaDoc nfe) {
190       formatcombo.select(0);
191     }
192     ComboHistory.restore(settings, STORE_DESTINATIONS, destinationcombo);
193     opencheckbox.setSelection(settings.getBoolean(STORE_OPENREPORT));
194   }
195   
196   public void saveWidgetValues() {
197     IDialogSettings settings = getDialogSettings();
198     settings.put(STORE_FORMAT, formatcombo.getSelectionIndex());
199     ComboHistory.save(settings, STORE_DESTINATIONS, destinationcombo);
200     settings.put(STORE_OPENREPORT, opencheckbox.getSelection());
201   }
202   
203   public ICoverageSession getSelectedSession() {
204     IStructuredSelection sel = (IStructuredSelection) sessionstable.getSelection();
205     return (ICoverageSession) sel.getFirstElement();
206   }
207   
208   public int getReportFormat() {
209     return formatcombo.getSelectionIndex();
210   }
211   
212   public String JavaDoc getDestination() {
213     return destinationcombo.getText().trim();
214   }
215   
216   public boolean getOpenReport() {
217     return opencheckbox.getSelection();
218   }
219
220 }
221
Popular Tags