KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > CheckboxDoubleListGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.misc;
12
13 import java.util.*;
14
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.jface.util.ListenerList;
17 import org.eclipse.jface.util.SafeRunnable;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.layout.*;
22 import org.eclipse.swt.widgets.Composite;
23
24 /**
25  * Workbench-level composite that combines two CheckboxListViewers.
26  * All viewer selection-driven interactions are handled within this object
27  */

28 public class CheckboxDoubleListGroup extends Composite implements ICheckStateListener, ISelectionChangedListener {
29     private Object JavaDoc root;
30     private Object JavaDoc currentList1Selection;
31     private Map checkedStateStore = new HashMap(9);
32     private ListenerList listeners = new ListenerList();
33     private boolean singleList1Check = false;
34     private boolean singleList2Check = false;
35     
36     private IStructuredContentProvider list1ContentProvider;
37     private IStructuredContentProvider list2ContentProvider;
38     private ILabelProvider list1LabelProvider;
39     private ILabelProvider list2LabelProvider;
40     
41     // widgets
42
private CheckboxTableViewer list1Viewer;
43     private CheckboxTableViewer list2Viewer;
44 /**
45  * Create an instance of this class. Use this constructor if you want
46  * the combined widget to act like others w.r.t. sizing and set its
47  * size according to whatever is required to fill its context.
48  *
49  * @param parent org.eclipse.swt.widgets.Composite
50  * @param rootObject java.lang.Object
51  * @param style int
52  * @param childPropertyName java.lang.String
53  * @param parentPropertyName java.lang.String
54  * @param listPropertyName java.lang.String
55  */

56 public CheckboxDoubleListGroup(
57     Composite parent,Object JavaDoc rootObject,
58     IStructuredContentProvider list1ContentProvider,ILabelProvider list1LabelProvider,
59     IStructuredContentProvider list2ContentProvider,ILabelProvider list2LabelProvider,
60     int style) {
61         
62     this(
63         parent,rootObject,
64         list1ContentProvider,list1LabelProvider,
65         list2ContentProvider,list2LabelProvider,
66         style,-1,-1);
67 }
68 /**
69  * Create an instance of this class. Use this constructor if you wish to specify
70  * the width and/or height of the combined widget (to only hardcode one of the
71  * sizing dimensions, specify the other dimension's value as -1)
72  *
73  * @param parent org.eclipse.swt.widgets.Composite
74  * @param style int
75  * @param rootObject java.lang.Object
76  * @param childPropertyName java.lang.String
77  * @param parentPropertyName java.lang.String
78  * @param listPropertyName java.lang.String
79  * @param width int
80  * @param height int
81  */

82 public CheckboxDoubleListGroup(
83     Composite parent,Object JavaDoc rootObject,
84     IStructuredContentProvider list1ContentProvider,ILabelProvider list1LabelProvider,
85     IStructuredContentProvider list2ContentProvider,ILabelProvider list2LabelProvider,
86     int style,int width,int height) {
87
88     super(parent,style);
89     root = rootObject;
90     this.list1ContentProvider = list1ContentProvider;
91     this.list2ContentProvider = list2ContentProvider;
92     this.list1LabelProvider = list1LabelProvider;
93     this.list2LabelProvider = list2LabelProvider;
94     createContents(parent,width,height);
95 }
96 /**
97  * Add the passed listener to self's collection of clients
98  * that listen for changes to element checked states
99  *
100  * @param listener ICheckStateListener
101  */

102 public void addCheckStateListener(ICheckStateListener listener) {
103     listeners.add(listener);
104 }
105 /**
106  * An item was checked in one of self's two views. Determine which
107  * view this occurred in and delegate appropriately
108  *
109  * @param event CheckStateChangedEvent
110  */

111 public void checkStateChanged(CheckStateChangedEvent event) {
112     if (event.getCheckable().equals(list1Viewer))
113         list1ItemChecked(event.getElement(),event.getChecked());
114     else
115         list2ItemChecked(event.getElement(),event.getChecked());
116
117     notifyCheckStateChangeListeners(event);
118 }
119 /**
120  * Compute the preferred size.
121  *
122  * @return org.eclipse.swt.graphics.Point
123  * @param wHint int
124  * @param hHint int
125  * @param changed boolean
126  */

127 public Point computeSize(int wHint,int hHint,boolean changed) {
128     return new Point(-1,-1);
129 }
130 /**
131  * Lay out and initialize self's visual components.
132  *
133  * @param parent org.eclipse.swt.widgets.Composite
134  * @param width int
135  * @param height int
136  */

137 protected void createContents(Composite parent,int width,int height) {
138     // group pane
139
Composite composite = new Composite(parent,SWT.NONE);
140     GridLayout layout = new GridLayout();
141     layout.numColumns = 2;
142     composite.setFont(parent.getFont());
143     composite.setLayout(layout);
144     composite.setLayoutData(new GridData(
145         GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
146     
147     createList1Viewer(createViewPane(composite, width/2, height/2));
148     createList2Viewer(createViewPane(composite, width/2, height/2));
149     
150     list1Viewer.setInput(root);
151 }
152 /**
153  * Create the left viewer for this group.
154  *
155  * @param parent org.eclipse.swt.widgets.Composite
156  */

157 protected void createList1Viewer(Composite parent) {
158     list1Viewer = CheckboxTableViewer.newCheckList(parent, SWT.NONE);
159     list1Viewer.setContentProvider(list1ContentProvider);
160     list1Viewer.setLabelProvider(list1LabelProvider);
161     list1Viewer.addCheckStateListener(this);
162     list1Viewer.addSelectionChangedListener(this);
163     list1Viewer.getTable().setFont(parent.getFont());
164 }
165 /**
166  * Create the right viewer for this group.
167  *
168  * @param parent org.eclipse.swt.widgets.Composite
169  */

170 protected void createList2Viewer(Composite parent) {
171     list2Viewer = CheckboxTableViewer.newCheckList(parent, SWT.NONE);
172     list2Viewer.setContentProvider(list2ContentProvider);
173     list2Viewer.setLabelProvider(list2LabelProvider);
174     list2Viewer.addCheckStateListener(this);
175     list2Viewer.getTable().setFont(parent.getFont());
176 }
177 /**
178  * Create a viewer pane in this group for the passed viewer.
179  *
180  * @param parent org.eclipse.swt.widgets.Composite
181  * @param width int
182  * @param height int
183  */

184 protected Composite createViewPane(Composite parent, int width, int height) {
185     Composite pane = new Composite(parent, SWT.BORDER);
186     GridData spec = new GridData(GridData.FILL_BOTH);
187     spec.widthHint = width;
188     spec.heightHint = height;
189     pane.setLayoutData(spec);
190     pane.setLayout(new FillLayout());
191     pane.setFont(parent.getFont());
192     return pane;
193 }
194 /**
195  * Answer a collection of all of the checked elements in the list 1
196  * portion of self
197  *
198  * @return java.util.Set
199  */

200 public Set getAllCheckedList1Items() {
201     return checkedStateStore.keySet();
202 }
203 /**
204  * Answer a flat collection of all of the checked elements in the
205  * list 2 portion of self
206  *
207  * @return java.util.Vector
208  */

209 public List getAllCheckedList2Items() {
210     List result = new ArrayList();
211     Iterator listCollectionsEnum = checkedStateStore.values().iterator();
212     
213     while (listCollectionsEnum.hasNext()) {
214         Iterator currentCollection = ((List)listCollectionsEnum.next()).iterator();
215         while (currentCollection.hasNext())
216             result.add(currentCollection.next());
217     }
218     
219     return result;
220 }
221 /**
222  * Answer the number of elements that have been checked by the
223  * user.
224  *
225  * @return int
226  */

227 public int getCheckedElementCount() {
228     return checkedStateStore.size();
229 }
230 /**
231  * Set the checked state of the passed list 1 element, as well
232  * as its associated list 2 elements
233  */

234 public void initialCheckList1Item(Object JavaDoc element) {
235     checkedStateStore.put(element,new ArrayList());
236     list1Viewer.setChecked(element,true);
237 }
238 /**
239  * Handle the checking of a list 1 item
240  */

241 protected void list1ItemChecked(Object JavaDoc listElement,boolean state) {
242
243     if (state) {
244         // if only one list 1 item can be checked at a time then clear the
245
// previous checked list 1 item, if any
246
if (singleList1Check) {
247             checkedStateStore.clear();
248             list1Viewer.setAllChecked(false);
249         }
250
251         checkedStateStore.put(listElement,new ArrayList());
252         
253     } else {
254         checkedStateStore.remove(listElement);
255         list2Viewer.setAllChecked(false);
256     }
257
258     // the following may seem redundant, but it allows other methods to invoke
259
// this method in order to fully simulate the user clicking a list 1 item
260
list1Viewer.setChecked(listElement,state);
261 }
262 /**
263  * Handle the checking of a list 2 item
264  */

265 protected void list2ItemChecked(Object JavaDoc listElement,boolean state) {
266     List checkedListItems = (List)checkedStateStore.get(currentList1Selection);
267
268     if (state) {
269         // if only one list 2 item can be checked at a time then clear the
270
// previous checked list 2 item, if any
271
if (singleList2Check) {
272             checkedListItems = null;
273             list2Viewer.setAllChecked(false);
274             list2Viewer.setChecked(listElement,true);
275         }
276
277         if (checkedListItems == null) {
278             list1ItemChecked(currentList1Selection,true);
279             checkedListItems = (List)checkedStateStore.get(currentList1Selection);
280         }
281
282         checkedListItems.add(listElement);
283
284     } else {
285         checkedListItems.remove(listElement);
286         if (checkedListItems.isEmpty())
287             list1ItemChecked(currentList1Selection,false);
288     }
289 }
290 /**
291  * Notify all checked state listeners that the passed element has had
292  * its checked state changed to the passed state
293  */

294 protected void notifyCheckStateChangeListeners(final CheckStateChangedEvent event) {
295     Object JavaDoc[] array = listeners.getListeners();
296     for (int i = 0; i < array.length; i ++) {
297         final ICheckStateListener l = (ICheckStateListener)array[i];
298         Platform.run(new SafeRunnable() {
299             public void run() {
300                 l.checkStateChanged(event);
301             }
302         });
303     }
304 }
305 /**
306  * Remove the passed listener from self's collection of clients
307  * that listen for changes to element checked states
308  *
309  * @param listener ICheckStateListener
310  */

311 public void removeCheckStateListener(ICheckStateListener listener) {
312     listeners.remove(listener);
313 }
314 /**
315  *Handle the selection of a list 1 item
316  *
317  *@param selection ISelection
318  */

319 public void selectionChanged(SelectionChangedEvent event) {
320     IStructuredSelection selection = (IStructuredSelection) event.getSelection();
321     final Object JavaDoc selectedElement = selection.getFirstElement();
322     if (selectedElement == null) {
323         currentList1Selection = null;
324         list2Viewer.setInput(currentList1Selection);
325         return;
326     }
327
328     // ie.- if not an item deselection
329
if (selectedElement != currentList1Selection) {
330         list2Viewer.setInput(selectedElement);
331         List listItemsToCheck = (List)checkedStateStore.get(selectedElement);
332         if (listItemsToCheck != null) {
333             Iterator listItemsEnum = listItemsToCheck.iterator();
334             while (listItemsEnum.hasNext())
335                 list2Viewer.setChecked(listItemsEnum.next(), true);
336         }
337     }
338     currentList1Selection = selectedElement;
339 }
340 /**
341  * Change the list 1 viewer's providers to those passed
342  *
343  * @param contentProvider ITreeContentProvider
344  * @param labelProvider ILabelProvider
345  */

346 public void setList1Providers(IStructuredContentProvider contentProvider, ILabelProvider labelProvider) {
347     list1Viewer.setContentProvider(contentProvider);
348     list1Viewer.setLabelProvider(labelProvider);
349 }
350 /**
351  * Set the sorter that is to be applied to self's list 1 viewer
352  */

353 public void setList1Sorter(ViewerSorter sorter) {
354     list1Viewer.setSorter(sorter);
355 }
356 /**
357  * Change the list 2 viewer's providers to those passed
358  *
359  * @param contentProvider ITreeContentProvider
360  * @param labelProvider ILabelProvider
361  */

362 public void setList2Providers(IStructuredContentProvider contentProvider, ILabelProvider labelProvider) {
363     list2Viewer.setContentProvider(contentProvider);
364     list2Viewer.setLabelProvider(labelProvider);
365 }
366 /**
367  * Set the sorter that is to be applied to self's list 2 viewer
368  *
369  * @param sorter IViewerSorter
370  */

371 public void setList2Sorter(ViewerSorter sorter) {
372     list2Viewer.setSorter(sorter);
373 }
374 /**
375  * Set the root element that determines the content of list viewer 1
376  */

377 public void setRoot(Object JavaDoc rootElement) {
378     root = rootElement;
379     checkedStateStore.clear();
380     list1Viewer.setInput(rootElement);
381 }
382 /**
383  * If this is set to true then only one list 1 item can be
384  * checked at a time. The default value for this is false.
385  *
386  * @param value boolean
387  */

388 public void setSingleList1Check(boolean value) {
389     singleList1Check = value;
390 }
391 /**
392  * If this is set to true then only one list 2 item can be
393  * checked at a time. The default value for this is false.
394  *
395  * @param value boolean
396  */

397 public void setSingleList2Check(boolean value) {
398     singleList2Check = value;
399 }
400 }
401
Popular Tags