1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
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
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 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.dialogs
;
17 import java
.util
.Arrays
;
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
;
45 * SelectSupplementaryResourcesDialog
47 public class SelectSupplementaryResourcesDialog
extends Dialog
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
52 private CheckboxTreeViewer fTreeViewer
;
53 private final IResource
[] fAvailableResources
;
54 private IResource
[] fReturndResources
;
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
64 * Parent shell of this dialog
68 public SelectSupplementaryResourcesDialog(Shell shell
, IResource
[] resources
) {
70 fAvailableResources
= Arrays
.copyOf(resources
, resources
.length
);
71 setShellStyle(SWT
.RESIZE
| getShellStyle());
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * @return A copy of the resources
81 public IResource
[] getResources() {
82 return Arrays
.copyOf(fReturndResources
, fReturndResources
.length
);
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
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
));
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
));
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
));
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() {
114 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
118 public void dispose() {
122 public boolean hasChildren(Object element
) {
123 if (element
instanceof IResource
[]) {
130 public Object
getParent(Object element
) {
135 public Object
[] getElements(Object inputElement
) {
136 return getChildren(inputElement
);
140 public Object
[] getChildren(Object parentElement
) {
141 if (parentElement
instanceof IResource
[]) {
142 return (Object
[]) parentElement
;
148 fTreeViewer
.setLabelProvider(new LabelProvider() {
150 public String
getText(Object element
) {
151 if (element
instanceof IResource
) {
152 IResource resource
= (IResource
) element
;
153 // remove .tracing/ segment
154 return resource
.getProjectRelativePath().removeFirstSegments(1).toString();
156 return super.getText(element
);
160 fTreeViewer
.setInput(fAvailableResources
);
162 fTreeViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
165 public void selectionChanged(SelectionChangedEvent event
) {
166 updateOKButtonEnablement();
170 Composite btComp
= new Composite(contextGroup
, SWT
.NONE
);
171 FillLayout layout
= new FillLayout(SWT
.VERTICAL
);
173 btComp
.setLayout(layout
);
175 GridData gd
= new GridData();
176 gd
.verticalAlignment
= SWT
.CENTER
;
177 btComp
.setLayoutData(gd
);
179 final Button selectAll
= new Button(btComp
, SWT
.PUSH
);
180 selectAll
.setText(Messages
.Dialog_SelectAll
);
181 selectAll
.addSelectionListener(new SelectionAdapter() {
183 public void widgetSelected(SelectionEvent e
) {
184 Object
[] items
= fAvailableResources
;
185 for (Object treeItem
: items
) {
186 fTreeViewer
.setChecked(treeItem
, true);
189 updateOKButtonEnablement();
193 final Button deselectAll
= new Button(btComp
, SWT
.PUSH
);
194 deselectAll
.setText(Messages
.Dialog_DeselectAll
);
195 deselectAll
.addSelectionListener(new SelectionAdapter() {
197 public void widgetSelected(SelectionEvent e
) {
198 Object
[] items
= fAvailableResources
;
199 for (Object treeItem
: items
) {
200 fTreeViewer
.setChecked(treeItem
, false);
203 updateOKButtonEnablement();
207 getShell().setMinimumSize(new Point(300, 150));
212 private void updateOKButtonEnablement() {
213 Object
[] checked
= fTreeViewer
.getCheckedElements();
214 getButton(IDialogConstants
.OK_ID
).setEnabled(checked
.length
> 0);
218 protected Control
createButtonBar(Composite parent
) {
219 Control control
= super.createButtonBar(parent
);
220 updateOKButtonEnablement();
225 protected void createButtonsForButtonBar(Composite parent
) {
226 createButton(parent
, IDialogConstants
.CANCEL_ID
, IDialogConstants
.CANCEL_LABEL
, true);
227 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
, true);
231 protected void okPressed() {
232 Object
[] checked
= fTreeViewer
.getCheckedElements();
234 fReturndResources
= new IResource
[checked
.length
];
235 for (int i
= 0; i
< checked
.length
; i
++) {
236 fReturndResources
[i
] = (IResource
) checked
[i
];