1 19 20 package ca.mcgill.sable.soot.attributes; 21 22 import java.util.*; 23 24 import org.eclipse.jface.text.*; 25 import org.eclipse.swt.custom.StyleRange; 26 import org.eclipse.swt.graphics.Color; 27 import org.eclipse.swt.graphics.RGB; 28 import org.eclipse.swt.widgets.Display; 29 import org.eclipse.swt.*; 30 import org.eclipse.ui.*; 31 import org.eclipse.ui.texteditor.AbstractTextEditor; 32 33 import ca.mcgill.sable.soot.SootPlugin; 34 import ca.mcgill.sable.soot.editors.*; 35 36 public abstract class AbstractAttributesColorer { 37 38 private ITextViewer viewer; 39 private IEditorPart editorPart; 40 private Color bgColor; 41 private Display display; 42 private ArrayList styleList; 43 private SootAttributesHandler handler; 44 private ColorManager colorManager; 45 46 protected void init(){ 47 setColorManager(SootPlugin.getDefault().getColorManager()); 48 setDisplay(getEditorPart().getSite().getShell().getDisplay()); 49 clearPres(); 50 } 51 52 protected abstract void computeColors(); 53 54 private void clearPres(){ 55 if (getEditorPart() == null) return; 56 if (getEditorPart().getEditorInput() != null){ 57 getDisplay().syncExec(new Runnable (){ 58 public void run() { 59 IRegion reg = ((AbstractTextEditor)getEditorPart()).getHighlightRange(); 60 ((AbstractTextEditor)getEditorPart()).setInput(getEditorPart().getEditorInput()); 61 if (reg != null){ 62 ((AbstractTextEditor)getEditorPart()).setHighlightRange(reg.getOffset(), reg.getLength(), true); 63 } 64 }; 65 }); 66 } 67 } 68 69 protected ArrayList sortAttrsByLength(ArrayList attrs){ 70 Iterator it = attrs.iterator(); 71 while(it.hasNext()){ 72 SootAttribute sa = (SootAttribute)it.next(); 73 int sLineOffset = 0; 74 int eLineOffset = 0; 75 try { 76 sLineOffset = getViewer().getDocument().getLineOffset((sa.getJavaStartLn()-1)); 77 eLineOffset = getViewer().getDocument().getLineOffset((sa.getJavaEndLn()-1)); 78 } 79 catch(Exception e){ 80 } 81 int sOffset = sLineOffset + sa.getJavaStartPos() - 1; 82 int eOffset = eLineOffset + sa.getJavaEndPos() - 1; 83 int length = sOffset - eOffset; 84 85 setLength(sa, length); 86 } 87 SootAttribute [] sorted = new SootAttribute[attrs.size()]; 88 attrs.toArray(sorted); 89 for (int i = 0; i< sorted.length; i++){ 90 for (int j = i; j < sorted.length; j++){ 91 if (sorted[j].getJavaLength() > sorted[i].getJavaLength()){ 92 SootAttribute temp = sorted[i]; 93 sorted[i] = sorted[j]; 94 sorted[j] = temp; 95 } 96 } 97 } 98 99 ArrayList sortedArray = new ArrayList(); 100 for (int i = sorted.length - 1; i >= 0; i--){ 101 sortedArray.add(sorted[i]); 102 } 103 104 return sortedArray; 105 } 106 107 protected abstract void setLength(SootAttribute sa, int length); 109 110 protected void changeStyles(){ 111 final StyleRange [] srs = new StyleRange[styleList.size()]; 112 styleList.toArray(srs); 113 114 getDisplay().asyncExec(new Runnable (){ 115 public void run(){ 116 for (int i = 0; i < srs.length; i++){ 117 try{ 118 getViewer().getTextWidget().setStyleRange(srs[i]); 120 } 121 catch(Exception e){ 122 System.out.println("Seting Style Range Ex: "+e.getMessage()); 123 } 124 } 125 }; 126 }); 127 } 128 129 protected void setAttributeTextColor(int sline, int eline, int spos, int epos, RGB colorKey, boolean fg){ 130 int sLineOffset = 0; 131 int eLineOffset = 0; 132 int [] offsets = new int[eline-sline+1]; 133 int [] starts = new int[eline-sline+1]; 134 int [] lengths = new int[eline-sline+1]; 135 int unColLen = spos < epos ? spos-1: epos-1; 136 137 try { 138 int j = 0; 139 for (int i = sline; i <= eline; i++){ 140 offsets[j] = getViewer().getDocument().getLineOffset((i-1)); 141 starts[j] = offsets[j] + unColLen; 142 lengths[j] = getViewer().getDocument().getLineOffset(i) - 1 - starts[j]; 143 j++; 144 } 145 sLineOffset = getViewer().getDocument().getLineOffset((sline-1)); 146 eLineOffset = getViewer().getDocument().getLineOffset((eline-1)); 147 } 148 catch(Exception e){ 149 return; 150 } 151 152 int sOffset = sLineOffset + spos - 1; 153 int eOffset = eLineOffset + epos - 1; 154 int len = eOffset - sOffset; 155 156 Color color = getColorManager().getColor(colorKey); 157 Color oldBgColor = getColorManager().getColor(IJimpleColorConstants.JIMPLE_DEFAULT); 158 159 StyleRange sr; 160 if (len > 0){ 161 if (sline == eline){ 162 if (fg){ 163 sr = new StyleRange(sOffset-1, len+1, color, getBgColor()); 164 } 165 else { 166 sr = new StyleRange(sOffset, len, oldBgColor, color); 167 } 168 getStyleList().add(sr); 169 } 170 else { 171 for (int i = 0; i < starts.length; i++){ 172 if (fg){ 173 sr = new StyleRange(starts[i]-1, lengths[i]+1, color, getBgColor()); 174 } 175 else { 176 sr = new StyleRange(starts[i], lengths[i], oldBgColor, color); 177 } 178 getStyleList().add(sr); 179 } 180 } 181 } 182 } 183 186 public Color getBgColor() { 187 return bgColor; 188 } 189 190 193 public ColorManager getColorManager() { 194 return colorManager; 195 } 196 197 200 public Display getDisplay() { 201 return display; 202 } 203 204 207 public IEditorPart getEditorPart() { 208 return editorPart; 209 } 210 211 214 public SootAttributesHandler getHandler() { 215 return handler; 216 } 217 218 221 public ArrayList getStyleList() { 222 return styleList; 223 } 224 225 228 public ITextViewer getViewer() { 229 return viewer; 230 } 231 232 235 public void setBgColor(Color color) { 236 bgColor = color; 237 } 238 239 242 public void setColorManager(ColorManager manager) { 243 colorManager = manager; 244 } 245 246 249 public void setDisplay(Display display) { 250 this.display = display; 251 } 252 253 256 public void setEditorPart(IEditorPart part) { 257 editorPart = part; 258 } 259 260 263 public void setHandler(SootAttributesHandler handler) { 264 this.handler = handler; 265 } 266 267 270 public void setStyleList(ArrayList list) { 271 styleList = list; 272 } 273 274 277 public void setViewer(ITextViewer viewer) { 278 this.viewer = viewer; 279 } 280 281 } 282 | Popular Tags |