1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 *******************************************************************************/
14 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
.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
;
43 * SelectSupplementaryResourcesDialog
45 public class SelectSupplementaryResourcesDialog
extends Dialog
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
50 private CheckboxTreeViewer fTreeViewer
;
51 private final IResource
[] fAvailableResources
;
52 private IResource
[] fReturndResources
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
62 * Parent shell of this dialog
66 public SelectSupplementaryResourcesDialog(Shell shell
, IResource
[] resources
) {
68 fAvailableResources
= Arrays
.copyOf(resources
, resources
.length
);
69 setShellStyle(SWT
.RESIZE
);
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 * @return A copy of the resources
79 public IResource
[] getResources() {
80 return Arrays
.copyOf(fReturndResources
, fReturndResources
.length
);
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
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
));
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
));
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
));
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() {
112 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
116 public void dispose() {
120 public boolean hasChildren(Object element
) {
121 if (element
instanceof IResource
[]) {
128 public Object
getParent(Object element
) {
133 public Object
[] getElements(Object inputElement
) {
134 return getChildren(inputElement
);
138 public Object
[] getChildren(Object parentElement
) {
139 if (parentElement
instanceof IResource
[]) {
140 return (Object
[]) parentElement
;
146 // fTreeViewer.setLabelProvider(new WorkbenchLabelProvider());
148 fTreeViewer
.setLabelProvider(new LabelProvider() {
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();
156 return super.getText(element
);
159 fTreeViewer
.setInput(fAvailableResources
);
161 Composite btComp
= new Composite(contextGroup
, SWT
.NONE
);
162 FillLayout layout
= new FillLayout(SWT
.VERTICAL
);
164 btComp
.setLayout(layout
);
166 GridData gd
= new GridData();
167 gd
.verticalAlignment
= SWT
.CENTER
;
168 btComp
.setLayoutData(gd
);
170 final Button selectAll
= new Button(btComp
, SWT
.PUSH
);
171 selectAll
.setText(Messages
.SelectSpplementaryResources_SelectAll
);
172 selectAll
.addSelectionListener(new SelectionAdapter() {
174 public void widgetSelected(SelectionEvent e
) {
175 Object
[] items
= fAvailableResources
;
176 for (Object treeItem
: items
) {
177 fTreeViewer
.setChecked(treeItem
, true);
182 final Button deselectAll
= new Button(btComp
, SWT
.PUSH
);
183 deselectAll
.setText(Messages
.SelectSpplementaryResources_DeselectAll
);
184 deselectAll
.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent e
) {
187 Object
[] items
= fAvailableResources
;
188 for (Object treeItem
: items
) {
189 fTreeViewer
.setChecked(treeItem
, false);
194 getShell().setMinimumSize(new Point(300, 150));
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);
206 protected void okPressed() {
207 Object
[] checked
= fTreeViewer
.getCheckedElements();
209 fReturndResources
= new IResource
[checked
.length
];
210 for (int i
= 0; i
< checked
.length
; i
++) {
211 fReturndResources
[i
] = (IResource
) checked
[i
];