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-2007 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.gsfret.navigation;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.List;
047: import java.util.Set;
048: import org.netbeans.modules.gsf.api.CancellableTask;
049: import org.netbeans.modules.gsf.api.ParserResult;
050: import org.netbeans.modules.gsf.api.StructureScanner;
051: import org.netbeans.modules.gsf.api.ElementHandle;
052: import org.netbeans.modules.gsf.api.ElementKind;
053: import org.netbeans.modules.gsf.api.Modifier;
054: import org.netbeans.modules.gsf.api.StructureItem;
055: import org.netbeans.napi.gsfret.source.CompilationInfo;
056: import org.netbeans.modules.gsf.GsfHtmlFormatter;
057: import org.netbeans.modules.gsf.Language;
058: import org.netbeans.modules.gsf.LanguageRegistry;
059:
060: /**
061: * This file is originally from Retouche, the Java Support
062: * infrastructure in NetBeans. I have modified the file as little
063: * as possible to make merging Retouche fixes back as simple as
064: * possible.
065: * <p>
066: *
067: * XXX Remove the ElementScanner class from here it should be wenough to
068: * consult the Elements class. It should also permit for showing inherited members.
069: *
070: * @author phrebejk
071: */
072: public class ElementScanningTask implements
073: CancellableTask<CompilationInfo> {
074:
075: private ClassMemberPanelUI ui;
076: private volatile boolean canceled;
077:
078: public ElementScanningTask(ClassMemberPanelUI ui) {
079: this .ui = ui;
080: }
081:
082: public void cancel() {
083: canceled = true;
084: // if ( scanner != null ) {
085: // scanner.cancel();
086: // }
087: }
088:
089: public void run(CompilationInfo info) throws Exception {
090:
091: canceled = false; // Task shared for one file needs reset first
092:
093: //System.out.println("The task is running" + info.getFileObject().getNameExt() + "=====================================" ) ;
094:
095: final List<StructureItem> items = new ArrayList<StructureItem>();
096: StructureItem rootDescription = new StructureItem() {
097: public String getName() {
098: return null;
099: }
100:
101: public String getHtml() {
102: return null;
103: }
104:
105: public ElementHandle getElementHandle() {
106: throw new UnsupportedOperationException(
107: "Not supported on the Root Node.");
108: }
109:
110: public ElementKind getKind() {
111: return ElementKind.OTHER;
112: }
113:
114: public Set<Modifier> getModifiers() {
115: return Collections.emptySet();
116: }
117:
118: public boolean isLeaf() {
119: return false;
120: }
121:
122: public List<? extends StructureItem> getNestedItems() {
123: return items;
124: }
125:
126: public long getPosition() {
127: return 0;
128: }
129:
130: public long getEndPosition() {
131: return Long.MAX_VALUE;
132: }
133: };
134:
135: Set<String> mimeTypes = info.getEmbeddedMimeTypes();
136: LanguageRegistry registry = LanguageRegistry.getInstance();
137: List<String> sortedMimes = new ArrayList<String>(mimeTypes);
138: // TODO - sort results by something more interesting than the alphabetical
139: // order of their mimetypes...
140: Collections.sort(sortedMimes);
141:
142: for (String mimeType : mimeTypes) {
143: Language language = registry
144: .getLanguageByMimeType(mimeType);
145: StructureScanner scanner = language.getStructure();
146: if (scanner != null) {
147: List<? extends StructureItem> children = scanner.scan(
148: info, new NavigatorFormatter());
149: for (StructureItem co : children) {
150: items.add(co);
151: }
152: }
153: }
154:
155: if (!canceled) {
156: ui.refresh(rootDescription, info.getFileObject());
157:
158: }
159: }
160:
161: private class NavigatorFormatter extends GsfHtmlFormatter {
162: @Override
163: public void name(ElementKind kind, boolean start) {
164: // No special formatting for names
165: }
166: }
167: }
|