1255b2f5846ad71321709775b141eae20c2531e3
[phpeclipse.git] /
1 package net.sourceforge.phpdt.externaltools.internal.ui;
2
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  **********************************************************************/
9
10 import java.util.Collection;
11 import java.util.HashSet;
12 import java.util.Iterator;
13
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;
31
32 /**
33  * This class was derived from
34  * org.eclipse.ui.internal.misc.CheckboxTreeAndListGroup
35  * 
36  */
37 public class TreeAndListGroup implements ISelectionChangedListener {
38         private Object root;
39
40         private Object currentTreeSelection;
41
42         private Collection listeners = new HashSet();
43
44         private ITreeContentProvider treeContentProvider;
45
46         private IStructuredContentProvider listContentProvider;
47
48         private ILabelProvider treeLabelProvider;
49
50         private ILabelProvider listLabelProvider;
51
52         // widgets
53         private TreeViewer treeViewer;
54
55         private TableViewer listViewer;
56
57         private boolean allowMultiselection = false;
58
59         /**
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)
63          * 
64          * @param parent
65          *            org.eclipse.swt.widgets.Composite
66          * @param style
67          *            int
68          * @param rootObject
69          *            java.lang.Object
70          * @param childPropertyName
71          *            java.lang.String
72          * @param parentPropertyName
73          *            java.lang.String
74          * @param listPropertyName
75          *            java.lang.String
76          * @param width
77          *            int
78          * @param height
79          *            int
80          */
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) {
86
87                 root = rootObject;
88                 this.treeContentProvider = treeContentProvider;
89                 this.listContentProvider = listContentProvider;
90                 this.treeLabelProvider = treeLabelProvider;
91                 this.listLabelProvider = listLabelProvider;
92                 createContents(parent, width, height, style);
93         }
94
95         /**
96          * This method must be called just before this window becomes visible.
97          */
98         public void aboutToOpen() {
99                 currentTreeSelection = null;
100
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));
106                 }
107                 treeViewer.getControl().setFocus();
108         }
109
110         /**
111          * Add the passed listener to collection of clients that listen for changes
112          * to list viewer selection state
113          * 
114          * @param listener
115          *            ISelectionChangedListener
116          */
117         public void addSelectionChangedListener(ISelectionChangedListener listener) {
118                 listeners.add(listener);
119         }
120
121         /**
122          * Notify all checked state listeners that the passed element has had its
123          * checked state changed to the passed state
124          */
125         protected void notifySelectionListeners(SelectionChangedEvent event) {
126                 Iterator listenersEnum = listeners.iterator();
127                 while (listenersEnum.hasNext()) {
128                         ((ISelectionChangedListener) listenersEnum.next())
129                                         .selectionChanged(event);
130                 }
131         }
132
133         /**
134          * Lay out and initialize self's visual components.
135          * 
136          * @param parent
137          *            org.eclipse.swt.widgets.Composite
138          * @param width
139          *            int
140          * @param height
141          *            int
142          */
143         protected void createContents(Composite parent, int width, int height,
144                         int style) {
145                 // group pane
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));
155
156                 createTreeViewer(composite, width / 2, height);
157                 createListViewer(composite, width / 2, height);
158
159                 initialize();
160         }
161
162         /**
163          * Create this group's list viewer.
164          */
165         protected void createListViewer(Composite parent, int width, int height) {
166                 int style;
167                 if (allowMultiselection) {
168                         style = SWT.MULTI;
169                 } else {
170                         style = SWT.SINGLE;
171                 }
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);
183                         }
184                 });
185         }
186
187         /**
188          * Create this group's tree viewer.
189          */
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());
197
198                 treeViewer = new TreeViewer(tree);
199                 treeViewer.setContentProvider(treeContentProvider);
200                 treeViewer.setLabelProvider(treeLabelProvider);
201                 treeViewer.addSelectionChangedListener(this);
202         }
203
204         public Table getListTable() {
205                 return listViewer.getTable();
206         }
207
208         public IStructuredSelection getListTableSelection() {
209                 ISelection selection = this.listViewer.getSelection();
210                 if (selection instanceof IStructuredSelection) {
211                         return (IStructuredSelection) selection;
212                 } else {
213                         return StructuredSelection.EMPTY;
214                 }
215         }
216
217         protected void initialListItem(Object element) {
218                 Object parent = treeContentProvider.getParent(element);
219                 selectAndRevealFolder(parent);
220         }
221
222         public void selectAndRevealFolder(Object treeElement) {
223                 treeViewer.reveal(treeElement);
224                 IStructuredSelection selection = new StructuredSelection(treeElement);
225                 treeViewer.setSelection(selection);
226         }
227
228         public void selectAndRevealFile(Object treeElement) {
229                 listViewer.reveal(treeElement);
230                 IStructuredSelection selection = new StructuredSelection(treeElement);
231                 listViewer.setSelection(selection);
232         }
233
234         /**
235          * Initialize this group's viewers after they have been laid out.
236          */
237         protected void initialize() {
238                 treeViewer.setInput(root);
239         }
240
241         /**
242          * Handle the selection of an item in the tree viewer
243          * 
244          * @param selection
245          *            ISelection
246          */
247         public void selectionChanged(SelectionChangedEvent event) {
248                 IStructuredSelection selection = (IStructuredSelection) event
249                                 .getSelection();
250                 Object selectedElement = selection.getFirstElement();
251                 if (selectedElement == null) {
252                         currentTreeSelection = null;
253                         listViewer.setInput(currentTreeSelection);
254                         return;
255                 }
256
257                 // ie.- if not an item deselection
258                 if (selectedElement != currentTreeSelection) {
259                         listViewer.setInput(selectedElement);
260                 }
261
262                 currentTreeSelection = selectedElement;
263         }
264
265         /**
266          * Set the list viewer's providers to those passed
267          * 
268          * @param contentProvider
269          *            ITreeContentProvider
270          * @param labelProvider
271          *            ILabelProvider
272          */
273         public void setListProviders(IStructuredContentProvider contentProvider,
274                         ILabelProvider labelProvider) {
275                 listViewer.setContentProvider(contentProvider);
276                 listViewer.setLabelProvider(labelProvider);
277         }
278
279         /**
280          * Set the sorter that is to be applied to self's list viewer
281          */
282         public void setListSorter(ViewerSorter sorter) {
283                 listViewer.setSorter(sorter);
284         }
285
286         /**
287          * Set the root of the widget to be new Root. Regenerate all of the tables
288          * and lists from this value.
289          * 
290          * @param newRoot
291          */
292         public void setRoot(Object newRoot) {
293                 this.root = newRoot;
294                 initialize();
295         }
296
297         /**
298          * Set the tree viewer's providers to those passed
299          * 
300          * @param contentProvider
301          *            ITreeContentProvider
302          * @param labelProvider
303          *            ILabelProvider
304          */
305         public void setTreeProviders(ITreeContentProvider contentProvider,
306                         ILabelProvider labelProvider) {
307                 treeViewer.setContentProvider(contentProvider);
308                 treeViewer.setLabelProvider(labelProvider);
309         }
310
311         /**
312          * Set the sorter that is to be applied to self's tree viewer
313          */
314         public void setTreeSorter(ViewerSorter sorter) {
315                 treeViewer.setSorter(sorter);
316         }
317
318         /**
319          * Set the focus on to the list widget.
320          */
321         public void setFocus() {
322
323                 this.treeViewer.getTree().setFocus();
324         }
325
326         public void setAllowMultiselection(boolean allowMultiselection) {
327                 this.allowMultiselection = allowMultiselection;
328
329         }
330 }