1 14 15 package org.eclipse.jface.viewers; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Widget; 23 24 34 public abstract class AbstractListViewer extends StructuredViewer { 35 36 39 private java.util.List listMap = new ArrayList (); 40 41 47 protected abstract void listAdd(String string, int index); 48 49 55 protected abstract void listSetItem(int index, String string); 56 57 67 protected abstract int[] listGetSelectionIndices(); 68 69 74 protected abstract int listGetItemCount(); 75 76 81 protected abstract void listSetItems(String [] labels); 82 83 86 protected abstract void listRemoveAll(); 87 88 94 protected abstract void listRemove(int index); 95 96 106 protected abstract void listSetSelection(int[] ixs); 107 108 113 protected abstract void listShowSelection(); 114 115 118 protected abstract void listDeselectAll(); 119 120 132 public void add(Object [] elements) { 133 assertElementsNotNull(elements); 134 Object [] filtered = filter(elements); 135 ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); 136 for (int i = 0; i < filtered.length; i++) { 137 Object element = filtered[i]; 138 int ix = indexForElement(element); 139 insertItem(labelProvider, element, ix); 140 } 141 } 142 143 private void insertItem(ILabelProvider labelProvider, Object element, int index) { 144 listAdd(getLabelProviderText(labelProvider, element), index); 145 listMap.add(index, element); 146 mapElement(element, getControl()); } 148 149 166 public void insert(Object element, int position) { 167 if (getComparator() != null || hasFilters()) { 168 add(element); 169 return; 170 } 171 172 insertItem((ILabelProvider) getLabelProvider(), element, position); 173 } 174 175 176 186 private String getLabelProviderText(ILabelProvider labelProvider, Object element){ 187 String text = labelProvider.getText(element); 188 if(text == null) { 189 return ""; } 191 return text; 192 } 193 194 208 public void add(Object element) { 209 add(new Object [] { element }); 210 } 211 212 216 protected Widget doFindInputItem(Object element) { 217 if (element != null && equals(element, getRoot())) { 218 return getControl(); 219 } 220 return null; 221 } 222 223 227 protected Widget doFindItem(Object element) { 228 if (element != null) { 229 if (listMapContains(element)) { 230 return getControl(); 231 } 232 } 233 return null; 234 } 235 236 239 protected void doUpdateItem(Widget data, Object element, boolean fullMap) { 240 if (element != null) { 241 int ix = getElementIndex(element); 242 if (ix >= 0) { 243 ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); 244 listSetItem(ix, getLabelProviderText(labelProvider,element)); 245 } 246 } 247 } 248 249 252 public abstract Control getControl(); 253 254 262 public Object getElementAt(int index) { 263 if (index >= 0 && index < listMap.size()) { 264 return listMap.get(index); 265 } 266 return null; 267 } 268 269 274 public IBaseLabelProvider getLabelProvider() { 275 return super.getLabelProvider(); 276 } 277 278 281 284 protected List getSelectionFromWidget() { 285 int[] ixs = listGetSelectionIndices(); 286 ArrayList list = new ArrayList (ixs.length); 287 for (int i = 0; i < ixs.length; i++) { 288 Object e = getElementAt(ixs[i]); 289 if (e != null) { 290 list.add(e); 291 } 292 } 293 return list; 294 } 295 296 300 protected int indexForElement(Object element) { 301 ViewerComparator comparator = getComparator(); 302 if (comparator == null) { 303 return listGetItemCount(); 304 } 305 int count = listGetItemCount(); 306 int min = 0, max = count - 1; 307 while (min <= max) { 308 int mid = (min + max) / 2; 309 Object data = listMap.get(mid); 310 int compare = comparator.compare(this, data, element); 311 if (compare == 0) { 312 while (compare == 0) { 314 ++mid; 315 if (mid >= count) { 316 break; 317 } 318 data = listMap.get(mid); 319 compare = comparator.compare(this, data, element); 320 } 321 return mid; 322 } 323 if (compare < 0) { 324 min = mid + 1; 325 } else { 326 max = mid - 1; 327 } 328 } 329 return min; 330 } 331 332 335 protected void inputChanged(Object input, Object oldInput) { 336 listMap.clear(); 337 Object [] children = getSortedChildren(getRoot()); 338 int size = children.length; 339 340 listRemoveAll(); 341 String [] labels = new String [size]; 342 for (int i = 0; i < size; i++) { 343 Object el = children[i]; 344 labels[i] = getLabelProviderText((ILabelProvider) getLabelProvider(),el); 345 listMap.add(el); 346 mapElement(el, getControl()); } 348 listSetItems(labels); 349 } 350 351 354 protected void internalRefresh(Object element) { 355 Control list = getControl(); 356 if (element == null || equals(element, getRoot())) { 357 if (listMap != null) { 359 listMap.clear(); 360 } 361 unmapAllElements(); 362 List selection = getSelectionFromWidget(); 363 364 int topIndex = -1; 365 if (selection == null || selection.isEmpty()) { 366 topIndex = listGetTopIndex(); 367 } 368 369 list.setRedraw(false); 370 listRemoveAll(); 371 372 Object [] children = getSortedChildren(getRoot()); 373 String [] items = new String [children.length]; 374 375 ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); 376 377 for (int i = 0; i < items.length; i++) { 378 Object el = children[i]; 379 items[i] = getLabelProviderText(labelProvider, el); 380 listMap.add(el); 381 mapElement(el, list); } 383 384 listSetItems(items); 385 list.setRedraw(true); 386 387 if (topIndex == -1) { 388 setSelectionToWidget(selection, false); 389 } else { 390 listSetTopIndex(Math.min(topIndex, children.length)); 391 } 392 } else { 393 doUpdateItem(list, element, true); 394 } 395 } 396 397 405 protected int listGetTopIndex(){ 406 return -1; 407 } 408 409 417 protected void listSetTopIndex(int index) { 418 } 419 420 425 private void internalRemove(final Object [] elements) { 426 Object input = getInput(); 427 for (int i = 0; i < elements.length; ++i) { 428 if (equals(elements[i], input)) { 429 setInput(null); 430 return; 431 } 432 int ix = getElementIndex(elements[i]); 433 if (ix >= 0) { 434 listRemove(ix); 435 listMap.remove(ix); 436 unmapElement(elements[i], getControl()); 437 } 438 } 439 } 440 441 452 public void remove(final Object [] elements) { 453 assertElementsNotNull(elements); 454 if (elements.length == 0) { 455 return; 456 } 457 preservingSelection(new Runnable () { 458 public void run() { 459 internalRemove(elements); 460 } 461 }); 462 } 463 464 477 public void remove(Object element) { 478 remove(new Object [] { element }); 479 } 480 481 489 public void setLabelProvider(IBaseLabelProvider labelProvider) { 490 Assert.isTrue(labelProvider instanceof ILabelProvider); 491 super.setLabelProvider(labelProvider); 492 } 493 494 497 protected void setSelectionToWidget(List in, boolean reveal) { 498 if (in == null || in.size() == 0) { listDeselectAll(); 500 } else { 501 int n = in.size(); 502 int[] ixs = new int[n]; 503 int count = 0; 504 for (int i = 0; i < n; ++i) { 505 Object el = in.get(i); 506 int ix = getElementIndex(el); 507 if (ix >= 0) { 508 ixs[count++] = ix; 509 } 510 } 511 if (count < n) { 512 System.arraycopy(ixs, 0, ixs = new int[count], 0, count); 513 } 514 listSetSelection(ixs); 515 if (reveal) { 516 listShowSelection(); 517 } 518 } 519 } 520 521 528 int getElementIndex(Object element) { 529 IElementComparer comparer = getComparer(); 530 if (comparer == null) { 531 return listMap.indexOf(element); 532 } 533 int size = listMap.size(); 534 for (int i = 0; i < size; i++) { 535 if (comparer.equals(element, listMap.get(i))) 536 return i; 537 } 538 return -1; 539 } 540 541 547 private boolean listMapContains(Object element) { 548 return getElementIndex(element) != -1; 549 } 550 551 } 552 | Popular Tags |