| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2014 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | * Patrick Tasse - Updated for TMF 2.0 |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.internal.gdbtrace.ui.views.project.dialogs; |
| 15 | |
| 16 | import java.io.File; |
| 17 | import java.io.IOException; |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | import org.eclipse.cdt.core.CCorePlugin; |
| 21 | import org.eclipse.cdt.core.IBinaryParser; |
| 22 | import org.eclipse.core.resources.ResourcesPlugin; |
| 23 | import org.eclipse.core.runtime.CoreException; |
| 24 | import org.eclipse.core.runtime.IPath; |
| 25 | import org.eclipse.core.runtime.IStatus; |
| 26 | import org.eclipse.core.runtime.Path; |
| 27 | import org.eclipse.core.runtime.Status; |
| 28 | import org.eclipse.jface.dialogs.IDialogConstants; |
| 29 | import org.eclipse.swt.SWT; |
| 30 | import org.eclipse.swt.events.SelectionAdapter; |
| 31 | import org.eclipse.swt.events.SelectionEvent; |
| 32 | import org.eclipse.swt.graphics.Font; |
| 33 | import org.eclipse.swt.layout.GridData; |
| 34 | import org.eclipse.swt.layout.GridLayout; |
| 35 | import org.eclipse.swt.widgets.Button; |
| 36 | import org.eclipse.swt.widgets.Composite; |
| 37 | import org.eclipse.swt.widgets.Control; |
| 38 | import org.eclipse.swt.widgets.Event; |
| 39 | import org.eclipse.swt.widgets.FileDialog; |
| 40 | import org.eclipse.swt.widgets.Label; |
| 41 | import org.eclipse.swt.widgets.Listener; |
| 42 | import org.eclipse.swt.widgets.Shell; |
| 43 | import org.eclipse.swt.widgets.Text; |
| 44 | import org.eclipse.tracecompass.internal.gdbtrace.ui.GdbTraceUIPlugin; |
| 45 | import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory; |
| 46 | import org.eclipse.ui.dialogs.SelectionStatusDialog; |
| 47 | |
| 48 | /** |
| 49 | * Dialog implementation for the Select Trace Executable command |
| 50 | * |
| 51 | * @author Francois Chouinard |
| 52 | */ |
| 53 | public class SelectTraceExecutableDialog extends SelectionStatusDialog { |
| 54 | |
| 55 | private final IStatus OK_STATUS = new Status(IStatus.OK, GdbTraceUIPlugin.PLUGIN_ID, ""); //$NON-NLS-1$ |
| 56 | private final IStatus PATH_ERROR_STATUS = new Status(IStatus.ERROR, GdbTraceUIPlugin.PLUGIN_ID, Messages.SelectTraceExecutableDialog_Message); |
| 57 | private final IStatus BINARY_ERROR_STATUS = new Status(IStatus.ERROR, GdbTraceUIPlugin.PLUGIN_ID, Messages.SelectTraceExecutableDialog_BinaryError); |
| 58 | |
| 59 | private Text fExecutableNameEntry; |
| 60 | private IPath fExecutablePath; |
| 61 | |
| 62 | /** |
| 63 | * Creates a SelectTraceExecutableDialog |
| 64 | * |
| 65 | * @param parentShell parent of the new dialog |
| 66 | */ |
| 67 | public SelectTraceExecutableDialog(Shell parentShell) { |
| 68 | super(parentShell); |
| 69 | setTitle(Messages.SelectTraceExecutableDialog_Title); |
| 70 | setStatusLineAboveButtons(true); |
| 71 | setHelpAvailable(false); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void computeResult() { |
| 76 | setResult(Arrays.asList(new IPath[] { fExecutablePath })); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void create() { |
| 81 | super.create(); |
| 82 | getButton(IDialogConstants.OK_ID).setEnabled(false); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected Control createDialogArea(Composite parent) { |
| 87 | Composite composite = (Composite) super.createDialogArea(parent); |
| 88 | composite.setLayout(new GridLayout()); |
| 89 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 90 | |
| 91 | createFolderNameGroup(composite); |
| 92 | |
| 93 | setStatusLineAboveButtons(true); |
| 94 | updateStatus(PATH_ERROR_STATUS); |
| 95 | |
| 96 | return composite; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates the folder name specification controls. |
| 101 | * |
| 102 | * @param parent the parent composite |
| 103 | */ |
| 104 | private void createFolderNameGroup(Composite parent) { |
| 105 | final Shell shell = parent.getShell(); |
| 106 | Font font = parent.getFont(); |
| 107 | Composite composite = new Composite(parent, SWT.NONE); |
| 108 | GridLayout layout = new GridLayout(3, false); |
| 109 | composite.setLayout(layout); |
| 110 | composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 111 | |
| 112 | // Executable name label |
| 113 | Label label = new Label(composite, SWT.NONE); |
| 114 | label.setFont(font); |
| 115 | label.setText(Messages.SelectTraceExecutableDialog_ExecutableName); |
| 116 | |
| 117 | // Executable name entry field |
| 118 | fExecutableNameEntry = new Text(composite, SWT.BORDER); |
| 119 | GridData data = new GridData(GridData.FILL_HORIZONTAL); |
| 120 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; |
| 121 | fExecutableNameEntry.setLayoutData(data); |
| 122 | fExecutableNameEntry.setFont(font); |
| 123 | fExecutableNameEntry.addListener(SWT.Modify, new Listener() { |
| 124 | @Override |
| 125 | public void handleEvent(Event event) { |
| 126 | fExecutablePath = Path.fromOSString(fExecutableNameEntry.getText()); |
| 127 | if (! validateExecutableName()) { |
| 128 | updateStatus(PATH_ERROR_STATUS); |
| 129 | } else if (! validateExecutableBinary()) { |
| 130 | updateStatus(BINARY_ERROR_STATUS); |
| 131 | } else { |
| 132 | updateStatus(OK_STATUS); |
| 133 | } |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | // Browse button |
| 138 | Button browseExecutableButton = new Button(composite, SWT.NONE); |
| 139 | browseExecutableButton.setText(Messages.SelectTraceExecutableDialog_Browse); |
| 140 | browseExecutableButton.addSelectionListener(new SelectionAdapter() { |
| 141 | @Override |
| 142 | public void widgetSelected(SelectionEvent event) { |
| 143 | FileDialog dlg = TmfFileDialogFactory.create(shell); |
| 144 | String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString(); |
| 145 | dlg.setFilterPath(workspacePath); |
| 146 | dlg.setText(Messages.SelectTraceExecutableDialog_ExecutablePrompt); |
| 147 | String path = dlg.open(); |
| 148 | if (path != null) { |
| 149 | fExecutableNameEntry.setText(path); |
| 150 | } |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Checks whether the executable location is valid. |
| 158 | */ |
| 159 | private boolean validateExecutableName() { |
| 160 | if (fExecutablePath != null) { |
| 161 | File file = new File(fExecutablePath.toOSString()); |
| 162 | return file.exists() && file.isFile(); |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Checks whether the executable location is a recognized executable. |
| 169 | */ |
| 170 | private boolean validateExecutableBinary() { |
| 171 | try { |
| 172 | IBinaryParser parser = CCorePlugin.getDefault().getDefaultBinaryParser(); |
| 173 | IBinaryParser.IBinaryFile binary = parser.getBinary(fExecutablePath); |
| 174 | if (binary instanceof IBinaryParser.IBinaryObject) { |
| 175 | return true; |
| 176 | } |
| 177 | } catch (CoreException e) { |
| 178 | } catch (IOException e) { |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the selected executable path. |
| 185 | * @return the executable path |
| 186 | */ |
| 187 | public IPath getExecutablePath() { |
| 188 | return fExecutablePath; |
| 189 | } |
| 190 | |
| 191 | } |