1 11 package org.eclipse.ui.internal.intro.impl.swt; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.Color; 15 import org.eclipse.swt.graphics.Point; 16 import org.eclipse.swt.graphics.Rectangle; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.layout.GridLayout; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.swt.widgets.Layout; 23 import org.eclipse.ui.forms.IFormColors; 24 import org.eclipse.ui.forms.events.HyperlinkAdapter; 25 import org.eclipse.ui.forms.events.HyperlinkEvent; 26 import org.eclipse.ui.forms.widgets.Form; 27 import org.eclipse.ui.forms.widgets.FormToolkit; 28 import org.eclipse.ui.forms.widgets.ImageHyperlink; 29 import org.eclipse.ui.forms.widgets.ScrolledPageBook; 30 import org.eclipse.ui.internal.intro.impl.IIntroConstants; 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.IntroHomePage; 37 import org.eclipse.ui.internal.intro.impl.model.IntroLink; 38 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot; 39 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser; 40 import org.eclipse.ui.internal.intro.impl.util.DialogUtil; 41 import org.eclipse.ui.internal.intro.impl.util.ImageUtil; 42 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 43 import org.eclipse.ui.internal.intro.impl.util.Util; 44 import org.eclipse.ui.intro.config.IIntroContentProviderSite; 45 46 50 public class RootPageForm implements IIntroConstants { 51 52 private FormToolkit toolkit; 53 private IntroHomePage rootPage; 54 private Form parentForm; 55 protected Label descriptionLabel; 56 private PageStyleManager rootPageStyleManager; 57 private IIntroContentProviderSite site; 58 private PageWidgetFactory factory; 59 60 class PageComposite extends Composite { 61 62 public PageComposite(Composite parent, int style) { 63 super(parent, style); 64 } 65 66 public Point computeSize(int wHint, int hHint, boolean changed) { 69 return ((RootPageLayout) getLayout()).computeSize(this, wHint, 70 hHint, changed); 71 } 72 } 73 74 class RootPageLayout extends Layout { 75 76 private int VERTICAL_SPACING = 20; 78 79 private int LABEL_MARGIN_WIDTH = 5; 80 81 84 protected Point computeSize(Composite composite, int wHint, int hHint, 85 boolean flushCache) { 86 int innerWHint = wHint; 87 if (wHint != SWT.DEFAULT) 88 innerWHint -= LABEL_MARGIN_WIDTH + LABEL_MARGIN_WIDTH; 89 Control[] children = composite.getChildren(); 90 Point s1 = children[0].computeSize(SWT.DEFAULT, SWT.DEFAULT); 91 Point s2 = children[1].computeSize(innerWHint, SWT.DEFAULT); 92 s2.x += LABEL_MARGIN_WIDTH; 93 int height = 2 * (s2.y + VERTICAL_SPACING + s1.y / 2); 94 Point size = new Point(Math.max(s1.x, s2.x), height + 5); 95 return size; 96 } 97 98 104 protected void layout(Composite composite, boolean flushCache) { 105 Control[] children = composite.getChildren(); 106 Rectangle carea = composite.getClientArea(); 107 Control content = children[0]; 108 Control label = children[1]; 109 Point contentSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT); 110 Point labelSize = label.computeSize(carea.width - 2 111 - LABEL_MARGIN_WIDTH * 2, SWT.DEFAULT); 112 content.setBounds(carea.width / 2 - contentSize.x / 2, carea.height 113 / 2 - contentSize.y / 2, contentSize.x, contentSize.y); 114 label.setBounds(LABEL_MARGIN_WIDTH, content.getLocation().y 115 + contentSize.y + VERTICAL_SPACING, carea.width 116 - LABEL_MARGIN_WIDTH * 2, labelSize.y); 117 } 118 } 119 120 private HyperlinkAdapter hyperlinkAdapter = new HyperlinkAdapter() { 121 122 public void linkActivated(HyperlinkEvent e) { 123 ImageHyperlink imageLink = (ImageHyperlink) e.getSource(); 124 IntroLink introLink = (IntroLink) imageLink.getData(INTRO_LINK); 125 IntroURLParser parser = new IntroURLParser(introLink.getUrl()); 126 if (parser.hasIntroUrl()) { 127 parser.getIntroURL().execute(); 129 return; 130 } else if (parser.hasProtocol()) { 131 Util.openBrowser(introLink.getUrl()); 132 return; 133 } 134 DialogUtil.displayInfoMessage(imageLink.getShell(), 135 Messages.HyperlinkAdapter_urlIs + introLink.getUrl()); 136 } 137 138 public void linkEntered(HyperlinkEvent e) { 139 ImageHyperlink imageLink = (ImageHyperlink) e.getSource(); 140 IntroLink introLink = (IntroLink) imageLink.getData(INTRO_LINK); 141 updateDescription(introLink.getText()); 142 } 143 144 public void linkExited(HyperlinkEvent e) { 145 updateDescription(""); } 148 149 private void updateDescription(String text) { 150 if (text == null) 151 text = ""; descriptionLabel.setText(text); 153 descriptionLabel.getParent().layout(); 154 } 155 }; 156 157 160 public RootPageForm(FormToolkit toolkit, IntroModelRoot modelRoot, 161 Form parentForm) { 162 this.toolkit = toolkit; 163 this.rootPage = modelRoot.getHomePage(); 164 this.parentForm = parentForm; 165 } 166 167 173 public void createPartControl(ScrolledPageBook mainPageBook, 174 SharedStyleManager shardStyleManager) { 175 rootPageStyleManager = new PageStyleManager(rootPage, shardStyleManager 177 .getProperties()); 178 179 parentForm.setText(rootPage.getTitle()); 181 182 Composite rootPageComposite = new PageComposite(mainPageBook 185 .getContainer(), SWT.NULL); 186 toolkit.adapt(rootPageComposite); 187 188 mainPageBook.registerPage(rootPage.getId(), rootPageComposite); 189 rootPageComposite.setLayout(new RootPageLayout()); 190 192 createRootPageContent(rootPageComposite); 194 195 descriptionLabel = createHoverLabel(rootPageComposite); 197 198 rootPageStyleManager = null; 200 } 201 202 205 private void createRootPageContent(Composite rootPageComposite) { 206 Composite contentComposite = toolkit.createComposite(rootPageComposite); 208 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); 209 contentComposite.setLayoutData(gd); 210 AbstractIntroElement[] children = (AbstractIntroElement[]) rootPage 211 .getChildrenOfType(AbstractIntroElement.GROUP 212 | AbstractIntroElement.LINK | AbstractIntroElement.CONTENT_PROVIDER); 213 int numChildren = children.length; 214 GridLayout layout = new GridLayout(); 215 layout.horizontalSpacing = rootPageStyleManager 217 .getPageHorizantalSpacing(); 218 layout.verticalSpacing = rootPageStyleManager.getPageVerticalSpacing(); 219 int numColumns = rootPageStyleManager.getPageNumberOfColumns(); 221 numColumns = numColumns == 0 ? numChildren : numColumns; 222 layout.numColumns = numColumns; 223 layout.horizontalSpacing = rootPageStyleManager 224 .getPageHorizantalSpacing(); 225 layout.verticalSpacing = rootPageStyleManager.getPageVerticalSpacing(); 226 contentComposite.setLayout(layout); 227 for (int i = 0; i < children.length; i++) { 228 if (((AbstractBaseIntroElement) children[i]).isFiltered()) 229 continue; 230 if (children[i].getType() == AbstractIntroElement.GROUP) 231 createGroupContent(contentComposite, (IntroGroup) children[i]); 232 else if (children[i].getType() == AbstractIntroElement.LINK) 233 createImageHyperlink(contentComposite, (IntroLink) children[i]); 234 else if (children[i].getType() == AbstractIntroElement.CONTENT_PROVIDER) 235 createContentProvider(contentComposite, (IntroContentProvider)children[i]); 236 } 237 } 238 239 242 private void createGroupContent(Composite parent, IntroGroup group) { 243 AbstractIntroElement[] children = (AbstractIntroElement[]) group 244 .getChildrenOfType(AbstractIntroElement.GROUP 245 | AbstractIntroElement.LINK | 246 AbstractIntroElement.CONTENT_PROVIDER); 247 int numChildren = children.length; 248 Composite contentComposite = toolkit.createComposite(parent); 250 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); 251 gd.horizontalSpan = rootPageStyleManager.getColSpan(group); 252 gd.verticalSpan = rootPageStyleManager.getRowSpan(group); 253 contentComposite.setLayoutData(gd); 254 GridLayout layout = new GridLayout(); 255 layout.horizontalSpacing = 20; 257 int numColumns = rootPageStyleManager.getNumberOfColumns(group); 259 numColumns = numColumns < 1 ? numChildren : numColumns; 260 layout.numColumns = numColumns; 261 layout.verticalSpacing = rootPageStyleManager.getVerticalSpacing(group); 262 layout.horizontalSpacing = rootPageStyleManager 263 .getHorizantalSpacing(group); 264 contentComposite.setLayout(layout); 265 for (int i = 0; i < children.length; i++) { 266 if (((AbstractBaseIntroElement) children[i]).isFiltered()) 267 continue; 268 if (children[i].getType() == AbstractIntroElement.GROUP) 269 createGroupContent(contentComposite, (IntroGroup) children[i]); 270 else if (children[i].getType() == AbstractIntroElement.LINK) 271 createImageHyperlink(contentComposite, (IntroLink) children[i]); 272 else if (children[i].getType() == AbstractIntroElement.CONTENT_PROVIDER) 273 createContentProvider(contentComposite, (IntroContentProvider)children[i]); 274 } 275 } 276 277 284 private void createImageHyperlink(Composite parent, IntroLink link) { 285 Composite container = toolkit.createComposite(parent); 288 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); 290 gd.horizontalSpan = rootPageStyleManager.getColSpan(link); 291 gd.verticalSpan = rootPageStyleManager.getRowSpan(link); 292 container.setLayoutData(gd); 293 294 GridLayout layout = new GridLayout(); 295 layout.marginWidth = 0; 296 layout.marginHeight = 0; 297 container.setLayout(layout); 298 ImageHyperlink imageLink = toolkit.createImageHyperlink(container, 299 SWT.NULL); 300 imageLink.setImage(rootPageStyleManager.getImage(link, "link-icon", ImageUtil.DEFAULT_ROOT_LINK)); 302 imageLink.setHoverImage(rootPageStyleManager.getImage(link, 303 "hover-icon", null)); gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); 306 imageLink.setLayoutData(gd); 307 imageLink.setData(INTRO_LINK, link); 309 imageLink.addHyperlinkListener(hyperlinkAdapter); 310 311 Label linkLabel = toolkit.createLabel(container, link.getLabel()); 313 gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); 314 linkLabel.setFont(PageStyleManager.getBannerFont()); 315 linkLabel.setLayoutData(gd); 316 } 317 318 324 private Label createHoverLabel(Composite body) { 325 Label label = toolkit.createLabel(body, "", SWT.WRAP); String key = StringUtil.concat(rootPage.getId(), ".", "hover-text.fg") .toString(); 328 Color fg = rootPageStyleManager.getColor(toolkit, key); 329 if (fg == null) 330 fg = toolkit.getColors().getColor(IFormColors.TITLE); 331 label.setForeground(fg); 332 label.setAlignment(SWT.CENTER); 333 label.setFont(PageStyleManager.getBannerFont()); 334 return label; 335 } 336 337 private void createContentProvider(Composite parent, IntroContentProvider providerElement) { 338 if (factory==null) { 339 factory = new PageWidgetFactory(toolkit, rootPageStyleManager); 340 factory.setContentProviderSite(site); 341 } 342 factory.createContentProvider(parent, providerElement); 343 } 344 public void setContentProviderSite(IIntroContentProviderSite site) { 345 this.site = site; 346 } 347 } 348 | Popular Tags |