KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdrshell > Support


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 package org.netbeans.mdrshell;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.io.PrintStream JavaDoc;
24
25 /** Helper singleton for StringBuffer
26  *
27  * @author Petr Hrebejk
28  * @version
29  */

30
31 class Support {
32
33     /** Support is a singleton.
34     */

35     private Support() {
36     }
37
38     /** Aligns the StringBuffer to length given by position
39     */

40     public static void ensurePosition( StringBuffer JavaDoc sb, int position ) {
41
42         if ( sb.length() < position ) {
43
44             while ( sb.length() < position ) {
45                 sb.append( " " );
46             }
47         }
48         else if ( sb.length() > position ) {
49             sb.setLength( position );
50         }
51     }
52     
53     
54     /** Default print function for results of commands.
55     */

56     public static void defaultPrint( PrintStream JavaDoc ps, Object JavaDoc object ) {
57         defaultPrint( ps, 0, object );
58     }
59     
60     /** Default print function for results of commands at given position on
61     * line.
62     */

63     public static void defaultPrint( PrintStream JavaDoc ps, int offset, Object JavaDoc object ) {
64         
65         if ( object == null ) {
66             ps.println("null");
67             return;
68         }
69
70         if ( object instanceof java.util.Iterator JavaDoc ) {
71             printIndent( ps, offset );
72             ps.print( "ITERATOR : {" );
73             for( Iterator JavaDoc it = (Iterator JavaDoc)object; it.hasNext(); ) {
74                 defaultPrint( ps, offset + 2, it.next() );
75             }
76             ps.println();
77             printIndent( ps, offset );
78             ps.println( "} // end of ITERATOR" );
79         }
80         else if ( object.getClass().isArray() ) {
81             printIndent( ps, offset );
82             ps.print( "ARRAY : [" );
83             for ( int i = 0; i < java.lang.reflect.Array.getLength( object ); i++ ) {
84                 //printIndent( ps, offset );
85
defaultPrint( ps, offset + 2, java.lang.reflect.Array.get( object, i ) );
86                 //ps.println( java.lang.reflect.Array.get( object, i ).toString() );
87
}
88             ps.println();
89             printIndent( ps, offset );
90             ps.println( "] // end of ARRAY" );
91         }
92         else if ( object instanceof Throwable JavaDoc ) {
93             ((Throwable JavaDoc)object).printStackTrace();
94         }
95         else if ( object instanceof Collection JavaDoc ) {
96             printIndent( ps, offset );
97             ps.print( "COLLECTION : {" );
98             for( Iterator JavaDoc it = ((Collection JavaDoc)object).iterator(); it.hasNext(); ) {
99                 defaultPrint( ps, offset + 2, it.next() );
100             }
101             ps.println();
102             printIndent( ps, offset );
103             ps.println( "} // end of COLLECTION" );
104         }
105         else {
106             printIndent( ps, offset );
107             ps.println( object.toString() );
108         }
109         
110     }
111
112     
113     private static void printIndent( PrintStream JavaDoc ps, int indent ) {
114         for( ; indent > 0; indent -- ) {
115             ps.print( " " );
116         }
117     }
118     
119 }
120
Popular Tags