Commit | Line | Data |
---|---|---|
6de2f761 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011, 2013 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.linuxtools.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.linuxtools.internal.gdbtrace.ui.GdbTraceUiPlugin; | |
30 | import org.eclipse.swt.SWT; | |
31 | import org.eclipse.swt.events.SelectionAdapter; | |
32 | import org.eclipse.swt.events.SelectionEvent; | |
33 | import org.eclipse.swt.graphics.Font; | |
34 | import org.eclipse.swt.layout.GridData; | |
35 | import org.eclipse.swt.layout.GridLayout; | |
36 | import org.eclipse.swt.widgets.Button; | |
37 | import org.eclipse.swt.widgets.Composite; | |
38 | import org.eclipse.swt.widgets.Control; | |
39 | import org.eclipse.swt.widgets.Event; | |
40 | import org.eclipse.swt.widgets.FileDialog; | |
41 | import org.eclipse.swt.widgets.Label; | |
42 | import org.eclipse.swt.widgets.Listener; | |
43 | import org.eclipse.swt.widgets.Shell; | |
44 | import org.eclipse.swt.widgets.Text; | |
45 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
46 | ||
47 | /** | |
48 | * Dialog implementation for the Select Trace Executable command | |
49 | * | |
50 | * @author Francois Chouinard | |
51 | */ | |
52 | public class SelectTraceExecutableDialog extends SelectionStatusDialog { | |
53 | ||
54 | private final IStatus OK_STATUS = new Status(IStatus.OK, GdbTraceUiPlugin.PLUGIN_ID, ""); //$NON-NLS-1$ | |
55 | private final IStatus PATH_ERROR_STATUS = new Status(IStatus.ERROR, GdbTraceUiPlugin.PLUGIN_ID, Messages.SelectTraceExecutableDialog_Message); | |
56 | private final IStatus BINARY_ERROR_STATUS = new Status(IStatus.ERROR, GdbTraceUiPlugin.PLUGIN_ID, Messages.SelectTraceExecutableDialog_BinaryError); | |
57 | ||
58 | private Text fExecutableNameEntry; | |
59 | private IPath fExecutablePath; | |
60 | ||
61 | /** | |
62 | * Creates a SelectTraceExecutableDialog | |
63 | * | |
64 | * @param parentShell parent of the new dialog | |
65 | */ | |
66 | public SelectTraceExecutableDialog(Shell parentShell) { | |
67 | super(parentShell); | |
68 | setTitle(Messages.SelectTraceExecutableDialog_Title); | |
69 | setStatusLineAboveButtons(true); | |
70 | setHelpAvailable(false); | |
71 | } | |
72 | ||
73 | @Override | |
74 | protected void computeResult() { | |
75 | setResult(Arrays.asList(new IPath[] { fExecutablePath })); | |
76 | } | |
77 | ||
78 | @Override | |
79 | public void create() { | |
80 | super.create(); | |
81 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
82 | } | |
83 | ||
84 | @Override | |
85 | protected Control createDialogArea(Composite parent) { | |
86 | Composite composite = (Composite) super.createDialogArea(parent); | |
87 | composite.setLayout(new GridLayout()); | |
88 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
89 | ||
90 | createFolderNameGroup(composite); | |
91 | ||
92 | setStatusLineAboveButtons(true); | |
93 | updateStatus(PATH_ERROR_STATUS); | |
94 | ||
95 | return composite; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Creates the folder name specification controls. | |
100 | * | |
101 | * @param parent the parent composite | |
102 | */ | |
103 | private void createFolderNameGroup(Composite parent) { | |
104 | final Shell shell = parent.getShell(); | |
105 | Font font = parent.getFont(); | |
106 | Composite composite = new Composite(parent, SWT.NONE); | |
107 | GridLayout layout = new GridLayout(3, false); | |
108 | composite.setLayout(layout); | |
109 | composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
110 | ||
111 | // Executable name label | |
112 | Label label = new Label(composite, SWT.NONE); | |
113 | label.setFont(font); | |
114 | label.setText(Messages.SelectTraceExecutableDialog_ExecutableName); | |
115 | ||
116 | // Executable name entry field | |
117 | fExecutableNameEntry = new Text(composite, SWT.BORDER); | |
118 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
119 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
120 | fExecutableNameEntry.setLayoutData(data); | |
121 | fExecutableNameEntry.setFont(font); | |
122 | fExecutableNameEntry.addListener(SWT.Modify, new Listener() { | |
123 | @Override | |
124 | public void handleEvent(Event event) { | |
125 | fExecutablePath = Path.fromOSString(fExecutableNameEntry.getText()); | |
126 | if (! validateExecutableName()) { | |
127 | updateStatus(PATH_ERROR_STATUS); | |
128 | } else if (! validateExecutableBinary()) { | |
129 | updateStatus(BINARY_ERROR_STATUS); | |
130 | } else { | |
131 | updateStatus(OK_STATUS); | |
132 | } | |
133 | } | |
134 | }); | |
135 | ||
136 | // Browse button | |
137 | Button browseExecutableButton = new Button(composite, SWT.NONE); | |
138 | browseExecutableButton.setText(Messages.SelectTraceExecutableDialog_Browse); | |
139 | browseExecutableButton.addSelectionListener(new SelectionAdapter() { | |
140 | @Override | |
141 | public void widgetSelected(SelectionEvent event) { | |
142 | FileDialog dlg = new FileDialog(shell); | |
143 | String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString(); | |
144 | dlg.setFilterPath(workspacePath); | |
145 | dlg.setText(Messages.SelectTraceExecutableDialog_ExecutablePrompt); | |
146 | String path = dlg.open(); | |
147 | if (path != null) { | |
148 | fExecutableNameEntry.setText(path); | |
149 | } | |
150 | } | |
151 | }); | |
152 | ||
153 | } | |
154 | ||
155 | /** | |
156 | * Checks whether the executable location is valid. | |
157 | */ | |
158 | private boolean validateExecutableName() { | |
159 | if (fExecutablePath != null) { | |
160 | File file = new File(fExecutablePath.toOSString()); | |
161 | return file.exists() && file.isFile(); | |
162 | } | |
163 | return false; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Checks whether the executable location is a recognized executable. | |
168 | */ | |
169 | private boolean validateExecutableBinary() { | |
170 | try { | |
171 | IBinaryParser parser = CCorePlugin.getDefault().getDefaultBinaryParser(); | |
172 | IBinaryParser.IBinaryFile binary = parser.getBinary(fExecutablePath); | |
173 | if (binary instanceof IBinaryParser.IBinaryObject) { | |
174 | return true; | |
175 | } | |
176 | } catch (CoreException e) { | |
177 | } catch (IOException e) { | |
178 | } | |
179 | return false; | |
180 | } | |
181 | ||
182 | /** | |
183 | * Returns the selected executable path. | |
184 | * @return the executable path | |
185 | */ | |
186 | public IPath getExecutablePath() { | |
187 | return fExecutablePath; | |
188 | } | |
189 | ||
190 | } |