tmf: Improve Delete Supplementary Files command
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / dialogs / SelectSupplementaryResourcesDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 - Copied and adapted from NewFolderDialog
11 * Marc-Andre Laperle - Add select/deselect all
12 * Patrick Tasse - Add support for folder elements
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.internal.tmf.ui.project.dialogs;
16
17 import java.util.Arrays;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.viewers.CheckboxTreeViewer;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.ITreeContentProvider;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.jface.viewers.SelectionChangedEvent;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.FillLayout;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Group;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Tree;
41 import org.eclipse.ui.ISharedImages;
42 import org.eclipse.ui.PlatformUI;
43
44 /**
45 * SelectSupplementaryResourcesDialog
46 */
47 public class SelectSupplementaryResourcesDialog extends Dialog {
48
49 // ------------------------------------------------------------------------
50 // Members
51 // ------------------------------------------------------------------------
52 private CheckboxTreeViewer fTreeViewer;
53 private final IResource[] fAvailableResources;
54 private IResource[] fReturndResources;
55
56 // ------------------------------------------------------------------------
57 // Constructor
58 // ------------------------------------------------------------------------
59
60 /**
61 * Constructor.
62 *
63 * @param shell
64 * Parent shell of this dialog
65 * @param resources
66 * Available resources
67 */
68 public SelectSupplementaryResourcesDialog(Shell shell, IResource[] resources) {
69 super(shell);
70 fAvailableResources = Arrays.copyOf(resources, resources.length);
71 setShellStyle(SWT.RESIZE | getShellStyle());
72 }
73
74 // ------------------------------------------------------------------------
75 // Getters/Setters
76 // ------------------------------------------------------------------------
77
78 /**
79 * @return A copy of the resources
80 */
81 public IResource[] getResources() {
82 return Arrays.copyOf(fReturndResources, fReturndResources.length);
83 }
84
85 // ------------------------------------------------------------------------
86 // Dialog
87 // ------------------------------------------------------------------------
88
89 @Override
90 protected void configureShell(Shell newShell) {
91 super.configureShell(newShell);
92 newShell.setText(Messages.SelectSpplementaryResources_DialogTitle);
93 newShell.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ETOOL_DELETE));
94 }
95
96 @Override
97 protected Control createDialogArea(Composite parent) {
98 Composite composite = (Composite) super.createDialogArea(parent);
99 composite.setLayout(new GridLayout());
100 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
101
102 Group contextGroup = new Group(composite, SWT.SHADOW_NONE);
103 contextGroup.setText(Messages.SelectSpplementaryResources_ResourcesGroupTitle);
104 contextGroup.setLayout(new GridLayout(2, false));
105 contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
106
107 fTreeViewer = new CheckboxTreeViewer(contextGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
108 GridData data = new GridData(GridData.FILL_BOTH);
109 Tree tree = fTreeViewer.getTree();
110 tree.setLayoutData(data);
111 fTreeViewer.setContentProvider(new ITreeContentProvider() {
112
113 @Override
114 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
115 }
116
117 @Override
118 public void dispose() {
119 }
120
121 @Override
122 public boolean hasChildren(Object element) {
123 return false;
124 }
125
126 @Override
127 public Object getParent(Object element) {
128 return null;
129 }
130
131 @Override
132 public Object[] getElements(Object inputElement) {
133 if (inputElement instanceof IResource[]) {
134 return (Object[]) inputElement;
135 }
136 return null;
137 }
138
139 @Override
140 public Object[] getChildren(Object parentElement) {
141 return null;
142 }
143 });
144
145 fTreeViewer.setLabelProvider(new LabelProvider() {
146 @Override
147 public String getText(Object element) {
148 if (element instanceof IResource) {
149 IResource resource = (IResource) element;
150 // remove .tracing/ segment
151 return resource.getProjectRelativePath().removeFirstSegments(1).toString();
152 }
153 return super.getText(element);
154 }
155 });
156
157 fTreeViewer.setInput(fAvailableResources);
158
159 setAllChecked(true);
160
161 fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
162
163 @Override
164 public void selectionChanged(SelectionChangedEvent event) {
165 updateOKButtonEnablement();
166 }
167 });
168
169 Composite btComp = new Composite(contextGroup, SWT.NONE);
170 FillLayout layout = new FillLayout(SWT.VERTICAL);
171 layout.spacing = 4;
172 btComp.setLayout(layout);
173
174 GridData gd = new GridData();
175 gd.verticalAlignment = SWT.CENTER;
176 btComp.setLayoutData(gd);
177
178 final Button selectAll = new Button(btComp, SWT.PUSH);
179 selectAll.setText(Messages.Dialog_SelectAll);
180 selectAll.addSelectionListener(new SelectionAdapter() {
181 @Override
182 public void widgetSelected(SelectionEvent e) {
183 setAllChecked(true);
184
185 updateOKButtonEnablement();
186 }
187 });
188
189 final Button deselectAll = new Button(btComp, SWT.PUSH);
190 deselectAll.setText(Messages.Dialog_DeselectAll);
191 deselectAll.addSelectionListener(new SelectionAdapter() {
192 @Override
193 public void widgetSelected(SelectionEvent e) {
194 setAllChecked(false);
195
196 updateOKButtonEnablement();
197 }
198 });
199
200 getShell().setMinimumSize(new Point(300, 150));
201
202 return composite;
203 }
204
205 private void setAllChecked(boolean state) {
206 for (Object element : fAvailableResources) {
207 fTreeViewer.setChecked(element, state);
208 }
209 }
210
211 private void updateOKButtonEnablement() {
212 Object[] checked = fTreeViewer.getCheckedElements();
213 getButton(IDialogConstants.OK_ID).setEnabled(checked.length > 0);
214 }
215
216 @Override
217 protected Control createButtonBar(Composite parent) {
218 Control control = super.createButtonBar(parent);
219 updateOKButtonEnablement();
220 return control;
221 }
222
223 @Override
224 protected void createButtonsForButtonBar(Composite parent) {
225 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true);
226 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
227 }
228
229 @Override
230 protected void okPressed() {
231 Object[] checked = fTreeViewer.getCheckedElements();
232
233 fReturndResources = new IResource[checked.length];
234 for (int i = 0; i < checked.length; i++) {
235 fReturndResources[i] = (IResource) checked[i];
236 }
237 super.okPressed();
238 }
239
240 }
This page took 0.040018 seconds and 5 git commands to generate.