initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiOutlinePage.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.wiki.editor;
9
10 import java.net.MalformedURLException;
11 import java.net.URL;
12
13 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
14 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
15
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Composite;
18
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.AbstractTreeViewer;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.LabelProvider;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.viewers.Viewer;
28
29 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
30
31 public class WikiOutlinePage extends ContentOutlinePage {
32
33   private WikiEditor fEditor;
34
35   private static class WikiContentProvider implements ITreeContentProvider {
36
37     public Object[] getChildren(Object parentElement) {
38       return ((WikipediaSection) parentElement).getChildren();
39     }
40
41     public Object getParent(Object element) {
42       return ((WikipediaSection) element).getParent();
43     }
44
45     public boolean hasChildren(Object element) {
46       Object[] children = getChildren(element);
47       return children != null && children.length != 0;
48     }
49
50     public Object[] getElements(Object inputElement) {
51       return getChildren(inputElement);
52     }
53
54     public void dispose() {
55       // do nothing
56     }
57
58     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
59       // do nothing
60     }
61   }
62
63   private static class WikiLabelProvider extends LabelProvider {
64     private static URL fgIconBaseURL = WikiEditorPlugin.getDefault().getBundle().getEntry("/icons/");
65
66     private final Image fIngredientsSectionIcon = createImage("ingredients.gif");
67
68     private final Image fIngredientIcon = createImage("ingredient.gif");
69
70     private final Image fPreparationSectionIcon = createImage("preparation.gif");
71
72     private final Image fStepIcon = createImage("step.gif");
73
74     public static Image createImage(String icon) {
75       try {
76         ImageDescriptor id = ImageDescriptor.createFromURL(new URL(fgIconBaseURL, icon));
77         return id.createImage();
78       } catch (MalformedURLException e) {
79         // no icon ...
80       }
81       return null;
82     }
83
84     public String getText(Object element) {
85       return ((WikipediaSection) element).getName();
86     }
87
88     public Image getImage(Object element) {
89       if (element instanceof WikipediaSection)
90         return fStepIcon;
91       return super.getImage(element);
92     }
93
94     public void dispose() {
95       super.dispose();
96       if (fIngredientsSectionIcon != null)
97         fIngredientsSectionIcon.dispose();
98       if (fIngredientIcon != null)
99         fIngredientIcon.dispose();
100       if (fPreparationSectionIcon != null)
101         fPreparationSectionIcon.dispose();
102       if (fStepIcon != null)
103         fStepIcon.dispose();
104     }
105   }
106
107   public WikiOutlinePage(WikiEditor editor) {
108     fEditor = editor;
109   }
110
111   public void createControl(Composite parent) {
112     super.createControl(parent);
113     TreeViewer treeViewer = getTreeViewer();
114     treeViewer.setLabelProvider(new WikiLabelProvider());
115     treeViewer.setContentProvider(new WikiContentProvider());
116     treeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
117     treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
118       public void selectionChanged(SelectionChangedEvent event) {
119         if (!(event.getSelection() instanceof IStructuredSelection))
120           return;
121         IStructuredSelection selection = (IStructuredSelection) event.getSelection();
122         if (selection.size() != 1)
123           return;
124         Object element = selection.getFirstElement();
125         if (!(element instanceof WikipediaSection))
126           return;
127         WikipediaSection recipeElement = (WikipediaSection) element;
128         fEditor.selectAndReveal(recipeElement.getOffset(), recipeElement.getLength());
129       }
130     });
131     setWiki(fEditor.getSection());
132   }
133
134   public void setWiki(WikipediaSection section) {
135     getTreeViewer().setInput(section);
136   }
137
138   public void dispose() {
139     super.dispose();
140     fEditor.outlinePageClosed();
141     fEditor = null;
142   }
143 }