KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.universal;
12
13 import java.io.FileWriter JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExecutableExtension;
24 import org.eclipse.core.runtime.IProduct;
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.core.runtime.Preferences;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IMenuListener;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.MenuManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.preference.PreferencePage;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.eclipse.jface.viewers.CellEditor;
35 import org.eclipse.jface.viewers.CheckStateChangedEvent;
36 import org.eclipse.jface.viewers.CheckboxTableViewer;
37 import org.eclipse.jface.viewers.ComboBoxCellEditor;
38 import org.eclipse.jface.viewers.ICellModifier;
39 import org.eclipse.jface.viewers.ICheckStateListener;
40 import org.eclipse.jface.viewers.ISelectionChangedListener;
41 import org.eclipse.jface.viewers.IStructuredContentProvider;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.viewers.ITableLabelProvider;
44 import org.eclipse.jface.viewers.LabelProvider;
45 import org.eclipse.jface.viewers.SelectionChangedEvent;
46 import org.eclipse.jface.viewers.StructuredSelection;
47 import org.eclipse.jface.viewers.TableViewer;
48 import org.eclipse.jface.viewers.Viewer;
49 import org.eclipse.jface.viewers.ViewerComparator;
50 import org.eclipse.jface.viewers.ViewerDropAdapter;
51 import org.eclipse.swt.SWT;
52 import org.eclipse.swt.custom.BusyIndicator;
53 import org.eclipse.swt.dnd.DND;
54 import org.eclipse.swt.dnd.DragSourceEvent;
55 import org.eclipse.swt.dnd.DragSourceListener;
56 import org.eclipse.swt.dnd.Transfer;
57 import org.eclipse.swt.dnd.TransferData;
58 import org.eclipse.swt.events.PaintEvent;
59 import org.eclipse.swt.events.PaintListener;
60 import org.eclipse.swt.events.SelectionAdapter;
61 import org.eclipse.swt.events.SelectionEvent;
62 import org.eclipse.swt.graphics.Image;
63 import org.eclipse.swt.graphics.Rectangle;
64 import org.eclipse.swt.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Canvas;
68 import org.eclipse.swt.widgets.Composite;
69 import org.eclipse.swt.widgets.Control;
70 import org.eclipse.swt.widgets.FileDialog;
71 import org.eclipse.swt.widgets.Label;
72 import org.eclipse.swt.widgets.TabFolder;
73 import org.eclipse.swt.widgets.TabItem;
74 import org.eclipse.swt.widgets.Table;
75 import org.eclipse.swt.widgets.TableColumn;
76 import org.eclipse.swt.widgets.TableItem;
77 import org.eclipse.ui.IWorkbench;
78 import org.eclipse.ui.IWorkbenchPreferencePage;
79 import org.eclipse.ui.IWorkbenchWindow;
80 import org.eclipse.ui.PlatformUI;
81 import org.eclipse.ui.internal.intro.impl.IntroPlugin;
82 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
83 import org.eclipse.ui.internal.intro.universal.util.BundleUtil;
84 import org.eclipse.ui.internal.intro.universal.util.ImageUtil;
85 import org.eclipse.ui.internal.intro.universal.util.Log;
86 import org.eclipse.ui.intro.IIntroManager;
87 import org.eclipse.ui.intro.IIntroPart;
88 import org.eclipse.ui.intro.config.CustomizableIntroPart;
89 import org.eclipse.ui.intro.config.IIntroURL;
90 import org.eclipse.ui.intro.config.IntroURLFactory;
91 import org.osgi.framework.Bundle;
92
93
94 public class WelcomeCustomizationPreferencePage extends PreferencePage implements IWorkbenchPreferencePage,
95         IExecutableExtension {
96
97     private static final String JavaDoc INTRO_ROOT_PAGES = "INTRO_ROOT_PAGES"; //$NON-NLS-1$
98
private static final String JavaDoc INTRO_DATA = "INTRO_DATA"; //$NON-NLS-1$
99
private static final String JavaDoc INTRO_THEME = "INTRO_THEME"; //$NON-NLS-1$
100
private TabFolder tabFolder;
101     private String JavaDoc firstPageId;
102     private Composite pageContainer;
103     private TableViewer themes;
104     private TableViewer available;
105     private TableViewer topLeft;
106     private TableViewer topRight;
107     private TableViewer bottomLeft;
108     private TableViewer bottomRight;
109     private CheckboxTableViewer rootPages;
110     private ArrayList JavaDoc introRootPages = new ArrayList JavaDoc();
111     private ArrayList JavaDoc themeList = new ArrayList JavaDoc();
112     private IntroTheme introTheme;
113     private String JavaDoc introThemeId;
114     private IntroData introData;
115     private Canvas themePreview;
116     private TableContentProvider contentProvider;
117     private TableLabelProvider labelProvider;
118     private Button applyToAll;
119     private Image extensionImage;
120     private Image themeImage;
121     private Image ihighImage;
122     private Image ilowImage;
123     private Image inewImage;
124     private Image icalloutImage;
125     private static final Transfer[] TRANSFER_TYPES = new Transfer[] { ExtensionDataTransfer.getInstance() };
126
127
128     private static final RootPage ROOT_PAGE_TABLE[] = new RootPage[] {
129             new RootPage(IUniversalIntroConstants.ID_OVERVIEW,
130                     Messages.WelcomeCustomizationPreferencePage_overview,
131                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_overview),
132             new RootPage(IUniversalIntroConstants.ID_FIRSTSTEPS,
133                     Messages.WelcomeCustomizationPreferencePage_firststeps,
134                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_firststeps),
135             new RootPage(IUniversalIntroConstants.ID_TUTORIALS,
136                     Messages.WelcomeCustomizationPreferencePage_tutorials,
137                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_tutorials),
138             new RootPage(IUniversalIntroConstants.ID_SAMPLES,
139                     Messages.WelcomeCustomizationPreferencePage_samples,
140                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_samples),
141             new RootPage(IUniversalIntroConstants.ID_WHATSNEW,
142                     Messages.WelcomeCustomizationPreferencePage_whatsnew,
143                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_whatsnew),
144             new RootPage(IUniversalIntroConstants.ID_WEBRESOURCES,
145                     Messages.WelcomeCustomizationPreferencePage_webresources,
146                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_webresources),
147             new RootPage(IUniversalIntroConstants.ID_MIGRATE,
148                     Messages.WelcomeCustomizationPreferencePage_migrate,
149                     Messages.WelcomeCustomizationPreferencePage_NoMnemonic_migrate) };
150
151     static class RootPage {
152
153         public String JavaDoc id;
154         public String JavaDoc name;
155         public String JavaDoc nameNoMnemonic;
156
157         public RootPage(String JavaDoc id, String JavaDoc name, String JavaDoc nameNoMnemonic) {
158             this.id = id;
159             this.name = name;
160             this.nameNoMnemonic = nameNoMnemonic;
161         }
162         
163         public String JavaDoc getName() {
164             return name;
165         }
166
167         public String JavaDoc getNameNoMnemonic() {
168             return nameNoMnemonic;
169         }
170
171         public boolean equals(Object JavaDoc obj) {
172             if (obj == this)
173                 return true;
174             if (obj == null)
175                 return false;
176             if (obj instanceof RootPage) {
177                 RootPage page = (RootPage) obj;
178                 return page.id.equals(id) && page.name.equals(name);
179             }
180             return false;
181         }
182
183         public String JavaDoc toString() {
184             return name;
185         }
186     }
187
188     class TableContentProvider implements IStructuredContentProvider {
189
190         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
191             if (inputElement == ROOT_PAGE_TABLE)
192                 return ROOT_PAGE_TABLE;
193             if (inputElement instanceof GroupData) {
194                 return ((GroupData) inputElement).getChildren();
195             }
196             if (inputElement == themes) {
197                 return themeList.toArray();
198             }
199             return new Object JavaDoc[0];
200         }
201
202         public void dispose() {
203         }
204
205         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
206         }
207     }
208
209     class TableDragSourceListener implements DragSourceListener {
210
211         TableViewer viewer;
212         BaseData[] sel;
213
214         public TableDragSourceListener(TableViewer viewer) {
215             this.viewer = viewer;
216         }
217
218         public void dragStart(DragSourceEvent event) {
219             IStructuredSelection ssel = (IStructuredSelection) viewer.getSelection();
220             if (ssel.size() > 0) {
221                 event.doit = true;
222             } else {
223                 event.doit = false;
224             }
225         };
226
227         public void dragSetData(DragSourceEvent event) {
228             IStructuredSelection ssel = (IStructuredSelection) viewer.getSelection();
229             BaseData[] array = new BaseData[ssel.size()];
230             int i = 0;
231             for (Iterator JavaDoc iter = ssel.iterator(); iter.hasNext();) {
232                 array[i++] = (BaseData) iter.next();
233             }
234             event.data = array;
235             sel = array;
236         }
237
238         public void dragFinished(DragSourceEvent event) {
239             if (event.detail == DND.DROP_MOVE) {
240                 GroupData gd = (GroupData) viewer.getInput();
241                 for (int i = 0; i < sel.length; i++) {
242                     BaseData ed = sel[i];
243                     gd.remove(ed);
244                 }
245                 viewer.refresh();
246                 updateColumnSizes(viewer);
247             }
248             sel = null;
249         }
250     }
251
252     class TableDropTargetListener extends ViewerDropAdapter {
253
254         public TableDropTargetListener(TableViewer viewer) {
255             super(viewer);
256         }
257
258         public boolean performDrop(Object JavaDoc data) {
259             BaseData target = (BaseData) getCurrentTarget();
260             int loc = getCurrentLocation();
261             GroupData gd = (GroupData) getViewer().getInput();
262             if (gd == null)
263                 gd = createTargetGd(getViewer());
264             BaseData[] sel = (BaseData[]) data;
265
266             int index = target != null ? gd.getIndexOf(target) : -1;
267             int startingIndex = getStartIndex(gd, sel);
268             if (target != null) {
269                 if (loc == LOCATION_AFTER
270                         || (loc == LOCATION_ON && startingIndex != -1 && startingIndex < index))
271                     index++;
272                 else if (index > 0 && loc == LOCATION_BEFORE)
273                     index--;
274             }
275
276             for (int i = 0; i < sel.length; i++) {
277                 BaseData ed = sel[i];
278                 if (index == -1)
279                     gd.add(ed);
280                 else
281                     gd.add(index++, ed);
282             }
283             if (getViewer().getInput() != null)
284                 getViewer().refresh();
285             else
286                 getViewer().setInput(gd);
287             updateColumnSizes((TableViewer) getViewer());
288             return true;
289         }
290
291         private int getStartIndex(GroupData gd, BaseData[] sel) {
292             for (int i = 0; i < sel.length; i++) {
293                 BaseData ed = sel[i];
294                 int index = gd.getIndexOf(ed.getId());
295                 if (index != -1)
296                     return index;
297             }
298             return -1;
299         }
300
301         public boolean validateDrop(Object JavaDoc target, int operation, TransferData transferType) {
302             return ExtensionDataTransfer.getInstance().isSupportedType(transferType);
303         }
304     }
305
306     class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
307
308         public String JavaDoc getText(Object JavaDoc obj) {
309             if (obj instanceof RootPage) {
310                 return ((RootPage) obj).getNameNoMnemonic();
311             }
312             if (obj instanceof ExtensionData) {
313                 ExtensionData ed = (ExtensionData) obj;
314                 String JavaDoc name = ed.getName();
315                 if (name != null && name.length() > 0)
316                     return name;
317                 return ed.getId();
318             }
319             if (obj instanceof SeparatorData) {
320                 return Messages.WelcomeCustomizationPreferencePage_horizontalSeparator;
321             }
322             if (obj instanceof IntroTheme) {
323                 IntroTheme bg = (IntroTheme) obj;
324                 return bg.getName();
325             }
326             return super.getText(obj);
327         }
328         
329         public Image getImage(Object JavaDoc obj) {
330             if (obj instanceof ExtensionData) {
331                 ExtensionData ed = (ExtensionData) obj;
332                 switch (ed.getImportance()) {
333                 case ExtensionData.HIGH:
334                     return ihighImage;
335                 case ExtensionData.LOW:
336                     return ilowImage;
337                 case ExtensionData.NEW:
338                     return inewImage;
339                 case ExtensionData.CALLOUT:
340                     return icalloutImage;
341                 }
342                 return extensionImage;
343             }
344             if (obj instanceof IntroTheme)
345                 return themeImage;
346             return null;
347         }
348
349         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
350             if (columnIndex == 0)
351                 return getImage(element);
352             return null;
353         }
354
355         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
356             if (columnIndex == 1 || element instanceof IntroTheme || element instanceof RootPage)
357                 return getText(element);
358             return null;
359         }
360     }
361
362     class IntroTheme {
363         IConfigurationElement element;
364         Image previewImage;
365
366         public String JavaDoc getName() {
367             return element.getAttribute("name"); //$NON-NLS-1$
368
}
369
370         public String JavaDoc getId() {
371             return element.getAttribute("id"); //$NON-NLS-1$
372
}
373
374         public IntroTheme(IConfigurationElement element) {
375             this.element = element;
376         }
377         
378         public Image getPreviewImage() {
379             if (previewImage==null) {
380                 String JavaDoc path = element.getAttribute("previewImage"); //$NON-NLS-1$
381
if (path!=null) {
382                     String JavaDoc bid = element.getDeclaringExtension().getNamespaceIdentifier();
383                     Bundle bundle = Platform.getBundle(bid);
384                     if (bundle!=null) {
385                         ImageDescriptor desc = ImageUtil.createImageDescriptor(bundle, path);
386                         previewImage = desc.createImage();
387                     }
388                 }
389             }
390             return previewImage;
391         }
392         
393         public void dispose() {
394             if (previewImage!=null) {
395                 previewImage.dispose();
396                 previewImage=null;
397             }
398         }
399     }
400
401     public WelcomeCustomizationPreferencePage() {
402     }
403
404     public WelcomeCustomizationPreferencePage(String JavaDoc title) {
405         super(title);
406     }
407
408     public WelcomeCustomizationPreferencePage(String JavaDoc title, ImageDescriptor image) {
409         super(title, image);
410     }
411
412     protected Control createContents(Composite parent) {
413         Composite container = new Composite(parent, SWT.NULL);
414         GridData data = new GridData();
415         data.verticalAlignment = GridData.FILL;
416         data.horizontalAlignment = GridData.FILL;
417         container.setLayoutData(data);
418         GridLayout layout = new GridLayout();
419         layout.marginHeight = 0;
420         layout.marginWidth = 0;
421         container.setLayout(layout);
422         tabFolder = new TabFolder(container, SWT.TOP);
423         tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
424         tabFolder.addSelectionListener(new SelectionAdapter() {
425
426             public void widgetSelected(SelectionEvent e) {
427                 TabItem[] selection = tabFolder.getSelection();
428                 onTabChange(selection[0]);
429             }
430         });
431         applyToAll = new Button(container, SWT.CHECK);
432         applyToAll.setText(Messages.WelcomeCustomizationPreferencePage_applyToAll);
433         contentProvider = new TableContentProvider();
434         labelProvider = new TableLabelProvider();
435         if (isCustomizationMode()) {
436             Button serialize = new Button(container, SWT.PUSH);
437             serialize.setText(Messages.WelcomeCustomizationPreferencePage_serialize);
438             serialize.addSelectionListener(new SelectionAdapter() {
439
440                 public void widgetSelected(SelectionEvent e) {
441                     doSerializeState();
442                 }
443             });
444         }
445         extensionImage = ImageUtil.createImage("full/obj16/extension_obj.gif"); //$NON-NLS-1$
446
ihighImage = ImageUtil.createImage("full/obj16/ihigh_obj.gif"); //$NON-NLS-1$
447
ilowImage = ImageUtil.createImage("full/obj16/ilow_obj.gif"); //$NON-NLS-1$
448
inewImage = ImageUtil.createImage("full/obj16/inew_obj.gif"); //$NON-NLS-1$
449
icalloutImage = ImageUtil.createImage("full/obj16/icallout_obj.gif"); //$NON-NLS-1$
450
themeImage = ImageUtil.createImage("full/obj16/image_obj.gif"); //$NON-NLS-1$
451
addPages();
452         org.eclipse.jface.dialogs.Dialog.applyDialogFont(container);
453         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, "org.eclipse.ui.intro.universal.universalWelcomePreference"); //$NON-NLS-1$
454
return container;
455     }
456
457     private void doSerializeState() {
458         FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
459         fd.setText(Messages.WelcomeCustomizationPreferencePage_serializeTitle);
460         String JavaDoc fileName = fd.open();
461         if (fileName != null) {
462             try {
463                 FileWriter JavaDoc writer = new FileWriter JavaDoc(fileName);
464                 PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(writer);
465                 introData.write(pwriter);
466                 pwriter.close();
467             } catch (IOException JavaDoc e) {
468                 // show an error dialog in addition
469
Log.error("Error while saving the intro data file", e); //$NON-NLS-1$
470
}
471         }
472     }
473
474     private boolean isCustomizationMode() {
475         /*
476          * String[] args = Platform.getApplicationArgs(); for (int i = 0; i < args.length; i++) { if
477          * (args[i].equalsIgnoreCase("-welcomeCustomization")) //$NON-NLS-1$ return true; } return
478          * false;
479          */

480         return true;
481     }
482
483     public void init(IWorkbench workbench) {
484     }
485
486     private void addPages() {
487         loadData(false);
488         addHomePage();
489         createPageContainer();
490         addRootPages();
491         updateWidgetsFromData();
492         selectFirstPage();
493     }
494
495     private void addRootPages() {
496         addPage(IUniversalIntroConstants.ID_OVERVIEW);
497         addPage(IUniversalIntroConstants.ID_FIRSTSTEPS);
498         addPage(IUniversalIntroConstants.ID_TUTORIALS);
499         addPage(IUniversalIntroConstants.ID_SAMPLES);
500         addPage(IUniversalIntroConstants.ID_WHATSNEW);
501         addPage(IUniversalIntroConstants.ID_WEBRESOURCES);
502         addPage(IUniversalIntroConstants.ID_MIGRATE);
503     }
504
505     private void createPageContainer() {
506         pageContainer = new Composite(tabFolder, SWT.NULL);
507         GridLayout layout = new GridLayout();
508         //layout.horizontalSpacing = 10;
509
pageContainer.setLayout(layout);
510         layout.numColumns = 4;
511         //layout.makeColumnsEqualWidth = true;
512
Label label;
513         label = new Label(pageContainer, SWT.WRAP);
514         label.setText(Messages.WelcomeCustomizationPreferencePage_pageDesc);
515         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
516         gd.horizontalSpan = 4;
517         label.setLayoutData(gd);
518         label = new Label(pageContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
519         gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
520         gd.horizontalSpan = 4;
521         label.setLayoutData(gd);
522         label = new Label(pageContainer, SWT.NULL);
523         label.setText(Messages.WelcomeCustomizationPreferencePage_available);
524         gd = new GridData();
525         label.setLayoutData(gd);
526         label = new Label(pageContainer, SWT.SEPARATOR | SWT.VERTICAL);
527         gd = new GridData(GridData.VERTICAL_ALIGN_FILL);
528         gd.verticalSpan = 3;
529         gd.widthHint = 10;
530         label.setLayoutData(gd);
531         
532         label = new Label(pageContainer, SWT.NULL);
533         label.setText(Messages.WelcomeCustomizationPreferencePage_left);
534         label = new Label(pageContainer, SWT.NULL);
535         label.setText(Messages.WelcomeCustomizationPreferencePage_right);
536         available = createTableViewer(pageContainer, "hidden"); //$NON-NLS-1$
537
available.setComparator(new ViewerComparator());
538         gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
539         gd.verticalSpan = 2;
540         gd.widthHint = 100;
541         //gd.horizontalSpan = 2;
542
available.getControl().setLayoutData(gd);
543
544         topLeft = createTableViewer(pageContainer, "top-left"); //$NON-NLS-1$
545
gd = new GridData(GridData.FILL_BOTH);
546         gd.widthHint = 100;
547         topLeft.getControl().setLayoutData(gd);
548
549         topRight = createTableViewer(pageContainer, "top-right"); //$NON-NLS-1$
550
gd = new GridData(GridData.FILL_BOTH);
551         gd.widthHint = 100;
552         topRight.getControl().setLayoutData(gd);
553
554         //label = new Label(pageContainer, SWT.NULL);
555
//label.setText(Messages.WelcomeCustomizationPreferencePage_bottom);
556
bottomLeft = createTableViewer(pageContainer, "bottom-left"); //$NON-NLS-1$
557
gd = new GridData(GridData.FILL_BOTH);
558         gd.widthHint = 100;
559         bottomLeft.getControl().setLayoutData(gd);
560         
561         bottomRight = createTableViewer(pageContainer, "bottom-right"); //$NON-NLS-1$
562
gd = new GridData(GridData.FILL_BOTH);
563         gd.widthHint = 100;
564         bottomRight.getControl().setLayoutData(gd);
565     }
566
567     private void updatePageContainer(String JavaDoc pageId, PageData pd) {
568         if (pageId == null)
569             return;
570         refreshQuadrant(topLeft, pd, IUniversalIntroConstants.DIV_LAYOUT_TOP_LEFT);
571         refreshQuadrant(topRight, pd, IUniversalIntroConstants.DIV_LAYOUT_TOP_RIGHT);
572         refreshQuadrant(bottomLeft, pd, IUniversalIntroConstants.DIV_LAYOUT_BOTTOM_LEFT);
573         refreshQuadrant(bottomRight, pd, IUniversalIntroConstants.DIV_LAYOUT_BOTTOM_RIGHT);
574         refreshQuadrant(available, pd, IUniversalIntroConstants.HIDDEN);
575     }
576
577     private void refreshQuadrant(TableViewer viewer, PageData pd, String JavaDoc quadrant) {
578         GroupData gd = pd!=null?pd.findGroup(quadrant):null;
579         viewer.setInput(gd);
580         if (gd!=null)
581             updateColumnSizes(viewer);
582     }
583
584     private void onTabChange(TabItem item) {
585         String JavaDoc id = (String JavaDoc) item.getData();
586         if (item.getControl() == pageContainer)
587             updatePageContainer(id, (PageData) item.getData("pageData")); //$NON-NLS-1$
588
}
589
590     private void loadData(boolean fromDefault) {
591         IProduct product = Platform.getProduct();
592         if (product == null)
593             return;
594         String JavaDoc pid = product.getId();
595         introRootPages.clear();
596         Preferences prefs = UniversalIntroPlugin.getDefault().getPluginPreferences();
597         Preferences iprefs = IntroPlugin.getDefault().getPluginPreferences();
598         // 1. Root pages
599
// try product-qualified value first
600
String JavaDoc key = pid + "_" + INTRO_ROOT_PAGES; //$NON-NLS-1$
601
String JavaDoc rootPages = fromDefault ? prefs.getDefaultString(key) : prefs.getString(key);
602         if (rootPages.length() == 0) {
603             rootPages = fromDefault ? prefs.getDefaultString(INTRO_ROOT_PAGES) : prefs
604                     .getString(INTRO_ROOT_PAGES);
605         }
606         if (rootPages.length() > 0) {
607             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(rootPages, ","); //$NON-NLS-1$
608
while (stok.hasMoreTokens()) {
609                 String JavaDoc tok = stok.nextToken().trim();
610                 introRootPages.add(tok);
611             }
612         }
613         // 3. Active theme
614
key = pid + "_" + INTRO_THEME; //$NON-NLS-1$
615
String JavaDoc value = fromDefault ? iprefs.getDefaultString(key) : iprefs.getString(key);
616         if (value.length() == 0) {
617             key = INTRO_THEME;
618             value = fromDefault ? iprefs.getDefaultString(key) : iprefs.getString(key);
619         }
620         if (value.length() > 0)
621             introThemeId = value;
622         // 4. Intro data
623
key = pid + "_" + INTRO_DATA; //$NON-NLS-1$
624
value = fromDefault ? prefs.getDefaultString(key) : prefs.getString(key);
625         if (value.length() == 0) {
626             key = INTRO_DATA;
627             value = fromDefault ? prefs.getDefaultString(key) : prefs.getString(key);
628         }
629         if (value.length() == 0)
630             value = null;
631         if (value != null && value.startsWith("product:")) //$NON-NLS-1$
632
value = value.substring(8);
633         value = BundleUtil.getResolvedResourceLocation(value, product.getDefiningBundle());
634         introData = new IntroData(pid, value, true);
635         introData.addImplicitContent();
636     }
637
638     public void dispose() {
639         Iterator JavaDoc iter = themeList.iterator();
640         while (iter.hasNext()) {
641             ((IntroTheme)iter.next()).dispose();
642         }
643         extensionImage.dispose();
644         ihighImage.dispose();
645         ilowImage.dispose();
646         inewImage.dispose();
647         icalloutImage.dispose();
648         themeImage.dispose();
649         super.dispose();
650     }
651
652     private void updateIntroThemeFromData() {
653         if (introThemeId != null) {
654             for (int i = 0; i < themeList.size(); i++) {
655                 IntroTheme theme = (IntroTheme) themeList.get(i);
656                 if (theme.getId().equals(introThemeId)) {
657                     introTheme = theme;
658                     break;
659                 }
660             }
661         }
662         updateThemePreview();
663     }
664
665     private void updateWidgetsFromData() {
666         // sync up intro background part
667
updateIntroThemeFromData();
668         // sync up the root page checklist
669
rootPages.setInput(ROOT_PAGE_TABLE);
670         ArrayList JavaDoc selected = new ArrayList JavaDoc();
671         for (int i = 0; i < ROOT_PAGE_TABLE.length; i++) {
672             String JavaDoc id = ROOT_PAGE_TABLE[i].id;
673             if (introRootPages.contains(id))
674                 selected.add(ROOT_PAGE_TABLE[i]);
675         }
676         rootPages.setCheckedElements(selected.toArray());
677     }
678
679     private void updateThemePreview() {
680         themes.setInput(themes);
681         if (introTheme != null)
682             themes.setSelection(new StructuredSelection(introTheme), true);
683         themePreview.redraw();
684     }
685
686     public boolean performOk() {
687         saveData();
688         BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
689
690             public void run() {
691                 restartIntro();
692             }
693         });
694         return true;
695     }
696
697     /**
698      * Remember the current page, close intro, reopen it and show the saved page.
699      *
700      */

