KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > actions > TypeDescription


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.modules.java.actions;
21
22 import java.io.IOException JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.TypeElement;
26 import javax.swing.Icon JavaDoc;
27 import org.netbeans.api.java.source.CancellableTask;
28 import org.netbeans.api.java.source.ClasspathInfo;
29 import org.netbeans.api.java.source.CompilationController;
30 import org.netbeans.api.java.source.ElementHandle;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.UiUtils;
33 import org.netbeans.modules.java.ui.Icons;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.util.Exceptions;
37
38 /**
39  *
40  * @author Petr Hrebejk
41  */

42 class TypeDescription implements Comparable JavaDoc<TypeDescription> {
43         
44     private static final String JavaDoc EMPTY_STRING = ""; // NOI18N
45

46     private Icon JavaDoc icon;
47     
48     private final GoToTypeAction.CacheItem cacheItem;
49     
50     private final ElementHandle<TypeElement> handle;
51     private String JavaDoc simpleName;
52     private String JavaDoc outerName;
53     private String JavaDoc packageName;
54
55     public TypeDescription(GoToTypeAction.CacheItem cacheItem, final ElementHandle<TypeElement> handle ) {
56        this.cacheItem = cacheItem;
57        this.handle = handle;
58        init();
59     }
60     
61     public void open() {
62         if ( cacheItem.isBinary() ) {
63             final ClasspathInfo ci = ClasspathInfo.create(cacheItem.getRoot());
64             JavaSource js = JavaSource.create( ci );
65             final ElementHandle<TypeElement> eh = handle;
66             final Element[] el = new Element[1];
67             try {
68                 js.runUserActionTask(new CancellableTask<CompilationController>() {
69
70                     public void cancel() {
71                     }
72
73                     public void run(CompilationController info) {
74                         el[0] = eh.resolve (info);
75                         UiUtils.open(ci, el[0]);
76                     }
77
78                 }, true);
79             }
80             catch( IOException JavaDoc e ) {
81                 Logger.getLogger(TypeDescription.class.getName()).info("Source not found: " + eh.getBinaryName());
82                 Exceptions.printStackTrace(e);
83             }
84         }
85         else {
86             //XXX: Why is this different? Why not UiUtils.open () is used?
87
FileObject folder = cacheItem.getRoot().getFileObject(packageName.replace(".", "/")); // NOI18N
88
if (folder != null) {
89                 FileObject[] ch = folder.getChildren();
90                 String JavaDoc name = outerName == null ? simpleName : outerName; // NOI18N
91
int lastDot = name.indexOf('.'); //NOI18N
92
if ( lastDot != -1 ) {
93                     name = name.substring(0, lastDot );
94                 }
95                 for (FileObject fileObject : ch) {
96                     if ( name.equals( fileObject.getName() ) &&
97                          "java".equals( fileObject.getExt().toLowerCase() ) ) {
98                         UiUtils.open(fileObject, handle);
99                     }
100                 }
101             }
102             else {
103                 Logger.getLogger(TypeDescription.class.getName()).info("Package " + packageName +" doesn't exist in root: " + FileUtil.getFileDisplayName(cacheItem.getRoot()));
104             }
105         }
106     }
107
108     public String JavaDoc getSimpleName() {
109         return simpleName;
110     }
111
112     public FileObject getFileObject() {
113         return cacheItem.getRoot();
114     }
115
116     public String JavaDoc getTypeName() {
117         StringBuilder JavaDoc sb = new StringBuilder JavaDoc( simpleName );
118         if( outerName != null ) {
119             sb.append(" in ").append( outerName );
120         }
121         return sb.toString();
122     }
123     
124     public String JavaDoc getPackageName() {
125         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
126         sb.append( " (").append( packageName == null ? "Default Package" : packageName).append(")");
127         return sb.toString();
128                 
129         
130     }
131     
132     public String JavaDoc getProjectName() {
133         String JavaDoc projectName = cacheItem.getProjectName();
134         return projectName == null ? "" : projectName; // NOI18N
135
}
136     
137     public Icon JavaDoc getProjectIcon() {
138         return cacheItem.getProjectIcon();
139     }
140     
141     private void init() {
142         final String JavaDoc typeName = this.handle.getBinaryName();
143         int lastDot = typeName.lastIndexOf('.'); // NOI18N
144
int lastDollar = typeName.lastIndexOf('$'); // NOI18N
145
if ( lastDot == -1 ) {
146             if ( lastDollar == -1 ) {
147                 simpleName = typeName;
148             }
149             else {
150                 simpleName = typeName.substring(lastDollar + 1);
151                 outerName = typeName.substring(0, lastDollar ).replace( '$', '.'); //NOI18N;
152
}
153         }
154         else {
155             packageName = typeName.substring( 0, lastDot );
156             
157             if ( lastDollar == -1 ) {
158                 simpleName = typeName.substring( lastDot + 1 ).replace( '$', '.'); //NOI18N
159
}
160             else {
161                 simpleName = typeName.substring(lastDollar + 1);
162                 outerName = typeName.substring(lastDot + 1, lastDollar ).replace( '$', '.'); //NOI18N;
163
}
164                         
165         }
166         icon = Icons.getElementIcon (handle.getKind(), null);
167     }
168
169     public String JavaDoc toString() {
170         
171         StringBuilder JavaDoc sb = new StringBuilder JavaDoc( simpleName );
172         if( outerName != null ) {
173             sb.append(" in ").append( outerName );
174         }
175         sb.append( " (").append( packageName == null ? "Default Package" : packageName).append(")");
176         if (cacheItem.getProjectName() != null ) {
177             sb.append( " [").append( cacheItem.getProjectName()).append("]");
178         }
179         
180         return sb.toString();
181     }
182
183     
184     public int compareTo( TypeDescription td ) {
185        int cmpr = compareStrings( simpleName, td.simpleName );
186        if ( cmpr != 0 ) {
187            return cmpr;
188        }
189        cmpr = compareStrings( outerName, td.outerName );
190        if ( cmpr != 0 ) {
191            return cmpr;
192        }
193        return compareStrings( packageName, td.packageName );
194     }
195     
196     public synchronized Icon JavaDoc getIcon() {
197         return icon;
198     }
199         
200     private int compareStrings(String JavaDoc s1, String JavaDoc s2) {
201         if( s1 == null ) {
202             s1 = EMPTY_STRING;
203         }
204         if ( s2 == null ) {
205             s2 = EMPTY_STRING;
206         }
207         return s1.compareTo( s2 );
208     }
209     
210     
211 }
212
Popular Tags