package XAspects; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.part.ViewPart; import edu.neu.ccs.demeter.aplib.cd.ClassGraph; public class XAspectsViewer extends ViewPart { StyledText output; public XAspectsViewer() {} /* (non-Javadoc) * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) */ public void createPartControl(Composite parent) { // create the StyledText object output = new StyledText(parent, 0); // set viewer not editable output.setEditable(false); } /* (non-Javadoc) * @see org.eclipse.ui.part.WorkbenchPart#setFocus() */ public void setFocus() {} // add an item to the list public void add(String s) { output.append(s + '\n'); } // clear the list public void clear() { output.setText(""); } // print the highlighted ClassGraph based on provided cg, // list of nodes and list of edges to be highlighted. public void printColoredCg(ClassGraph cg, List edgeSets, boolean nodesOnly) { // clear the current contents and print the cg clear(); add(cg.toString()); // create the highlight color (Blue) Color hColor = new Color(Display.getCurrent(), new RGB(0, 0, 255)); // pull out the viewer contents for reference String text = output.getText(); // perform highlighting for each edgeset in the list ListIterator edgeSetsIterator = edgeSets.listIterator(); while (edgeSetsIterator.hasNext()) { // convert the edgeset to string and strip out unnecessary parts String tmp = edgeSetsIterator.next().toString(); tmp = tmp.substring(3, tmp.indexOf(':')); // get the source class name int commaIndex = tmp.indexOf(','); String source = tmp.substring(0, commaIndex); // get the edge part name commaIndex = tmp.indexOf(',', commaIndex + 1); String edge = tmp.substring(source.length() + 1, commaIndex); // get the target class name String target = tmp.substring(source.length() + edge.length() + 2, tmp.length()); // find the source and highlight int sourceIndex = text.indexOf(source + " ="); output.setStyleRange(new StyleRange(sourceIndex, source.length(), hColor, null)); // find the edge and highlight int edgeIndex = text.indexOf("<" + edge + ">", sourceIndex); // check whether we're highlighting nodes and edges or just nodes if (nodesOnly) output.setStyleRange(new StyleRange(edgeIndex + edge.length() + 2, target.length() + 1, hColor, null)); else output.setStyleRange(new StyleRange(edgeIndex, edge.length() + target.length() + 3, hColor, null)); // find the target and highlight int targetIndex = text.indexOf(target + " ="); output.setStyleRange(new StyleRange(targetIndex, target.length(), hColor, null)); } } // print highlighted class graph based on list of classes public void printColoredCg(ClassGraph cg, Collection classList) { // clear the current contents and print the cg clear(); add(cg.toString()); // create the highlight color (Blue) Color hColor = new Color(Display.getCurrent(), new RGB(0, 0, 255)); // pull out the viewer contents for reference String text = output.getText(); Iterator classListIter = classList.iterator(); while (classListIter.hasNext()) { // get the class name String className = classListIter.next().toString(); // highlight every occurrence of each class name int index = text.indexOf(className); while (index != -1) { output.setStyleRange(new StyleRange(index, className.length(), hColor, null)); index = text.indexOf(className, index + 1); } } } // print highlighted cg based on wildcard string public void printColoredCg(ClassGraph cg, String wildcard) { // clear viewer and print cg clear(); add(cg.toString()); // remove the * wildcard = wildcard.substring(0, wildcard.indexOf("*")); // get the cg text String text = output.getText(); // create the highlight color (Blue) Color hColor = new Color(Display.getCurrent(), new RGB(0, 0, 255)); int sIndex = text.indexOf(wildcard); while (sIndex != -1) { int eIndex = endOfWordHelper(sIndex, text); if (beginsWordHelper(sIndex, text)) output.setStyleRange(new StyleRange(sIndex, eIndex - sIndex, hColor, null)); sIndex = text.indexOf(wildcard, sIndex + 1); } } // finds the next index indicating end of word in the cd private int endOfWordHelper(int sIndex, String text) { int retVal = -1; int tmp1 = text.indexOf(" ", sIndex); int tmp2 = text.indexOf(".", sIndex); if (tmp1 < tmp2) retVal = tmp1; else retVal = tmp2; return retVal; } // see if an index marks the beginning of a word (class) in the cd private boolean beginsWordHelper(int sIndex, String text) { boolean retVal = false; char prevChar = text.charAt(sIndex - 1);; if (!((prevChar >= 'A') && (prevChar <= 'z'))) retVal = true; return retVal; } }