1 11 package org.eclipse.jface.viewers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.ListenerList; 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.jface.util.SafeRunnable; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Table; 23 import org.eclipse.swt.widgets.TableColumn; 24 import org.eclipse.swt.widgets.TableItem; 25 import org.eclipse.swt.widgets.Widget; 26 27 37 public class CheckboxTableViewer extends TableViewer implements ICheckable { 38 39 42 private ListenerList checkStateListeners = new ListenerList(); 43 44 61 public CheckboxTableViewer(Composite parent) { 62 this(parent, SWT.BORDER); 63 } 64 65 96 public CheckboxTableViewer(Composite parent, int style) { 97 this(createTable(parent, style)); 98 } 99 100 118 public static CheckboxTableViewer newCheckList(Composite parent, int style) { 119 Table table = new Table(parent, SWT.CHECK | style); 120 return new CheckboxTableViewer(table); 121 } 122 123 131 public CheckboxTableViewer(Table table) { 132 super(table); 133 } 134 135 138 public void addCheckStateListener(ICheckStateListener listener) { 139 checkStateListeners.add(listener); 140 } 141 142 149 protected static Table createTable(Composite parent, int style) { 150 Table table = new Table(parent, SWT.CHECK | style); 151 152 new TableColumn(table, SWT.NONE); 156 TableLayout layout = new TableLayout(); 157 layout.addColumnData(new ColumnWeightData(100)); 158 table.setLayout(layout); 159 160 return table; 161 } 162 163 171 private void fireCheckStateChanged(final CheckStateChangedEvent event) { 172 Object [] array = checkStateListeners.getListeners(); 173 for (int i = 0; i < array.length; i++) { 174 final ICheckStateListener l = (ICheckStateListener) array[i]; 175 SafeRunnable.run(new SafeRunnable() { 176 public void run() { 177 l.checkStateChanged(event); 178 } 179 }); 180 } 181 } 182 183 186 public boolean getChecked(Object element) { 187 Widget widget = findItem(element); 188 if (widget instanceof TableItem) { 189 return ((TableItem) widget).getChecked(); 190 } 191 return false; 192 } 193 194 205 public Object [] getCheckedElements() { 206 TableItem[] children = getTable().getItems(); 207 ArrayList v = new ArrayList (children.length); 208 for (int i = 0; i < children.length; i++) { 209 TableItem item = children[i]; 210 if (item.getChecked()) { 211 v.add(item.getData()); 212 } 213 } 214 return v.toArray(); 215 } 216 217 224 public boolean getGrayed(Object element) { 225 Widget widget = findItem(element); 226 if (widget instanceof TableItem) { 227 return ((TableItem) widget).getGrayed(); 228 } 229 return false; 230 } 231 232 243 public Object [] getGrayedElements() { 244 TableItem[] children = getTable().getItems(); 245 List v = new ArrayList (children.length); 246 for (int i = 0; i < children.length; i++) { 247 TableItem item = children[i]; 248 if (item.getGrayed()) { 249 v.add(item.getData()); 250 } 251 } 252 return v.toArray(); 253 } 254 255 258 public void handleSelect(SelectionEvent event) { 259 if (event.detail == SWT.CHECK) { 260 super.handleSelect(event); 262 TableItem item = (TableItem) event.item; 263 Object data = item.getData(); 264 if (data != null) { 265 fireCheckStateChanged(new CheckStateChangedEvent(this, data, 266 item.getChecked())); 267 } 268 } else { 269 super.handleSelect(event); 270 } 271 } 272 273 276 protected void preservingSelection(Runnable updateCode) { 277 278 TableItem[] children = getTable().getItems(); 279 CustomHashtable checked = newHashtable(children.length * 2 + 1); 280 CustomHashtable grayed = newHashtable(children.length * 2 + 1); 281 282 for (int i = 0; i < children.length; i++) { 283 TableItem item = children[i]; 284 Object data = item.getData(); 285 if (data != null) { 286 if (item.getChecked()) { 287 checked.put(data, data); 288 } 289 if (item.getGrayed()) { 290 grayed.put(data, data); 291 } 292 } 293 } 294 295 super.preservingSelection(updateCode); 296 297 children = getTable().getItems(); 298 for (int i = 0; i < children.length; i++) { 299 TableItem item = children[i]; 300 Object data = item.getData(); 301 if (data != null) { 302 item.setChecked(checked.containsKey(data)); 303 item.setGrayed(grayed.containsKey(data)); 304 } 305 } 306 } 307 308 311 public void removeCheckStateListener(ICheckStateListener listener) { 312 checkStateListeners.remove(listener); 313 } 314 315 322 public void setAllChecked(boolean state) { 323 TableItem[] children = getTable().getItems(); 324 for (int i = 0; i < children.length; i++) { 325 TableItem item = children[i]; 326 item.setChecked(state); 327 } 328 } 329 330 336 public void setAllGrayed(boolean state) { 337 TableItem[] children = getTable().getItems(); 338 for (int i = 0; i < children.length; i++) { 339 TableItem item = children[i]; 340 item.setGrayed(state); 341 } 342 } 343 344 347 public boolean setChecked(Object element, boolean state) { 348 Assert.isNotNull(element); 349 Widget widget = findItem(element); 350 if (widget instanceof TableItem) { 351 ((TableItem) widget).setChecked(state); 352 return true; 353 } 354 return false; 355 } 356 357 370 public void setCheckedElements(Object [] elements) { 371 assertElementsNotNull(elements); 372 CustomHashtable set = newHashtable(elements.length * 2 + 1); 373 for (int i = 0; i < elements.length; ++i) { 374 set.put(elements[i], elements[i]); 375 } 376 TableItem[] items = getTable().getItems(); 377 for (int i = 0; i < items.length; ++i) { 378 TableItem item = items[i]; 379 Object element = item.getData(); 380 if (element != null) { 381 boolean check = set.containsKey(element); 382 if (item.getChecked() != check) { 384 item.setChecked(check); 385 } 386 } 387 } 388 } 389 390 399 public boolean setGrayed(Object element, boolean state) { 400 Assert.isNotNull(element); 401 Widget widget = findItem(element); 402 if (widget instanceof TableItem) { 403 ((TableItem) widget).setGrayed(state); 404 return true; 405 } 406 return false; 407 } 408 409 422 public void setGrayedElements(Object [] elements) { 423 assertElementsNotNull(elements); 424 CustomHashtable set = newHashtable(elements.length * 2 + 1); 425 for (int i = 0; i < elements.length; ++i) { 426 set.put(elements[i], elements[i]); 427 } 428 TableItem[] items = getTable().getItems(); 429 for (int i = 0; i < items.length; ++i) { 430 TableItem item = items[i]; 431 Object element = item.getData(); 432 if (element != null) { 433 boolean gray = set.containsKey(element); 434 if (item.getGrayed() != gray) { 436 item.setGrayed(gray); 437 } 438 } 439 } 440 } 441 } 442 | Popular Tags |