package tool.editors;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.custom.CaretEvent;
import org.eclipse.swt.custom.CaretListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
import tool.ToolEditorActivator;
public class ToolContentOutlinePage extends ContentOutlinePage implements CaretListener {
/**
* A segment element.
*/
protected static class Segment {
public String name;
public Position position;
public String iconString;
public Segment(String name, Position position, String iconString) {
this .name = name;
this .position = position;
this .iconString = iconString;
}
public String toString() {
return name;
}
}
/**
* Divides the editor's document into ten segments and provides elements for them.
*/
protected Object fInput;
protected IDocumentProvider fDocumentProvider;
protected ITextEditor fTextEditor;
protected ToolEditorContentProvider contentProvider;
/**
* Creates a content outline page using the given provider and the given editor.
*
* @param provider the document provider
* @param editor the editor
*/
public ToolContentOutlinePage(IDocumentProvider provider,
ITextEditor editor) {
super ();
fDocumentProvider = provider;
fTextEditor = editor;
}
/* (non-Javadoc)
* Method declared on ContentOutlinePage
*/
public void createControl(Composite parent) {
super .createControl(parent);
TreeViewer viewer = getTreeViewer();
contentProvider = new ToolEditorContentProvider(fDocumentProvider);
viewer.setContentProvider(contentProvider);
viewer.setLabelProvider(new ILabelProvider() {
@Override
public void removeListener(ILabelProviderListener arg0) {
}
@Override
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
@Override
public void dispose() {
}
@Override
public void addListener(ILabelProviderListener arg0) {
}
@Override
public String getText(Object element) {
if (element instanceof Segment){
return element.toString();
}
return null;
}
@Override
public Image getImage(Object element) {
if (element instanceof Segment){
Image image = ToolEditorActivator.getImage(((Segment)element).iconString);
return image;
}
return null;
}
});
viewer.addSelectionChangedListener(this );
if (fInput != null){
viewer.setInput(fInput);
contentProvider.setfInput(fInput);
}
}
/* (non-Javadoc)
* Method declared on ContentOutlinePage
*/
public void selectionChanged(SelectionChangedEvent event) {
super .selectionChanged(event);
ISelection selection = event.getSelection();
if (selection.isEmpty())
fTextEditor.resetHighlightRange();
else {
Segment segment = (Segment) ((IStructuredSelection) selection).getFirstElement();
int start = segment.position.getOffset();
int length = segment.position.getLength();
try {
fTextEditor.setHighlightRange(start, length, true);
} catch (IllegalArgumentException x) {
fTextEditor.resetHighlightRange();
}
}
}
/**
* Sets the input of the outline page
*
* @param input the input of this outline page
*/
public void setInput(Object input) {
fInput = input;
if (contentProvider != null)
contentProvider.setfInput(input);
update();
}
/**
* Updates the outline page.
*/
public void update() {
TreeViewer viewer = getTreeViewer();
if (viewer != null) {
Control control = viewer.getControl();
if (control != null && !control.isDisposed()) {
control.setRedraw(false);
viewer.setInput(fInput);
viewer.expandAll();
control.setRedraw(true);
}
}
}
@Override
public void caretMoved(CaretEvent event) {
try {
ITypedRegion region = fDocumentProvider.getDocument(fInput).getPartition(event.caretOffset);
if (getTreeViewer()!=null) {
getTreeViewer().setSelection(new StructuredSelection(region));
getTreeViewer().reveal(region);
getTreeViewer().expandToLevel(region, TreeViewer.ALL_LEVELS);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}