KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > presentations > FormIntroPartImplementation


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.presentations;
12
13 import org.eclipse.core.runtime.IRegistryChangeEvent;
14 import org.eclipse.jface.action.IToolBarManager;
15 import org.eclipse.jface.action.Separator;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.IPropertyListener;
24 import org.eclipse.ui.actions.ActionFactory;
25 import org.eclipse.ui.forms.HyperlinkSettings;
26 import org.eclipse.ui.forms.events.HyperlinkAdapter;
27 import org.eclipse.ui.forms.events.HyperlinkEvent;
28 import org.eclipse.ui.forms.widgets.Form;
29 import org.eclipse.ui.forms.widgets.FormToolkit;
30 import org.eclipse.ui.forms.widgets.Hyperlink;
31 import org.eclipse.ui.forms.widgets.ScrolledPageBook;
32 import org.eclipse.ui.internal.intro.impl.IntroPlugin;
33 import org.eclipse.ui.internal.intro.impl.Messages;
34 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage;
35 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPartImplementation;
36 import org.eclipse.ui.internal.intro.impl.model.History;
37 import org.eclipse.ui.internal.intro.impl.model.IntroHomePage;
38 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
39 import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager;
40 import org.eclipse.ui.internal.intro.impl.swt.PageForm;
41 import org.eclipse.ui.internal.intro.impl.swt.PageFormWithNavigation;
42 import org.eclipse.ui.internal.intro.impl.swt.PageStyleManager;
43 import org.eclipse.ui.internal.intro.impl.swt.RootPageForm;
44 import org.eclipse.ui.internal.intro.impl.swt.SharedStyleManager;
45 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
46 import org.eclipse.ui.internal.intro.impl.util.Util;
47 import org.eclipse.ui.intro.config.CustomizableIntroPart;
48 import org.eclipse.ui.intro.config.IIntroContentProvider;
49 import org.eclipse.ui.intro.config.IIntroContentProviderSite;
50 import org.eclipse.ui.intro.config.IntroConfigurer;
51
52 /**
53  * This is a UI Forms based implementation of an Intro Part Presentation.
54  */

