KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > SourceModel


1 /*
2  * @(#)SourceModel.java 1.11 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

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 /**
48  * Represents and manages one source file.
49  * Caches source lines. Holds other source file info.
50  */

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     // Cached line-by-line access.
62

63     //### Unify this with source model used in source view?
64
//### What is our cache-management policy for these?
65
//### Even with weak refs, we won't discard any part of the
66
//### source if the SourceModel object is reachable.
67
/**
68      * List of Line.
69      */

70     private List sourceLines = null;
71
72     public static class Line {
73         public String JavaDoc text;
74         public boolean hasBreakpoint = false;
75         public ReferenceType refType = null;
76         Line(String JavaDoc 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     // 132 characters long, all printable characters.
88
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 JavaDoc message) {
101         this.path = null;
102         setMessage(message);
103     }
104
105     private void setMessage(String JavaDoc message) {
106         isActuallySource = false;
107         sourceLines = new ArrayList();
108         sourceLines.add(new Line(message));
109     }
110
111     // **** Implement ListModel *****
112

113     public Object JavaDoc 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     // ***** Other functionality *****
128

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; // list is 0-indexed
142
if (index >= sourceLines.size() || index < 0) {
143         return null;
144     } else {
145         return (Line)sourceLines.get(index);
146     }
147     }
148
149     public String JavaDoc 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         // Logically is Set
160
if (classes.indexOf(refType) == -1) {
161             classes.add(refType);
162             if (sourceLines != null) {
163                 markClassLines(refType);
164             }
165         }
166     }
167
168     /**
169      * @return List of currently known {@link com.sun.jdi.ReferenceType}
170      * in this source file.
171      */

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     /**
195      * Mark executable lines and breakpoints, but only
196      * when sourceLines is set.
197      */

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                 // do nothing
210
}
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 JavaDoc 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 JavaDoc expandTabs(String JavaDoc s) {
242     int col = 0;
243     int len = s.length();
244     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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