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