1 11 package org.eclipse.ui.internal.cheatsheets.views; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.help.HelpSystem; 17 import org.eclipse.help.IContext; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.FocusEvent; 21 import org.eclipse.swt.events.FocusListener; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.graphics.Color; 25 import org.eclipse.swt.graphics.Font; 26 import org.eclipse.swt.graphics.FontData; 27 import org.eclipse.swt.graphics.Image; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Label; 34 import org.eclipse.swt.widgets.Widget; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.cheatsheets.AbstractItemExtensionElement; 37 import org.eclipse.ui.forms.events.ExpansionAdapter; 38 import org.eclipse.ui.forms.events.ExpansionEvent; 39 import org.eclipse.ui.forms.events.HyperlinkAdapter; 40 import org.eclipse.ui.forms.events.HyperlinkEvent; 41 import org.eclipse.ui.forms.widgets.ExpandableComposite; 42 import org.eclipse.ui.forms.widgets.FormText; 43 import org.eclipse.ui.forms.widgets.FormToolkit; 44 import org.eclipse.ui.forms.widgets.ImageHyperlink; 45 import org.eclipse.ui.forms.widgets.TableWrapData; 46 import org.eclipse.ui.forms.widgets.TableWrapLayout; 47 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin; 48 import org.eclipse.ui.internal.cheatsheets.CheatSheetStopWatch; 49 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource; 50 import org.eclipse.ui.internal.cheatsheets.Messages; 51 import org.eclipse.ui.internal.cheatsheets.data.IParserTags; 52 import org.eclipse.ui.internal.cheatsheets.data.Item; 53 54 public abstract class ViewItem { 55 56 public final static byte VIEWITEM_ADVANCE = 0; 57 public final static byte VIEWITEM_DONOT_ADVANCE = 1; 58 private Composite bodyComp; 59 60 protected FormText bodyText; 61 protected FormText completionText; 62 protected Composite bodyWrapperComposite; 64 protected Composite buttonComposite; 65 protected Composite completionComposite; 66 67 private boolean buttonExpanded = true; 68 private boolean completionMessageExpanded = false; 69 private Label checkDoneLabel; 70 private boolean completed = false; 71 72 protected Item item; 73 74 protected Color itemColor; 76 77 private boolean isSkipped = false; 78 private ExpandableComposite mainItemComposite; 79 80 private Composite parent; 81 protected CheatSheetViewer viewer; 82 protected CheatSheetPage page; 83 private Composite titleComposite; 84 private boolean bold = true; 85 private Font boldFont; 86 private Font regularFont; 87 private boolean initialized = false; 88 protected ArrayList listOfSubItemCompositeHolders; 89 90 93 public ViewItem(CheatSheetPage page, Item item, Color itemColor, CheatSheetViewer viewer) { 94 super(); 95 this.page = page; 96 this.parent = page.getForm().getBody(); 97 this.item = item; 98 this.itemColor = itemColor; 99 this.viewer = viewer; 100 addItem(); 101 } 102 103 private void addItem() { 105 CheatSheetStopWatch.startStopWatch("ViewItem.addItem()"); CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after getBannerBackground: "); 108 checkDoneLabel = page.getToolkit().createLabel(parent, " "); CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create checkDoneLabel: "); 112 mainItemComposite = page.getToolkit().createSection(parent, ExpandableComposite.TWISTIE|ExpandableComposite.COMPACT); 113 mainItemComposite.setBackground(itemColor); 114 mainItemComposite.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 115 String title = item.getTitle(); 116 if (title != null) { 117 mainItemComposite.setText(ViewUtilities.escapeForLabel(title)); 118 } 119 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create mainItemComposite: "); 121 122 mainItemComposite.addExpansionListener(new ExpansionAdapter() { 123 public void expansionStateChanged(ExpansionEvent e) { 124 page.getForm().reflow(true); 125 } 126 }); 127 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after addExpansionListener: "); 129 int number = 0; 132 ArrayList itemExts = item.getItemExtensions(); 133 134 if((itemExts != null && itemExts.size() > 0) || item.getContextId() != null || item.getHref() != null) { 135 titleComposite = page.getToolkit().createComposite(mainItemComposite); 137 titleComposite.setBackground(itemColor); 138 } 139 140 if(itemExts != null) { 141 for (int g = 0; g < itemExts.size(); g++) { 142 AbstractItemExtensionElement[] eea = (AbstractItemExtensionElement[]) itemExts.get(g); 143 number += eea.length; 144 for (int x = 0; x < eea.length; x++) { 145 eea[x].createControl(titleComposite); 146 } 147 } 148 } 149 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create item extensions: "); 151 if(item.getContextId() != null || item.getHref() != null) { 153 number++; 155 ImageHyperlink helpButton = createButton(titleComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_HELP), this, itemColor, Messages.HELP_BUTTON_TOOLTIP); 156 helpButton.addHyperlinkListener(new HyperlinkAdapter() { 157 public void linkActivated(HyperlinkEvent e) { 158 if(item.getContextId() != null) { 160 openInfopop(e.widget); 161 } else { 162 openHelpTopic(); 164 } 165 } 166 }); 167 } 168 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create help button: "); 170 if(number > 0) { 171 mainItemComposite.setTextClient(titleComposite); 172 GridLayout layout = new GridLayout(number, false); 173 GridData data = new GridData(GridData.FILL_BOTH); 174 175 titleComposite.setLayout(layout); 176 titleComposite.setLayoutData(data); 177 layout.marginWidth = 0; 178 layout.marginHeight = 0; 179 layout.verticalSpacing = 0; 180 } 181 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after setTextClient: "); 183 bodyWrapperComposite = page.getToolkit().createComposite(mainItemComposite); 185 mainItemComposite.setClient(bodyWrapperComposite); 186 TableWrapLayout wrapperLayout = new TableWrapLayout(); 187 bodyWrapperComposite.setLayout(wrapperLayout); 188 bodyWrapperComposite.setBackground(itemColor); 189 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create bodyWrapperComposite: "); 191 bodyText = page.getToolkit().createFormText(bodyWrapperComposite, false); 192 bodyText.addSelectionListener(new SelectionAdapter() { 193 public void widgetSelected(SelectionEvent e) { 194 Action copyAction = viewer.getCopyAction(); 195 if (copyAction!=null) 196 copyAction.setEnabled(bodyText.canCopy()); 197 } 198 }); 199 bodyText.addFocusListener(new FocusListener() { 200 public void focusGained(FocusEvent e) { 201 Action copyAction = viewer.getCopyAction(); 202 if (copyAction!=null) 203 copyAction.setEnabled(bodyText.canCopy()); 204 } 205 public void focusLost(FocusEvent e) { 206 Action copyAction = viewer.getCopyAction(); 207 if (copyAction!=null) 208 copyAction.setEnabled(false); 209 } 210 }); 211 bodyText.setText(item.getDescription(), item.getDescription().startsWith(IParserTags.FORM_START_TAG), false); 213 214 bodyText.setBackground(itemColor); 216 bodyText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 217 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create FormText: "); 219 224 if(!item.isDynamic()) { 225 handleButtons(); 226 } 227 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after handleButtons(): "); 229 setButtonsVisible(false); 230 setCollapsed(); 231 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after setting buttons and item collapsed: "); 233 boldFont = mainItemComposite.getFont(); 234 FontData[] fontDatas = boldFont.getFontData(); 235 for (int i = 0; i < fontDatas.length; i++) { 236 fontDatas[i].setStyle(fontDatas[i].getStyle() ^ SWT.BOLD); 237 } 238 regularFont = new Font(mainItemComposite.getDisplay(), fontDatas); 239 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after font initlization: "); 241 setBold(false); 242 CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after setBold: "); } 244 245 protected ImageHyperlink createButtonWithText(Composite parent, Image image, ViewItem item, Color color, String linkText) { 246 ImageHyperlink button = page.getToolkit().createImageHyperlink(parent, SWT.NULL); 247 button.setImage(image); 248 button.setData(item); 249 button.setBackground(color); 250 button.setText(linkText); 251 button.setToolTipText(linkText); 252 return button; 253 } 254 255 protected ImageHyperlink createButton(Composite parent, Image image, ViewItem item, Color color, String toolTipText) { 256 ImageHyperlink button = new ImageHyperlink(parent, SWT.NULL); 257 page.getToolkit().adapt(button, true, true); 258 button.setImage(image); 259 button.setData(item); 260 button.setBackground(color); 261 button.setToolTipText(toolTipText); 262 265 return button; 266 } 267 268 public void dispose() { 269 if (checkDoneLabel != null) 270 checkDoneLabel.dispose(); 271 if (bodyText != null) 272 bodyText.dispose(); 273 if (buttonComposite != null) 274 buttonComposite.dispose(); 275 if (completionComposite != null) 276 completionComposite.dispose(); 277 if (bodyComp != null) 278 bodyComp.dispose(); 279 if (bodyWrapperComposite != null) 280 bodyWrapperComposite.dispose(); 281 if (mainItemComposite != null) 282 mainItemComposite.dispose(); 283 if (titleComposite != null) 284 titleComposite.dispose(); 285 if (regularFont != null) 286 regularFont.dispose(); 287 288 ArrayList itemExts = item.getItemExtensions(); 289 if (itemExts != null) { 290 for (int g = 0; g < itemExts.size(); g++) { 291 AbstractItemExtensionElement[] eea = (AbstractItemExtensionElement[]) itemExts.get(g); 292 for (int x = 0; x < eea.length; x++) { 293 eea[x].dispose(); 294 } 295 } 296 } 297 } 298 299 302 303 Image getCompleteImage() { 304 return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_COMPLETE); 305 } 306 307 310 public Item getItem() { 311 return item; 312 } 313 314 318 319 Composite getMainItemComposite() { 320 return mainItemComposite; 321 } 322 323 326 327 Image getSkipImage() { 328 return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_SKIP); 329 } 330 331 333 abstract void handleButtons(); 334 335 336 boolean isBold() { 337 return bold; 338 } 339 340 344 public boolean isCompleted() { 345 return completed; 346 } 347 348 public boolean isExpanded() { 349 return mainItemComposite.isExpanded(); 350 } 351 352 public boolean isCompletionMessageExpanded() { 353 return completionMessageExpanded; 354 } 355 356 362 public boolean isInDialogMode() { 363 return viewer.isInDialogMode(); 364 } 365 366 367 boolean isSkipped() { 368 return isSkipped; 369 } 370 371 374 private void openHelpTopic() { 375 if (item == null || item.getHref() == null) { 376 return; 377 } 378 379 PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(item.getHref()); 380 } 381 382 385 private void openInfopop(Widget widget) { 386 if (item == null || item.getContextId() == null) { 387 return; 388 } 389 390 IContext context = HelpSystem.getContext(item.getContextId()); 391 392 if (context != null) { 393 Point point = widget.getDisplay().getCursorLocation(); 395 point = new Point(point.x + 15, point.y); 396 PlatformUI.getWorkbench().getHelpSystem().displayContext(context, point.x, point.y); 398 } 399 } 400 401 public void setAsCurrentActiveItem() { 402 setColorAsCurrent(true); 403 setButtonsVisible(true); 404 setBold(true); 405 setExpanded(); 406 setFocus(); 407 } 408 409 protected void setFocus() { 410 mainItemComposite.setFocus(); 411 FormToolkit.ensureVisible(getMainItemComposite()); 412 } 413 414 415 void setAsNormalCollapsed() { 416 setBold(false); 417 setColorAsCurrent(false); 418 if (mainItemComposite.isExpanded()) 419 setCollapsed(); 420 } 421 422 423 void setAsNormalNonCollapsed() { 424 setColorAsCurrent(false); 425 setBold(false); 426 } 427 428 private void setBodyColor(Color color) { 429 mainItemComposite.setBackground(color); 430 setBackgroundColor(bodyWrapperComposite, color); 431 setBackgroundColor(buttonComposite, color); 432 setBackgroundColor(completionComposite, color); 433 } 434 435 439 protected void setBackgroundColor(Composite composite, Color color) { 440 if (composite != null) { 441 composite.setBackground(color); 442 Control[] children = composite.getChildren(); 443 for (int i = 0; i < children.length; i++) { 444 children[i].setBackground(color); 445 } 446 } 447 } 448 449 450 void setBold(boolean value) { 451 if(value) { 452 mainItemComposite.setFont(boldFont); 453 if(initialized) 454 mainItemComposite.layout(); 455 } else { 456 mainItemComposite.setFont(regularFont); 457 if(initialized) 458 mainItemComposite.layout(); 459 } 460 bold = value; 461 } 462 463 465 void setButtonsVisible(boolean isVisible) { 466 if (buttonExpanded != isVisible) { 467 if (listOfSubItemCompositeHolders != null) { 468 for (Iterator iter = listOfSubItemCompositeHolders.iterator(); iter.hasNext(); ){ 469 ((SubItemCompositeHolder)iter.next()).setButtonsVisible(isVisible); 470 } 471 } else if (buttonComposite != null) { 472 buttonComposite.setVisible(isVisible); 473 } 474 } 475 476 if(isVisible && initialized) { 477 FormToolkit.ensureVisible(getMainItemComposite()); 478 } 479 buttonExpanded = isVisible; 480 } 481 482 protected void setCompletionMessageExpanded(boolean isFinalItem) { 483 if (hasCompletionMessage()) { 484 if (completionComposite == null) { 485 createCompletionComposite(isFinalItem); 486 } 487 if (!completionMessageExpanded) { 488 completionComposite.setVisible(true); 489 completionMessageExpanded = true; 490 } 491 } 492 } 493 494 abstract void createCompletionComposite(boolean isFinalItem); 495 496 protected void setCompletionMessageCollapsed() { 497 if (completionComposite != null) { 498 if (completionMessageExpanded) { 499 completionComposite.dispose(); 500 completionComposite = null; 501 page.getForm().reflow(true); 502 } 503 } 504 completionMessageExpanded = false; 505 } 506 507 509 void setCollapsed() { 510 if (mainItemComposite.isExpanded()) { 511 mainItemComposite.setExpanded(false); 512 if(initialized) { 513 page.getForm().reflow(true); 514 FormToolkit.ensureVisible(getMainItemComposite()); 515 } 516 } 517 } 518 519 private void setColorAsCurrent(boolean active) { 520 if (active) { 521 setTitleColor(page.getActiveColor()); 522 setBodyColor(page.getActiveColor()); 523 } else { 524 setTitleColor(itemColor); 525 setBodyColor(itemColor); 526 } 527 } 528 529 531 void setComplete() { 532 completed = true; 533 checkDoneLabel.setImage(getCompleteImage()); 534 535 if(initialized) { 536 checkDoneLabel.getParent().layout(); 537 } 538 } 539 540 542 void setExpanded() { 543 if (!mainItemComposite.isExpanded()) { 544 mainItemComposite.setExpanded(true); 545 if(initialized) { 546 page.getForm().reflow(true); 547 FormToolkit.ensureVisible(getMainItemComposite()); 548 } 549 } 550 } 551 552 553 void setIncomplete() { 554 checkDoneLabel.setImage(null); 555 completed = false; 556 setStartImage(); 557 } 558 559 563 564 void setItemColor(Color itemColor) { 565 this.itemColor = itemColor; 566 } 567 568 569 void setOriginalColor() { 570 setTitleColor(itemColor); 571 setBodyColor(itemColor); 572 setBold(false); 573 } 574 575 576 abstract void setRestartImage(); 577 578 579 void setSkipped() { 580 isSkipped = true; 581 checkDoneLabel.setImage(getSkipImage()); 582 583 if(initialized) { 584 checkDoneLabel.getParent().layout(); 585 } 586 } 587 588 abstract void setStartImage(); 589 590 private void setTitleColor(Color bg) { 591 if(titleComposite != null) { 592 titleComposite.setBackground(bg); 593 594 Control[] titlechildren = titleComposite.getChildren(); 595 for (int i = 0; i < titlechildren.length; i++) { 596 titlechildren[i].setBackground(bg); 597 } 598 } 599 } 600 601 public void initialized() { 602 initialized = true; 603 } 604 605 public boolean canCopy() { 606 return (bodyText!=null && !bodyText.isDisposed())?bodyText.canCopy():false; 607 } 608 public void copy() { 609 if (bodyText!=null && !bodyText.isDisposed()) 610 bodyText.copy(); 611 } 612 613 abstract boolean hasCompletionMessage(); 614 615 } 616 | Popular Tags |