Commit | Line | Data |
---|---|---|
d04ec5a7 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 Ericsson |
d04ec5a7 MK |
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 | * Matthew Khouzam - Initial API and implementation | |
59607b97 | 11 | * Marc-Andre Laperle - Remember last selected directory |
d04ec5a7 MK |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace; |
d04ec5a7 MK |
15 | |
16 | import java.io.File; | |
17 | ||
59607b97 | 18 | import org.eclipse.jface.dialogs.IDialogSettings; |
d04ec5a7 MK |
19 | import org.eclipse.jface.viewers.IStructuredSelection; |
20 | import org.eclipse.swt.SWT; | |
21 | import org.eclipse.swt.events.SelectionEvent; | |
22 | import org.eclipse.swt.events.SelectionListener; | |
23 | import org.eclipse.swt.layout.GridData; | |
24 | import org.eclipse.swt.layout.GridLayout; | |
25 | import org.eclipse.swt.widgets.Button; | |
26 | import org.eclipse.swt.widgets.Composite; | |
27 | import org.eclipse.swt.widgets.DirectoryDialog; | |
28 | import org.eclipse.swt.widgets.Display; | |
29 | import org.eclipse.swt.widgets.FileDialog; | |
30 | import org.eclipse.swt.widgets.Table; | |
31 | import org.eclipse.swt.widgets.TableItem; | |
32 | import org.eclipse.ui.IWorkbench; | |
33 | ||
34 | /** | |
35 | * <b>Select the directories to scan for traces</b> this page is the second of | |
36 | * three pages shown. This one selects the files to be scanned. | |
37 | * | |
38 | * @author Matthew Khouzam | |
d04ec5a7 MK |
39 | */ |
40 | public class ImportTraceWizardSelectDirectoriesPage extends AbstractImportTraceWizardPage { | |
41 | ||
42 | /** | |
43 | * ID | |
44 | */ | |
45 | public static String ID = "org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.ImportTraceWizardPagePopulate"; //$NON-NLS-1$ | |
46 | ||
59607b97 MAL |
47 | private static final String STORE_DIRECTORY_ID = ID + ".STORE_DIRECTORY_ID"; //$NON-NLS-1$ |
48 | ||
d04ec5a7 MK |
49 | /** |
50 | * Constructor. Creates the trace wizard page. | |
51 | * | |
52 | * @param name | |
53 | * The name of the page. | |
54 | * @param selection | |
55 | * The current selection | |
56 | */ | |
57 | protected ImportTraceWizardSelectDirectoriesPage(String name, IStructuredSelection selection) { | |
58 | super(name, selection); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Constructor | |
63 | * | |
64 | * @param workbench | |
65 | * The workbench reference. | |
66 | * @param selection | |
67 | * The current selection | |
68 | */ | |
69 | public ImportTraceWizardSelectDirectoriesPage(IWorkbench workbench, IStructuredSelection selection) { | |
70 | super(workbench, selection); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public void createControl(Composite parent) { | |
75 | super.createControl(parent); | |
76 | final Composite control = (Composite) this.getControl(); | |
77 | control.setLayout(new GridLayout(2, false)); | |
78 | control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
79 | ||
80 | final Table selectedFiles = new Table(control, SWT.H_SCROLL | SWT.V_SCROLL); | |
81 | selectedFiles.clearAll(); | |
82 | selectedFiles.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
83 | selectedFiles.setLinesVisible(true); | |
84 | ||
85 | Composite buttonArea = new Composite(control, SWT.None); | |
86 | buttonArea.setLayout(new GridLayout()); | |
87 | buttonArea.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false)); | |
88 | ||
89 | Button addFile = new Button(buttonArea, SWT.PUSH); | |
90 | addFile.setText(Messages.ImportTraceWizardAddFile); | |
91 | addFile.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); | |
92 | addFile.addSelectionListener(new AddFileHandler()); | |
93 | addFile.setAlignment(SWT.CENTER); | |
94 | ||
95 | Button addDirectory = new Button(buttonArea, SWT.PUSH); | |
96 | addDirectory.setText(Messages.ImportTraceWizardAddDirectory); | |
97 | addDirectory.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); | |
98 | addDirectory.addSelectionListener(new AddDirectoryHandler()); | |
99 | addDirectory.setAlignment(SWT.CENTER); | |
100 | ||
101 | Button removeFile = new Button(buttonArea, SWT.PUSH); | |
102 | removeFile.setText(Messages.ImportTraceWizardRemove); | |
103 | removeFile.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); | |
104 | removeFile.addSelectionListener(new RemoveFileHandler(selectedFiles)); | |
105 | removeFile.setAlignment(SWT.CENTER); | |
106 | ||
107 | // int maxSize = Math.max(addFile.getSize().x, Math.max(addDirectory.getSize().x, removeFile.getSize().x)); | |
108 | // int maxHeight = Math.max(addFile.getSize().y, Math.max(addDirectory.getSize().y, removeFile.getSize().y)); | |
109 | // addFile.setSize(maxSize, maxHeight); | |
110 | // addDirectory.setSize(maxSize, maxHeight); | |
111 | // removeFile.setSize(maxSize, maxHeight); | |
112 | ||
32a0863e | 113 | this.setTitle(Messages.ImportTraceWizardDirectoryTitle); |
d04ec5a7 MK |
114 | } |
115 | ||
116 | private void updateButtons() { | |
117 | BatchImportTraceWizard wiz = getBatchWizard(); | |
118 | updateTable(); | |
119 | wiz.getContainer().updateButtons(); | |
120 | } | |
121 | ||
122 | private void updateTable() { | |
123 | final Table selectedFiles = (Table) ((Composite) getControl()).getChildren()[0]; | |
124 | selectedFiles.clearAll(); | |
125 | selectedFiles.setItemCount(0); | |
126 | for (String s : ((BatchImportTraceWizard) getWizard()).getFileNames()) { | |
127 | TableItem ti = new TableItem(selectedFiles, SWT.None); | |
128 | ti.setText(s); | |
129 | } | |
130 | } | |
131 | ||
132 | @Override | |
133 | public boolean canFlipToNextPage() { | |
134 | final Table selectedFiles = (Table) ((Composite) getControl()).getChildren()[0]; | |
135 | boolean canLoad = selectedFiles.getItemCount() > 0; | |
136 | if (canLoad) { | |
137 | setErrorMessage(null); | |
138 | } else { | |
139 | setErrorMessage(Messages.ImportTraceWizardDirectoryHint); | |
140 | } | |
141 | return canLoad; | |
142 | } | |
143 | ||
144 | private final class AddFileHandler implements SelectionListener { | |
145 | @Override | |
146 | public void widgetSelected(SelectionEvent e) { | |
147 | ||
148 | FileDialog dialog = new | |
149 | FileDialog(Display.getCurrent().getActiveShell(), SWT.NONE); | |
59607b97 MAL |
150 | |
151 | String lastDirectory = getLastSelectedDirectory(); | |
152 | if (lastDirectory != null) { | |
153 | dialog.setFilterPath(lastDirectory); | |
154 | } | |
155 | ||
d04ec5a7 MK |
156 | String fn = dialog.open(); |
157 | if (null != fn) { | |
158 | File f = new File(fn); | |
159 | if (f.exists()) { | |
160 | getBatchWizard().addFileToScan(fn); | |
59607b97 | 161 | saveSelectedDirectory(f.getParentFile()); |
d04ec5a7 MK |
162 | } |
163 | } | |
164 | updateButtons(); | |
165 | } | |
166 | ||
167 | @Override | |
168 | public void widgetDefaultSelected(SelectionEvent e) { | |
169 | } | |
170 | } | |
171 | ||
172 | private final class AddDirectoryHandler implements SelectionListener { | |
173 | @Override | |
174 | public void widgetSelected(SelectionEvent e) { | |
175 | ||
176 | // BUG BUG BUG BUG BUG IN SWT. Cannot read multiple files in a | |
177 | // fake directory. | |
178 | ||
179 | // FileDialog dialog = new | |
180 | // FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN | | |
181 | // SWT.MULTI); | |
182 | // dialog.setFilterPath("."); | |
183 | // if (null != dialog.open()) { | |
184 | // for (String fn : dialog.getFileNames()) { | |
185 | // final String pathname = dialog.getFilterPath() + | |
186 | // File.separator + fn; | |
187 | // File f = new File(pathname); | |
188 | // if (f.exists()) { | |
189 | // ((BatchImportTraceWizard) getWizard()).addFile(fn, f); | |
190 | // } | |
191 | // } | |
192 | // } | |
193 | ||
194 | DirectoryDialog dialog = new | |
195 | DirectoryDialog(Display.getCurrent().getActiveShell(), SWT.NONE); | |
59607b97 MAL |
196 | String lastDirectory = getLastSelectedDirectory(); |
197 | if (lastDirectory != null) { | |
198 | dialog.setFilterPath(lastDirectory); | |
199 | } | |
200 | ||
d04ec5a7 MK |
201 | String fn = dialog.open(); |
202 | if (null != fn) { | |
203 | File f = new File(fn); | |
204 | if (f.exists()) { | |
205 | getBatchWizard().addFileToScan(fn); | |
59607b97 | 206 | saveSelectedDirectory(f); |
d04ec5a7 MK |
207 | } |
208 | } | |
209 | updateButtons(); | |
210 | } | |
211 | ||
212 | @Override | |
213 | public void widgetDefaultSelected(SelectionEvent e) { | |
214 | } | |
215 | } | |
216 | ||
59607b97 MAL |
217 | private String getLastSelectedDirectory() { |
218 | final IDialogSettings settings = getDialogSettings(); | |
219 | if (settings != null) { | |
220 | final String directory = settings.get(STORE_DIRECTORY_ID); | |
221 | if (directory != null && !directory.isEmpty()) { | |
222 | final File file = new File(directory); | |
223 | if (file.exists()) { | |
224 | return directory.toString(); | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | return null; | |
230 | } | |
231 | ||
232 | private void saveSelectedDirectory(File directory) { | |
233 | final IDialogSettings settings = getDialogSettings(); | |
234 | if (settings != null && directory != null && directory.exists()) { | |
235 | settings.put(STORE_DIRECTORY_ID, directory.toString()); | |
236 | } | |
237 | } | |
238 | ||
d04ec5a7 MK |
239 | private final class RemoveFileHandler implements SelectionListener { |
240 | private final Table selectedFiles; | |
241 | ||
242 | private RemoveFileHandler(Table selectedFiles) { | |
243 | this.selectedFiles = selectedFiles; | |
244 | } | |
245 | ||
246 | @Override | |
247 | public void widgetSelected(SelectionEvent e) { | |
248 | TableItem selectedToRemove[] = selectedFiles.getSelection(); | |
249 | for (TableItem victim : selectedToRemove) { | |
250 | String victimName = victim.getText(); | |
251 | ((BatchImportTraceWizard) getWizard()).removeFile(victimName); | |
252 | } | |
253 | updateButtons(); | |
254 | } | |
255 | ||
256 | @Override | |
257 | public void widgetDefaultSelected(SelectionEvent e) { | |
258 | } | |
259 | } | |
260 | } |