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