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