package tool.editors;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.text.IRegion;
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.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
import tool.ToolPlugin;
import tool.model.ToolClass;
import tool.model.ToolConstant;
import tool.model.ToolEventHandler;
import tool.model.ToolMethod;
public class ToolMethodContentOutlinePage extends ContentOutlinePage {
/**
* 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 class ContentProvider implements ITreeContentProvider {
protected final static String SEGMENTS = "__method_segments"; //$NON-NLS-1$
protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(
SEGMENTS);
protected List<Segment> fContent = new ArrayList<Segment>(10);
protected void parse(IDocument document) {
try {
FindReplaceDocumentAdapter frAdapter = new FindReplaceDocumentAdapter(document);
// do the methods
IRegion region = frAdapter.find(0, ToolMethod.CEX_SIGNATURE_REGEX, true, false, false, true);
String methodSignature = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
methodSignature = document.get(region.getOffset(), region.getLength());
Matcher sig = ToolMethod.cexSignaturePattern.matcher(methodSignature);
if (sig.find()){
methodSignature = sig.group(1);
methodSignature = methodSignature.replaceAll("\\s","");
methodSignature = methodSignature.substring(methodSignature.indexOf(".")+1);
fContent.add(new Segment(methodSignature, p, "icons/private_method.gif"));
}
region = frAdapter.find(offset+length, ToolMethod.CEX_SIGNATURE_REGEX, true, false, false, true);
}
// do the event handlers
region = frAdapter.find(0, ToolEventHandler.CEX_SIGNATURE_REGEX, true, false, false, true);
String ehSignature = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
ehSignature = document.get(region.getOffset(), region.getLength());
Matcher sig = ToolEventHandler.cexSignaturePattern.matcher(ehSignature);
if (sig.find()){
ehSignature = sig.group(1);
ehSignature = ehSignature.replaceAll("\\s","");
ehSignature = ehSignature.substring(ehSignature.indexOf(".")+1);
fContent.add(new Segment(ehSignature, p, "icons/private_event_handler.gif"));
}
region = frAdapter.find(offset+length, ToolEventHandler.CEX_SIGNATURE_REGEX, true, false, false, true);
}
// do constants
region = frAdapter.find(0, ToolConstant.PLAN_REGEX, true, false, false, true);
String constText = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
constText = document.get(region.getOffset(), region.getLength());
fContent.add(new Segment(constText, p, "icons/private_constant.gif"));
region = frAdapter.find(offset+length, ToolConstant.PLAN_REGEX, true, false, false, true);
}
// do classes
region = frAdapter.find(0, ToolClass.CLASS_REGEX, true, false, false, true);
String classText = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
classText = document.get(region.getOffset(), region.getLength());
fContent.add(new Segment(classText, p, "icons/class.gif"));
region = frAdapter.find(offset+length, ToolClass.CLASS_REGEX, true, false, false, true);
}
} catch (BadLocationException e) {
} catch (BadPositionCategoryException x) {
}
}
/*
* @see IContentProvider#inputChanged(Viewer, Object, Object)
*/
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
if (oldInput != null) {
IDocument document = fDocumentProvider
.getDocument(oldInput);
if (document != null) {
try {
document.removePositionCategory(SEGMENTS);
} catch (BadPositionCategoryException x) {
}
document.removePositionUpdater(fPositionUpdater);
}
}
fContent.clear();
if (newInput != null) {
IDocument document = fDocumentProvider
.getDocument(newInput);
if (document != null) {
document.addPositionCategory(SEGMENTS);
document.addPositionUpdater(fPositionUpdater);
parse(document);
}
}
}
/*
* @see IContentProvider#dispose
*/
public void dispose() {
if (fContent != null) {
fContent.clear();
fContent = null;
}
}
/*
* @see IContentProvider#isDeleted(Object)
*/
public boolean isDeleted(Object element) {
return false;
}
/*
* @see IStructuredContentProvider#getElements(Object)
*/
public Object[] getElements(Object element) {
return fContent.toArray();
}
/*
* @see ITreeContentProvider#hasChildren(Object)
*/
public boolean hasChildren(Object element) {
return element == fInput;
}
/*
* @see ITreeContentProvider#getParent(Object)
*/
public Object getParent(Object element) {
if (element instanceof Segment)
return fInput;
return null;
}
/*
* @see ITreeContentProvider#getChildren(Object)
*/
public Object[] getChildren(Object element) {
if (element == fInput)
return fContent.toArray();
return new Object[0];
}
}
protected Object fInput;
protected IDocumentProvider fDocumentProvider;
protected ITextEditor fTextEditor;
/**
* Creates a content outline page using the given provider and the given editor.
*
* @param provider the document provider
* @param editor the editor
*/
public ToolMethodContentOutlinePage(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();
viewer.setContentProvider(new 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){
return ToolPlugin.getImage(((Segment)element).iconString);
}
return null;
}
});
viewer.addSelectionChangedListener(this );
if (fInput != null)
viewer.setInput(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;
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);
}
}
}
}