Java tutorial
/******************************************************************************* * Copyright (c) 2008-09 Phil Zoio and Ric Wright * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/org/documents/epl-v10.html * * This code is in part derived from Eclipse wizard code, but also originally * written by Phil Zoio as detailed in the article: * http://www.realsolve.co.uk/site/tech/jface-text.php * * Contributors: * Phil Zoio - 2004 - Original implementation * Ric Wright - 2008-2009 - Bug fixes and tweaks * ********************************************************************************/ package com.geofx.xmleditor.outline; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.views.contentoutline.ContentOutlinePage; import com.geofx.xmleditor.xml.XMLElement; /** * */ public class EditorContentOutlinePage extends ContentOutlinePage { private ITextEditor editor; private IEditorInput input; private OutlineContentProvider outlineContentProvider; private OutlineLabelProvider outlineLabelProvider; public EditorContentOutlinePage(ITextEditor editor) { super(); this.editor = editor; } public void createControl(Composite parent) { super.createControl(parent); TreeViewer viewer = getTreeViewer(); outlineContentProvider = new OutlineContentProvider(editor.getDocumentProvider()); viewer.setContentProvider(outlineContentProvider); outlineLabelProvider = new OutlineLabelProvider(); viewer.setLabelProvider(outlineLabelProvider); viewer.addSelectionChangedListener(this); //control is created after input is set if (input != null) viewer.setInput(input); } /** * Sets the input of the outline page */ public void setInput(Object input) { this.input = (IEditorInput) input; update(); } /* * Change in selection */ public void selectionChanged(SelectionChangedEvent event) { super.selectionChanged(event); //find out which item in tree viewer we have selected, and set highlight range accordingly ISelection selection = event.getSelection(); if (selection.isEmpty()) editor.resetHighlightRange(); else { XMLElement element = (XMLElement) ((IStructuredSelection) selection).getFirstElement(); int start = element.getPosition().getOffset(); int length = element.getPosition().getLength(); try { editor.setHighlightRange(start, length, true); } catch (IllegalArgumentException x) { editor.resetHighlightRange(); } } } /** * The editor is saved, so we should refresh representation * * @param tableNamePositions */ public void update() { //set the input so that the outlines parse can be called //update the tree viewer state TreeViewer viewer = getTreeViewer(); if (viewer != null) { Control control = viewer.getControl(); if (control != null && !control.isDisposed()) { control.setRedraw(false); viewer.setInput(input); viewer.expandAll(); control.setRedraw(true); } } } }