KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > EditorDebug


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.TextAction JavaDoc;
28 import javax.swing.text.Segment JavaDoc;
29
30 /**
31 * Various internal tests
32 *
33 * @author Miloslav Metelka
34 * @version 0.10
35 */

36
37 public class EditorDebug {
38
39     private EditorDebug() {
40         // instance creation has no sense
41
}
42
43     public static void dumpPlanes(BaseDocument doc) {
44         /*
45         Class markClasses[] = new Class[] {
46                                   MarkFactory.LineMark.class,
47                                   MarkFactory.CaretMark.class,
48                                   MarkFactory.DrawMark.class,
49                                   MarkFactory.SyntaxMark.class,
50                                   Mark.class
51                               };
52         char markChars[] = new char[] {
53                                'L', 'C', 'D', 'S', 'B'
54                            };
55         System.out.println("--------------------------- DUMP OF MARK PLANES --------------------------------"); // NOI18N
56         System.out.println("Mark legend:\nD - DrawMark\n" // NOI18N
57                            + "S - SyntaxMark\nB - BaseMark\n" // NOI18N
58                            + "L - LineMark\nC - CaretMark"); // NOI18N
59         System.out.println(doc.op.markPlanesToString(markClasses, markChars));
60         System.out.println("--------------------------------------------------------------------------------\n"); // NOI18N
61          */

62     }
63
64     public static void dumpSyntaxMarks(final BaseDocument doc) {
65         System.out.println("--------------------------- DUMP OF SYNTAX MARKS --------------------------------"); // NOI18N
66
final int docLen = doc.getLength();
67         // Suspended because of docmarks rewrite due to #11692
68
/* doc.op.renderMarks(
69             new DocMarks.Renderer() {
70                 public void render() {
71                     int markCnt = getMarkCnt();
72                     int index = 0;
73                     int pos = 0;
74                     int lastPos = pos;
75                     int lastMarkPos = 0;
76                     int maxMarkDistance = 0;
77                     int minMarkDistance = docLen;
78                     Mark markArray[] = getMarkArray();
79                     SyntaxSeg.Slot slot = SyntaxSeg.getFreeSlot();
80                     Syntax syntax = doc.getFreeSyntax();
81
82                     try {
83                         syntax.load(null, slot.array, 0, 0, false, 0);
84                         while (index < markCnt) {
85                             Mark mark = markArray[index++];
86                             pos += getRelPos(mark);
87                             if (mark instanceof MarkFactory.SyntaxMark) {
88                                 MarkFactory.SyntaxMark syntaxMark = (MarkFactory.SyntaxMark)mark;
89                                 int delta = pos - lastMarkPos;
90                                 if (delta > maxMarkDistance) {
91                                     maxMarkDistance = delta;
92                                 }
93                                 if (delta < minMarkDistance) {
94                                     minMarkDistance = delta;
95                                 }
96                                 lastMarkPos = pos;
97
98                                 int preScan = syntax.getPreScan();
99                                 int loadPos = lastPos - preScan;
100                                 int scanLen = pos - loadPos;
101                                 try {
102                                     slot.load(doc, loadPos, scanLen);
103                                 } catch (BadLocationException e) {
104                                     e.printStackTrace();
105                                 }
106                                 syntax.relocate(slot.array, slot.offset + preScan,
107                                                 scanLen - preScan, (pos == docLen), pos);
108
109                                 while (syntax.nextToken() != null) { }
110
111                                 lastPos = pos;
112                                 try {
113                                     System.out.println(((syntaxMark == doc.op.eolMark)
114                                                         ? "!!EOLMark!!" : "syntaxMark:") // NOI18N
115                                                        + " getOffset()=" + Utilities.debugPosition(doc, syntaxMark.getOffset()) // NOI18N
116                                                        + ", getLine()=" + syntaxMark.getLine() // NOI18N
117                                                        + ", " + syntaxMark // NOI18N
118                                                        + ",\n StateInfo=" + syntaxMark.getStateInfo() // NOI18N
119                                                        + ",\n Syntax:" + syntax); // NOI18N
120                                 } catch (InvalidMarkException e) {
121                                     e.printStackTrace();
122                                 }
123                             }
124                         }
125                     } finally {
126                         doc.releaseSyntax(syntax);
127                         SyntaxSeg.releaseSlot(slot);
128                     }
129
130                     System.out.println("Maximum mark distance is " + maxMarkDistance // NOI18N
131                                        + "\nMinimum mark distance is " + minMarkDistance); // NOI18N
132                 }
133             }
134         );
135  */

136         System.out.println("--------------------------------------------------------------------------------\n"); // NOI18N
137
}
138
139     public static void test(JTextComponent JavaDoc component) {
140         /*
141         BaseTextUI ui = ((BaseTextUI)component.getUI());
142         BaseView view = (BaseView)ui.getRootView(component);
143         view = (BaseView)view.getView(0);
144         final BaseDocument doc = (BaseDocument)component.getDocument();
145         EditorUI editorUI = ui.getEditorUI();
146         final int docLen = doc.getLength();
147
148         System.out.println("\n------------------------- Registry --------------------------------"); // NOI18N
149         System.out.println(Registry.registryToString());
150
151         System.out.println("\n------------------------- DEBUGGING INFORMATION --------------------------------"); // NOI18N
152         String buf = "Document: " + doc // NOI18N
153                      + "\nview.mainHeight=" + ((LeafView)view).mainHeight // NOI18N
154                      + "\nDoubleBuffering=" + component.isDoubleBuffered(); // NOI18N
155         buf += "\ncomponent.getLocation()=" + component.getLocation() // NOI18N
156                + "\ncomponent.getSize()=" + component.getSize() // NOI18N
157                + "\nvirtualSize=" + editorUI.virtualSize; // NOI18N
158         buf += "\nEditorUI LAYERS:\n" + editorUI.getDrawLayerList(); // NOI18N
159         System.out.println(buf);
160
161         System.out.println(doc.op.infoToString());
162
163         buf = "\n------------------------ CR occurence test ------------------------\n"; // NOI18N
164         try {
165             char chars[] = doc.getChars(0, docLen);
166             int i;
167             for (i = 0; i < docLen; i++) {
168                 if (chars[i] == '\r') {
169                     buf += "CR found at pos=" + i + ", line=" + doc.op.getLine(i) + "\n"; // NOI18N
170                     break;
171                 }
172             }
173             if (i == docLen) {
174                 buf += "No CR found. CR occurence test suceeded."; // NOI18N
175             }
176         } catch (BadLocationException e) {
177             e.printStackTrace();
178         }
179         System.out.println(buf);
180          */

181     }
182
183     public static void checkSettings(Class JavaDoc kitClass) throws Exception JavaDoc {
184         int readBufferSize = SettingsUtil.getInteger(kitClass, SettingsNames.READ_BUFFER_SIZE,
185                              SettingsDefaults.defaultReadBufferSize);
186
187         int writeBufferSize = SettingsUtil.getInteger(kitClass, SettingsNames.WRITE_BUFFER_SIZE,
188                               SettingsDefaults.defaultWriteBufferSize);
189
190         int readMarkDistance = SettingsUtil.getInteger(kitClass, SettingsNames.READ_MARK_DISTANCE,
191                                SettingsDefaults.defaultReadMarkDistance);
192
193         int markDistance = SettingsUtil.getInteger(kitClass, SettingsNames.MARK_DISTANCE,
194                            SettingsDefaults.defaultMarkDistance);
195
196         int maxMarkDistance = SettingsUtil.getInteger(kitClass, SettingsNames.MAX_MARK_DISTANCE,
197                               SettingsDefaults.defaultMaxMarkDistance);
198
199         int minMarkDistance = SettingsUtil.getInteger(kitClass, SettingsNames.MIN_MARK_DISTANCE,
200                               SettingsDefaults.defaultMinMarkDistance);
201
202         int syntaxUpdateBatchSize = SettingsUtil.getInteger(kitClass, SettingsNames.SYNTAX_UPDATE_BATCH_SIZE,
203                                     SettingsDefaults.defaultSyntaxUpdateBatchSize);
204
205
206         // Now perform checks
207
if (maxMarkDistance < markDistance) {
208             throw new Exception JavaDoc("maxMarkDistance=" + maxMarkDistance // NOI18N
209
+ " < markDistance=" + markDistance); // NOI18N
210
}
211
212         if (markDistance < minMarkDistance) {
213             throw new Exception JavaDoc("markDistance=" + markDistance // NOI18N
214
+ " < minMarkDistance=" + minMarkDistance); // NOI18N
215
}
216
217         if (readMarkDistance < minMarkDistance) {
218             throw new Exception JavaDoc("readMarkDistance=" + readMarkDistance // NOI18N
219
+ " < minMarkDistance=" + minMarkDistance); // NOI18N
220
}
221
222         if (syntaxUpdateBatchSize < maxMarkDistance) {
223             throw new Exception JavaDoc("syntaxUpdateBatchSize=" + syntaxUpdateBatchSize // NOI18N
224
+ " < maxMarkDistance=" + maxMarkDistance); // NOI18N
225
}
226
227     }
228
229     /** Replace '\n', '\r' and '\t' in the string so they are identifiable. */
230     public static String JavaDoc debugString(String JavaDoc s) {
231         return (s != null) ? debugChars(s.toCharArray(), 0, s.length())
232             : "NULL STRING"; // NOI18N
233
}
234
235     public static String JavaDoc debugChars(Segment JavaDoc seg) {
236         return debugChars(seg.array, seg.offset, seg.count);
237     }
238
239     public static String JavaDoc debugChars(char chars[]) {
240         return debugChars(chars, 0, chars.length);
241     }
242
243     /** Replace '\n', '\r' and '\t' in the char array so they are identifiable. */
244     public static String JavaDoc debugChars(char chars[], int offset, int len) {
245         if (len < 0) {
246             return "EditorDebug.debugChars() !ERROR! len=" + len + " < 0"; // NOI18N
247
}
248         if (offset < 0) {
249             return "EditorDebug.debugChars() !ERROR! offset=" + offset + " < 0"; // NOI18N
250
}
251         if (offset + len > chars.length) {
252             return "EditorDebug.debugChars() !ERROR! offset=" + offset + " + len=" + len // NOI18N
253
+ " > chars.length=" + chars.length; // NOI18N
254
}
255         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len);
256         int endOffset = offset + len;
257         for (; offset < endOffset; offset++) {
258             switch (chars[offset]) {
259                 case '\n':
260                     sb.append("\\n"); // NOI18N
261
break;
262                 case '\t':
263                     sb.append("\\t"); // NOI18N
264
break;
265                 case '\r':
266                     sb.append("\\r"); // NOI18N
267
break;
268                 default:
269                     sb.append(chars[offset]);
270             }
271         }
272         return sb.toString();
273     }
274
275     public static String JavaDoc debugChar(char ch) {
276         switch (ch) {
277             case '\n':
278                 return "\\n"; // NOI18N
279
case '\t':
280                 return "\\t"; // NOI18N
281
case '\r':
282                 return "\\r"; // NOI18N
283
default:
284                 return String.valueOf(ch);
285         }
286     }
287
288     public static String JavaDoc debugPairs(int[] pairs) {
289         String JavaDoc ret;
290         if (pairs == null) {
291             ret = "Null pairs"; // NOI18N
292
} else if (pairs.length == 0) {
293             ret = "No pairs"; // NOI18N
294
} else {
295             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
296             for (int i = 0; i < pairs.length; i += 2) {
297                 sb.append('[');
298                 sb.append(pairs[i]);
299                 sb.append(", "); // NOI18N
300
sb.append(pairs[i + 1]);
301                 if (i < pairs.length - 1) {
302                     sb.append("]\n"); // NOI18N
303
}
304             }
305             ret = sb.toString();
306         }
307
308         return ret;
309     }
310
311     public static String JavaDoc debugArray(Object JavaDoc[] array) {
312         String JavaDoc ret;
313         if (array == null) {
314             ret = "Null array"; // NOI18N
315
} else if (array.length == 0) {
316             ret = "Empty array"; // NOI18N
317
} else {
318             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
319             for (int i = 0; i < array.length; i++) {
320                 sb.append('[');
321                 sb.append(i);
322                 sb.append("]="); // NOI18N
323
sb.append(array[i]);
324                 if (i != array.length - 1) {
325                     sb.append('\n');
326                 }
327             }
328             ret = sb.toString();
329         }
330         return ret;
331     }
332
333     public static String JavaDoc debugArray(int[] array) {
334         String JavaDoc ret;
335         if (array == null) {
336             ret = "Null array"; // NOI18N
337
} else if (array.length == 0) {
338             ret = "Empty array"; // NOI18N
339
} else {
340             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
341             for (int i = 0; i < array.length; i++) {
342                 sb.append('[');
343                 sb.append(i);
344                 sb.append("]="); // NOI18N
345
sb.append(array[i]);
346                 if (i != array.length - 1) {
347                     sb.append('\n');
348                 }
349             }
350             ret = sb.toString();
351         }
352         return ret;
353     }
354
355     public static String JavaDoc debugBlocks(BaseDocument doc, int[] blocks) {
356         String JavaDoc ret;
357         if (blocks == null) {
358             ret = "Null blocks"; // NOI18N
359
} else if (blocks.length == 0) {
360             ret = "Empty blocks"; // NOI18N
361
} else if (blocks.length % 2 != 0) {
362             ret = "Blocks.length=" + blocks.length + " is not even!"; // NOI18N
363
} else {
364             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
365             for (int i = 0; i < blocks.length; i += 2) {
366                 sb.append('[');
367                 sb.append(i);
368                 sb.append("]=("); // NOI18N
369
sb.append(blocks[i]);
370                 sb.append(", "); // NOI18N
371
sb.append(blocks[i + 1]);
372                 sb.append(") or ("); // NOI18N
373
sb.append(Utilities.debugPosition(doc, blocks[i]));
374                 sb.append(", "); // NOI18N
375
sb.append(Utilities.debugPosition(doc, blocks[i + 1]));
376                 sb.append(')');
377
378                 if (i != blocks.length - 1) {
379                     sb.append('\n');
380                 }
381             }
382             ret = sb.toString();
383         }
384         return ret;
385     }
386
387     public static String JavaDoc debugList(List JavaDoc l) {
388         String JavaDoc ret;
389         if (l == null) {
390             ret = "Null list"; // NOI18N
391
} else if (l.size() == 0) {
392             ret = "Empty list"; // NOI18N
393
} else {
394             int cnt = l.size();
395             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
396             for (int i = 0; i < cnt; i++) {
397                 sb.append('[');
398                 sb.append(i);
399                 sb.append("]="); // NOI18N
400
sb.append(l.get(i));
401                 if (i != cnt - 1) {
402                     sb.append('\n');
403                 }
404             }
405             ret = sb.toString();
406         }
407         return ret;
408     }
409
410     public static String JavaDoc debugIterator(Iterator JavaDoc i) {
411         String JavaDoc ret;
412         if (i == null) {
413             ret = "Null iterator"; // NOI18N
414
} else if (!i.hasNext()) {
415             ret = "Empty iterator"; // NOI18N
416
} else {
417             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
418             int ind = 0;
419             while (i.hasNext()) {
420                 sb.append('[');
421                 sb.append(ind++);
422                 sb.append("]="); // NOI18N
423
sb.append(i.next().toString());
424                 if (i.hasNext()) {
425                     sb.append('\n');
426                 }
427             }
428             ret = sb.toString();
429         }
430         return ret;
431     }
432
433 }
434
Popular Tags