1 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 28 public class CheckboxDoubleListGroup extends Composite implements ICheckStateListener, ISelectionChangedListener { 29 private Object root; 30 private Object 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 private CheckboxTableViewer list1Viewer; 43 private CheckboxTableViewer list2Viewer; 44 56 public CheckboxDoubleListGroup( 57 Composite parent,Object 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 82 public CheckboxDoubleListGroup( 83 Composite parent,Object 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 102 public void addCheckStateListener(ICheckStateListener listener) { 103 listeners.add(listener); 104 } 105 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 127 public Point computeSize(int wHint,int hHint,boolean changed) { 128 return new Point(-1,-1); 129 } 130 137 protected void createContents(Composite parent,int width,int height) { 138 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 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 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 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 200 public Set getAllCheckedList1Items() { 201 return checkedStateStore.keySet(); 202 } 203 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 227 public int getCheckedElementCount() { 228 return checkedStateStore.size(); 229 } 230 234 public void initialCheckList1Item(Object element) { 235 checkedStateStore.put(element,new ArrayList()); 236 list1Viewer.setChecked(element,true); 237 } 238 241 protected void list1ItemChecked(Object listElement,boolean state) { 242 243 if (state) { 244 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 list1Viewer.setChecked(listElement,state); 261 } 262 265 protected void list2ItemChecked(Object listElement,boolean state) { 266 List checkedListItems = (List)checkedStateStore.get(currentList1Selection); 267 268 if (state) { 269 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 294 protected void notifyCheckStateChangeListeners(final CheckStateChangedEvent event) { 295 Object [] 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 311 public void removeCheckStateListener(ICheckStateListener listener) { 312 listeners.remove(listener); 313 } 314 319 public void selectionChanged(SelectionChangedEvent event) { 320 IStructuredSelection selection = (IStructuredSelection) event.getSelection(); 321 final Object selectedElement = selection.getFirstElement(); 322 if (selectedElement == null) { 323 currentList1Selection = null; 324 list2Viewer.setInput(currentList1Selection); 325 return; 326 } 327 328 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 346 public void setList1Providers(IStructuredContentProvider contentProvider, ILabelProvider labelProvider) { 347 list1Viewer.setContentProvider(contentProvider); 348 list1Viewer.setLabelProvider(labelProvider); 349 } 350 353 public void setList1Sorter(ViewerSorter sorter) { 354 list1Viewer.setSorter(sorter); 355 } 356 362 public void setList2Providers(IStructuredContentProvider contentProvider, ILabelProvider labelProvider) { 363 list2Viewer.setContentProvider(contentProvider); 364 list2Viewer.setLabelProvider(labelProvider); 365 } 366 371 public void setList2Sorter(ViewerSorter sorter) { 372 list2Viewer.setSorter(sorter); 373 } 374 377 public void setRoot(Object rootElement) { 378 root = rootElement; 379 checkedStateStore.clear(); 380 list1Viewer.setInput(rootElement); 381 } 382 388 public void setSingleList1Check(boolean value) { 389 singleList1Check = value; 390 } 391 397 public void setSingleList2Check(boolean value) { 398 singleList2Check = value; 399 } 400 } 401 | Popular Tags |