1 11 package org.eclipse.ui.internal.intro.impl.swt; 12 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.SWTError; 16 import org.eclipse.swt.graphics.Color; 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Control; 20 import org.eclipse.swt.widgets.Label; 21 import org.eclipse.ui.forms.IFormColors; 22 import org.eclipse.ui.forms.events.HyperlinkAdapter; 23 import org.eclipse.ui.forms.events.HyperlinkEvent; 24 import org.eclipse.ui.forms.widgets.FormText; 25 import org.eclipse.ui.forms.widgets.FormToolkit; 26 import org.eclipse.ui.forms.widgets.Hyperlink; 27 import org.eclipse.ui.forms.widgets.ImageHyperlink; 28 import org.eclipse.ui.forms.widgets.Section; 29 import org.eclipse.ui.forms.widgets.TableWrapData; 30 import org.eclipse.ui.forms.widgets.TableWrapLayout; 31 import org.eclipse.ui.internal.intro.impl.Messages; 32 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement; 33 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement; 34 import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider; 35 import org.eclipse.ui.internal.intro.impl.model.IntroGroup; 36 import org.eclipse.ui.internal.intro.impl.model.IntroHTML; 37 import org.eclipse.ui.internal.intro.impl.model.IntroImage; 38 import org.eclipse.ui.internal.intro.impl.model.IntroLink; 39 import org.eclipse.ui.internal.intro.impl.model.IntroSeparator; 40 import org.eclipse.ui.internal.intro.impl.model.IntroText; 41 import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager; 42 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser; 43 import org.eclipse.ui.internal.intro.impl.util.DialogUtil; 44 import org.eclipse.ui.internal.intro.impl.util.ImageUtil; 45 import org.eclipse.ui.internal.intro.impl.util.Log; 46 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 47 import org.eclipse.ui.internal.intro.impl.util.Util; 48 import org.eclipse.ui.intro.config.IIntroContentProvider; 49 import org.eclipse.ui.intro.config.IIntroContentProviderSite; 50 51 54 public class PageWidgetFactory { 55 56 protected HyperlinkAdapter hyperlinkAdapter = new HyperlinkAdapter() { 57 58 public void linkActivated(HyperlinkEvent e) { 59 String url = (String ) e.getHref(); 60 IntroURLParser parser = new IntroURLParser(url); 61 if (parser.hasIntroUrl()) { 62 parser.getIntroURL().execute(); 64 return; 65 } else if (parser.hasProtocol()) { 66 Util.openBrowser(url); 67 return; 68 } 69 DialogUtil.displayInfoMessage(((Control) e.getSource()).getShell(), 70 Messages.HyperlinkAdapter_urlIs + " " + url); } 72 73 public void linkEntered(HyperlinkEvent e) { 74 } 75 76 public void linkExited(HyperlinkEvent e) { 77 } 78 }; 79 80 81 protected FormToolkit toolkit; 82 protected PageStyleManager styleManager; 83 protected IIntroContentProviderSite site; 84 85 86 89 protected PageWidgetFactory(FormToolkit toolkit, 90 PageStyleManager styleManager) { 91 this.toolkit = toolkit; 92 this.styleManager = styleManager; 93 } 94 95 public void setContentProviderSite(IIntroContentProviderSite site) { 96 this.site = site; 97 } 98 99 public void createIntroElement(Composite parent, 100 AbstractIntroElement element) { 101 boolean isFiltered = getFilterState(element); 103 if (isFiltered) 104 return; 105 106 Control c = null; 107 switch (element.getType()) { 108 case AbstractIntroElement.GROUP: 109 IntroGroup group = (IntroGroup) element; 110 c = createGroup(parent, group); 111 updateLayoutData(c, element); 112 Composite newParent = (Composite) c; 114 if (c instanceof Section) 115 newParent = (Composite) ((Section) newParent).getClient(); 117 AbstractIntroElement[] children = group.getChildren(); 118 for (int i = 0; i < children.length; i++) 119 createIntroElement(newParent, children[i]); 120 break; 121 case AbstractIntroElement.LINK: 122 IntroLink link = (IntroLink) element; 123 c = createImageHyperlink(parent, link); 124 updateLayoutData(c, element); 125 break; 126 case AbstractIntroElement.TEXT: 127 IntroText text = (IntroText) element; 128 c = createText(parent, text); 129 updateLayoutData(c, element); 130 break; 131 case AbstractIntroElement.IMAGE: 132 IntroImage image = (IntroImage) element; 133 c = createImage(parent, image); 134 if (c!=null) 135 updateLayoutData(c, element); 136 break; 137 case AbstractIntroElement.HTML: 138 IntroHTML html = (IntroHTML) element; 139 if (html.isInlined()) { 140 IntroText htmlText = html.getIntroText(); 141 if (htmlText != null) 142 c = createText(parent, htmlText); 143 else { 144 IntroImage htmlImage = html.getIntroImage(); 145 if (htmlImage != null) 146 c = createImage(parent, htmlImage); 147 } 148 } else { 149 String embddedLink = html.getSrc(); 151 if (embddedLink == null) 152 break; 153 String linkText = StringUtil 154 .concat( 155 "<p><a HREF=\"http://org.eclipse.ui.intro/openBrowser?url=", embddedLink, "\">", Messages.HTML_embeddedLink, "</a></p>").toString(); linkText = generateFormText(linkText); 159 c = createFormText(parent, linkText, null); 160 } 161 if (c != null) 162 updateLayoutData(c, element); 163 break; 164 case AbstractIntroElement.CONTENT_PROVIDER: 165 IntroContentProvider provider = (IntroContentProvider) element; 166 c = createContentProvider(parent, provider); 167 updateLayoutData(c, element); 168 break; 169 case AbstractIntroElement.HR: 170 IntroSeparator sep = (IntroSeparator)element; 171 c = createSeparator(parent, sep); 172 updateLayoutData(c, element); 173 break; 174 175 default: 176 break; 177 } 178 } 179 180 181 private void updateLayoutData(Control c, AbstractIntroElement element) { 182 TableWrapData currentTd = (TableWrapData) c.getLayoutData(); 183 if (currentTd == null) { 184 currentTd = new TableWrapData(TableWrapData.FILL, 185 TableWrapData.FILL); 186 currentTd.grabHorizontal = true; 187 c.setLayoutData(currentTd); 188 } 189 190 currentTd.colspan = styleManager 191 .getColSpan((AbstractBaseIntroElement) element); 192 currentTd.rowspan = styleManager 193 .getRowSpan((AbstractBaseIntroElement) element); 194 195 } 196 197 private Composite createGroup(Composite parent, IntroGroup group) { 198 String label = group.getLabel(); 199 String description = styleManager.getDescription(group); 200 boolean expandable = group.isExpandable(); 201 boolean expanded = group.isExpanded(); 202 Composite client = null; 203 Composite control = null; 204 if (description != null || label != null || expandable) { 205 int style = description != null ? Section.DESCRIPTION : SWT.NULL; 206 if (expandable) 207 style |= Section.TWISTIE | Section.FOCUS_TITLE | Section.CLIENT_INDENT; 208 if (expanded) 209 style |= Section.EXPANDED; 210 Section section = toolkit.createSection(parent, style); 211 if (label != null) 212 section.setText(label); 213 if (description != null) 214 section.setDescription(description); 215 colorControl(section, group); 216 client = toolkit.createComposite(section, SWT.WRAP); 217 section.setClient(client); 218 control = section; 219 } else { 220 client = toolkit.createComposite(parent, SWT.WRAP); 221 control = client; 222 } 223 224 TableWrapLayout layout = new TableWrapLayout(); 225 int numColumns = styleManager.getNumberOfColumns(group); 226 numColumns = numColumns < 1 ? 1 : numColumns; 227 layout.numColumns = numColumns; 228 layout.makeColumnsEqualWidth = styleManager.getEqualWidth(group); 229 layout.verticalSpacing = styleManager.getVerticalSpacing(group); 230 layout.horizontalSpacing = styleManager.getHorizantalSpacing(group); 231 client.setLayout(layout); 232 return control; 234 } 235 236 242 private Control createImageHyperlink(Composite parent, IntroLink link) { 243 Control control; 244 Hyperlink linkControl; 245 boolean showLinkDescription = styleManager.getShowLinkDescription(); 246 Image linkImage = styleManager.getImage(link, "link-icon", ImageUtil.DEFAULT_LINK); 248 249 if (showLinkDescription && link.getText() != null) { 250 Composite container = toolkit.createComposite(parent); 251 TableWrapLayout layout = new TableWrapLayout(); 252 layout.leftMargin = layout.rightMargin = 0; 253 layout.topMargin = layout.bottomMargin = 0; 254 layout.verticalSpacing = 0; 255 layout.numColumns = 2; 256 container.setLayout(layout); 257 258 ImageHyperlink ilabel = toolkit.createImageHyperlink(container, SWT.NULL); 260 ilabel.setImage(linkImage); 261 ilabel.setHoverImage(styleManager.getImage(link, "hover-icon", null)); 263 ilabel.setHref(link.getUrl()); 264 ilabel.addHyperlinkListener(hyperlinkAdapter); 265 TableWrapData td = new TableWrapData(); 266 td.valign = TableWrapData.TOP; 267 td.rowspan = 2; 268 ilabel.setLayoutData(td); 269 270 linkControl = toolkit.createHyperlink(container, null, SWT.WRAP); 271 td = new TableWrapData(TableWrapData.LEFT, TableWrapData.BOTTOM); 272 td.grabVertical = true; 273 linkControl.setLayoutData(td); 274 277 Control desc = createText(container, link.getIntroText()); 278 td = new TableWrapData(TableWrapData.FILL, TableWrapData.TOP); 279 td.grabHorizontal = true; 280 td.grabVertical = true; 281 desc.setLayoutData(td); 282 control = container; 283 } else { 284 ImageHyperlink imageLink = toolkit.createImageHyperlink(parent, 285 SWT.WRAP | SWT.CENTER); 286 imageLink.setImage(linkImage); 287 imageLink.setHoverImage(styleManager.getImage(link, "hover-icon", null)); 289 TableWrapData td = new TableWrapData(); 290 td.grabHorizontal = true; 291 imageLink.setLayoutData(td); 292 linkControl = imageLink; 293 control = linkControl; 294 } 295 linkControl.setText(link.getLabel()); 296 linkControl.setFont(PageStyleManager.getBannerFont()); 297 colorControl(linkControl, link); 298 linkControl.setHref(link.getUrl()); 299 linkControl.addHyperlinkListener(hyperlinkAdapter); 300 return control; 302 } 303 304 310 protected Control createText(Composite parent, IntroText text) { 311 Color fg = styleManager.getColor(toolkit, text); 312 boolean isBold = styleManager.isBold(text); 313 if (text.isFormatted()) 316 return createFormText(parent, generateFormText(text.getText()), fg); 317 318 if (isBold) 320 return createFormText(parent, generateBoldFormText(text.getText()), 321 fg); 322 return createText(parent, text.getText(), fg); 323 } 324 325 private Control createFormText(Composite parent, String text, Color fg) { 326 FormText formText = toolkit.createFormText(parent, false); 327 formText.addHyperlinkListener(hyperlinkAdapter); 328 try { 329 formText.setText(text, true, true); 330 } catch (SWTError e) { 331 Log.error(e.getMessage(), e); 332 return createText(parent, text, fg); 333 } 334 if (fg != null) 335 formText.setForeground(fg); 336 return formText; 337 } 338 339 340 private Control createText(Composite parent, String text, Color fg) { 341 Label label = toolkit.createLabel(parent, text, SWT.WRAP); 342 if (fg != null) 343 label.setForeground(fg); 344 return label; 345 } 346 347 348 349 protected Control createImage(Composite parent, IntroImage image) { 350 Label ilabel = null; 351 Image imageFile = styleManager.getImage(image); 352 if (imageFile != null) { 353 ilabel = toolkit.createLabel(parent, null, SWT.LEFT); 354 ilabel.setImage(imageFile); 355 if (image.getAlt() != null) 356 ilabel.setToolTipText(image.getAlt()); 357 } 358 if (ilabel!=null) { 361 TableWrapData td = new TableWrapData(); 362 ilabel.setLayoutData(td); 363 } 364 return ilabel; 365 } 366 367 public Control createContentProvider(Composite parent, 368 IntroContentProvider provider) { 369 Composite container = toolkit.createComposite(parent); 374 TableWrapLayout layout = new TableWrapLayout(); 375 layout.topMargin = 0; 376 layout.bottomMargin = 0; 377 layout.leftMargin = 0; 378 layout.rightMargin = 0; 379 container.setLayout(layout); 380 container.setData(provider); 381 382 383 IIntroContentProvider providerClass = ContentProviderManager.getInst() 384 .getContentProvider(provider); 385 if (providerClass == null) 386 providerClass = ContentProviderManager.getInst() 388 .createContentProvider(provider, site); 389 390 if (providerClass != null) { 391 try { 392 providerClass.createContent(provider.getId(), container, 393 toolkit); 394 } catch (Exception e) { 395 Log.error( 396 "Failed to create the content of Intro model content provider: " + provider.getClassName(), e); 398 providerClass = null; 400 } 401 } 402 403 if (providerClass == null) { 404 IntroText text = provider.getIntroText(); 406 if (text != null) 407 createText(container, text); 408 } 409 return container; 410 } 411 412 protected Control createSeparator(Composite parent, IntroSeparator sep) { 413 String key = sep.getParentPage().getId()+".separator.fg"; Color fg = styleManager.getColor(toolkit, key); 415 Composite l = new Composite(parent, SWT.NULL); 417 if (fg!=null) 418 l.setBackground(fg); 419 else 420 l.setBackground(toolkit.getColors().getColor(IFormColors.SEPARATOR)); 421 TableWrapData td = new TableWrapData(TableWrapData.FILL, 422 TableWrapData.FILL); 423 td.grabHorizontal = true; 424 td.maxHeight = 1; 425 l.setLayoutData(td); 426 return l; 427 } 428 429 private void colorControl(Control elementControl, 430 AbstractBaseIntroElement element) { 431 Color fg = styleManager.getColor(toolkit, element); 432 if (fg != null) 433 elementControl.setForeground(fg); 434 Color bg = styleManager.getBackgrond(toolkit, element); 435 if (bg != null) 436 elementControl.setBackground(bg); 437 } 438 439 440 446 private String generateFormText(String text) { 447 StringBuffer sbuf = new StringBuffer (); 448 sbuf.append("<form>"); if (text.startsWith("<p>")) sbuf.append(text); 451 else { 452 sbuf.append("<p>"); sbuf.append(text); 454 sbuf.append("</p>"); } 456 sbuf.append("</form>"); return sbuf.toString(); 458 } 459 460 466 private String generateBoldFormText(String text) { 467 StringBuffer sbuf = new StringBuffer (); 468 sbuf.append("<form>"); sbuf.append("<p>"); sbuf.append("<b>"); sbuf.append(text); 472 sbuf.append("</b>"); sbuf.append("</p>"); sbuf.append("</form>"); return sbuf.toString(); 476 } 477 478 485 private boolean getFilterState(AbstractIntroElement element) { 486 if (element.isOfType(AbstractIntroElement.BASE_ELEMENT)) 487 return ((AbstractBaseIntroElement) element).isFiltered(); 488 return false; 489 } 490 491 492 } 493 | Popular Tags |