package tool.editors;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.texteditor.IDocumentProvider;
import tool.editors.ToolContentOutlinePage.Segment;
public class ToolEditorContentProvider implements ITreeContentProvider {
public static final String CEX_SIGNATURE_REGEX = "^method\\s+(?s)(.+?)(?-s)begin";
public static final Pattern cexSignaturePattern = Pattern.compile(CEX_SIGNATURE_REGEX, Pattern.MULTILINE);
public static final String ATTRIBUTE_REGEX = "has\\s+(public|private|publicinternal)\\s+attribute\\s+(\\S+?):\\s+(.+?)\\s*.+?;";
public static final Pattern attributePattern = Pattern.compile(ATTRIBUTE_REGEX);
public static final String VIRTUAL_ATTRIBUTE_REGEX = "has\\s+(public|private|publicinternal)\\s+virtual\\s+attribute\\s+(\\S+?):\\s+(.+?)\\s*.+?;";
public static final Pattern virtualAttributePattern = Pattern.compile(VIRTUAL_ATTRIBUTE_REGEX);
public static final String WINDOW_REGEX = "has\\s+public\\s+generated\\s+attribute\\s+\"(\\<.+\\>)\":\\s+(.+)\\s*;";
public static final Pattern windowAttributePattern = Pattern.compile(WINDOW_REGEX);
public static final String CONSTANT_REGEX = "has\\s+(public|private)\\s+constant\\s+(.+)\\s+=\\s+(.+?)\\s*.*?;";
public static final Pattern constantPattern = Pattern.compile(CONSTANT_REGEX);
public static final String PLAN_CONSTANT_REGEX = "constant\\s+(.+)\\s+=\\s+(.+?)\\s*.*?;";
public static final Pattern planConstantPattern = Pattern.compile(PLAN_CONSTANT_REGEX);
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 IDocumentProvider fDocumentProvider;
protected Object fInput;
public Object getfInput() {
return fInput;
}
public void setfInput(Object fInput) {
this.fInput = fInput;
}
public ToolEditorContentProvider(IDocumentProvider fDocumentProvider) {
super();
this.fDocumentProvider = fDocumentProvider;
}
protected String makeIconString(String member, String flag){
String iconString = "icons/"
+ ((flag.startsWith("public")) ? "public_" : "private_")
+ member
+ ".gif";
return iconString;
}
protected void parse(IDocument document) {
try {
FindReplaceDocumentAdapter frAdapter = new FindReplaceDocumentAdapter(document);
// do the attributes
IRegion region = frAdapter.find(0, ATTRIBUTE_REGEX, true, false, false, true);
String attributeLabel = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
attributeLabel = document.get(region.getOffset(), region.getLength());
Matcher sig = attributePattern.matcher(attributeLabel);
if (sig.find()){
attributeLabel = sig.group(2);
fContent.add(new Segment(attributeLabel, p, makeIconString("attribute", sig.group(1))));
}
region = frAdapter.find(offset+length, ATTRIBUTE_REGEX, true, false, false, true);
}
// do the virtual attributes
region = frAdapter.find(0, VIRTUAL_ATTRIBUTE_REGEX, true, false, false, true);
attributeLabel = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
attributeLabel = document.get(region.getOffset(), region.getLength());
Matcher sig = virtualAttributePattern.matcher(attributeLabel);
if (sig.find()){
attributeLabel = sig.group(2);
fContent.add(new Segment(attributeLabel, p, makeIconString("virtual", sig.group(1))));
}
region = frAdapter.find(offset+length, VIRTUAL_ATTRIBUTE_REGEX, true, false, false, true);
}
// do the window attributes
region = frAdapter.find(0, WINDOW_REGEX, true, false, false, true);
attributeLabel = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
attributeLabel = document.get(region.getOffset(), region.getLength());
Matcher sig = windowAttributePattern.matcher(attributeLabel);
if (sig.find()){
attributeLabel = sig.group(1);
fContent.add(new Segment(attributeLabel, p, "icons/window_attribute.gif"));
}
region = frAdapter.find(offset+length, WINDOW_REGEX, true, false, false, true);
}
// do the class constants
region = frAdapter.find(0, CONSTANT_REGEX, true, false, false, true);
attributeLabel = null;
while (region != null){
int offset = region.getOffset();
int length = region.getLength();
Position p = new Position(offset, length);
document.addPosition(SEGMENTS, p);
attributeLabel = document.get(region.getOffset(), region.getLength());
Matcher sig = constantPattern.matcher(attributeLabel);
if (sig.find()){
attributeLabel = sig.group(2);
fContent.add(new Segment(attributeLabel, p, makeIconString("constant", sig.group(1))));
}
region = frAdapter.find(offset+length, CONSTANT_REGEX, true, false, false, true);
}
// do the method implementations
region = frAdapter.find(0, 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 = cexSignaturePattern.matcher(methodSignature);
if (sig.find()){
methodSignature = sig.group(1);
fContent.add(new Segment(makeLabel(methodSignature), p, "icons/private_method.gif"));
}
region = frAdapter.find(offset+length, 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];
}
private String makeLabel(String source){
String target = source.replaceAll("\\s","");
target = target.substring(target.indexOf(".")+1);
return target;
}
}