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.modules.java.source.ui;
043:
044: import java.awt.Toolkit;
045: import java.io.IOException;
046: import java.util.logging.Logger;
047: import javax.lang.model.element.Element;
048: import javax.lang.model.element.TypeElement;
049: import javax.swing.Icon;
050: import org.netbeans.api.java.source.ClasspathInfo;
051: import org.netbeans.api.java.source.CompilationController;
052: import org.netbeans.api.java.source.ElementHandle;
053: import org.netbeans.api.java.source.JavaSource;
054: import org.netbeans.api.java.source.SourceUtils;
055: import org.netbeans.api.java.source.Task;
056: import org.netbeans.api.java.source.ui.ElementOpen;
057: import org.netbeans.modules.java.ui.Icons;
058: import org.netbeans.spi.jumpto.type.TypeDescriptor;
059: import org.openide.awt.StatusDisplayer;
060: import org.openide.filesystems.FileObject;
061: import org.openide.util.Exceptions;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @todo Resolve with TypeDescription
067: *
068: * @author Petr Hrebejk
069: */
070: public class JavaTypeDescription extends TypeDescriptor {
071:
072: private static final String EMPTY_STRING = ""; // NOI18N
073:
074: private Icon icon;
075:
076: private final JavaTypeProvider.CacheItem cacheItem;
077:
078: private final ElementHandle<TypeElement> handle;
079: private String simpleName;
080: private String outerName;
081: private String packageName;
082:
083: public JavaTypeDescription(JavaTypeProvider.CacheItem cacheItem,
084: final ElementHandle<TypeElement> handle) {
085: this .cacheItem = cacheItem;
086: this .handle = handle;
087: init();
088: }
089:
090: public void open() {
091: final ClasspathInfo ci = ClasspathInfo.create(cacheItem
092: .getRoot());
093: if (cacheItem.isBinary()) {
094: final JavaSource js = JavaSource.create(ci);
095: final ElementHandle<TypeElement> eh = handle;
096: final Element[] el = new Element[1];
097: try {
098: js.runUserActionTask(new Task<CompilationController>() {
099:
100: public void run(CompilationController info) {
101: el[0] = eh.resolve(info);
102: if (!ElementOpen.open(ci, el[0])) {
103: final String message = NbBundle.getMessage(
104: JavaTypeDescription.class,
105: "LBL_JavaTypeDescription_nosource",
106: eh.getQualifiedName());
107: StatusDisplayer.getDefault().setStatusText(
108: message);
109: Toolkit.getDefaultToolkit().beep();
110: }
111: }
112:
113: }, true);
114: } catch (IOException e) {
115: Logger
116: .getLogger(JavaTypeDescription.class.getName())
117: .info("Source not found: " + eh.getBinaryName());
118: Exceptions.printStackTrace(e);
119: }
120: } else {
121: final FileObject file = SourceUtils.getFile(handle, ci);
122: boolean opened = false;
123: if (file != null) {
124: opened = ElementOpen.open(file, handle);
125: }
126: if (!opened) {
127: StringBuilder name = new StringBuilder();
128: if (packageName != null) {
129: name.append(packageName);
130: name.append('.'); //NOI18N
131: }
132: if (outerName != null) {
133: name.append(outerName);
134: } else {
135: name.append(simpleName);
136: }
137: final String message = NbBundle.getMessage(
138: JavaTypeDescription.class,
139: "LBL_JavaTypeDescription_nosource", name
140: .toString());
141: StatusDisplayer.getDefault().setStatusText(message);
142: Toolkit.getDefaultToolkit().beep();
143: }
144: }
145: }
146:
147: public String getSimpleName() {
148: return simpleName;
149: }
150:
151: public String getOuterName() {
152: return outerName;
153: }
154:
155: public FileObject getFileObject() {
156: return cacheItem.getRoot();
157: }
158:
159: public String getTypeName() {
160: StringBuilder sb = new StringBuilder(simpleName);
161: if (outerName != null) {
162: sb.append(" in ").append(outerName);
163: }
164: return sb.toString();
165: }
166:
167: public String getContextName() {
168: StringBuilder sb = new StringBuilder();
169: sb.append(" (").append(
170: packageName == null ? "Default Package" : packageName)
171: .append(")");
172: return sb.toString();
173:
174: }
175:
176: public String getProjectName() {
177: String projectName = cacheItem.getProjectName();
178: return projectName == null ? "" : projectName; // NOI18N
179: }
180:
181: public Icon getProjectIcon() {
182: return cacheItem.getProjectIcon();
183: }
184:
185: public ElementHandle<TypeElement> getHandle() {
186: return handle;
187: }
188:
189: private void init() {
190: final String typeName = this .handle.getBinaryName();
191: int lastDot = typeName.lastIndexOf('.'); // NOI18N
192: int lastDollar = typeName.lastIndexOf('$'); // NOI18N
193: if (lastDot == -1) {
194: if (lastDollar == -1) {
195: simpleName = typeName;
196: } else {
197: simpleName = typeName.substring(lastDollar + 1);
198: outerName = typeName.substring(0, lastDollar).replace(
199: '$', '.'); //NOI18N;
200: }
201: } else {
202: packageName = typeName.substring(0, lastDot);
203:
204: if (lastDollar == -1) {
205: simpleName = typeName.substring(lastDot + 1).replace(
206: '$', '.'); //NOI18N
207: } else {
208: simpleName = typeName.substring(lastDollar + 1);
209: outerName = typeName.substring(lastDot + 1, lastDollar)
210: .replace('$', '.'); //NOI18N;
211: }
212:
213: }
214: icon = Icons.getElementIcon(handle.getKind(), null);
215: }
216:
217: public String toString() {
218:
219: StringBuilder sb = new StringBuilder(simpleName);
220: if (outerName != null) {
221: sb.append(" in ").append(outerName);
222: }
223: sb.append(" (").append(
224: packageName == null ? "Default Package" : packageName)
225: .append(")");
226: if (cacheItem.getProjectName() != null) {
227: sb.append(" [").append(cacheItem.getProjectName()).append(
228: "]");
229: }
230:
231: return sb.toString();
232: }
233:
234: //
235: // //public int compareTo( JavaTypeDescription td ) {
236: // public int compareTo( TypeDescriptor descriptor ) {
237: // if (descriptor instanceof JavaTypeDescription) {
238: // JavaTypeDescription td = (JavaTypeDescription)descriptor;
239: // int cmpr = compareStrings( simpleName, td.simpleName );
240: // if ( cmpr != 0 ) {
241: // return cmpr;
242: // }
243: // cmpr = compareStrings( outerName, td.outerName );
244: // if ( cmpr != 0 ) {
245: // return cmpr;
246: // }
247: // return compareStrings( packageName, td.packageName );
248: // } else {
249: // int cmpr = compareStrings(simpleName, descriptor.getTypeName());
250: // if (cmpr != 0) {
251: // return cmpr;
252: // }
253: // return compareStrings(outerName, descriptor.getPackageName());
254: // }
255: // }
256:
257: public synchronized Icon getIcon() {
258: return icon;
259: }
260:
261: // private int compareStrings(String s1, String s2) {
262: // if( s1 == null ) {
263: // s1 = EMPTY_STRING;
264: // }
265: // if ( s2 == null ) {
266: // s2 = EMPTY_STRING;
267: // }
268: // return s1.compareTo( s2 );
269: // }
270:
271: public int getOffset() {
272: throw new UnsupportedOperationException("Not supported yet.");
273: }
274: }
|