1 11 package org.eclipse.ui.internal.progress; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jface.dialogs.IDialogConstants; 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.jface.viewers.ViewerComparator; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.ScrolledComposite; 21 import org.eclipse.swt.events.ControlEvent; 22 import org.eclipse.swt.events.ControlListener; 23 import org.eclipse.swt.events.FocusAdapter; 24 import org.eclipse.swt.events.FocusEvent; 25 import org.eclipse.swt.graphics.Point; 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.Control; 30 import org.eclipse.swt.widgets.Text; 31 import org.eclipse.swt.widgets.Widget; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 34 35 43 public class VirtualProgressViewer extends AbstractProgressViewer { 44 45 Composite control; 46 47 private ScrolledComposite scrolled; 48 49 private Composite noEntryArea; 50 51 58 public VirtualProgressViewer(Composite parent, int style) { 59 scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | style); 60 int height = JFaceResources.getDefaultFont().getFontData()[0] 61 .getHeight(); 62 scrolled.getVerticalBar().setIncrement(height * 2); 63 scrolled.setExpandHorizontal(true); 64 scrolled.setExpandVertical(true); 65 66 control = new Composite(scrolled, SWT.NONE); 67 68 control.addControlListener(new ControlListener() { 69 74 public void controlMoved(ControlEvent e) { 75 updateVisibleItems(); 76 77 } 78 79 84 public void controlResized(ControlEvent e) { 85 updateVisibleItems(); 86 87 } 88 }); 89 GridLayout layout = new GridLayout(); 90 layout.marginHeight = 0; 91 layout.marginWidth = 0; 92 control.setLayout(layout); 93 control.setBackground(parent.getDisplay().getSystemColor( 94 SWT.COLOR_LIST_BACKGROUND)); 95 96 control.addFocusListener(new FocusAdapter() { 97 102 public void focusGained(FocusEvent e) { 103 setFocus(); 104 } 105 }); 106 107 PlatformUI.getWorkbench().getHelpSystem().setHelp(control, 108 IWorkbenchHelpContextIds.RESPONSIVE_UI); 109 110 scrolled.setContent(control); 111 hookControl(control); 112 113 noEntryArea = new Composite(scrolled, SWT.NONE); 114 noEntryArea.setLayout(new GridLayout()); 115 116 Text noEntryLabel = new Text(noEntryArea, SWT.SINGLE); 117 noEntryLabel.setText(ProgressMessages.ProgressView_NoOperations); 118 noEntryLabel.setBackground(noEntryArea.getDisplay().getSystemColor( 119 SWT.COLOR_WIDGET_BACKGROUND)); 120 GridData textData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 121 noEntryLabel.setLayoutData(textData); 122 noEntryLabel.setEditable(false); 123 124 PlatformUI.getWorkbench().getHelpSystem().setHelp(noEntryLabel, 125 IWorkbenchHelpContextIds.RESPONSIVE_UI); 126 127 } 128 129 134 public void add(Object [] elements) { 135 ViewerComparator sorter = getComparator(); 136 ArrayList newItems = new ArrayList (control.getChildren().length 137 + elements.length); 138 139 Control[] existingChildren = control.getChildren(); 140 for (int i = 0; i < existingChildren.length; i++) { 141 newItems.add(existingChildren[i].getData()); 142 } 143 144 for (int i = 0; i < elements.length; i++) { 145 newItems.add(elements[i]); 146 } 147 148 JobTreeElement[] infos = new JobTreeElement[newItems.size()]; 149 newItems.toArray(infos); 150 151 if (sorter != null) { 152 sorter.sort(this, infos); 153 } 154 155 for (int i = 0; i < existingChildren.length; i++) { 157 ((VirtualInfoItem) existingChildren[i]).dispose(); 158 } 159 160 for (int i = 0; i < newItems.size(); i++) { 161 VirtualInfoItem item = createNewItem(infos[i]); 162 item.setColor(i); 163 } 164 165 control.layout(true); 166 updateForShowingProgress(); 167 } 168 169 172 private void updateForShowingProgress() { 173 if (control.getChildren().length > 0) { 174 scrolled.setContent(control); 175 } else { 176 scrolled.setContent(noEntryArea); 177 } 178 } 179 180 186 private VirtualInfoItem createNewItem(JobTreeElement info) { 187 final VirtualInfoItem item = new VirtualInfoItem(control, SWT.NONE, 188 info); 189 190 item.setIndexListener(new VirtualInfoItem.IndexListener() { 191 196 public void selectNext() { 197 VirtualProgressViewer.this.selectNext(item); 198 199 } 200 201 206 public void selectPrevious() { 207 VirtualProgressViewer.this.selectPrevious(item); 208 209 } 210 211 216 public void select() { 217 218 Control[] children = control.getChildren(); 219 for (int i = 0; i < children.length; i++) { 220 VirtualInfoItem child = (VirtualInfoItem) children[i]; 221 if (!item.equals(child)) { 222 child.selectWidgets(false); 223 } 224 } 225 item.selectWidgets(true); 226 227 } 228 }); 229 230 item.refresh(); 232 return item; 233 } 234 235 240 protected void selectPrevious(VirtualInfoItem item) { 241 Control[] children = control.getChildren(); 242 for (int i = 0; i < children.length; i++) { 243 VirtualInfoItem child = (VirtualInfoItem) children[i]; 244 if (item.equals(child)) { 245 VirtualInfoItem previous; 246 if (i == 0) { 247 previous = (VirtualInfoItem) children[children.length - 1]; 248 } else { 249 previous = (VirtualInfoItem) children[i - 1]; 250 } 251 252 item.selectWidgets(false); 253 previous.selectWidgets(true); 254 return; 255 } 256 } 257 } 258 259 264 protected void selectNext(VirtualInfoItem item) { 265 Control[] children = control.getChildren(); 266 for (int i = 0; i < children.length; i++) { 267 VirtualInfoItem child = (VirtualInfoItem) children[i]; 268 if (item.equals(child)) { 269 VirtualInfoItem next; 270 if (i == children.length - 1) { 271 next = (VirtualInfoItem) children[0]; 272 } else { 273 next = (VirtualInfoItem) children[i + 1]; 274 } 275 item.selectWidgets(false); 276 next.selectWidgets(true); 277 278 return; 279 } 280 } 281 282 } 283 284 289 protected Widget doFindInputItem(Object element) { 290 return null; 291 } 292 293 298 protected Widget doFindItem(Object element) { 299 Control[] existingChildren = control.getChildren(); 300 for (int i = 0; i < existingChildren.length; i++) { 301 if (existingChildren[i].isDisposed() 302 || existingChildren[i].getData() == null) { 303 continue; 304 } 305 if (existingChildren[i].getData().equals(element)) { 306 return existingChildren[i]; 307 } 308 } 309 return null; 310 } 311 312 318 protected void doUpdateItem(Widget item, Object element, boolean fullMap) { 319 if (usingElementMap()) { 320 unmapElement(item); 321 } 322 item.dispose(); 323 add(new Object [] { element }); 324 } 325 326 331 public Control getControl() { 332 return scrolled; 333 } 334 335 340 protected List getSelectionFromWidget() { 341 return new ArrayList (0); 342 } 343 344 350 protected void inputChanged(Object input, Object oldInput) { 351 super.inputChanged(input, oldInput); 352 refreshAll(); 353 updateForShowingProgress(); 354 } 355 356 361 protected void internalRefresh(Object element) { 362 if (element == null) { 363 return; 364 } 365 366 if (element.equals(getRoot())) { 367 refreshAll(); 368 return; 369 } 370 Widget widget = findItem(element); 371 if (widget == null) { 372 add(new Object [] { element }); 373 return; 374 } 375 ((VirtualInfoItem) widget).refresh(); 376 377 Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT); 379 size.x += IDialogConstants.HORIZONTAL_SPACING; 380 size.y += IDialogConstants.VERTICAL_SPACING; 381 382 scrolled.setMinSize(size); 383 } 384 385 390 public void remove(Object [] elements) { 391 392 for (int i = 0; i < elements.length; i++) { 393 394 if (((JobTreeElement) elements[i]).isJobInfo() 396 && FinishedJobs.getInstance().isFinished( 397 (JobInfo) elements[i])) { 398 Widget item = doFindItem(elements[i]); 399 if (item != null) { 400 ((VirtualInfoItem) item).refresh(); 401 } 402 403 } else { 404 Widget item = doFindItem(elements[i]); 405 if (item != null) { 406 unmapElement(elements[i]); 407 item.dispose(); 408 } 409 } 410 } 411 412 Control[] existingChildren = control.getChildren(); 413 for (int i = 0; i < existingChildren.length; i++) { 414 VirtualInfoItem item = (VirtualInfoItem) existingChildren[i]; 415 item.setColor(i); 416 } 417 control.layout(true); 418 updateForShowingProgress(); 419 } 420 421 426 public void reveal(Object element) { 427 428 } 429 430 436 protected void setSelectionToWidget(List l, boolean reveal) { 437 438 } 439 440 444 public void cancelSelection() { 445 446 } 447 448 452 public void setFocus() { 453 Control[] children = control.getChildren(); 454 if (children.length > 0) { 455 for (int i = 0; i < children.length; i++) { 456 VirtualInfoItem item = (VirtualInfoItem) children[i]; 457 item.setButtonFocus(); 458 return; 459 } 460 } else 461 noEntryArea.setFocus(); 462 } 463 464 467 private void refreshAll() { 468 469 Object [] infos = getSortedChildren(getRoot()); 470 Control[] existingChildren = control.getChildren(); 471 472 for (int i = 0; i < existingChildren.length; i++) { 473 existingChildren[i].dispose(); 474 475 } 476 for (int i = 0; i < infos.length; i++) { 478 VirtualInfoItem item = createNewItem((JobTreeElement) infos[i]); 479 item.setColor(i); 480 } 481 482 control.layout(true); 483 updateForShowingProgress(); 484 485 } 486 487 491 private void updateVisibleItems() { 492 Control[] children = control.getChildren(); 493 int top = scrolled.getOrigin().y; 494 int bottom = top + scrolled.getParent().getBounds().height; 495 for (int i = 0; i < children.length; i++) { 496 VirtualInfoItem item = (VirtualInfoItem) children[i]; 497 item.setDisplayed(top, bottom); 498 499 } 500 } 501 502 } 503 | Popular Tags |