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
) {
127 public Object
getParent(Object element
) {
132 public Object
[] getElements(Object inputElement
) {
133 if (inputElement
instanceof IResource
[]) {
134 return (Object
[]) inputElement
;
140 public Object
[] getChildren(Object parentElement
) {
145 fTreeViewer
.setLabelProvider(new LabelProvider() {
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();
153 return super.getText(element
);
157 fTreeViewer
.setInput(fAvailableResources
);
161 fTreeViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
164 public void selectionChanged(SelectionChangedEvent event
) {
165 updateOKButtonEnablement();
169 Composite btComp
= new Composite(contextGroup
, SWT
.NONE
);
170 FillLayout layout
= new FillLayout(SWT
.VERTICAL
);
172 btComp
.setLayout(layout
);
174 GridData gd
= new GridData();
175 gd
.verticalAlignment
= SWT
.CENTER
;
176 btComp
.setLayoutData(gd
);
178 final Button selectAll
= new Button(btComp
, SWT
.PUSH
);
179 selectAll
.setText(Messages
.Dialog_SelectAll
);
180 selectAll
.addSelectionListener(new SelectionAdapter() {
182 public void widgetSelected(SelectionEvent e
) {
185 updateOKButtonEnablement();
189 final Button deselectAll
= new Button(btComp
, SWT
.PUSH
);
190 deselectAll
.setText(Messages
.Dialog_DeselectAll
);
191 deselectAll
.addSelectionListener(new SelectionAdapter() {
193 public void widgetSelected(SelectionEvent e
) {
194 setAllChecked(false);
196 updateOKButtonEnablement();
200 getShell().setMinimumSize(new Point(300, 150));
205 private void setAllChecked(boolean state
) {
206 for (Object element
: fAvailableResources
) {
207 fTreeViewer
.setChecked(element
, state
);
211 private void updateOKButtonEnablement() {
212 Object
[] checked
= fTreeViewer
.getCheckedElements();
213 getButton(IDialogConstants
.OK_ID
).setEnabled(checked
.length
> 0);
217 protected Control
createButtonBar(Composite parent
) {
218 Control control
= super.createButtonBar(parent
);
219 updateOKButtonEnablement();
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);
230 protected void okPressed() {
231 Object
[] checked
= fTreeViewer
.getCheckedElements();
233 fReturndResources
= new IResource
[checked
.length
];
234 for (int i
= 0; i
< checked
.length
; i
++) {
235 fReturndResources
[i
] = (IResource
) checked
[i
];