History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/tools/example/debug/gui/SourceModel.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/tools/example/debug/gui/SourceModel.java+247−0
@@ -0,0 +1,247 @@
1/*
2 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.example.debug.gui;
27
28import java.io.*;
29import java.util.*;
30
31import com.sun.jdi.*;
32import com.sun.jdi.request.*;
33
34import javax.swing.*;
35
36/**
37 * Represents and manages one source file.
38 * Caches source lines. Holds other source file info.
39 */
40public class SourceModel extends AbstractListModel {
41
42 private File path;
43
44 boolean isActuallySource = true;
45
46 private List<ReferenceType> classes = new ArrayList<ReferenceType>();
47
48 private Environment env;
49
50 // Cached line-by-line access.
51
52 //### Unify this with source model used in source view?
53 //### What is our cache-management policy for these?
54 //### Even with weak refs, we won't discard any part of the
55 //### source if the SourceModel object is reachable.
56 /**
57 * List of Line.
58 */
59 private List<Line> sourceLines = null;
60
61 public static class Line {
62 public String text;
63 public boolean hasBreakpoint = false;
64 public ReferenceType refType = null;
65 Line(String text) {
66 this.text = text;
67 }
68 public boolean isExecutable() {
69 return refType != null;
70 }
71 public boolean hasBreakpoint() {
72 return hasBreakpoint;
73 }
74 };
75
76 // 132 characters long, all printable characters.
77 public static final Line prototypeCellValue = new Line(
78 "abcdefghijklmnopqrstuvwxyz" +
79 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
80 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
81 "1234567890~!@#$%^&*()_+{}|" +
82 ":<>?`-=[];',.XXXXXXXXXXXX/\\\"");
83
84 SourceModel(Environment env, File path) {
85 this.env = env;
86 this.path = path;
87 }
88
89 public SourceModel(String message) {
90 this.path = null;
91 setMessage(message);
92 }
93
94 private void setMessage(String message) {
95 isActuallySource = false;
96 sourceLines = new ArrayList<Line>();
97 sourceLines.add(new Line(message));
98 }
99
100 // **** Implement ListModel *****
101
102 @Override
103 public Object getElementAt(int index) {
104 if (sourceLines == null) {
105 initialize();
106 }
107 return sourceLines.get(index);
108 }
109
110 @Override
111 public int getSize() {
112 if (sourceLines == null) {
113 initialize();
114 }
115 return sourceLines.size();
116 }
117
118 // ***** Other functionality *****
119
120 public File fileName() {
121 return path;
122 }
123
124 public BufferedReader sourceReader() throws IOException {
125 return new BufferedReader(new FileReader(path));
126 }
127
128 public Line line(int lineNo) {
129 if (sourceLines == null) {
130 initialize();
131 }
132 int index = lineNo - 1; // list is 0-indexed
133 if (index >= sourceLines.size() || index < 0) {
134 return null;
135 } else {
136 return sourceLines.get(index);
137 }
138 }
139
140 public String sourceLine(int lineNo) {
141 Line line = line(lineNo);
142 if (line == null) {
143 return null;
144 } else {
145 return line.text;
146 }
147 }
148
149 void addClass(ReferenceType refType) {
150 // Logically is Set
151 if (classes.indexOf(refType) == -1) {
152 classes.add(refType);
153 if (sourceLines != null) {
154 markClassLines(refType);
155 }
156 }
157 }
158
159 /**
160 * @return List of currently known {@link com.sun.jdi.ReferenceType}
161 * in this source file.
162 */
163 public List<ReferenceType> referenceTypes() {
164 return Collections.unmodifiableList(classes);
165 }
166
167 private void initialize() {
168 try {
169 rawInit();
170 } catch (IOException exc) {
171 setMessage("[Error reading source code]");
172 }
173 }
174
175 public void showBreakpoint(int ln, boolean hasBreakpoint) {
176 line(ln).hasBreakpoint = hasBreakpoint;
177 fireContentsChanged(this, ln, ln);
178 }
179
180 public void showExecutable(int ln, ReferenceType refType) {
181 line(ln).refType = refType;
182 fireContentsChanged(this, ln, ln);
183 }
184
185 /**
186 * Mark executable lines and breakpoints, but only
187 * when sourceLines is set.
188 */
189 private void markClassLines(ReferenceType refType) {
190 for (Method meth : refType.methods()) {
191 try {
192 for (Location loc : meth.allLineLocations()) {
193 showExecutable(loc.lineNumber(), refType);
194 }
195 } catch (AbsentInformationException exc) {
196 // do nothing
197 }
198 }
199 for (BreakpointRequest bp :
200 env.getExecutionManager().eventRequestManager().breakpointRequests()) {
201 if (bp.location() != null) {
202 Location loc = bp.location();
203 if (loc.declaringType().equals(refType)) {
204 showBreakpoint(loc.lineNumber(),true);
205 }
206 }
207 }
208 }
209
210 private void rawInit() throws IOException {
211 sourceLines = new ArrayList<Line>();
212 BufferedReader reader = sourceReader();
213 try {
214 String line = reader.readLine();
215 while (line != null) {
216 sourceLines.add(new Line(expandTabs(line)));
217 line = reader.readLine();
218 }
219 } finally {
220 reader.close();
221 }
222 for (ReferenceType refType : classes) {
223 markClassLines(refType);
224 }
225 }
226
227 private String expandTabs(String s) {
228 int col = 0;
229 int len = s.length();
230 StringBuffer sb = new StringBuffer(132);
231 for (int i = 0; i < len; i++) {
232 char c = s.charAt(i);
233 sb.append(c);
234 if (c == '\t') {
235 int pad = (8 - (col % 8));
236 for (int j = 0; j < pad; j++) {
237 sb.append(' ');
238 }
239 col += pad;
240 } else {
241 col++;
242 }
243 }
244 return sb.toString();
245 }
246
247}