001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.impl.swt;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.layout.GridData;
014: import org.eclipse.swt.layout.GridLayout;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.swt.widgets.Control;
017: import org.eclipse.ui.forms.events.HyperlinkAdapter;
018: import org.eclipse.ui.forms.events.HyperlinkEvent;
019: import org.eclipse.ui.forms.widgets.Form;
020: import org.eclipse.ui.forms.widgets.FormToolkit;
021: import org.eclipse.ui.forms.widgets.ScrolledPageBook;
022: import org.eclipse.ui.internal.intro.impl.IIntroConstants;
023: import org.eclipse.ui.internal.intro.impl.Messages;
024: import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage;
025: import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
026: import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser;
027: import org.eclipse.ui.internal.intro.impl.util.DialogUtil;
028: import org.eclipse.ui.internal.intro.impl.util.Util;
029: import org.eclipse.ui.intro.config.IIntroContentProviderSite;
030:
031: /**
032: * A Form that represents an Intro Page. It is swapped in the main page book in
033: * the FormIntroPartImplementation class. It has a page book for swapping in
034: * categories (content) of Intro Pages.
035: */
036: public class PageForm implements IIntroConstants {
037:
038: protected FormToolkit toolkit;
039: private ScrolledPageBook categoryPageBook;
040: protected IntroModelRoot model;
041: private Form parentForm;
042: protected Form pageForm;
043: // private SharedStyleManager sharedStyleManager;
044:
045: // Id to this page. There is only a single instance of this page in the
046: // main page book.
047: public static String PAGE_FORM_ID = "pageFormId"; //$NON-NLS-1$
048:
049: // site is cached to hand down to the PageWidgetFactory for creating the UI
050: // for content providers..
051: private IIntroContentProviderSite site;
052:
053: protected HyperlinkAdapter hyperlinkAdapter = new HyperlinkAdapter() {
054:
055: public void linkActivated(HyperlinkEvent e) {
056: String url = (String) e.getHref();
057: IntroURLParser parser = new IntroURLParser(url);
058: if (parser.hasIntroUrl()) {
059: // execute the action embedded in the IntroURL
060: parser.getIntroURL().execute();
061: return;
062: } else if (parser.hasProtocol()) {
063: Util.openBrowser(url);
064: return;
065: }
066: DialogUtil.displayInfoMessage(((Control) e.getSource())
067: .getShell(), Messages.HyperlinkAdapter_urlIs
068: + " " + url); //$NON-NLS-1$
069: }
070:
071: public void linkEntered(HyperlinkEvent e) {
072: }
073:
074: public void linkExited(HyperlinkEvent e) {
075: }
076: };
077:
078: /**
079: *
080: */
081: public PageForm(FormToolkit toolkit, IntroModelRoot modelRoot,
082: Form parentForm) {
083: this .toolkit = toolkit;
084: this .model = modelRoot;
085: this .parentForm = parentForm;
086: }
087:
088: /**
089: * Create a Form for holding pages without navigation.
090: *
091: * @param pageBook
092: */
093: public void createPartControl(ScrolledPageBook mainPageBook,
094: SharedStyleManager sharedStyleManager) {
095:
096: // Cash the shared style manager. We need to pass it around to category
097: // forms. So, do not null it!
098: // this.sharedStyleManager = sharedStyleManager;
099:
100: // creating page in Main page book.
101: pageForm = toolkit.createForm(mainPageBook.getContainer());
102: mainPageBook.registerPage(getId(), pageForm);
103: GridLayout layout = new GridLayout();
104: layout.marginWidth = 0;
105: layout.marginHeight = 0;
106: pageForm.getBody().setLayout(layout);
107: // Util.highlight(pageForm.getBody(), SWT.COLOR_RED);
108:
109: // Get form body. Form body is one column grid layout. Add page book
110: // and navigation UI to it.
111: categoryPageBook = toolkit.createPageBook(pageForm.getBody(),
112: SWT.H_SCROLL | SWT.V_SCROLL);
113: categoryPageBook
114: .setLayoutData(new GridData(GridData.FILL_BOTH));
115:
116: // pageForm.setText(rootPageStyleManager.getPageSubTitle());
117: }
118:
119: protected String getId() {
120: return PAGE_FORM_ID;
121: }
122:
123: /**
124: * This method is called when the current page changes. It creates the
125: * PageContentForm if necessary, and handles showing the page in the Page
126: * Book. It creates a model PageContentForm for the current page.
127: *
128: * @param pageID
129: */
130: public void showPage(AbstractIntroPage page,
131: SharedStyleManager sharedStyleManager) {
132:
133: if (!categoryPageBook.hasPage(page.getId())) {
134: // if we do not have a category form for this page create one.
135: PageContentForm categoryForm = new PageContentForm(toolkit,
136: model, page);
137: categoryForm.setContentProviderSite(site);
138: // load style manager only once, here.
139: PageStyleManager styleManager = new PageStyleManager(page,
140: sharedStyleManager.getProperties());
141: categoryForm.createPartControl(categoryPageBook,
142: styleManager);
143: }
144: categoryPageBook.showPage(page.getId());
145:
146: // Get cached page subtitle from control data.
147: Composite pageComposite = (Composite) categoryPageBook
148: .getCurrentPage();
149: // update main Form title.
150: parentForm.setText(model.getCurrentPage().getTitle());
151: // update this page form's title, ie: Page subtitle, if it exists.
152: pageForm.setText((String) pageComposite.getData(PAGE_SUBTITLE));
153:
154: // TODO need to transfer focus to the first link in
155: // the page somehow; we may need IIntroPage interface with
156: // a few methods like 'setFocus()' etc.
157: // DG
158: }
159:
160: public void reflow() {
161: categoryPageBook.reflow(true);
162: }
163:
164: public boolean hasPage(String pageId) {
165: return categoryPageBook.hasPage(pageId);
166: }
167:
168: public void removePage(String pageId) {
169: categoryPageBook.removePage(pageId);
170: }
171:
172: public void setContentProviderSite(IIntroContentProviderSite site) {
173: this.site = site;
174: }
175:
176: }
|