1 package net.sourceforge.phpdt.externaltools.internal.ui;
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8 **********************************************************************/
10 import java.util.Collection;
11 import java.util.HashSet;
12 import java.util.Iterator;
14 import org.eclipse.jface.viewers.ILabelProvider;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.ITreeContentProvider;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.viewers.ViewerSorter;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.Tree;
33 * This class was derived from
34 * org.eclipse.ui.internal.misc.CheckboxTreeAndListGroup
37 public class TreeAndListGroup implements ISelectionChangedListener {
40 private Object currentTreeSelection;
42 private Collection listeners = new HashSet();
44 private ITreeContentProvider treeContentProvider;
46 private IStructuredContentProvider listContentProvider;
48 private ILabelProvider treeLabelProvider;
50 private ILabelProvider listLabelProvider;
53 private TreeViewer treeViewer;
55 private TableViewer listViewer;
57 private boolean allowMultiselection = false;
60 * Create an instance of this class. Use this constructor if you wish to
61 * specify the width and/or height of the combined widget (to only hardcode
62 * one of the sizing dimensions, specify the other dimension's value as -1)
65 * org.eclipse.swt.widgets.Composite
70 * @param childPropertyName
72 * @param parentPropertyName
74 * @param listPropertyName
81 public TreeAndListGroup(Composite parent, Object rootObject,
82 ITreeContentProvider treeContentProvider,
83 ILabelProvider treeLabelProvider,
84 IStructuredContentProvider listContentProvider,
85 ILabelProvider listLabelProvider, int style, int width, int height) {
88 this.treeContentProvider = treeContentProvider;
89 this.listContentProvider = listContentProvider;
90 this.treeLabelProvider = treeLabelProvider;
91 this.listLabelProvider = listLabelProvider;
92 createContents(parent, width, height, style);
96 * This method must be called just before this window becomes visible.
98 public void aboutToOpen() {
99 currentTreeSelection = null;
101 // select the first element in the list
102 Object[] elements = treeContentProvider.getElements(root);
103 Object primary = elements.length > 0 ? elements[0] : null;
104 if (primary != null) {
105 treeViewer.setSelection(new StructuredSelection(primary));
107 treeViewer.getControl().setFocus();
111 * Add the passed listener to collection of clients that listen for changes
112 * to list viewer selection state
115 * ISelectionChangedListener
117 public void addSelectionChangedListener(ISelectionChangedListener listener) {
118 listeners.add(listener);
122 * Notify all checked state listeners that the passed element has had its
123 * checked state changed to the passed state
125 protected void notifySelectionListeners(SelectionChangedEvent event) {
126 Iterator listenersEnum = listeners.iterator();
127 while (listenersEnum.hasNext()) {
128 ((ISelectionChangedListener) listenersEnum.next())
129 .selectionChanged(event);
134 * Lay out and initialize self's visual components.
137 * org.eclipse.swt.widgets.Composite
143 protected void createContents(Composite parent, int width, int height,
146 Composite composite = new Composite(parent, style);
147 composite.setFont(parent.getFont());
148 GridLayout layout = new GridLayout();
149 layout.numColumns = 2;
150 layout.makeColumnsEqualWidth = true;
151 layout.marginHeight = 0;
152 layout.marginWidth = 0;
153 composite.setLayout(layout);
154 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
156 createTreeViewer(composite, width / 2, height);
157 createListViewer(composite, width / 2, height);
163 * Create this group's list viewer.
165 protected void createListViewer(Composite parent, int width, int height) {
167 if (allowMultiselection) {
172 listViewer = new TableViewer(parent, SWT.BORDER | style);
173 GridData data = new GridData(GridData.FILL_BOTH);
174 data.widthHint = width;
175 data.heightHint = height;
176 listViewer.getTable().setLayoutData(data);
177 listViewer.getTable().setFont(parent.getFont());
178 listViewer.setContentProvider(listContentProvider);
179 listViewer.setLabelProvider(listLabelProvider);
180 listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
181 public void selectionChanged(SelectionChangedEvent event) {
182 notifySelectionListeners(event);
188 * Create this group's tree viewer.
190 protected void createTreeViewer(Composite parent, int width, int height) {
191 Tree tree = new Tree(parent, SWT.BORDER);
192 GridData data = new GridData(GridData.FILL_BOTH);
193 data.widthHint = width;
194 data.heightHint = height;
195 tree.setLayoutData(data);
196 tree.setFont(parent.getFont());
198 treeViewer = new TreeViewer(tree);
199 treeViewer.setContentProvider(treeContentProvider);
200 treeViewer.setLabelProvider(treeLabelProvider);
201 treeViewer.addSelectionChangedListener(this);
204 public Table getListTable() {
205 return listViewer.getTable();
208 public IStructuredSelection getListTableSelection() {
209 ISelection selection = this.listViewer.getSelection();
210 if (selection instanceof IStructuredSelection) {
211 return (IStructuredSelection) selection;
213 return StructuredSelection.EMPTY;
217 protected void initialListItem(Object element) {
218 Object parent = treeContentProvider.getParent(element);
219 selectAndRevealFolder(parent);
222 public void selectAndRevealFolder(Object treeElement) {
223 treeViewer.reveal(treeElement);
224 IStructuredSelection selection = new StructuredSelection(treeElement);
225 treeViewer.setSelection(selection);
228 public void selectAndRevealFile(Object treeElement) {
229 listViewer.reveal(treeElement);
230 IStructuredSelection selection = new StructuredSelection(treeElement);
231 listViewer.setSelection(selection);
235 * Initialize this group's viewers after they have been laid out.
237 protected void initialize() {
238 treeViewer.setInput(root);
242 * Handle the selection of an item in the tree viewer
247 public void selectionChanged(SelectionChangedEvent event) {
248 IStructuredSelection selection = (IStructuredSelection) event
250 Object selectedElement = selection.getFirstElement();
251 if (selectedElement == null) {
252 currentTreeSelection = null;
253 listViewer.setInput(currentTreeSelection);
257 // ie.- if not an item deselection
258 if (selectedElement != currentTreeSelection) {
259 listViewer.setInput(selectedElement);
262 currentTreeSelection = selectedElement;
266 * Set the list viewer's providers to those passed
268 * @param contentProvider
269 * ITreeContentProvider
270 * @param labelProvider
273 public void setListProviders(IStructuredContentProvider contentProvider,
274 ILabelProvider labelProvider) {
275 listViewer.setContentProvider(contentProvider);
276 listViewer.setLabelProvider(labelProvider);
280 * Set the sorter that is to be applied to self's list viewer
282 public void setListSorter(ViewerSorter sorter) {
283 listViewer.setSorter(sorter);
287 * Set the root of the widget to be new Root. Regenerate all of the tables
288 * and lists from this value.
292 public void setRoot(Object newRoot) {
298 * Set the tree viewer's providers to those passed
300 * @param contentProvider
301 * ITreeContentProvider
302 * @param labelProvider
305 public void setTreeProviders(ITreeContentProvider contentProvider,
306 ILabelProvider labelProvider) {
307 treeViewer.setContentProvider(contentProvider);
308 treeViewer.setLabelProvider(labelProvider);
312 * Set the sorter that is to be applied to self's tree viewer
314 public void setTreeSorter(ViewerSorter sorter) {
315 treeViewer.setSorter(sorter);
319 * Set the focus on to the list widget.
321 public void setFocus() {
323 this.treeViewer.getTree().setFocus();
326 public void setAllowMultiselection(boolean allowMultiselection) {
327 this.allowMultiselection = allowMultiselection;