1 8 package com.mountainminds.eclemma.internal.ui.wizards; 9 10 import java.io.File ; 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 48 public class SessionExportPage1 extends WizardPage { 49 50 private static final String ID = "SessionExportPage1"; 52 private static final int LIST_HEIGHT = 120; 53 private static final int TEXT_FIELD_WIDTH = 250; 54 55 private static final String STORE_PREFIX = ID + "."; private static final String STORE_FORMAT = STORE_PREFIX + "format"; private static final String STORE_DESTINATIONS = STORE_PREFIX + "destinations"; private static final String STORE_OPENREPORT = STORE_PREFIX + "openreport"; 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 IPath path = Path.fromOSString(destinationcombo.getText()); 109 path = path.removeFileExtension(); 110 String 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 ext = ISessionExporter.DEFAULT_EXTENSIONS[formatcombo.getSelectionIndex()]; 144 fd.setFilterExtensions(new String [] { "*." + ext, "*.*"} ); String file = fd.open(); 146 if (file != null) { 147 destinationcombo.setText(file); 148 } 149 } 150 151 private void update() { 152 if (getSelectedSession() == null) { 154 setErrorMessage(UIMessages.ExportReportPage1NoSession_message); 155 setPageComplete(false); 156 return; 157 } 158 if (getDestination().length() == 0) { 160 setMessage(UIMessages.ExportReportPage1MissingDestination_message); 161 setPageComplete(false); 162 return; 163 } 164 File f = new File (getDestination()); 166 File p = f.getParentFile(); 167 if (f.isDirectory() || (p != null && !p.isDirectory())) { 168 setErrorMessage(UIMessages.ExportReportPage1InvalidDestination_message); 169 setPageComplete(false); 170 return; 171 } 172 String exta = Path.fromOSString(getDestination()).getFileExtension(); 174 String 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 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 getDestination() { 213 return destinationcombo.getText().trim(); 214 } 215 216 public boolean getOpenReport() { 217 return opencheckbox.getSelection(); 218 } 219 220 } 221 | Popular Tags |