001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor;
043:
044: import java.awt.event.ActionEvent;
045: import java.util.Iterator;
046: import java.util.List;
047: import javax.swing.text.JTextComponent;
048: import javax.swing.text.BadLocationException;
049: import javax.swing.text.TextAction;
050: import javax.swing.text.Segment;
051:
052: /**
053: * Various internal tests
054: *
055: * @author Miloslav Metelka
056: * @version 0.10
057: */
058:
059: public class EditorDebug {
060:
061: private EditorDebug() {
062: // instance creation has no sense
063: }
064:
065: public static void dumpPlanes(BaseDocument doc) {
066: /*
067: Class markClasses[] = new Class[] {
068: MarkFactory.LineMark.class,
069: MarkFactory.CaretMark.class,
070: MarkFactory.DrawMark.class,
071: MarkFactory.SyntaxMark.class,
072: Mark.class
073: };
074: char markChars[] = new char[] {
075: 'L', 'C', 'D', 'S', 'B'
076: };
077: System.out.println("--------------------------- DUMP OF MARK PLANES --------------------------------"); // NOI18N
078: System.out.println("Mark legend:\nD - DrawMark\n" // NOI18N
079: + "S - SyntaxMark\nB - BaseMark\n" // NOI18N
080: + "L - LineMark\nC - CaretMark"); // NOI18N
081: System.out.println(doc.op.markPlanesToString(markClasses, markChars));
082: System.out.println("--------------------------------------------------------------------------------\n"); // NOI18N
083: */
084: }
085:
086: public static void dumpSyntaxMarks(final BaseDocument doc) {
087: System.out
088: .println("--------------------------- DUMP OF SYNTAX MARKS --------------------------------"); // NOI18N
089: final int docLen = doc.getLength();
090: // Suspended because of docmarks rewrite due to #11692
091: /* doc.op.renderMarks(
092: new DocMarks.Renderer() {
093: public void render() {
094: int markCnt = getMarkCnt();
095: int index = 0;
096: int pos = 0;
097: int lastPos = pos;
098: int lastMarkPos = 0;
099: int maxMarkDistance = 0;
100: int minMarkDistance = docLen;
101: Mark markArray[] = getMarkArray();
102: SyntaxSeg.Slot slot = SyntaxSeg.getFreeSlot();
103: Syntax syntax = doc.getFreeSyntax();
104:
105: try {
106: syntax.load(null, slot.array, 0, 0, false, 0);
107: while (index < markCnt) {
108: Mark mark = markArray[index++];
109: pos += getRelPos(mark);
110: if (mark instanceof MarkFactory.SyntaxMark) {
111: MarkFactory.SyntaxMark syntaxMark = (MarkFactory.SyntaxMark)mark;
112: int delta = pos - lastMarkPos;
113: if (delta > maxMarkDistance) {
114: maxMarkDistance = delta;
115: }
116: if (delta < minMarkDistance) {
117: minMarkDistance = delta;
118: }
119: lastMarkPos = pos;
120:
121: int preScan = syntax.getPreScan();
122: int loadPos = lastPos - preScan;
123: int scanLen = pos - loadPos;
124: try {
125: slot.load(doc, loadPos, scanLen);
126: } catch (BadLocationException e) {
127: e.printStackTrace();
128: }
129: syntax.relocate(slot.array, slot.offset + preScan,
130: scanLen - preScan, (pos == docLen), pos);
131:
132: while (syntax.nextToken() != null) { }
133:
134: lastPos = pos;
135: try {
136: System.out.println(((syntaxMark == doc.op.eolMark)
137: ? "!!EOLMark!!" : "syntaxMark:") // NOI18N
138: + " getOffset()=" + Utilities.debugPosition(doc, syntaxMark.getOffset()) // NOI18N
139: + ", getLine()=" + syntaxMark.getLine() // NOI18N
140: + ", " + syntaxMark // NOI18N
141: + ",\n StateInfo=" + syntaxMark.getStateInfo() // NOI18N
142: + ",\n Syntax:" + syntax); // NOI18N
143: } catch (InvalidMarkException e) {
144: e.printStackTrace();
145: }
146: }
147: }
148: } finally {
149: doc.releaseSyntax(syntax);
150: SyntaxSeg.releaseSlot(slot);
151: }
152:
153: System.out.println("Maximum mark distance is " + maxMarkDistance // NOI18N
154: + "\nMinimum mark distance is " + minMarkDistance); // NOI18N
155: }
156: }
157: );
158: */
159: System.out
160: .println("--------------------------------------------------------------------------------\n"); // NOI18N
161: }
162:
163: public static void test(JTextComponent component) {
164: /*
165: BaseTextUI ui = ((BaseTextUI)component.getUI());
166: BaseView view = (BaseView)ui.getRootView(component);
167: view = (BaseView)view.getView(0);
168: final BaseDocument doc = (BaseDocument)component.getDocument();
169: EditorUI editorUI = ui.getEditorUI();
170: final int docLen = doc.getLength();
171:
172: System.out.println("\n------------------------- Registry --------------------------------"); // NOI18N
173: System.out.println(Registry.registryToString());
174:
175: System.out.println("\n------------------------- DEBUGGING INFORMATION --------------------------------"); // NOI18N
176: String buf = "Document: " + doc // NOI18N
177: + "\nview.mainHeight=" + ((LeafView)view).mainHeight // NOI18N
178: + "\nDoubleBuffering=" + component.isDoubleBuffered(); // NOI18N
179: buf += "\ncomponent.getLocation()=" + component.getLocation() // NOI18N
180: + "\ncomponent.getSize()=" + component.getSize() // NOI18N
181: + "\nvirtualSize=" + editorUI.virtualSize; // NOI18N
182: buf += "\nEditorUI LAYERS:\n" + editorUI.getDrawLayerList(); // NOI18N
183: System.out.println(buf);
184:
185: System.out.println(doc.op.infoToString());
186:
187: buf = "\n------------------------ CR occurence test ------------------------\n"; // NOI18N
188: try {
189: char chars[] = doc.getChars(0, docLen);
190: int i;
191: for (i = 0; i < docLen; i++) {
192: if (chars[i] == '\r') {
193: buf += "CR found at pos=" + i + ", line=" + doc.op.getLine(i) + "\n"; // NOI18N
194: break;
195: }
196: }
197: if (i == docLen) {
198: buf += "No CR found. CR occurence test suceeded."; // NOI18N
199: }
200: } catch (BadLocationException e) {
201: e.printStackTrace();
202: }
203: System.out.println(buf);
204: */
205: }
206:
207: public static void checkSettings(Class kitClass) throws Exception {
208: int readBufferSize = SettingsUtil.getInteger(kitClass,
209: SettingsNames.READ_BUFFER_SIZE,
210: SettingsDefaults.defaultReadBufferSize);
211:
212: int writeBufferSize = SettingsUtil.getInteger(kitClass,
213: SettingsNames.WRITE_BUFFER_SIZE,
214: SettingsDefaults.defaultWriteBufferSize);
215:
216: int readMarkDistance = SettingsUtil.getInteger(kitClass,
217: SettingsNames.READ_MARK_DISTANCE,
218: SettingsDefaults.defaultReadMarkDistance);
219:
220: int markDistance = SettingsUtil.getInteger(kitClass,
221: SettingsNames.MARK_DISTANCE,
222: SettingsDefaults.defaultMarkDistance);
223:
224: int maxMarkDistance = SettingsUtil.getInteger(kitClass,
225: SettingsNames.MAX_MARK_DISTANCE,
226: SettingsDefaults.defaultMaxMarkDistance);
227:
228: int minMarkDistance = SettingsUtil.getInteger(kitClass,
229: SettingsNames.MIN_MARK_DISTANCE,
230: SettingsDefaults.defaultMinMarkDistance);
231:
232: int syntaxUpdateBatchSize = SettingsUtil.getInteger(kitClass,
233: SettingsNames.SYNTAX_UPDATE_BATCH_SIZE,
234: SettingsDefaults.defaultSyntaxUpdateBatchSize);
235:
236: // Now perform checks
237: if (maxMarkDistance < markDistance) {
238: throw new Exception("maxMarkDistance=" + maxMarkDistance // NOI18N
239: + " < markDistance=" + markDistance); // NOI18N
240: }
241:
242: if (markDistance < minMarkDistance) {
243: throw new Exception("markDistance=" + markDistance // NOI18N
244: + " < minMarkDistance=" + minMarkDistance); // NOI18N
245: }
246:
247: if (readMarkDistance < minMarkDistance) {
248: throw new Exception("readMarkDistance=" + readMarkDistance // NOI18N
249: + " < minMarkDistance=" + minMarkDistance); // NOI18N
250: }
251:
252: if (syntaxUpdateBatchSize < maxMarkDistance) {
253: throw new Exception("syntaxUpdateBatchSize="
254: + syntaxUpdateBatchSize // NOI18N
255: + " < maxMarkDistance=" + maxMarkDistance); // NOI18N
256: }
257:
258: }
259:
260: /** Replace '\n', '\r' and '\t' in the string so they are identifiable. */
261: public static String debugString(String s) {
262: return (s != null) ? debugChars(s.toCharArray(), 0, s.length())
263: : "NULL STRING"; // NOI18N
264: }
265:
266: public static String debugChars(Segment seg) {
267: return debugChars(seg.array, seg.offset, seg.count);
268: }
269:
270: public static String debugChars(char chars[]) {
271: return debugChars(chars, 0, chars.length);
272: }
273:
274: /** Replace '\n', '\r' and '\t' in the char array so they are identifiable. */
275: public static String debugChars(char chars[], int offset, int len) {
276: if (len < 0) {
277: return "EditorDebug.debugChars() !ERROR! len=" + len
278: + " < 0"; // NOI18N
279: }
280: if (offset < 0) {
281: return "EditorDebug.debugChars() !ERROR! offset=" + offset
282: + " < 0"; // NOI18N
283: }
284: if (offset + len > chars.length) {
285: return "EditorDebug.debugChars() !ERROR! offset=" + offset
286: + " + len=" + len // NOI18N
287: + " > chars.length=" + chars.length; // NOI18N
288: }
289: StringBuffer sb = new StringBuffer(len);
290: int endOffset = offset + len;
291: for (; offset < endOffset; offset++) {
292: switch (chars[offset]) {
293: case '\n':
294: sb.append("\\n"); // NOI18N
295: break;
296: case '\t':
297: sb.append("\\t"); // NOI18N
298: break;
299: case '\r':
300: sb.append("\\r"); // NOI18N
301: break;
302: default:
303: sb.append(chars[offset]);
304: }
305: }
306: return sb.toString();
307: }
308:
309: public static String debugChar(char ch) {
310: switch (ch) {
311: case '\n':
312: return "\\n"; // NOI18N
313: case '\t':
314: return "\\t"; // NOI18N
315: case '\r':
316: return "\\r"; // NOI18N
317: default:
318: return String.valueOf(ch);
319: }
320: }
321:
322: public static String debugPairs(int[] pairs) {
323: String ret;
324: if (pairs == null) {
325: ret = "Null pairs"; // NOI18N
326: } else if (pairs.length == 0) {
327: ret = "No pairs"; // NOI18N
328: } else {
329: StringBuffer sb = new StringBuffer();
330: for (int i = 0; i < pairs.length; i += 2) {
331: sb.append('[');
332: sb.append(pairs[i]);
333: sb.append(", "); // NOI18N
334: sb.append(pairs[i + 1]);
335: if (i < pairs.length - 1) {
336: sb.append("]\n"); // NOI18N
337: }
338: }
339: ret = sb.toString();
340: }
341:
342: return ret;
343: }
344:
345: public static String debugArray(Object[] array) {
346: String ret;
347: if (array == null) {
348: ret = "Null array"; // NOI18N
349: } else if (array.length == 0) {
350: ret = "Empty array"; // NOI18N
351: } else {
352: StringBuffer sb = new StringBuffer();
353: for (int i = 0; i < array.length; i++) {
354: sb.append('[');
355: sb.append(i);
356: sb.append("]="); // NOI18N
357: sb.append(array[i]);
358: if (i != array.length - 1) {
359: sb.append('\n');
360: }
361: }
362: ret = sb.toString();
363: }
364: return ret;
365: }
366:
367: public static String debugArray(int[] array) {
368: String ret;
369: if (array == null) {
370: ret = "Null array"; // NOI18N
371: } else if (array.length == 0) {
372: ret = "Empty array"; // NOI18N
373: } else {
374: StringBuffer sb = new StringBuffer();
375: for (int i = 0; i < array.length; i++) {
376: sb.append('[');
377: sb.append(i);
378: sb.append("]="); // NOI18N
379: sb.append(array[i]);
380: if (i != array.length - 1) {
381: sb.append('\n');
382: }
383: }
384: ret = sb.toString();
385: }
386: return ret;
387: }
388:
389: public static String debugBlocks(BaseDocument doc, int[] blocks) {
390: String ret;
391: if (blocks == null) {
392: ret = "Null blocks"; // NOI18N
393: } else if (blocks.length == 0) {
394: ret = "Empty blocks"; // NOI18N
395: } else if (blocks.length % 2 != 0) {
396: ret = "Blocks.length=" + blocks.length + " is not even!"; // NOI18N
397: } else {
398: StringBuffer sb = new StringBuffer();
399: for (int i = 0; i < blocks.length; i += 2) {
400: sb.append('[');
401: sb.append(i);
402: sb.append("]=("); // NOI18N
403: sb.append(blocks[i]);
404: sb.append(", "); // NOI18N
405: sb.append(blocks[i + 1]);
406: sb.append(") or ("); // NOI18N
407: sb.append(Utilities.debugPosition(doc, blocks[i]));
408: sb.append(", "); // NOI18N
409: sb.append(Utilities.debugPosition(doc, blocks[i + 1]));
410: sb.append(')');
411:
412: if (i != blocks.length - 1) {
413: sb.append('\n');
414: }
415: }
416: ret = sb.toString();
417: }
418: return ret;
419: }
420:
421: public static String debugList(List l) {
422: String ret;
423: if (l == null) {
424: ret = "Null list"; // NOI18N
425: } else if (l.size() == 0) {
426: ret = "Empty list"; // NOI18N
427: } else {
428: int cnt = l.size();
429: StringBuffer sb = new StringBuffer();
430: for (int i = 0; i < cnt; i++) {
431: sb.append('[');
432: sb.append(i);
433: sb.append("]="); // NOI18N
434: sb.append(l.get(i));
435: if (i != cnt - 1) {
436: sb.append('\n');
437: }
438: }
439: ret = sb.toString();
440: }
441: return ret;
442: }
443:
444: public static String debugIterator(Iterator i) {
445: String ret;
446: if (i == null) {
447: ret = "Null iterator"; // NOI18N
448: } else if (!i.hasNext()) {
449: ret = "Empty iterator"; // NOI18N
450: } else {
451: StringBuffer sb = new StringBuffer();
452: int ind = 0;
453: while (i.hasNext()) {
454: sb.append('[');
455: sb.append(ind++);
456: sb.append("]="); // NOI18N
457: sb.append(i.next().toString());
458: if (i.hasNext()) {
459: sb.append('\n');
460: }
461: }
462: ret = sb.toString();
463: }
464: return ret;
465: }
466:
467: }
|