701     private void restartIntro() {
702         IIntroManager manager = PlatformUI.getWorkbench().getIntroManager();
703         IIntroPart part = manager.getIntro();
704         if (part != null && part instanceof CustomizableIntroPart) {
705             IntroModelRoot modelRoot = IntroPlugin.getDefault().getIntroModelRoot();
706             String JavaDoc currentPageId = modelRoot.getCurrentPageId();
707             IWorkbenchWindow window = part.getIntroSite().getWorkbenchWindow();
708             boolean standby = manager.isIntroStandby(part);
709             PlatformUI.getWorkbench().getIntroManager().closeIntro(part);
710             IntroPlugin.getDefault().resetVolatileImageRegistry();
711             UniversalIntroPlugin.getDefault().resetVolatileImageRegistry();
712             part = PlatformUI.getWorkbench().getIntroManager().showIntro(window, standby);
713             if (part != null) {
714                 StringBuffer JavaDoc url = new StringBuffer JavaDoc();
715                 url.append("http://org.eclipse.ui.intro/showPage?id="); //$NON-NLS-1$
716
url.append(currentPageId);
717                 IIntroURL introURL = IntroURLFactory.createIntroURL(url.toString());
718                 if (introURL != null)
719                     introURL.execute();
720             }
721         }
722     }
723
724     protected void performDefaults() {
725         loadData(true);
726         // Dispose all the root page tabs
727
TabItem[] items = tabFolder.getItems();
728         for (int i = 0; i < items.length; i++) {
729             if (items[i].getData("pageData") != null) //$NON-NLS-1$
730
items[i].dispose();
731         }
732         // Add them back in based on the checked state
733
addRootPages();
734         updateWidgetsFromData();
735         // Get the items again
736
items = tabFolder.getItems();
737         // Select root
738
onTabChange(items[0]);
739         super.performDefaults();
740     }
741
742     private void saveData() {
743         Preferences prefs = UniversalIntroPlugin.getDefault().getPluginPreferences();
744         Preferences iprefs = IntroPlugin.getDefault().getPluginPreferences();
745         boolean toAll = applyToAll.getSelection();
746         IProduct product = Platform.getProduct();
747         if (product == null)
748             return;
749         String JavaDoc pid = product.getId();
750         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
751         for (int i = 0; i < introRootPages.size(); i++) {
752             if (i > 0)
753                 sbuf.append(","); //$NON-NLS-1$
754
sbuf.append((String JavaDoc) introRootPages.get(i));
755         }
756         String JavaDoc key = pid + "_" + INTRO_ROOT_PAGES; //$NON-NLS-1$
757
prefs.setValue(key, sbuf.toString());
758         if (toAll) {
759             key = INTRO_ROOT_PAGES;
760             prefs.setValue(key, sbuf.toString());
761         }
762         // store page layouts
763
StringWriter JavaDoc writer = new StringWriter JavaDoc();
764         PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(writer);
765         introData.write(pwriter);
766         pwriter.close();
767         String JavaDoc value = writer.toString();
768         key = pid + "_" + INTRO_DATA; //$NON-NLS-1$
769
prefs.setValue(key, value);
770         if (toAll) {
771             key = INTRO_DATA;
772             prefs.setValue(key, value);
773         }
774         if (introTheme != null) {
775             key = pid + "_" + INTRO_THEME; //$NON-NLS-1$
776
value = introTheme.getId();
777             iprefs.setValue(key, value);
778         }
779         if (toAll) {
780             key = INTRO_THEME;
781             iprefs.setValue(key, value);
782         }
783         UniversalIntroPlugin.getDefault().savePluginPreferences();
784         IntroPlugin.getDefault().savePluginPreferences();
785     }
786
787     private void addHomePage() {
788         TabItem item = new TabItem(tabFolder, SWT.NULL);
789         item.setText(Messages.WelcomeCustomizationPreferencePage_home);
790         Composite container = new Composite(tabFolder, SWT.NULL);
791         GridLayout layout = new GridLayout();
792         layout.numColumns = 2;
793         container.setLayout(layout);
794         Composite leftColumn = new Composite(container, SWT.NULL);
795         layout = new GridLayout();
796         layout.marginWidth = layout.marginHeight = 0;
797         leftColumn.setLayout(layout);
798         leftColumn.setLayoutData(new GridData(GridData.FILL_BOTH));
799         Composite rightColumn = new Composite(container, SWT.NULL);
800         layout = new GridLayout();
801         layout.marginWidth = layout.marginHeight = 0;
802         rightColumn.setLayout(layout);
803         rightColumn.setLayoutData(new GridData(GridData.FILL_BOTH));
804
805         Label themeLabel = new Label(leftColumn, SWT.NULL);
806         themeLabel.setText(Messages.WelcomeCustomizationPreferencePage_background);
807         GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
808         themeLabel.setLayoutData(gd);
809         themes = new TableViewer(leftColumn, SWT.BORDER);
810         themes.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
811         themes.setContentProvider(contentProvider);
812         themes.setLabelProvider(labelProvider);
813         themes.addSelectionChangedListener(new ISelectionChangedListener() {
814             public void selectionChanged(SelectionChangedEvent e) {
815                 Object JavaDoc sel = ((StructuredSelection) e.getSelection()).getFirstElement();
816                 introTheme = (IntroTheme) sel;
817                 themePreview.redraw();
818             }
819         });
820         loadThemes();
821         Label previewLabel = new Label(rightColumn, SWT.NULL);
822         previewLabel.setText(Messages.WelcomeCustomizationPreferencePage_preview);
823         themePreview = new Canvas(rightColumn, SWT.NULL);
824         gd = new GridData();
825         gd.widthHint = 160+20;
826         gd.heightHint = 120+20;
827         themePreview.setLayoutData(gd);
828         themePreview.addPaintListener(new PaintListener() {
829
830             public void paintControl(PaintEvent e) {
831                 if (introTheme == null)
832                     return;
833                 Image bgImage = introTheme.getPreviewImage();
834                 if (bgImage == null)
835                     return;
836                 //Rectangle carea = themePreview.getClientArea();
837
Rectangle ibounds = bgImage.getBounds();
838                 e.gc.drawImage(bgImage, 0, 0, ibounds.width, ibounds.height, 10, 10, 160, 120);
839             }
840         });
841         Label label = new Label(container, SWT.NULL);
842         label.setText(Messages.WelcomeCustomizationPreferencePage_rootpages);
843         gd = new GridData();
844         gd.horizontalSpan = 2;
845         label.setLayoutData(gd);
846         rootPages = CheckboxTableViewer.newCheckList(container, SWT.BORDER);
847         rootPages.setContentProvider(contentProvider);
848         rootPages.setLabelProvider(labelProvider);
849         gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
850         gd.horizontalSpan = 2;
851         rootPages.getControl().setLayoutData(gd);
852         rootPages.addCheckStateListener(new ICheckStateListener() {
853
854             public void checkStateChanged(CheckStateChangedEvent event) {
855                 RootPage page = (RootPage) event.getElement();
856                 boolean checked = event.getChecked();
857                 onPageChecked(page.id, checked);
858             }
859         });
860         item.setControl(container);
861     }
862     
863     private void loadThemes() {
864         IConfigurationElement [] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.intro.configExtension"); //$NON-NLS-1$
865
for (int i=0; i<elements.length; i++) {
866             if (elements[i].getName().equals("theme")) { //$NON-NLS-1$
867
themeList.add(new IntroTheme(elements[i]));
868             }
869         }
870     }
871
872     private void addPage(String JavaDoc id) {
873         if (!getRootPageSelected(id))
874             return;
875         TabItem item = new TabItem(tabFolder, SWT.NULL);
876         item.setText(getRootPageName(id));
877         item.setControl(pageContainer);
878         item.setData(id);
879         PageData pd = introData.getPage(id);
880         item.setData("pageData", pd); //$NON-NLS-1$
881
}
882
883     private void onPageChecked(String JavaDoc id, boolean checked) {
884         TabItem[] items = tabFolder.getItems();
885         if (checked) {
886             for (int i = 0; i < items.length; i++) {
887                 TabItem item = items[i];
888                 if (item.getData() != null)
889                     item.dispose();
890             }
891             introRootPages.add(id);
892             addRootPages();
893         } else {
894             for (int i = 0; i < items.length; i++) {
895                 TabItem item = items[i];
896                 String JavaDoc itemId = (String JavaDoc) item.getData();
897                 if (itemId != null && itemId.equals(id)) {
898                     item.dispose();
899                     introRootPages.remove(id);
900                     return;
901                 }
902             }
903         }
904     }
905
906     private String JavaDoc getRootPageName(String JavaDoc id) {
907         for (int i = 0; i < ROOT_PAGE_TABLE.length; i++) {
908             if (ROOT_PAGE_TABLE[i].id.equals(id))
909                 return ROOT_PAGE_TABLE[i].getName();
910         }
911         return "?"; //$NON-NLS-1$
912
}
913
914     private boolean getRootPageSelected(String JavaDoc id) {
915         for (int i = 0; i < introRootPages.size(); i++) {
916             String JavaDoc cid = (String JavaDoc) introRootPages.get(i);
917             if (cid.equals(id))
918                 return true;
919         }
920         return false;
921     }
922
923     public void setInitializationData(IConfigurationElement config, String JavaDoc propertyName, Object JavaDoc data)
924             throws CoreException {
925     }
926
927     private void createPopupMenu(final TableViewer viewer) {
928         MenuManager manager = new MenuManager();
929         manager.createContextMenu(viewer.getControl());
930         viewer.getControl().setMenu(manager.getMenu());
931         manager.setRemoveAllWhenShown(true);
932         manager.addMenuListener(new IMenuListener() {
933
934             public void menuAboutToShow(IMenuManager manager) {
935                 fillPopupMenu(manager, viewer);
936             }
937         });
938     }
939
940     private void addDNDSupport(TableViewer viewer) {
941         viewer.addDragSupport(DND.DROP_MOVE, TRANSFER_TYPES, new TableDragSourceListener(viewer));
942         viewer.addDropSupport(DND.DROP_MOVE, TRANSFER_TYPES, new TableDropTargetListener(viewer));
943     }
944
945     private TableViewer createTableViewer(Composite parent, String JavaDoc id) {
946         final Table table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
947         final TableColumn column1 = new TableColumn(table, SWT.NULL);
948         column1.setMoveable(false);
949         column1.setWidth(20);
950         column1.setResizable(false);
951         final TableColumn column2 = new TableColumn(table, SWT.NULL);
952         column2.setResizable(true);
953         final TableViewer viewer = new TableViewer(table);
954         CellEditor[] editors = new CellEditor[2];
955         editors[0] = new ComboBoxCellEditor(table, ExtensionData.IMPORTANCE_NAME_TABLE, SWT.READ_ONLY);
956         viewer.setCellEditors(editors);
957         viewer.setColumnProperties(new String JavaDoc[] { IUniversalIntroConstants.P_IMPORTANCE,
958                 IUniversalIntroConstants.P_NAME });
959         viewer.setCellModifier(new ICellModifier() {
960
961             public boolean canModify(Object JavaDoc element, String JavaDoc property) {
962                 return property.equals(IUniversalIntroConstants.P_IMPORTANCE);
963             }
964
965             public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
966                 if (element instanceof ExtensionData) {
967                     ExtensionData ed = (ExtensionData) element;
968                     if (property.equals(IUniversalIntroConstants.P_IMPORTANCE))
969                         return new Integer JavaDoc(ed.getImportance());
970                 }
971                 return null;
972             }
973
974             public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value) {
975                 Integer JavaDoc ivalue = (Integer JavaDoc) value;
976                 TableItem item = (TableItem) element;
977                 ExtensionData ed = (ExtensionData) item.getData();
978                 ed.setImportance(ivalue.intValue());
979                 viewer.update(ed, new String JavaDoc [] {IUniversalIntroConstants.P_IMPORTANCE});
980             }
981         });
982         viewer.setContentProvider(contentProvider);
983         viewer.setLabelProvider(labelProvider);
984         viewer.setData("id", id); //$NON-NLS-1$
985
createPopupMenu(viewer);
986         addDNDSupport(viewer);
987         return viewer;
988     }
989
990     private void fillPopupMenu(IMenuManager manager, final TableViewer viewer) {
991         StructuredSelection ssel = (StructuredSelection) viewer.getSelection();
992         
993         manager.add(new Separator());
994         Action addSeparator = new Action(Messages.WelcomeCustomizationPreferencePage_addSeparator) {
995             public void run() {
996                 doAddSeparator(viewer);
997             }
998         };
999         
1000        manager.add(addSeparator);
1001        manager.add(new Separator());
1002
1003        if (ssel.size() == 1 && viewer != available) {
1004            Action upAction = new Action(Messages.WelcomeCustomizationPreferencePage_up) {
1005
1006                public void run() {
1007                    doMove(viewer, true);
1008                }
1009            };
1010            Action downAction = new Action(Messages.WelcomeCustomizationPreferencePage_down) {
1011
1012                public void run() {
1013                    doMove(viewer, false);
1014                }
1015            };
1016            BaseData ed = (BaseData) ssel.getFirstElement();
1017            GroupData gd = (GroupData) viewer.getInput();
1018            upAction.setEnabled(gd.canMoveUp(ed));
1019            downAction.setEnabled(gd.canMoveDown(ed));
1020            manager.add(upAction);
1021            manager.add(downAction);
1022        }
1023        if (ssel.size() > 0) {
1024            manager.add(new Separator());
1025            MenuManager menu = new MenuManager(Messages.WelcomeCustomizationPreferencePage_moveTo);
1026            addMoveToAction(menu, available, viewer,
1027                    Messages.WelcomeCustomizationPreferencePage_menu_available);
1028            addMoveToAction(menu, topLeft, viewer, Messages.WelcomeCustomizationPreferencePage_menu_top_left);
1029            addMoveToAction(menu, topRight, viewer, Messages.WelcomeCustomizationPreferencePage_menu_top_right);
1030            addMoveToAction(menu, bottomLeft, viewer, Messages.WelcomeCustomizationPreferencePage_menu_bottom_left);
1031            addMoveToAction(menu, bottomRight, viewer, Messages.WelcomeCustomizationPreferencePage_menu_bottom_right);
1032            manager.add(menu);
1033            
1034            boolean addDeleteSeparator=false;
1035            
1036            for (Iterator JavaDoc iter=ssel.iterator(); iter.hasNext();) {
1037                Object JavaDoc obj = iter.next();
1038                if (obj instanceof SeparatorData)
1039                    addDeleteSeparator=true;
1040                else {
1041                    addDeleteSeparator=false;
1042                    break;
1043                }
1044            }
1045            if (addDeleteSeparator) {
1046                Action deleteSeparator = new Action(Messages.WelcomeCustomizationPreferencePage_removeSeparator) {
1047                    public void run() {
1048                        doRemoveSeparators(viewer);
1049                    }
1050                };
1051                manager.add(deleteSeparator);
1052            }
1053        }
1054    }
1055
1056    private void addMoveToAction(MenuManager menu, final TableViewer target, final TableViewer source,
1057            String JavaDoc name) {
1058        if (source == target)
1059            return;
1060        Action action = new Action(name) {
1061
1062            public void run() {
1063                doMoveTo(source, target);
1064            }
1065        };
1066        menu.add(action);
1067    }
1068
1069    private void doMove(Viewer viewer, boolean up) {
1070        Object JavaDoc obj = ((StructuredSelection) viewer.getSelection()).getFirstElement();
1071        GroupData gd = (GroupData) viewer.getInput();
1072        if (up)
1073            gd.moveUp((BaseData) obj);
1074        else
1075            gd.moveDown((BaseData) obj);
1076        viewer.refresh();
1077    }
1078    
1079    private void doAddSeparator(Viewer viewer) {
1080        Object JavaDoc obj = ((StructuredSelection) viewer.getSelection()).getFirstElement();
1081        GroupData gd = (GroupData) viewer.getInput();
1082        if (gd == null) {
1083            gd = createTargetGd(viewer);
1084            viewer.setInput(gd);
1085        }
1086        gd.addSeparator((BaseData)obj);
1087        viewer.refresh();
1088        updateColumnSizes((TableViewer)viewer);
1089    }
1090
1091    private void doRemoveSeparators(Viewer viewer) {
1092        StructuredSelection ssel = ((StructuredSelection) viewer.getSelection());
1093        GroupData gd = (GroupData) viewer.getInput();
1094        for (Iterator JavaDoc iter=ssel.iterator(); iter.hasNext();) {
1095            SeparatorData sdata = (SeparatorData)iter.next();
1096            gd.remove(sdata);
1097        }
1098        viewer.refresh();
1099    }
1100
1101    private void doMoveTo(TableViewer source, TableViewer target) {
1102        Object JavaDoc[] selObjs = ((StructuredSelection) source.getSelection()).toArray();
1103        GroupData sourceGd = (GroupData) source.getInput();
1104        GroupData targetGd = (GroupData) target.getInput();
1105        if (targetGd == null) {
1106            targetGd = createTargetGd(target);
1107        }
1108        for (int i = 0; i < selObjs.length; i++) {
1109            BaseData ed = (BaseData) selObjs[i];
1110            sourceGd.remove(ed);
1111            targetGd.add(ed);
1112        }
1113        source.refresh();
1114        updateColumnSizes(source);
1115        if (target.getInput() != null)
1116            target.refresh();
1117        else
1118            target.setInput(targetGd);
1119        updateColumnSizes(target);
1120    }
1121
1122    private void updateColumnSizes(TableViewer viewer) {
1123        TableColumn sc = viewer.getTable().getColumn(1);
1124        sc.pack();
1125    }
1126
1127    private GroupData createTargetGd(Viewer target) {
1128        GroupData targetGd = null;
1129        if (target == topLeft)
1130            targetGd = new GroupData(PageData.P_TOP_LEFT, false);
1131        else if (target == topRight)
1132            targetGd = new GroupData(PageData.P_TOP_RIGHT, false);
1133        else if (target == bottomLeft)
1134            targetGd = new GroupData(PageData.P_BOTTOM_LEFT, false);
1135        else if (target == bottomRight)
1136            targetGd = new GroupData(PageData.P_BOTTOM_RIGHT, false);
1137        else if (target == available)
1138            targetGd = new GroupData(IUniversalIntroConstants.HIDDEN, false);
1139        else
1140            return null;
1141        TabItem[] items = tabFolder.getSelection();
1142        PageData pd = (PageData) items[0].getData("pageData"); //$NON-NLS-1$
1143
if (pd == null) {
1144            String JavaDoc pageId = (String JavaDoc)items[0].getData();
1145            pd = new PageData(pageId);
1146            items[0].setData("pageData", pd); //$NON-NLS-1$
1147
introRootPages.add(pageId);
1148        }
1149        pd.add(targetGd);
1150        return targetGd;
1151    }
1152
1153    public void setCurrentPage(String JavaDoc pageId) {
1154        firstPageId = pageId;
1155    }
1156
1157    private void selectFirstPage() {
1158        if (firstPageId == null)
1159            return;
1160        TabItem[] items = tabFolder.getItems();
1161        for (int i = 0; i < items.length; i++) {
1162            TabItem item = items[i];
1163            PageData pd = (PageData) item.getData("pageData"); //$NON-NLS-1$
1164
if (pd != null && pd.getId().equals(firstPageId)) {
1165                tabFolder.setSelection(i);
1166                onTabChange(item);
1167                return;
1168            }
1169        }
1170    }
1171}
1172
Popular Tags