55 public class FormIntroPartImplementation extends
56         AbstractIntroPartImplementation implements IIntroContentProviderSite,
57         IPropertyListener {
58
59     private FormToolkit toolkit;
60     private ScrolledPageBook mainPageBook;
61     private PageForm pageForm;
62     private PageFormWithNavigation pageFormWithNav;
63     // cache model instance for reuse.
64
private IntroModelRoot model = getModel();
65     private SharedStyleManager sharedStyleManager;
66
67     // static SWT Intro. This is the link shown on the center of a page in a
68
// static SWT intro.
69
private Hyperlink welcomeLink;
70
71     static {
72         // REVISIT: register all common images here. Even if this part
73
// implementation is created again, the images will remain in plugin
74
// registry.
75
ImageUtil.registerImage(ImageUtil.DEFAULT_ROOT_LINK, "overview_48.gif"); //$NON-NLS-1$
76
ImageUtil.registerImage(ImageUtil.DEFAULT_SMALL_ROOT_LINK,
77             "overview_32.gif"); //$NON-NLS-1$
78
ImageUtil.registerImage(ImageUtil.DEFAULT_FORM_BG, "form_banner.gif"); //$NON-NLS-1$
79
ImageUtil.registerImage(ImageUtil.DEFAULT_LINK, "welcome_item.gif"); //$NON-NLS-1$
80
}
81
82
83     protected void updateNavigationActionsState() {
84         if (getModel().isDynamic()) {
85             forwardAction.setEnabled(history.canNavigateForward());
86             backAction.setEnabled(history.canNavigateBackward());
87             return;
88         }
89         // no actions are added in static swt.
90
}
91
92
93     public FormIntroPartImplementation() {
94         // Shared style manager
95
sharedStyleManager = new SharedStyleManager(getModel());
96     }
97
98     public void createPartControl(Composite container) {
99         if (getModel().isDynamic())
100             dynamicCreatePartControl(container);
101         else {
102             staticCreatePartControl(container);
103         }
104     }
105
106
107
108     /*
109      * create dynamic UI forms Intro, ie: swt intro.
110      */

111     private void dynamicCreatePartControl(Composite container) {
112         // Create single toolkit instance, which is disposed of on dispose of
113
// intro part. also define background of all presentation.
114
toolkit = new FormToolkit(container.getDisplay());
115         // Define presentation title color
116
Color bg = sharedStyleManager.getColor(toolkit, "bg"); //$NON-NLS-1$
117
if (bg != null) {
118             toolkit.setBackground(bg);
119         }
120         toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(
121             HyperlinkSettings.UNDERLINE_HOVER);
122
123         // Define presentation title color and image.
124
Form mainForm = toolkit.createForm(container);
125         Color fg = sharedStyleManager.getColor(toolkit, "title.fg"); //$NON-NLS-1$
126
if (fg != null)
127             mainForm.setForeground(fg);
128         Image bgImage = sharedStyleManager.getImage("title.image", null, null); //$NON-NLS-1$
129
if (bgImage != null) {
130             mainForm.setBackgroundImage(bgImage);
131             String JavaDoc repeat = sharedStyleManager
132                 .getProperty("title.image.repeat"); //$NON-NLS-1$
133
if (repeat != null && repeat.equalsIgnoreCase("true")) //$NON-NLS-1$
134

135                 mainForm.setBackgroundImageTiled(true);
136         }
137
138         mainPageBook = createMainPageBook(toolkit, mainForm);
139         // Add this presentation as a listener to model.
140
getModel().addPropertyListener(this);
141
142         addToolBarActions();
143     }
144
145
146     /**
147      * The main page book that holds Intro pages. It has two pages, one that
148      * holds the home page, and one that holds all other pages. If the
149      * presentation is configured to not show the home page with the Home Page
150      * layout, then this page book will only have one page.
151      *
152      * @param toolkit
153      * @param form
154      * @return
155      */

156     private ScrolledPageBook createMainPageBook(FormToolkit toolkit, Form form) {
157         // get body and create page book in it. Body has GridLayout.
158
Composite body = form.getBody();
159         body.setLayout(new GridLayout());
160         // make sure page book expands h and v.
161
ScrolledPageBook pageBook = toolkit.createPageBook(body, SWT.V_SCROLL
162                 | SWT.H_SCROLL);
163         pageBook.setLayoutData(new GridData(GridData.FILL_BOTH));
164
165         // Create root page in root page layout form, only if needed.
166
if (sharedStyleManager.useCustomHomePagelayout()) {
167             // if we do not have a root page form, create one
168
RootPageForm rootPageForm = new RootPageForm(toolkit, model, form);
169             rootPageForm.createPartControl(pageBook, sharedStyleManager);
170             rootPageForm.setContentProviderSite(this);
171         }
172
173         // Create the two Page forms .
174
pageForm = new PageForm(toolkit, model, form);
175         pageForm.setContentProviderSite(this);
176         pageForm.createPartControl(pageBook, sharedStyleManager);
177
178         pageFormWithNav = new PageFormWithNavigation(toolkit, model, form);
179         pageFormWithNav.setContentProviderSite(this);
180         pageFormWithNav.createPartControl(pageBook, sharedStyleManager);
181
182         // now determine which page to show. Show it and add it to history.
183
// if the cached page is a URL ignore it. We do not want to launch a
184
// browser on startup.
185
String JavaDoc cachedPage = getCachedCurrentPage();
186         if (cachedPage != null & !History.isURL(cachedPage))
187             // this will create the page in the page form.
188
model.setCurrentPageId(cachedPage);
189
190         AbstractIntroPage pageToShow = getModel().getCurrentPage();
191         // load style manager here to test for navigation.
192
PageStyleManager styleManager = new PageStyleManager(pageToShow,
193             sharedStyleManager.getProperties());
194         boolean pageHasNavigation = styleManager.showHomePageNavigation();
195         if (pageToShow != null) {
196             if (pageBook.hasPage(pageToShow.getId()))
197                 // we are showing Home Page.
198
pageBook.showPage(pageToShow.getId());
199             else {
200                 if (pageHasNavigation) {
201                     // page or Home Page with a page layout and navigation, set
202
// the page id to the static PageFormWithNavigation id.
203
// first create the correct content.
204
pageFormWithNav.showPage(pageToShow, sharedStyleManager);
205                     // then show the page
206
pageBook
207                         .showPage(PageFormWithNavigation.PAGE_FORM_WITH_NAVIGATION_ID);
208                 } else {
209                     // page or Home Page with a regular page layout, set the
210
// page id to the static PageForm id. first create the
211
// correct content.
212
pageForm.showPage(pageToShow, sharedStyleManager);
213                     // then show the page
214
pageBook.showPage(PageForm.PAGE_FORM_ID);
215                 }
216             }
217             updateHistory(pageToShow);
218         }
219
220         return pageBook;
221     }
222
223     public void dispose() {
224         if (toolkit != null)
225             toolkit.dispose();
226     }
227
228     /**
229      * Handle model property changes. The UI is notified here of a change to the
230      * current page in the model. This happens if an intro URL showPage method
231      * is executed.
232      *
233      * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object,
234      * int)
235      */

236     public void propertyChanged(Object JavaDoc source, int propId) {
237         if (propId == IntroModelRoot.CURRENT_PAGE_PROPERTY_ID) {
238             String JavaDoc pageId = getModel().getCurrentPageId();
239             if (pageId == null || pageId.equals("")) //$NON-NLS-1$
240
// If page ID was not set properly. exit.
241
return;
242
243             showPage(getModel().getCurrentPage());
244         }
245     }
246
247     protected void addToolBarActions() {
248         // Handle menus:
249
IActionBars actionBars = getIntroPart().getIntroSite().getActionBars();
250         IToolBarManager toolBarManager = actionBars.getToolBarManager();
251         actionBars.setGlobalActionHandler(ActionFactory.FORWARD.getId(),
252             forwardAction);
253         actionBars.setGlobalActionHandler(ActionFactory.BACK.getId(),
254             backAction);
255         toolBarManager.add(new Separator(IntroConfigurer.TB_ADDITIONS));
256         toolBarManager.add(homeAction);
257         toolBarManager.add(backAction);
258         toolBarManager.add(forwardAction);
259         toolBarManager.update(true);
260         actionBars.updateActionBars();
261         updateNavigationActionsState();
262     }
263
264
265
266     protected void doStandbyStateChanged(boolean standby,
267             boolean isStandbyPartNeeded) {
268         if (getModel().isDynamic())
269             dynamicStandbyStateChanged(standby, isStandbyPartNeeded);
270         else
271             staticStandbyStateChanged(standby);
272     }
273
274
275     public void dynamicStandbyStateChanged(boolean standby,
276             boolean isStandbyPartNeeded) {
277         // handle action enablement first
278
if (isStandbyPartNeeded | standby) {
279             homeAction.setEnabled(false);
280             forwardAction.setEnabled(false);
281             backAction.setEnabled(false);
282         } else {
283             homeAction.setEnabled(true);
284             updateNavigationActionsState();
285         }
286
287         if (isStandbyPartNeeded)
288             // we have a standby part, nothing more to do in presentation.
289
return;
290
291         // try to show a cached page.
292
AbstractIntroPage pageToShow = null;
293         if (standby) {
294             // we are in standby. Show standby page, in PageForm.
295
pageToShow = getModel().getStandbyPage();
296             if (pageToShow == null)
297                 pageToShow = getModel().getHomePage();
298         } else
299             // if we are showing a regular intro page, or if the Home Page
300
// has a regular page layout, set the page id to the static PageForm
301
// id.
302
pageToShow = getModel().getCurrentPage();
303
304         showPage(pageToShow);
305     }
306
307     private boolean showPage(AbstractIntroPage pageToShow) {
308         boolean pageisCached = showCachedPage(pageToShow);
309
310         if (!pageisCached) {
311             // page has not been shown before.
312
// load style manager here to test for navigation.
313
PageStyleManager styleManager = new PageStyleManager(pageToShow,
314                 sharedStyleManager.getProperties());
315             boolean pageHasNavigation = styleManager.showHomePageNavigation();
316             if (pageHasNavigation) {
317                 // page or Home Page with a regular page layout, set the
318
// page id to the static PageFormWithNavigation id. first
319
// create the correct content.
320
pageFormWithNav.showPage(pageToShow, sharedStyleManager);
321                 // then show the page
322
mainPageBook
323                     .showPage(PageFormWithNavigation.PAGE_FORM_WITH_NAVIGATION_ID);
324             } else {
325                 // page or Home Page with a regular page layout, set the
326
// page id to the static PageFormWithNavigation id. first
327
// create the correct content.
328
pageForm.showPage(pageToShow, sharedStyleManager);
329                 // then show the page
330
mainPageBook.showPage(PageForm.PAGE_FORM_ID);
331             }
332         }
333
334         return true;
335     }
336
337     private boolean showCachedPage(AbstractIntroPage page) {
338         String JavaDoc formPageId = null;
339         if (pageForm.hasPage(page.getId())) {
340             pageForm.showPage(page, sharedStyleManager);
341             formPageId = PageForm.PAGE_FORM_ID;
342         } else if (pageFormWithNav.hasPage(page.getId())) {
343             pageFormWithNav.showPage(page, sharedStyleManager);
344             formPageId = PageFormWithNavigation.PAGE_FORM_WITH_NAVIGATION_ID;
345         } else if (mainPageBook.hasPage(page.getId()))
346             formPageId = page.getId();
347         else
348             return false;
349
350         mainPageBook.showPage(formPageId);
351         return true;
352     }
353
354     private void removeCachedPage(AbstractIntroPage page) {
355         if (pageForm.hasPage(page.getId()))
356             pageForm.removePage(page.getId());
357         else if (pageFormWithNav.hasPage(page.getId()))
358             pageFormWithNav.removePage(page.getId());
359         else if (mainPageBook.hasPage(page.getId()))
360             mainPageBook.removePage(page.getId());
361         else
362             return;
363     }
364
365
366     /**
367      * Clear page cache for the page that contains this provider. Remove the
368      * form from the correct pagebook that refers to the page we need to
369      * refresh. This will force a call to createContents on all content
370      * providers the next time this page needs to be displayed.
371      *
372      * @see org.eclipse.ui.intro.config.IIntroContentProviderSite#reflow(org.eclipse.ui.intro.config.IIntroContentProvider,
373      * boolean)
374      */

375     public void reflow(IIntroContentProvider provider, boolean incremental) {
376         AbstractIntroPage page = ContentProviderManager.getInst()
377             .getContentProviderParentPage(provider);
378         if (incremental) {
379             if (pageForm.hasPage(page.getId()))
380                 pageForm.reflow();
381             else if (pageFormWithNav.hasPage(page.getId()))
382                 pageFormWithNav.reflow();
383             else if (mainPageBook.hasPage(page.getId()))
384                 mainPageBook.reflow(true);
385         }
386         else {
387             removeCachedPage(page);
388             showPage(model.getCurrentPage());
389         }
390     }
391     
392     public void setFocus() {
393         if (model.isDynamic()) {
394             if (mainPageBook.getCurrentPage() != null)
395                 mainPageBook.getCurrentPage().setFocus();
396         }
397     }
398
399
400     /*
401      * (non-Javadoc)
402      *
403      * @see org.eclipse.ui.internal.intro.impl.model.AbstractIntroPartImplementation#navigateBackward()
404      */

405     public boolean navigateBackward() {
406         boolean success = false;
407         if (getModel().isDynamic()) {
408             // dynamic case. Uses navigation history.
409
if (history.canNavigateBackward()) {
410                 history.navigateHistoryBackward();
411                 if (history.currentLocationIsUrl())
412                     success = Util.openBrowser(history
413                         .getCurrentLocationAsUrl());
414                 else {
415                     // Set current page, and this will triger regen.
416
CustomizableIntroPart currentIntroPart = (CustomizableIntroPart) IntroPlugin
417                         .getIntro();
418                     currentIntroPart.getControl().setRedraw(false);
419                     success = getModel().setCurrentPageId(
420                         history.getCurrentLocationAsPage().getId());
421                     currentIntroPart.getControl().setRedraw(true);
422                 }
423             }
424         }
425
426         updateNavigationActionsState();
427         return success;
428     }
429
430
431     /*
432      * (non-Javadoc)
433      *
434      * @see org.eclipse.ui.internal.intro.impl.model.AbstractIntroPartImplementation#navigateForward()
435      */

436     public boolean navigateForward() {
437         boolean success = false;
438
439         if (getModel().isDynamic()) {
440             // dynamic case. Uses navigation history.
441
if (history.canNavigateForward()) {
442                 history.navigateHistoryForward();
443                 if (history.currentLocationIsUrl())
444                     success = Util.openBrowser(history
445                         .getCurrentLocationAsUrl());
446                 else {
447                     // Set current page, and this will triger regen.
448
CustomizableIntroPart currentIntroPart = (CustomizableIntroPart) IntroPlugin
449                         .getIntro();
450                     currentIntroPart.getControl().setRedraw(false);
451                     success = getModel().setCurrentPageId(
452                         history.getCurrentLocationAsPage().getId());
453                     currentIntroPart.getControl().setRedraw(true);
454                 }
455             }
456         }
457         updateNavigationActionsState();
458         return success;
459     }
460
461     public boolean navigateHome() {
462         IntroHomePage rootPage = getModel().getHomePage();
463         if (getModel().isDynamic()) {
464             CustomizableIntroPart currentIntroPart = (CustomizableIntroPart) IntroPlugin
465                 .getIntro();
466             currentIntroPart.getControl().setRedraw(false);
467             boolean success = false;
468             success = getModel().setCurrentPageId(rootPage.getId());
469             updateHistory(rootPage);
470             currentIntroPart.getControl().setRedraw(true);
471             return success;
472         }
473         // static model. Nothing to do.
474
return false;
475     }
476
477
478     /*
479      * (non-Javadoc)
480      *
481      * @see org.eclipse.ui.internal.intro.impl.model.AbstractIntroPartImplementation#handleRegistryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
482      */

483     protected void handleRegistryChanged(IRegistryChangeEvent event) {
484         if (getModel().isDynamic()) {
485             IntroPlugin.closeIntro();
486             IntroPlugin.showIntro(false);
487         }
488     }
489
490
491
492     // *********** Static case ******************
493
/*
494      * create static UI forms Intro. For this, we only launch the url of the
495      * root page.
496      */

497     private void staticCreatePartControl(Composite parent) {
498         toolkit = new FormToolkit(parent.getDisplay());
499         toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(
500             HyperlinkSettings.UNDERLINE_HOVER);
501
502         // create a page that has only one link. The URL and tooltip will be set
503
// by the standby listener.
504
welcomeLink = createStaticPage(parent);
505     }
506
507
508     private Hyperlink createStaticPage(Composite parent) {
509         Form mainForm = toolkit.createForm(parent);
510         Composite body = mainForm.getBody();
511
512         GridLayout gl = new GridLayout();
513         body.setLayout(gl);
514         String JavaDoc label = Messages.StaticHTML_welcome;
515         Hyperlink link = toolkit.createHyperlink(body, label, SWT.WRAP);
516         link.setFont(PageStyleManager.getHeaderFont());
517         GridData gd = new GridData(GridData.GRAB_HORIZONTAL
518                 | GridData.GRAB_VERTICAL);
519         gd.horizontalAlignment = GridData.CENTER;
520         gd.verticalAlignment = GridData.CENTER;
521         link.setLayoutData(gd);
522         link.addHyperlinkListener(new HyperlinkAdapter() {
523
524             public void linkActivated(HyperlinkEvent e) {
525                 Hyperlink link = (Hyperlink) e.getSource();
526                 Util.openBrowser((String JavaDoc) link.getHref());
527                 return;
528             }
529         });
530
531         return link;
532     }
533
534     public void staticStandbyStateChanged(boolean standby) {
535         IntroHomePage homePage = getModel().getHomePage();
536         IntroHomePage standbyPage = getModel().getStandbyPage();
537         if (standbyPage == null)
538             standbyPage = homePage;
539
540         if (standby) {
541             welcomeLink.setHref(standbyPage.getUrl());
542             welcomeLink.setToolTipText(standbyPage.getUrl());
543         } else {
544             welcomeLink.setHref(homePage.getUrl());
545             welcomeLink.setToolTipText(homePage.getUrl());
546         }
547     }
548
549
550 }
Popular Tags