Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / dialogs / SelectSupplementaryResourcesDialog.java
CommitLineData
e12ecd30 1/*******************************************************************************
339d539c 2 * Copyright (c) 2009, 2014 Ericsson
a0a88f65 3 *
e12ecd30
BH
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
a0a88f65 8 *
e12ecd30 9 * Contributors:
31cff5ca
MAL
10 * Francois Chouinard - Copied and adapted from NewFolderDialog
11 * Marc-Andre Laperle - Add select/deselect all
339d539c 12 * Patrick Tasse - Add support for folder elements
e12ecd30
BH
13 *******************************************************************************/
14
15package org.eclipse.linuxtools.internal.tmf.ui.project.dialogs;
16
7daa4edf 17import java.util.ArrayList;
e12ecd30 18import java.util.Arrays;
7daa4edf
PT
19import java.util.Collection;
20import java.util.List;
21import java.util.Map.Entry;
e12ecd30
BH
22
23import org.eclipse.core.resources.IResource;
7daa4edf 24import org.eclipse.core.runtime.IPath;
e12ecd30
BH
25import org.eclipse.jface.dialogs.Dialog;
26import org.eclipse.jface.dialogs.IDialogConstants;
7daa4edf 27import org.eclipse.jface.viewers.CheckStateChangedEvent;
e12ecd30 28import org.eclipse.jface.viewers.CheckboxTreeViewer;
7daa4edf 29import org.eclipse.jface.viewers.ICheckStateListener;
52a6cd96 30import org.eclipse.jface.viewers.ISelectionChangedListener;
e12ecd30
BH
31import org.eclipse.jface.viewers.ITreeContentProvider;
32import org.eclipse.jface.viewers.LabelProvider;
52a6cd96 33import org.eclipse.jface.viewers.SelectionChangedEvent;
e12ecd30 34import org.eclipse.jface.viewers.Viewer;
7daa4edf
PT
35import org.eclipse.linuxtools.internal.tmf.ui.Activator;
36import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement;
37import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
38import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
e12ecd30 39import org.eclipse.swt.SWT;
31cff5ca
MAL
40import org.eclipse.swt.events.SelectionAdapter;
41import org.eclipse.swt.events.SelectionEvent;
7daa4edf 42import org.eclipse.swt.graphics.Image;
e12ecd30 43import org.eclipse.swt.graphics.Point;
31cff5ca 44import org.eclipse.swt.layout.FillLayout;
e12ecd30
BH
45import org.eclipse.swt.layout.GridData;
46import org.eclipse.swt.layout.GridLayout;
31cff5ca 47import org.eclipse.swt.widgets.Button;
e12ecd30
BH
48import org.eclipse.swt.widgets.Composite;
49import org.eclipse.swt.widgets.Control;
50import org.eclipse.swt.widgets.Group;
51import org.eclipse.swt.widgets.Shell;
52import org.eclipse.swt.widgets.Tree;
53import org.eclipse.ui.ISharedImages;
54import org.eclipse.ui.PlatformUI;
55
7daa4edf
PT
56import com.google.common.collect.Multimap;
57
e12ecd30 58/**
a0a88f65 59 * SelectSupplementaryResourcesDialog
e12ecd30
BH
60 */
61public class SelectSupplementaryResourcesDialog extends Dialog {
62
7daa4edf
PT
63 // ------------------------------------------------------------------------
64 // Constants
65 // ------------------------------------------------------------------------
66 private static final Image EXPERIMENT_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/experiment.gif"); //$NON-NLS-1$
67 private static final Image TRACE_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/trace.gif"); //$NON-NLS-1$
68 private static final Image RESOURCE_IMAGE = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
69
e12ecd30
BH
70 // ------------------------------------------------------------------------
71 // Members
72 // ------------------------------------------------------------------------
73 private CheckboxTreeViewer fTreeViewer;
7daa4edf
PT
74 private final Multimap<TmfCommonProjectElement, IResource> fResourceMap;
75 private IResource[] fReturnedResources;
e12ecd30
BH
76
77 // ------------------------------------------------------------------------
78 // Constructor
79 // ------------------------------------------------------------------------
80
a0a88f65
AM
81 /**
82 * Constructor.
83 *
84 * @param shell
85 * Parent shell of this dialog
7daa4edf
PT
86 * @param resourceMap
87 * Map of element to supplementary resources
a0a88f65 88 */
7daa4edf 89 public SelectSupplementaryResourcesDialog(Shell shell, Multimap<TmfCommonProjectElement, IResource> resourceMap) {
e12ecd30 90 super(shell);
7daa4edf 91 fResourceMap = resourceMap;
c30c5cbb 92 setShellStyle(SWT.RESIZE | getShellStyle());
e12ecd30
BH
93 }
94
95 // ------------------------------------------------------------------------
96 // Getters/Setters
97 // ------------------------------------------------------------------------
a0a88f65
AM
98
99 /**
7daa4edf 100 * @return A copy of the selected resources
a0a88f65 101 */
e12ecd30 102 public IResource[] getResources() {
7daa4edf 103 return Arrays.copyOf(fReturnedResources, fReturnedResources.length);
e12ecd30 104 }
a0a88f65 105
e12ecd30
BH
106 // ------------------------------------------------------------------------
107 // Dialog
108 // ------------------------------------------------------------------------
109
e12ecd30
BH
110 @Override
111 protected void configureShell(Shell newShell) {
112 super.configureShell(newShell);
113 newShell.setText(Messages.SelectSpplementaryResources_DialogTitle);
114 newShell.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ETOOL_DELETE));
115 }
a0a88f65 116
e12ecd30
BH
117 @Override
118 protected Control createDialogArea(Composite parent) {
119 Composite composite = (Composite) super.createDialogArea(parent);
120 composite.setLayout(new GridLayout());
121 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
122
123 Group contextGroup = new Group(composite, SWT.SHADOW_NONE);
124 contextGroup.setText(Messages.SelectSpplementaryResources_ResourcesGroupTitle);
31cff5ca 125 contextGroup.setLayout(new GridLayout(2, false));
e12ecd30 126 contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
a0a88f65 127
e12ecd30
BH
128 fTreeViewer = new CheckboxTreeViewer(contextGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
129 GridData data = new GridData(GridData.FILL_BOTH);
130 Tree tree = fTreeViewer.getTree();
131 tree.setLayoutData(data);
132 fTreeViewer.setContentProvider(new ITreeContentProvider() {
133
134 @Override
135 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
136 }
137
138 @Override
139 public void dispose() {
140 }
141
142 @Override
143 public boolean hasChildren(Object element) {
7daa4edf 144 return element instanceof TmfCommonProjectElement;
e12ecd30
BH
145 }
146
147 @Override
148 public Object getParent(Object element) {
7daa4edf
PT
149 if (element instanceof IResource) {
150 getParentElement((IResource) element);
151 }
e12ecd30
BH
152 return null;
153 }
154
155 @Override
156 public Object[] getElements(Object inputElement) {
7daa4edf 157 if (inputElement instanceof Object[]) {
099a269e
PT
158 return (Object[]) inputElement;
159 }
160 return null;
e12ecd30
BH
161 }
162
163 @Override
164 public Object[] getChildren(Object parentElement) {
7daa4edf
PT
165 if (parentElement instanceof TmfCommonProjectElement) {
166 return fResourceMap.get((TmfCommonProjectElement) parentElement).toArray();
167 }
a0a88f65 168 return null;
e12ecd30
BH
169 }
170 });
171
339d539c
PT
172 fTreeViewer.setLabelProvider(new LabelProvider() {
173 @Override
174 public String getText(Object element) {
175 if (element instanceof IResource) {
176 IResource resource = (IResource) element;
7daa4edf
PT
177 TmfCommonProjectElement projectElement = getParentElement(resource);
178 // remove .tracing/<supplementary folder> segments
179 IPath suppFolderPath = projectElement.getTraceSupplementaryFolder(projectElement.getElementPath()).getFullPath();
180 return resource.getFullPath().removeFirstSegments(suppFolderPath.segmentCount()).toString();
181 } else if (element instanceof TmfCommonProjectElement) {
182 TmfCommonProjectElement projectElement = (TmfCommonProjectElement) element;
183 return projectElement.getElementPath();
339d539c
PT
184 }
185 return super.getText(element);
186 }
7daa4edf
PT
187
188 @Override
189 public Image getImage(Object element) {
190 if (element instanceof IResource) {
191 return RESOURCE_IMAGE;
192 } else if (element instanceof TmfTraceElement) {
193 return TRACE_IMAGE;
194 } else if (element instanceof TmfExperimentElement) {
195 return EXPERIMENT_IMAGE;
196 }
197 return null;
198 }
199
339d539c
PT
200 });
201
7daa4edf 202 fTreeViewer.setInput(fResourceMap.keySet().toArray());
e12ecd30 203
7daa4edf 204 fTreeViewer.expandAll();
099a269e
PT
205 setAllChecked(true);
206
7daa4edf
PT
207 fTreeViewer.addCheckStateListener(new ICheckStateListener() {
208 @Override
209 public void checkStateChanged(CheckStateChangedEvent event) {
210 if (event.getElement() instanceof TmfCommonProjectElement) {
211 fTreeViewer.setSubtreeChecked(event.getElement(), event.getChecked());
212 fTreeViewer.setGrayed(event.getElement(), false);
213 } else if (event.getElement() instanceof IResource) {
214 TmfCommonProjectElement projectElement = getParentElement((IResource) event.getElement());
215 int checkedCount = 0;
216 Collection<IResource> resources = fResourceMap.get(projectElement);
217 for (IResource resource : resources) {
218 if (fTreeViewer.getChecked(resource)) {
219 checkedCount++;
220 }
221 }
222 if (checkedCount == resources.size()) {
223 fTreeViewer.setChecked(projectElement, true);
224 fTreeViewer.setGrayed(projectElement, false);
225 } else if (checkedCount > 0) {
226 fTreeViewer.setGrayChecked(projectElement, true);
227 } else {
228 fTreeViewer.setGrayChecked(projectElement, false);
229 }
230 }
231 }
232 });
52a6cd96 233
7daa4edf 234 fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
52a6cd96
MAL
235 @Override
236 public void selectionChanged(SelectionChangedEvent event) {
237 updateOKButtonEnablement();
238 }
239 });
240
31cff5ca
MAL
241 Composite btComp = new Composite(contextGroup, SWT.NONE);
242 FillLayout layout = new FillLayout(SWT.VERTICAL);
243 layout.spacing = 4;
244 btComp.setLayout(layout);
245
246 GridData gd = new GridData();
247 gd.verticalAlignment = SWT.CENTER;
248 btComp.setLayoutData(gd);
249
250 final Button selectAll = new Button(btComp, SWT.PUSH);
195355a9 251 selectAll.setText(Messages.Dialog_SelectAll);
31cff5ca
MAL
252 selectAll.addSelectionListener(new SelectionAdapter() {
253 @Override
254 public void widgetSelected(SelectionEvent e) {
099a269e 255 setAllChecked(true);
52a6cd96
MAL
256
257 updateOKButtonEnablement();
31cff5ca
MAL
258 }
259 });
260
261 final Button deselectAll = new Button(btComp, SWT.PUSH);
195355a9 262 deselectAll.setText(Messages.Dialog_DeselectAll);
31cff5ca
MAL
263 deselectAll.addSelectionListener(new SelectionAdapter() {
264 @Override
265 public void widgetSelected(SelectionEvent e) {
099a269e 266 setAllChecked(false);
52a6cd96
MAL
267
268 updateOKButtonEnablement();
31cff5ca
MAL
269 }
270 });
271
e12ecd30
BH
272 getShell().setMinimumSize(new Point(300, 150));
273
274 return composite;
275 }
276
7daa4edf
PT
277 private TmfCommonProjectElement getParentElement(IResource resource) {
278 for (Entry<TmfCommonProjectElement, IResource> entry : fResourceMap.entries()) {
279 if (entry.getValue().equals(resource)) {
280 return entry.getKey();
281 }
282 }
283 return null;
284 }
285
099a269e 286 private void setAllChecked(boolean state) {
7daa4edf
PT
287 for (Object element : fResourceMap.keySet()) {
288 fTreeViewer.setSubtreeChecked(element, state);
289 fTreeViewer.setGrayed(element, false);
099a269e
PT
290 }
291 }
292
52a6cd96
MAL
293 private void updateOKButtonEnablement() {
294 Object[] checked = fTreeViewer.getCheckedElements();
295 getButton(IDialogConstants.OK_ID).setEnabled(checked.length > 0);
296 }
297
298 @Override
299 protected Control createButtonBar(Composite parent) {
300 Control control = super.createButtonBar(parent);
301 updateOKButtonEnablement();
302 return control;
303 }
304
e12ecd30
BH
305 @Override
306 protected void createButtonsForButtonBar(Composite parent) {
31cff5ca
MAL
307 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true);
308 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
e12ecd30
BH
309 }
310
311 @Override
312 protected void okPressed() {
7daa4edf
PT
313 Object[] checkedElements = fTreeViewer.getCheckedElements();
314 List<IResource> checkedResources = new ArrayList<>(checkedElements.length);
315 for (Object checked : checkedElements) {
316 if (checked instanceof IResource) {
317 checkedResources.add((IResource) checked);
318 }
e12ecd30 319 }
7daa4edf 320 fReturnedResources = checkedResources.toArray(new IResource[0]);
e12ecd30
BH
321 super.okPressed();
322 }
323
e12ecd30 324}
This page took 0.079334 seconds and 5 git commands to generate.