1 7 34 35 package com.sun.tools.example.debug.gui; 36 37 import java.io.*; 38 import java.util.*; 39 40 import com.sun.jdi.*; 41 import com.sun.jdi.request.*; 42 43 import com.sun.tools.example.debug.bdi.*; 44 45 import javax.swing.*; 46 47 51 public class SourceModel extends AbstractListModel { 52 53 private File path; 54 55 boolean isActuallySource = true; 56 57 private List classes = new ArrayList(); 58 59 private Environment env; 60 61 63 70 private List sourceLines = null; 71 72 public static class Line { 73 public String text; 74 public boolean hasBreakpoint = false; 75 public ReferenceType refType = null; 76 Line(String text) { 77 this.text = text; 78 } 79 public boolean isExecutable() { 80 return refType != null; 81 } 82 public boolean hasBreakpoint() { 83 return hasBreakpoint; 84 } 85 }; 86 87 public static final Line prototypeCellValue = new Line( 89 "abcdefghijklmnopqrstuvwxyz" + 90 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 91 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 92 "1234567890~!@#$%^&*()_+{}|" + 93 ":<>?`-=[];',.XXXXXXXXXXXX/\\\""); 94 95 SourceModel(Environment env, File path) { 96 this.env = env; 97 this.path = path; 98 } 99 100 public SourceModel(String message) { 101 this.path = null; 102 setMessage(message); 103 } 104 105 private void setMessage(String message) { 106 isActuallySource = false; 107 sourceLines = new ArrayList(); 108 sourceLines.add(new Line(message)); 109 } 110 111 113 public Object getElementAt(int index) { 114 if (sourceLines == null) { 115 initialize(); 116 } 117 return sourceLines.get(index); 118 } 119 120 public int getSize() { 121 if (sourceLines == null) { 122 initialize(); 123 } 124 return sourceLines.size(); 125 } 126 127 129 public File fileName() { 130 return path; 131 } 132 133 public BufferedReader sourceReader() throws IOException { 134 return new BufferedReader(new FileReader(path)); 135 } 136 137 public Line line(int lineNo) { 138 if (sourceLines == null) { 139 initialize(); 140 } 141 int index = lineNo - 1; if (index >= sourceLines.size() || index < 0) { 143 return null; 144 } else { 145 return (Line)sourceLines.get(index); 146 } 147 } 148 149 public String sourceLine(int lineNo) { 150 Line line = line(lineNo); 151 if (line == null) { 152 return null; 153 } else { 154 return line.text; 155 } 156 } 157 158 void addClass(ReferenceType refType) { 159 if (classes.indexOf(refType) == -1) { 161 classes.add(refType); 162 if (sourceLines != null) { 163 markClassLines(refType); 164 } 165 } 166 } 167 168 172 public List referenceTypes() { 173 return Collections.unmodifiableList(classes); 174 } 175 176 private void initialize() { 177 try { 178 rawInit(); 179 } catch (IOException exc) { 180 setMessage("[Error reading source code]"); 181 } 182 } 183 184 public void showBreakpoint(int ln, boolean hasBreakpoint) { 185 line(ln).hasBreakpoint = hasBreakpoint; 186 fireContentsChanged(this, ln, ln); 187 } 188 189 public void showExecutable(int ln, ReferenceType refType) { 190 line(ln).refType = refType; 191 fireContentsChanged(this, ln, ln); 192 } 193 194 198 private void markClassLines(ReferenceType refType) { 199 List methods = refType.methods(); 200 for (Iterator mit = methods.iterator(); mit.hasNext();) { 201 Method meth = (Method)mit.next(); 202 try { 203 List lines = meth.allLineLocations(); 204 for (Iterator lit = lines.iterator(); lit.hasNext();) { 205 Location loc = (Location)lit.next(); 206 showExecutable(loc.lineNumber(), refType); 207 } 208 } catch (AbsentInformationException exc) { 209 } 211 } 212 List bps = env.getExecutionManager().eventRequestManager().breakpointRequests(); 213 for (Iterator it = bps.iterator(); it.hasNext();) { 214 BreakpointRequest bp = (BreakpointRequest)it.next(); 215 if (bp.location() != null) { 216 Location loc = bp.location(); 217 if (loc.declaringType().equals(refType)) { 218 showBreakpoint(loc.lineNumber(),true); 219 } 220 } 221 } 222 } 223 224 private void rawInit() throws IOException { 225 sourceLines = new ArrayList(); 226 BufferedReader reader = sourceReader(); 227 try { 228 String line = reader.readLine(); 229 while (line != null) { 230 sourceLines.add(new Line(expandTabs(line))); 231 line = reader.readLine(); 232 } 233 } finally { 234 reader.close(); 235 } 236 for (Iterator it = classes.iterator(); it.hasNext();) { 237 markClassLines((ClassType)it.next()); 238 } 239 } 240 241 private String expandTabs(String s) { 242 int col = 0; 243 int len = s.length(); 244 StringBuffer sb = new StringBuffer (132); 245 for (int i = 0; i < len; i++) { 246 char c = s.charAt(i); 247 sb.append(c); 248 if (c == '\t') { 249 int pad = (8 - (col % 8)); 250 for (int j = 0; j < pad; j++) { 251 sb.append(' '); 252 } 253 col += pad; 254 } else { 255 col++; 256 } 257 } 258 return sb.toString(); 259 } 260 261 } 262 | Popular Tags |