1 19 20 package ca.mcgill.sable.soot.attributes; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.ResourceBundle ; 25 import org.eclipse.core.resources.*; 26 import org.eclipse.core.runtime.*; 27 28 import org.eclipse.jdt.core.*; 29 import org.eclipse.jface.text.*; 30 import org.eclipse.jface.text.source.*; 31 import org.eclipse.swt.graphics.Rectangle; 32 import org.eclipse.ui.IEditorInput; 33 import org.eclipse.ui.IMarkerResolution; 34 import org.eclipse.ui.IWorkbenchWindow; 35 import org.eclipse.ui.PartInitException; 36 import org.eclipse.ui.texteditor.*; 37 38 39 import ca.mcgill.sable.soot.editors.JimpleEditor; 40 import ca.mcgill.sable.soot.ui.PopupListSelector; 41 import ca.mcgill.sable.soot.*; 42 43 public abstract class SootAttributeSelectAction extends ResourceAction { 44 45 AbstractTextEditor editor; 46 AbstractTextEditor linkToEditor; 47 IVerticalRulerInfo rulerInfo; 48 AbstractMarkerAnnotationModel model; 49 int lineNumber; 50 51 55 public SootAttributeSelectAction(ResourceBundle bundle, String prefix, ITextEditor editor, IVerticalRulerInfo rulerInfo) { 56 57 super(bundle, prefix); 58 setEditor((AbstractTextEditor)editor); 59 60 setRulerInfo(rulerInfo); 61 } 62 63 64 public IResource getResource(AbstractTextEditor textEditor) { 65 IEditorInput input= textEditor.getEditorInput(); 66 return (IResource) ((IAdaptable) input).getAdapter(IResource.class); 67 } 68 69 protected IDocument getDocument() { 70 IDocumentProvider provider= getEditor().getDocumentProvider(); 71 return provider.getDocument(getEditor().getEditorInput()); 72 } 73 74 public void run() { 75 76 IAnnotationModel model = getEditor().getDocumentProvider().getAnnotationModel(getEditor().getEditorInput()); 78 if (model instanceof AbstractMarkerAnnotationModel){ 79 setModel((AbstractMarkerAnnotationModel)model); 80 } 81 82 int markerLine = getRulerInfo().getLineOfLastMouseButtonActivity(); 83 IResource rec = getResource(getEditor()); 84 try { 85 IMarker [] markers = rec.findMarkers("ca.mcgill.sable.soot.sootattributemarker", true, IResource.DEPTH_INFINITE); 86 for (int i = 0; i < markers.length; i++){ 87 if (getModel().getMarkerPosition(markers[i]) == null) continue; 88 setLineNumber(getDocument().getLineOfOffset(getModel().getMarkerPosition(markers[i]).getOffset())); 89 90 91 if (getLineNumber() == markerLine){ 92 93 ArrayList links = getMarkerLinks(); 94 Iterator lit = links.iterator(); 95 String [] list = getMarkerLabels(links); 96 if ((list == null) || (list.length == 0)) { 97 } 99 else { 100 IWorkbenchWindow window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getWorkbenchWindow(); 101 102 PopupListSelector popup = new PopupListSelector(window.getShell()); 103 popup.setItems(list); 104 105 int listWidth = getListWidth(list); 106 107 if (getEditor() instanceof JimpleEditor){ 108 int topIndex = ((JimpleEditor)getEditor()).getViewer().getTopIndex(); 109 Rectangle rect = new Rectangle(320, (getLineNumber()+1-topIndex), listWidth, 45 ); 110 111 popup.open(rect); 112 } 113 else { 114 int topIndex = ((ITextViewer)((AbstractTextEditor)getEditor()).getAdapter(ITextOperationTarget.class)).getTopIndex(); 115 int pos = getModel().getMarkerPosition(markers[i]).getOffset(); 116 pos = pos / getLineNumber(); 117 Rectangle rect = new Rectangle(320, getLineNumber()+1-topIndex, listWidth, 45 ); 118 popup.open(rect); 119 120 } 121 122 handleSelection(popup.getSelected(), links); 123 } 124 } 125 } 126 } 127 catch(CoreException e){ 128 129 } 130 catch (BadLocationException e1){ 131 } 132 133 } 134 135 public int getListWidth(String [] list){ 136 int width = 0; 137 for (int i = 0; i < list.length; i++){ 138 139 String next = list[i]; 140 width = next.length() > width ? next.length() : width; 141 } 142 143 return width * 6; 144 } 145 146 public void handleSelection(String selected, ArrayList links){ 147 if (selected == null) return; 148 try { 149 int toShow = 0; 150 String className; 151 Iterator it = links.iterator(); 152 while (it.hasNext()){ 153 LinkAttribute la = (LinkAttribute)it.next(); 154 if (la.getLabel().equals(selected)){ 155 toShow = getLinkLine(la) - 1; 157 className = la.getClassName(); 158 findClass(className); 159 } 160 } 161 int selOffset = getLinkToEditor().getDocumentProvider().getDocument(getLinkToEditor().getEditorInput()).getLineOffset(toShow); 162 if ((selOffset != -1) && (selOffset != 0)){ 163 164 if (getLinkToEditor() instanceof JimpleEditor){ 165 166 ((JimpleEditor)getLinkToEditor()).getViewer().setRangeIndication(selOffset, 1, true); 167 SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(getLinkToEditor()); 168 169 } 170 else { 171 SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(getLinkToEditor()); 172 ((AbstractTextEditor)SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()).selectAndReveal(selOffset, 0); 173 ((AbstractTextEditor)SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()).setHighlightRange(selOffset, 1, true); 174 175 } 176 } 177 } 178 catch(BadLocationException e){ 179 System.out.println(e.getMessage()); 180 } 181 } 182 183 protected abstract int getLinkLine(LinkAttribute la); 184 185 public abstract void findClass(String className); 186 187 188 public String removeExt(String fileName){ 189 return fileName.substring(0, fileName.lastIndexOf(".")); 190 } 191 192 public abstract ArrayList getMarkerLinks(); 193 194 public String [] getMarkerLabels(ArrayList links){ 195 196 if ((links == null) || (links.size() == 0)) return null; 197 ArrayList list = new ArrayList (); 198 String [] attributeTexts = new String [links.size()]; 199 Iterator it = links.iterator(); 200 while (it.hasNext()){ 201 list.add(((LinkAttribute)it.next()).getLabel()); 202 } 203 list.toArray(attributeTexts); 204 return attributeTexts; 205 206 } 207 208 public String [] fixLabels(String [] at){ 209 for (int i = 0; i < at.length; i++){ 210 String temp = at[i]; 211 temp.replaceAll("<", "<"); 212 temp.replaceAll(">", ">"); 213 at[i] = temp; 214 } 215 return at; 216 } 217 218 public void getMarkerResolutions(IMarker marker){ 219 220 SootAttributeResolutionGenerator sarg = new SootAttributeResolutionGenerator(); 221 if (sarg.hasResolutions(marker)){ 222 IMarkerResolution [] res = sarg.getResolutions(marker); 223 for (int i = 0; i < res.length; i++){ 224 } 226 } 227 } 228 229 232 public AbstractTextEditor getEditor() { 233 return editor; 234 } 235 236 239 public void setEditor(AbstractTextEditor editor) { 240 this.editor = editor; 241 } 242 243 246 public IVerticalRulerInfo getRulerInfo() { 247 return rulerInfo; 248 } 249 250 253 public void setRulerInfo(IVerticalRulerInfo info) { 254 rulerInfo = info; 255 } 256 257 260 public AbstractMarkerAnnotationModel getModel() { 261 return model; 262 } 263 264 267 public void setModel(AbstractMarkerAnnotationModel model) { 268 this.model = model; 269 } 270 271 274 public int getLineNumber() { 275 return lineNumber; 276 } 277 278 281 public void setLineNumber(int i) { 282 lineNumber = i; 283 } 284 285 288 public AbstractTextEditor getLinkToEditor() { 289 return linkToEditor; 290 } 291 292 295 public void setLinkToEditor(AbstractTextEditor editor) { 296 linkToEditor = editor; 297 } 298 299 } 300 | Popular Tags |