initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiOutlinePage.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiOutlinePage.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiOutlinePage.java
new file mode 100644 (file)
index 0000000..92f74e1
--- /dev/null
@@ -0,0 +1,143 @@
+/***********************************************************************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors: IBM Corporation - initial API and implementation
+ **********************************************************************************************************************************/
+package net.sourceforge.phpeclipse.wiki.editor;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
+import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
+
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Composite;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.AbstractTreeViewer;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.viewers.Viewer;
+
+import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
+
+public class WikiOutlinePage extends ContentOutlinePage {
+
+  private WikiEditor fEditor;
+
+  private static class WikiContentProvider implements ITreeContentProvider {
+
+    public Object[] getChildren(Object parentElement) {
+      return ((WikipediaSection) parentElement).getChildren();
+    }
+
+    public Object getParent(Object element) {
+      return ((WikipediaSection) element).getParent();
+    }
+
+    public boolean hasChildren(Object element) {
+      Object[] children = getChildren(element);
+      return children != null && children.length != 0;
+    }
+
+    public Object[] getElements(Object inputElement) {
+      return getChildren(inputElement);
+    }
+
+    public void dispose() {
+      // do nothing
+    }
+
+    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+      // do nothing
+    }
+  }
+
+  private static class WikiLabelProvider extends LabelProvider {
+    private static URL fgIconBaseURL = WikiEditorPlugin.getDefault().getBundle().getEntry("/icons/");
+
+    private final Image fIngredientsSectionIcon = createImage("ingredients.gif");
+
+    private final Image fIngredientIcon = createImage("ingredient.gif");
+
+    private final Image fPreparationSectionIcon = createImage("preparation.gif");
+
+    private final Image fStepIcon = createImage("step.gif");
+
+    public static Image createImage(String icon) {
+      try {
+        ImageDescriptor id = ImageDescriptor.createFromURL(new URL(fgIconBaseURL, icon));
+        return id.createImage();
+      } catch (MalformedURLException e) {
+        // no icon ...
+      }
+      return null;
+    }
+
+    public String getText(Object element) {
+      return ((WikipediaSection) element).getName();
+    }
+
+    public Image getImage(Object element) {
+      if (element instanceof WikipediaSection)
+        return fStepIcon;
+      return super.getImage(element);
+    }
+
+    public void dispose() {
+      super.dispose();
+      if (fIngredientsSectionIcon != null)
+        fIngredientsSectionIcon.dispose();
+      if (fIngredientIcon != null)
+        fIngredientIcon.dispose();
+      if (fPreparationSectionIcon != null)
+        fPreparationSectionIcon.dispose();
+      if (fStepIcon != null)
+        fStepIcon.dispose();
+    }
+  }
+
+  public WikiOutlinePage(WikiEditor editor) {
+    fEditor = editor;
+  }
+
+  public void createControl(Composite parent) {
+    super.createControl(parent);
+    TreeViewer treeViewer = getTreeViewer();
+    treeViewer.setLabelProvider(new WikiLabelProvider());
+    treeViewer.setContentProvider(new WikiContentProvider());
+    treeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
+    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
+      public void selectionChanged(SelectionChangedEvent event) {
+        if (!(event.getSelection() instanceof IStructuredSelection))
+          return;
+        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
+        if (selection.size() != 1)
+          return;
+        Object element = selection.getFirstElement();
+        if (!(element instanceof WikipediaSection))
+          return;
+        WikipediaSection recipeElement = (WikipediaSection) element;
+        fEditor.selectAndReveal(recipeElement.getOffset(), recipeElement.getLength());
+      }
+    });
+    setWiki(fEditor.getSection());
+  }
+
+  public void setWiki(WikipediaSection section) {
+    getTreeViewer().setInput(section);
+  }
+
+  public void dispose() {
+    super.dispose();
+    fEditor.outlinePageClosed();
+    fEditor = null;
+  }
+}
\ No newline at end of file