KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > UniversalIntroConfigurer


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

9
10 package org.eclipse.ui.internal.intro.universal;
11
12 import java.io.IOException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.eclipse.core.runtime.FileLocator;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProduct;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Preferences;
28 import org.eclipse.help.internal.util.ProductPreferences;
29 import org.eclipse.help.internal.util.SequenceResolver;
30 import org.eclipse.jface.action.Action;
31 import org.eclipse.ui.internal.intro.impl.model.ExtensionMap;
32 import org.eclipse.ui.internal.intro.universal.contentdetect.ContentDetector;
33 import org.eclipse.ui.internal.intro.universal.util.ImageUtil;
34 import org.eclipse.ui.internal.intro.universal.util.PreferenceArbiter;
35 import org.eclipse.ui.intro.IIntroSite;
36 import org.eclipse.ui.intro.config.IntroConfigurer;
37 import org.eclipse.ui.intro.config.IntroElement;
38 import org.osgi.framework.Bundle;
39
40 /**
41  * This class provides for dynamic configuration of the shared intro
42  * implementation based on the data file associated with the product.
43  *
44  * @since 3.2
45  */

46 public class UniversalIntroConfigurer extends IntroConfigurer implements
47         IUniversalIntroConstants {
48     
49     private IntroData primaryIntroData;
50     private IntroData[] secondaryIntroData;
51     private SequenceResolver sequenceResolver;
52
53     public UniversalIntroConfigurer() {
54         loadData();
55     }
56
57     public String JavaDoc getVariable(String JavaDoc variableName) {
58         if (variableName.equals(HIGH_CONTRAST)) {
59             boolean highContrast = ImageUtil.isHighContrast();
60             if (highContrast)
61                 return variableName;
62             else
63                 return ""; //$NON-NLS-1$
64
}
65         IProduct product = Platform.getProduct();
66         if (product != null) {
67             // try product property first
68
String JavaDoc value = getProductProperty(product, variableName);
69             if (value != null) {
70                 value = resolveVariable(product.getDefiningBundle(), value);
71                 return value;
72             }
73             // if intro description for the page is not defined
74
// return a blank string to prevent the variable
75
// from showing up in the page
76
if (variableName.startsWith(VAR_INTRO_DESCRIPTION_PREFIX))
77                 return ""; //$NON-NLS-1$
78
// nothing - try preferences
79
Preferences prefs = UniversalIntroPlugin.getDefault()
80                     .getPluginPreferences();
81             // try to prefix with a preduct id first
82
String JavaDoc key = product.getId() + "_" + variableName; //$NON-NLS-1$
83
value = prefs.getString(key);
84             if (value.length() == 0) {
85                 // try direct variable name
86
key = variableName;
87                 value = prefs.getString(key);
88             }
89             if (value.length() > 0)
90                 value = resolveVariable(product.getDefiningBundle(), value);
91             else {
92                 // pass it to the theme
93
value = getThemeProperty(variableName);
94             }
95             return value;
96         }
97         return null;
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.eclipse.ui.intro.config.IntroConfigurer#getMixinStyle(java.lang.String)
104      */

105     public String JavaDoc getMixinStyle(String JavaDoc pageId, String JavaDoc extensionId) {
106         // if active product has a preference, use it
107
if (primaryIntroData != null) {
108             int importance = getImportance(primaryIntroData, pageId, extensionId);
109             if (importance >= 0) {
110                 return ExtensionData.IMPORTANCE_STYLE_TABLE[importance];
111             }
112         }
113         // else, find the most referenced importance style from other products
114
int[] importanceRefs = new int[ExtensionData.IMPORTANCE_TABLE.length];
115         for (int i=0;i<secondaryIntroData.length;++i) {
116             IntroData data = secondaryIntroData[i];
117             int importance = getImportance(data, pageId, extensionId);
118             if (importance >= 0) {
119                 ++importanceRefs[importance];
120             }
121         }
122         int maxIndex = 0;
123         for (int i=1;i<importanceRefs.length;++i) {
124             if (importanceRefs[i] > importanceRefs[maxIndex]) {
125                 maxIndex = i;
126             }
127         }
128         if (importanceRefs[maxIndex] > 0) {
129             return ExtensionData.IMPORTANCE_STYLE_TABLE[maxIndex];
130         }
131         // nobody has a preference
132
return null;
133     }
134
135     /*
136      * Returns the given extension's importance as specified by the
137      * given intro data.
138      */

139     private int getImportance(IntroData data, String JavaDoc pageId, String JavaDoc extensionId) {
140         String JavaDoc pluginId = ExtensionMap.getInstance().getPluginId(extensionId);
141         if (ContentDetector.isNew(pluginId)) {
142             updateStartPage(pageId);
143             return ExtensionData.NEW;
144         }
145         PageData pdata = data.getPage(pageId);
146         if (pdata != null) {
147             ExtensionData ed = pdata.findExtension(extensionId, false);
148             if (ed != null) {
149                 return ed.getImportance();
150             }
151         }
152         // none specified
153
return -1;
154     }
155
156     /*
157      * Modify the start page if this is a root page and it's position
158      * in the root page list is earlier than the current start page
159      */

160     private void updateStartPage(String JavaDoc pageId) {
161         String JavaDoc currentStartPage = ExtensionMap.getInstance().getStartPage();
162         String JavaDoc ids = getVariable(VAR_INTRO_ROOT_PAGES);
163         if (ids != null) {
164             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(ids, ","); //$NON-NLS-1$
165
while (stok.hasMoreTokens()) {
166                 String JavaDoc id = stok.nextToken().trim();
167                 if (id.equals(pageId)) {
168                     ExtensionMap.getInstance().setStartPage(pageId);
169                     return;
170                 }
171                 if (id.equals(currentStartPage)) {
172                     // The current start page has higher priority than the new page
173
return;
174                 }
175             }
176         }
177     }
178
179     private String JavaDoc resolveVariable(Bundle bundle, String JavaDoc value) {
180         if (value != null) {
181             String JavaDoc path = null;
182             if (value.startsWith("intro:")) { //$NON-NLS-1$
183
bundle = UniversalIntroPlugin.getDefault().getBundle();
184                 path = value.substring(6);
185             } else if (value.startsWith("product:")) { //$NON-NLS-1$
186
path = value.substring(8);
187             } else
188                 return value;
189             try {
190                 URL JavaDoc url = bundle.getEntry(path);
191                 if (url != null) {
192                     URL JavaDoc localURL = FileLocator.toFileURL(url);
193                     return localURL.toString();
194                 }
195             } catch (IOException JavaDoc e) {
196                 // just use the value as-is
197
return value;
198             }
199         }
200         return null;
201     }
202
203     private String JavaDoc getProductProperty(IProduct product, String JavaDoc variableName) {
204         String JavaDoc value = product.getProperty(variableName);
205         if (value == null) {
206             // return default values
207
if (variableName.equals(VAR_INTRO_BACKGROUND_IMAGE))
208                 return "css/graphics/root/welcomebckgrd.jpg"; //$NON-NLS-1$
209
}
210         return value;
211     }
212
213     public IntroElement[] getGroupChildren(String JavaDoc pageId, String JavaDoc groupId) {
214         if (pageId.equals(ID_ROOT)) {
215             if (groupId.equals(DIV_PAGE_LINKS))
216                 return getRootPageLinks(false);
217             if (groupId.equals(DIV_ACTION_LINKS))
218                 return getRootPageActionLinks(false);
219         } else if (pageId.equals(ID_STANDBY)) {
220             if (groupId.equals(DIV_PAGE_LINKS))
221                 return getRootPageLinks(true);
222             if (groupId.equals(DIV_ACTION_LINKS))
223                 return getRootPageActionLinks(true);
224         } else {
225             // other pages
226
if (groupId.equals(DIV_PAGE_LINKS))
227                 return getNavLinks(pageId);
228             if (groupId.equals(DIV_LAYOUT_TOP_LEFT)
229                     || groupId.equals(DIV_LAYOUT_TOP_RIGHT)
230                     || groupId.equals(DIV_LAYOUT_BOTTOM_LEFT)
231                     || groupId.equals(DIV_LAYOUT_BOTTOM_RIGHT))
232                 return getContent(pageId, groupId);
233         }
234         return new IntroElement[0];
235     }
236
237     public IntroElement[] getLaunchBarShortcuts() {
238         ArrayList JavaDoc links = new ArrayList JavaDoc();
239         String JavaDoc ids = getVariable(VAR_INTRO_ROOT_PAGES);
240         if (ids != null) {
241             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(ids, ","); //$NON-NLS-1$
242
while (stok.hasMoreTokens()) {
243                 String JavaDoc id = stok.nextToken().trim();
244                 IntroElement page = createLaunchBarShortcut(id);
245                 if (page != null)
246                     links.add(page);
247             }
248         }
249         return (IntroElement[]) links.toArray(new IntroElement[links.size()]);
250     }
251
252     private IntroElement[] getRootPageLinks(boolean standby) {
253         ArrayList JavaDoc links = new ArrayList JavaDoc();
254         String JavaDoc ids = getVariable(VAR_INTRO_ROOT_PAGES);
255         if (ids != null) {
256             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(ids, ","); //$NON-NLS-1$
257
while (stok.hasMoreTokens()) {
258                 String JavaDoc id = stok.nextToken().trim();
259                 IntroElement page = createRootPageLink(id, standby);
260                 if (page != null)
261                     links.add(page);
262             }
263         }
264         // add workbench link if so configured by the theme
265
String JavaDoc wb = getVariable(VAR_WORKBENCH_AS_ROOT_LINK);
266         if (wb!=null && wb.equalsIgnoreCase("true")) { //$NON-NLS-1$
267
IntroElement page = createRootPageLink(ID_WORKBENCH, standby);
268             if (page !=null)
269                 links.add(page);
270         }
271         return (IntroElement[]) links.toArray(new IntroElement[links.size()]);
272     }
273
274     private IntroElement[] getRootPageActionLinks(boolean standby) {
275         String JavaDoc wb = getVariable(VAR_WORKBENCH_AS_ROOT_LINK);
276         // only create the workbench link if
277
// not already configured as a root link
278
if (wb==null || !wb.equalsIgnoreCase("true")) { //$NON-NLS-1$
279
IntroElement page = createRootPageLink(ID_WORKBENCH, standby);
280             if (page !=null)
281                 return new IntroElement[] { page };
282         }
283         return new IntroElement [0];
284     }
285
286     private IntroElement[] getNavLinks(String JavaDoc pageId) {
287         ArrayList JavaDoc links = new ArrayList JavaDoc();
288         String JavaDoc ids = getVariable(VAR_INTRO_ROOT_PAGES);
289         /*
290          * In high contrast mode the workbench link must be generated in the nav links
291          * otherwise it will not show
292          */

293         if (ImageUtil.isHighContrast()) {
294             ids = ids + ',' + IUniversalIntroConstants.ID_WORKBENCH;
295         }
296         if (ids != null) {
297             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(ids, ","); //$NON-NLS-1$
298
int [] counter = new int [1];
299             while (stok.hasMoreTokens()) {
300                 String JavaDoc id = stok.nextToken().trim();
301                 IntroElement page = createNavLink(id, pageId, counter);
302                 if (page != null)
303                     links.add(page);
304             }
305         }
306
307         return (IntroElement[]) links.toArray(new IntroElement[links.size()]);
308     }
309
310     private IntroElement createRootPageLink(String JavaDoc id, boolean standby) {
311
312         if (id.equals(ID_OVERVIEW))
313             return createRootLink(
314                     Messages.SharedIntroConfigurer_overview_name,
315                     createPageURL(id, standby),
316                     id,
317                     "overview_img", "$theme$/graphics/root/overview.gif", Messages.SharedIntroConfigurer_overview_alt, //$NON-NLS-1$ //$NON-NLS-2$
318
Messages.SharedIntroConfigurer_overview_tooltip, "left"); //$NON-NLS-1$
319
if (id.equals(ID_FIRSTSTEPS))
320             return createRootLink(
321                     Messages.SharedIntroConfigurer_firststeps_name,
322                     createPageURL(id, standby),
323                     id,
324                     "firststeps_img", "$theme$/graphics/root/firststeps.gif", Messages.SharedIntroConfigurer_firststeps_alt, //$NON-NLS-1$ //$NON-NLS-2$
325
Messages.SharedIntroConfigurer_firststeps_tooltip, "left"); //$NON-NLS-1$
326
if (id.equals(ID_TUTORIALS))
327             return createRootLink(
328                     Messages.SharedIntroConfigurer_tutorials_name,
329                     createPageURL(id, standby),
330                     id,
331                     "tutorials_img", "$theme$/graphics/root/tutorials.gif", Messages.SharedIntroConfigurer_tutorials_alt, //$NON-NLS-1$ //$NON-NLS-2$
332
Messages.SharedIntroConfigurer_tutorials_tooltip, "left"); //$NON-NLS-1$
333
if (id.equals(ID_SAMPLES))
334             return createRootLink(
335                     Messages.SharedIntroConfigurer_samples_name,
336                     createPageURL(id, standby),
337                     id,
338                     "samples_img", "$theme$/graphics/root/samples.gif", Messages.SharedIntroConfigurer_samples_alt, Messages.SharedIntroConfigurer_samples_tooltip, "right"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
339
if (id.equals(ID_WHATSNEW))
340             return createRootLink(
341                     Messages.SharedIntroConfigurer_whatsnew_name,
342                     createPageURL(id, standby),
343                     id,
344                     "whatsnew_img", "$theme$/graphics/root/whatsnew.gif", Messages.SharedIntroConfigurer_whatsnew_alt, //$NON-NLS-1$ //$NON-NLS-2$
345
Messages.SharedIntroConfigurer_whatsnew_tooltip, "right"); //$NON-NLS-1$
346
if (id.equals(ID_MIGRATE))
347             return createRootLink(
348                     Messages.SharedIntroConfigurer_migrate_name,
349                     createPageURL(id, standby),
350                     id,
351                     "migrate_img", "$theme$/graphics/root/migrate.gif", Messages.SharedIntroConfigurer_migrate_alt, Messages.SharedIntroConfigurer_migrate_tooltip, "right"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
352
if (id.equals(ID_WEBRESOURCES))
353             return createRootLink(
354                     Messages.SharedIntroConfigurer_webresources_name,
355                     createPageURL(id, standby),
356                     id,
357                     "webresources_img", "css/graphics/root/webresources.gif", //$NON-NLS-1$ //$NON-NLS-2$
358
Messages.SharedIntroConfigurer_webresources_alt,
359                     Messages.SharedIntroConfigurer_webresources_tooltip,
360                     "right"); //$NON-NLS-1$
361
if (id.equals(ID_WORKBENCH))
362             return createRootLink(
363                     Messages.SharedIntroConfigurer_workbench_name,
364                     "http://org.eclipse.ui.intro/switchToLaunchBar", //$NON-NLS-1$
365
id,
366                     "workbench_img", "css/graphics/root/workbench.gif", //$NON-NLS-1$ //$NON-NLS-2$
367
Messages.SharedIntroConfigurer_workbench_alt,
368                     Messages.SharedIntroConfigurer_workbench_tooltip,
369                     "right"); //$NON-NLS-1$
370
return null;
371     }
372
373     private IntroElement createNavLink(String JavaDoc id, String JavaDoc pageId, int [] counter) {
374         if (id.equals(ID_OVERVIEW))
375             return createNavLink(Messages.SharedIntroConfigurer_overview_nav,
376                     createPageURL(id, false), id, "left nav_link"+(++counter[0])); //$NON-NLS-1$
377
if (id.equals(ID_FIRSTSTEPS))
378             return createNavLink(Messages.SharedIntroConfigurer_firststeps_nav,
379                     createPageURL(id, false), id, "left nav_link"+(++counter[0])); //$NON-NLS-1$
380
if (id.equals(ID_TUTORIALS))
381             return createNavLink(Messages.SharedIntroConfigurer_tutorials_nav,
382                     createPageURL(id, false), id, "left nav_link"+(++counter[0])); //$NON-NLS-1$
383
if (id.equals(ID_SAMPLES))
384             return createNavLink(Messages.SharedIntroConfigurer_samples_nav,
385                     createPageURL(id, false), id, "right nav_link"+(++counter[0])); //$NON-NLS-1$
386
if (id.equals(ID_WHATSNEW))
387             return createNavLink(Messages.SharedIntroConfigurer_whatsnew_nav,
388                     createPageURL(id, false), id, "right nav_link"+(++counter[0])); //$NON-NLS-1$
389
if (id.equals(ID_MIGRATE))
390             return createNavLink(Messages.SharedIntroConfigurer_migrate_nav,
391                     createPageURL(id, false), id, "right nav_link"+(++counter[0])); //$NON-NLS-1$
392
if (id.equals(ID_WEBRESOURCES))
393             return createNavLink(
394                     Messages.SharedIntroConfigurer_webresources_nav,
395                     createPageURL(id, false), id, "right nav_link"+(++counter[0])); //$NON-NLS-1$
396
if (id.equals(ID_WORKBENCH))
397             return createNavLink(
398                     Messages.SharedIntroConfigurer_workbench_name,
399                     "http://org.eclipse.ui.intro/switchToLaunchBar", //$NON-NLS-1$
400
id,
401                     "right nav_link"+(++counter[0])); //$NON-NLS-1$
402
return null;
403     }
404
405     private String JavaDoc createPageURL(String JavaDoc id, boolean standby) {
406         String JavaDoc url = "http://org.eclipse.ui.intro/showPage?id=" + id; //$NON-NLS-1$
407
if (standby)
408             url += "&standby=false"; //$NON-NLS-1$
409
return url;
410     }
411
412     private IntroElement createLaunchBarShortcut(String JavaDoc id) {
413         if (id.equals(ID_OVERVIEW))
414             return createShortcutLink(
415                     getThemeProperty(LAUNCHBAR_OVERVIEW_ICON), Messages.SharedIntroConfigurer_overview_nav,
416                     id);
417         if (id.equals(ID_FIRSTSTEPS))
418             return createShortcutLink(
419                     getThemeProperty(LAUNCHBAR_FIRSTSTEPS_ICON), Messages.SharedIntroConfigurer_firststeps_nav,
420                     id);
421         if (id.equals(ID_TUTORIALS))
422             return createShortcutLink(
423                     getThemeProperty(LAUNCHBAR_TUTORIALS_ICON), Messages.SharedIntroConfigurer_tutorials_nav,
424                     id);
425         if (id.equals(ID_SAMPLES))
426             return createShortcutLink(
427                     getThemeProperty(LAUNCHBAR_SAMPLES_ICON), Messages.SharedIntroConfigurer_samples_nav,
428                     id);
429         if (id.equals(ID_WHATSNEW))
430             return createShortcutLink(
431                     getThemeProperty(LAUNCHBAR_WHATSNEW_ICON), Messages.SharedIntroConfigurer_whatsnew_nav,
432                     id);
433         if (id.equals(ID_MIGRATE))
434             return createShortcutLink(
435                     getThemeProperty(LAUNCHBAR_MIGRATE_ICON), Messages.SharedIntroConfigurer_migrate_nav,
436                     id);
437         if (id.equals(ID_WEBRESOURCES))
438             return createShortcutLink(
439                     getThemeProperty(LAUNCHBAR_WEBRESOURCES_ICON), Messages.SharedIntroConfigurer_webresources_nav,
440                     id);
441         return null;
442     }
443
444     private IntroElement createRootLink(String JavaDoc name, String JavaDoc url, String JavaDoc id,
445             String JavaDoc imgId, String JavaDoc imgSrc, String JavaDoc imgAlt, String JavaDoc imgText,
446             String JavaDoc styleId) {
447         IntroElement element = new IntroElement("link"); //$NON-NLS-1$
448
element.setAttribute("label", name); //$NON-NLS-1$
449
element.setAttribute("url", url); //$NON-NLS-1$
450
element.setAttribute("id", id); //$NON-NLS-1$
451
element.setAttribute("style-id", styleId);//$NON-NLS-1$
452
IntroElement img = new IntroElement("img"); //$NON-NLS-1$
453
img.setAttribute("id", imgId); //$NON-NLS-1$
454
img.setAttribute("style-id", "content-img"); //$NON-NLS-1$ //$NON-NLS-2$
455
// img.setAttribute("src", imgSrc); //$NON-NLS-1$
456
boolean highContrast = ImageUtil.isHighContrast();
457         if (highContrast) {
458             String JavaDoc key = HIGH_CONTRAST_PREFIX+id;
459             String JavaDoc value = getVariable(key);
460             if (value!=null)
461                 img.setAttribute("src", value); //$NON-NLS-1$
462
}
463         img.setAttribute("alt", imgAlt); //$NON-NLS-1$
464
IntroElement text = new IntroElement("text"); //$NON-NLS-1$
465
text.setValue(imgText);
466         element.addChild(img);
467         element.addChild(text);
468         return element;
469     }
470
471     private IntroElement createNavLink(String JavaDoc label, String JavaDoc url, String JavaDoc id,
472             String JavaDoc styleId) {
473         IntroElement element = new IntroElement("link"); //$NON-NLS-1$
474
element.setAttribute("label", label); //$NON-NLS-1$
475
element.setAttribute("url", url); //$NON-NLS-1$
476
element.setAttribute("id", id); //$NON-NLS-1$
477
boolean highContrast = ImageUtil.isHighContrast();
478         if (highContrast) {
479             IntroElement img = new IntroElement("img"); //$NON-NLS-1$
480
img.setAttribute("style-id", "content-img"); //$NON-NLS-1$ //$NON-NLS-2$
481
String JavaDoc key = HIGH_CONTRAST_NAV_PREFIX+id;
482             String JavaDoc value = getVariable(key);
483             if (value!=null)
484                 img.setAttribute("src", value); //$NON-NLS-1$
485
img.setAttribute("alt", label); //$NON-NLS-1$
486
element.addChild(img);
487             styleId += " "+HIGH_CONTRAST; //$NON-NLS-1$
488
}
489         element.setAttribute("style-id", styleId); //$NON-NLS-1$
490
return element;
491     }
492
493     private IntroElement createShortcutLink(String JavaDoc icon, String JavaDoc tooltip,
494             String JavaDoc id) {
495         IntroElement element = new IntroElement("shortcut"); //$NON-NLS-1$
496
element.setAttribute("icon", icon); //$NON-NLS-1$
497
element.setAttribute("tooltip", tooltip); //$NON-NLS-1$
498
element.setAttribute("url", createPageURL(id, false)); //$NON-NLS-1$
499
return element;
500     }
501
502     private void loadData() {
503         // load the active product's intro data first
504
IProduct product = Platform.getProduct();
505         if (product != null) {
506             String JavaDoc dataFile = getVariable(VAR_INTRO_DATA);
507             if (dataFile != null) {
508                 primaryIntroData = new IntroData(product.getId(), dataFile, true);
509             }
510         }
511         // load all other installed (but not running) products' intro data
512
List JavaDoc result = new ArrayList JavaDoc();
513         Properties JavaDoc[] prefs = ProductPreferences.getProductPreferences(false);
514         for (int i=0;i<prefs.length;++i) {
515             String JavaDoc key = UniversalIntroPlugin.PLUGIN_ID + '/' + VAR_INTRO_DATA;
516             String JavaDoc dataFile = prefs[i].getProperty(key);
517             if (dataFile != null) {
518                 String JavaDoc pluginId = ProductPreferences.getPluginId(prefs[i]);
519                 Bundle bundle = Platform.getBundle(pluginId);
520                 if (bundle != null) {
521                     String JavaDoc pid = ProductPreferences.getProductId(prefs[i]);
522                     dataFile = resolveVariable(bundle, dataFile);
523                     result.add(new IntroData(pid, dataFile, false));
524                 }
525             }
526         }
527         secondaryIntroData = (IntroData[])result.toArray(new IntroData[result.size()]);
528     }
529
530     private IntroElement[] getContent(String JavaDoc pageId, String JavaDoc groupId) {
531         List JavaDoc result = new ArrayList JavaDoc();
532         if (!ContentDetector.getNewContributors().isEmpty()) {
533             // Add a new content fallback anchor
534
IntroElement fallback = new IntroElement("anchor"); //$NON-NLS-1$
535
fallback.setAttribute("id", NEW_CONTENT_ANCHOR); //$NON-NLS-1$
536
result.add(fallback);
537         }
538         List JavaDoc anchors = getAnchors(pageId, groupId);
539         if (anchors != null) {
540             result.addAll(anchors);
541         }
542         // Add the fallback anchor
543
IntroElement fallback = new IntroElement("anchor"); //$NON-NLS-1$
544
fallback.setAttribute("id", DEFAULT_ANCHOR); //$NON-NLS-1$
545
result.add(fallback);
546         return (IntroElement[]) result.toArray(new IntroElement[result.size()]);
547     }
548
549     private List JavaDoc getAnchors(String JavaDoc pageId, String JavaDoc groupId) {
550         List JavaDoc primaryAnchors = null;
551         if (primaryIntroData != null) {
552             primaryAnchors = getAnchors(primaryIntroData, pageId, groupId);
553         }
554         if (primaryAnchors == null) {
555             primaryAnchors = Collections.EMPTY_LIST;
556         }
557         List JavaDoc secondaryAnchorsList = new ArrayList JavaDoc();
558         for (int i=0;i<secondaryIntroData.length;++i) {
559             IntroData idata = secondaryIntroData[i];
560             List JavaDoc anchors = getAnchors(idata, pageId, groupId);
561             if (anchors != null) {
562                 secondaryAnchorsList.add(anchors);
563             }
564         }
565         List JavaDoc[] secondaryAnchors = (List JavaDoc[])secondaryAnchorsList.toArray(new List JavaDoc[secondaryAnchorsList.size()]);
566         if (sequenceResolver == null) {
567             sequenceResolver = new SequenceResolver();
568         }
569         return sequenceResolver.getSequence(primaryAnchors, secondaryAnchors);
570     }
571     
572     private List JavaDoc getAnchors(IntroData data, String JavaDoc pageId, String JavaDoc groupId) {
573         PageData pdata = data.getPage(pageId);
574         if (pdata != null) {
575             List JavaDoc anchors = new ArrayList JavaDoc();
576             pdata.addAnchors(anchors, groupId);
577             return anchors;
578         }
579         return null;
580     }
581     
582     public String JavaDoc resolvePath(String JavaDoc extensionId, String JavaDoc path) {
583         boolean extensionRelativePath = false;
584         IPath ipath = new Path(path);
585         String JavaDoc pageId = ipath.segment(0);
586         String JavaDoc s2 = ipath.segment(1);
587         // if it's "@extension_id" then target that extension instead
588
if (s2.startsWith("@") && s2.length() > 1) { //$NON-NLS-1$
589
extensionId = s2.substring(1);
590         }
591         if (!s2.equals("@")) { //$NON-NLS-1$
592
extensionRelativePath = true;
593         }
594         if (!isHidden(extensionId, pageId)) {
595             String JavaDoc resolvedPath = resolveExtensionPath(extensionId, pageId);
596             if (resolvedPath != null) {
597                 if (extensionRelativePath) {
598                     // not done - use the resolved extension path to complete the source path
599
IPath p2 = new Path(resolvedPath);
600                     IPath p1 = ipath.removeFirstSegments(2);
601                     // remove the last anchor and append the relative path from the extension
602
resolvedPath = p2.removeLastSegments(1).append(p1).toString();
603                 }
604                 return resolvedPath;
605             }
606             return pageId + DEFAULT_CONTENT_PATH;
607         }
608         return null;
609     }
610
611     private String JavaDoc resolveExtensionPath(String JavaDoc extensionId, String JavaDoc pageId) {
612         // does the active product have a preference?
613
if (primaryIntroData != null) {
614             PageData pdata = primaryIntroData.getPage(pageId);
615             if (pdata != null) {
616                 String JavaDoc path = pdata.resolvePath(extensionId);
617                 if (path != null) {
618                     return path;
619                 }
620             }
621         }
622         // if not, do the others have preferences?
623
PreferenceArbiter arbiter = new PreferenceArbiter();
624         for (int i=0;i<secondaryIntroData.length;++i) {
625             IntroData idata = secondaryIntroData[i];
626             PageData pdata = idata.getPage(pageId);
627             if (pdata != null) {
628                 arbiter.consider(pdata.resolvePath(extensionId));
629             }
630         }
631         String JavaDoc path = (String JavaDoc)arbiter.getWinner();
632         if (path != null) {
633             return path;
634         }
635         // there was no clear winner; fall back to the default
636
return resolveDefaultPath(pageId, extensionId);
637     }
638     
639     private String JavaDoc resolveDefaultPath(String JavaDoc pageId, String JavaDoc extensionId) {
640         String JavaDoc pluginId = ExtensionMap.getInstance().getPluginId(extensionId);
641         if (ContentDetector.isNew(pluginId)) {
642             return pageId + IUniversalIntroConstants.NEW_CONTENT_PATH;
643         }
644         // does the active product have a preference?
645
if (primaryIntroData != null) {
646             PageData pdata = primaryIntroData.getPage(pageId);
647             if (pdata != null) {
648                 String JavaDoc path = pdata.resolveDefaultPath();
649                 if (path != null) {
650                     return path;
651                 }
652             }
653         }
654         // if not, do the others have preferences?
655
PreferenceArbiter arbiter = new PreferenceArbiter();
656         for (int i=0;i<secondaryIntroData.length;++i) {
657             IntroData idata = secondaryIntroData[i];
658             PageData pdata = idata.getPage(pageId);
659             if (pdata != null) {
660                 arbiter.consider(pdata.resolveDefaultPath());
661             }
662         }
663         return (String JavaDoc)arbiter.getWinner();
664     }
665
666     private boolean isHidden(String JavaDoc extensionId, String JavaDoc pageId) {
667         if (primaryIntroData != null) {
668             PageData pdata = primaryIntroData.getPage(pageId);
669             if (pdata != null) {
670                 return pdata.isHidden(extensionId);
671             }
672         }
673         return false;
674     }
675
676     public void init(IIntroSite site, Map JavaDoc themeProperties) {
677         super.init(site, themeProperties);
678         IConfigurationElement element = CustomizeAction.getPageElement();
679         if (element == null)
680             return;
681         Action customizeAction = new CustomizeAction(site, element);
682         customizeAction.setText(Messages.SharedIntroConfigurer_customize_label);
683         customizeAction
684                 .setToolTipText(Messages.SharedIntroConfigurer_customize_text);
685         customizeAction.setImageDescriptor(ImageUtil
686                 .createImageDescriptor("full/elcl16/configure.gif")); //$NON-NLS-1$
687
site.getActionBars().getToolBarManager().appendToGroup(TB_ADDITIONS,
688                 customizeAction);
689     }
690 }
Popular Tags