001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2004 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package ca.mcgill.sable.soot.attributes;
021:
022: import java.util.*;
023:
024: import org.eclipse.jface.text.*;
025: import org.eclipse.swt.custom.StyleRange;
026: import org.eclipse.swt.graphics.Color;
027: import org.eclipse.swt.graphics.RGB;
028: import org.eclipse.swt.widgets.Display;
029: import org.eclipse.swt.*;
030: import org.eclipse.ui.*;
031: import org.eclipse.ui.texteditor.AbstractTextEditor;
032:
033: import ca.mcgill.sable.soot.SootPlugin;
034: import ca.mcgill.sable.soot.editors.*;
035:
036: public abstract class AbstractAttributesColorer {
037:
038: private ITextViewer viewer;
039: private IEditorPart editorPart;
040: private Color bgColor;
041: private Display display;
042: private ArrayList styleList;
043: private SootAttributesHandler handler;
044: private ColorManager colorManager;
045:
046: protected void init() {
047: setColorManager(SootPlugin.getDefault().getColorManager());
048: setDisplay(getEditorPart().getSite().getShell().getDisplay());
049: clearPres();
050: }
051:
052: protected abstract void computeColors();
053:
054: private void clearPres() {
055: if (getEditorPart() == null)
056: return;
057: if (getEditorPart().getEditorInput() != null) {
058: getDisplay().syncExec(new Runnable() {
059: public void run() {
060: IRegion reg = ((AbstractTextEditor) getEditorPart())
061: .getHighlightRange();
062: ((AbstractTextEditor) getEditorPart())
063: .setInput(getEditorPart().getEditorInput());
064: if (reg != null) {
065: ((AbstractTextEditor) getEditorPart())
066: .setHighlightRange(reg.getOffset(), reg
067: .getLength(), true);
068: }
069: };
070: });
071: }
072: }
073:
074: protected ArrayList sortAttrsByLength(ArrayList attrs) {
075: Iterator it = attrs.iterator();
076: while (it.hasNext()) {
077: SootAttribute sa = (SootAttribute) it.next();
078: int sLineOffset = 0;
079: int eLineOffset = 0;
080: try {
081: sLineOffset = getViewer().getDocument().getLineOffset(
082: (sa.getJavaStartLn() - 1));
083: eLineOffset = getViewer().getDocument().getLineOffset(
084: (sa.getJavaEndLn() - 1));
085: } catch (Exception e) {
086: }
087: int sOffset = sLineOffset + sa.getJavaStartPos() - 1;
088: int eOffset = eLineOffset + sa.getJavaEndPos() - 1;
089: int length = sOffset - eOffset;
090:
091: setLength(sa, length);
092: }
093: SootAttribute[] sorted = new SootAttribute[attrs.size()];
094: attrs.toArray(sorted);
095: for (int i = 0; i < sorted.length; i++) {
096: for (int j = i; j < sorted.length; j++) {
097: if (sorted[j].getJavaLength() > sorted[i]
098: .getJavaLength()) {
099: SootAttribute temp = sorted[i];
100: sorted[i] = sorted[j];
101: sorted[j] = temp;
102: }
103: }
104: }
105:
106: ArrayList sortedArray = new ArrayList();
107: for (int i = sorted.length - 1; i >= 0; i--) {
108: sortedArray.add(sorted[i]);
109: }
110:
111: return sortedArray;
112: }
113:
114: // set java or jimple length;
115: protected abstract void setLength(SootAttribute sa, int length);
116:
117: protected void changeStyles() {
118: final StyleRange[] srs = new StyleRange[styleList.size()];
119: styleList.toArray(srs);
120:
121: getDisplay().asyncExec(new Runnable() {
122: public void run() {
123: for (int i = 0; i < srs.length; i++) {
124: try {
125: //System.out.println("Style Range: "+srs[i]);
126: getViewer().getTextWidget().setStyleRange(
127: srs[i]);
128: } catch (Exception e) {
129: System.out.println("Seting Style Range Ex: "
130: + e.getMessage());
131: }
132: }
133: };
134: });
135: }
136:
137: protected void setAttributeTextColor(int sline, int eline,
138: int spos, int epos, RGB colorKey, boolean fg) {
139: int sLineOffset = 0;
140: int eLineOffset = 0;
141: int[] offsets = new int[eline - sline + 1];
142: int[] starts = new int[eline - sline + 1];
143: int[] lengths = new int[eline - sline + 1];
144: int unColLen = spos < epos ? spos - 1 : epos - 1;
145:
146: try {
147: int j = 0;
148: for (int i = sline; i <= eline; i++) {
149: offsets[j] = getViewer().getDocument().getLineOffset(
150: (i - 1));
151: starts[j] = offsets[j] + unColLen;
152: lengths[j] = getViewer().getDocument().getLineOffset(i)
153: - 1 - starts[j];
154: j++;
155: }
156: sLineOffset = getViewer().getDocument().getLineOffset(
157: (sline - 1));
158: eLineOffset = getViewer().getDocument().getLineOffset(
159: (eline - 1));
160: } catch (Exception e) {
161: return;
162: }
163:
164: int sOffset = sLineOffset + spos - 1;
165: int eOffset = eLineOffset + epos - 1;
166: int len = eOffset - sOffset;
167:
168: Color color = getColorManager().getColor(colorKey);
169: Color oldBgColor = getColorManager().getColor(
170: IJimpleColorConstants.JIMPLE_DEFAULT);
171:
172: StyleRange sr;
173: if (len > 0) {
174: if (sline == eline) {
175: if (fg) {
176: sr = new StyleRange(sOffset - 1, len + 1, color,
177: getBgColor());
178: } else {
179: sr = new StyleRange(sOffset, len, oldBgColor, color);
180: }
181: getStyleList().add(sr);
182: } else {
183: for (int i = 0; i < starts.length; i++) {
184: if (fg) {
185: sr = new StyleRange(starts[i] - 1,
186: lengths[i] + 1, color, getBgColor());
187: } else {
188: sr = new StyleRange(starts[i], lengths[i],
189: oldBgColor, color);
190: }
191: getStyleList().add(sr);
192: }
193: }
194: }
195: }
196:
197: /**
198: * @return
199: */
200: public Color getBgColor() {
201: return bgColor;
202: }
203:
204: /**
205: * @return
206: */
207: public ColorManager getColorManager() {
208: return colorManager;
209: }
210:
211: /**
212: * @return
213: */
214: public Display getDisplay() {
215: return display;
216: }
217:
218: /**
219: * @return
220: */
221: public IEditorPart getEditorPart() {
222: return editorPart;
223: }
224:
225: /**
226: * @return
227: */
228: public SootAttributesHandler getHandler() {
229: return handler;
230: }
231:
232: /**
233: * @return
234: */
235: public ArrayList getStyleList() {
236: return styleList;
237: }
238:
239: /**
240: * @return
241: */
242: public ITextViewer getViewer() {
243: return viewer;
244: }
245:
246: /**
247: * @param color
248: */
249: public void setBgColor(Color color) {
250: bgColor = color;
251: }
252:
253: /**
254: * @param manager
255: */
256: public void setColorManager(ColorManager manager) {
257: colorManager = manager;
258: }
259:
260: /**
261: * @param display
262: */
263: public void setDisplay(Display display) {
264: this .display = display;
265: }
266:
267: /**
268: * @param part
269: */
270: public void setEditorPart(IEditorPart part) {
271: editorPart = part;
272: }
273:
274: /**
275: * @param handler
276: */
277: public void setHandler(SootAttributesHandler handler) {
278: this .handler = handler;
279: }
280:
281: /**
282: * @param list
283: */
284: public void setStyleList(ArrayList list) {
285: styleList = list;
286: }
287:
288: /**
289: * @param viewer
290: */
291: public void setViewer(ITextViewer viewer) {
292: this.viewer = viewer;
293: }
294:
295: }
|