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
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.wiki.editor;
10 import java.net.MalformedURLException;
13 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
14 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Composite;
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;
29 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
31 public class WikiOutlinePage extends ContentOutlinePage {
33 private WikiEditor fEditor;
35 private static class WikiContentProvider implements ITreeContentProvider {
37 public Object[] getChildren(Object parentElement) {
38 return ((WikipediaSection) parentElement).getChildren();
41 public Object getParent(Object element) {
42 return ((WikipediaSection) element).getParent();
45 public boolean hasChildren(Object element) {
46 Object[] children = getChildren(element);
47 return children != null && children.length != 0;
50 public Object[] getElements(Object inputElement) {
51 return getChildren(inputElement);
54 public void dispose() {
58 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
63 private static class WikiLabelProvider extends LabelProvider {
64 private static URL fgIconBaseURL = WikiEditorPlugin.getDefault().getBundle().getEntry("/icons/");
66 private final Image fIngredientsSectionIcon = createImage("ingredients.gif");
68 private final Image fIngredientIcon = createImage("ingredient.gif");
70 private final Image fPreparationSectionIcon = createImage("preparation.gif");
72 private final Image fStepIcon = createImage("step.gif");
74 public static Image createImage(String icon) {
76 ImageDescriptor id = ImageDescriptor.createFromURL(new URL(fgIconBaseURL, icon));
77 return id.createImage();
78 } catch (MalformedURLException e) {
84 public String getText(Object element) {
85 return ((WikipediaSection) element).getName();
88 public Image getImage(Object element) {
89 if (element instanceof WikipediaSection)
91 return super.getImage(element);
94 public void 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)
107 public WikiOutlinePage(WikiEditor editor) {
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))
121 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
122 if (selection.size() != 1)
124 Object element = selection.getFirstElement();
125 if (!(element instanceof WikipediaSection))
127 WikipediaSection recipeElement = (WikipediaSection) element;
128 fEditor.selectAndReveal(recipeElement.getOffset(), recipeElement.getLength());
131 setWiki(fEditor.getSection());
134 public void setWiki(WikipediaSection section) {
135 getTreeViewer().setInput(section);
138 public void dispose() {
140 fEditor.outlinePageClosed();