Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2011-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  *
  * Contributor(s):
  *
  * Portions Copyrighted 2011-2012 Sun Microsystems, Inc.
  */
 package org.netbeans.modules.java.navigation;
 
 import java.awt.Image;
 import java.util.List;

Author(s):
Dusan Balek
public class BreadCrumbsNodeImpl extends AbstractNode {
    private static final String COLOR = "#707070";
    private final Image icon;
    private String htmlDisplayName;
//    private OpenAction openAction;
    public BreadCrumbsNodeImpl(TreePathHandle tphImage iconString htmlDisplayNameFileObject fileObjectint[] pos) {
        super(Children.create(new ChildrenFactoryImpl(tph), false), Lookups.fixed(tphposnew OpenableImpl(fileObjectpos[0])));
        this. = icon;
        this. = htmlDisplayName;
//        this.openAction = new OpenAction(fileObject, pos);
    }
    @Override
    public String getHtmlDisplayName() {
        return ;
    }
    @Override
    public Action getPreferredAction() {
        return OpenAction.get(OpenAction.class);
    }
    @Override
    public boolean canCopy() {
        return false;
    }
    @Override
    public boolean canCut() {
        return false;
    }
    @Override
    public boolean canDestroy() {
        return false;
    }
    @Override
    public boolean canRename() {
        return false;
    }
    @Override
    public PasteType getDropType(Transferable tint actionint index) {
        return null;
    }
    @Override
    public Transferable drag() throws IOException {
        return null;
    }
    @Override
    protected void createPasteTypes(Transferable tList<PasteTypes) {
        // Do nothing
    }
    @Override
    public Image getIcon(int type) {
        return ;
    }
    @Override
    public Image getOpenedIcon(int type) {
        return ;
    }
    private static final String CONSTRUCTOR_NAME = "<init>";
    
    public static Node createBreadcrumbs(final CompilationInfo infoTreePath pathboolean elseSection) {
        final Trees trees = info.getTrees();
        final SourcePositions sp = trees.getSourcePositions();
        int[] pos = new int[] {(intsp.getStartPosition(path.getCompilationUnit(), path.getLeaf()), (intsp.getEndPosition(path.getCompilationUnit(), path.getLeaf())};
        TreePathHandle tph = TreePathHandle.create(pathinfo);
            final Tree leaf = path.getLeaf();
            switch (leaf.getKind()) {
                case :
                    return new BreadCrumbsNodeImpl(tphnull, FileUtil.getFileDisplayName(info.getFileObject()), info.getFileObject(), pos);
                case :
                case :
                case :
                case :
                    return new BreadCrumbsNodeImpl(tphiconFor(infopath), className(path), info.getFileObject(), pos);
                case :
                    MethodTree mt = (MethodTreeleaf;
                    CharSequence name;
                    if (mt.getName().contentEquals()) {
                        name = ((ClassTreepath.getParentPath().getLeaf()).getSimpleName();
                    } else {
                        name = mt.getName();
                    }
                    return new BreadCrumbsNodeImpl(tphiconFor(infopath), name.toString(), info.getFileObject(), pos);
                case :
                    return new BreadCrumbsNodeImpl(tphiconFor(infopath), ((VariableTreeleaf).getName().toString(), info.getFileObject(), pos);
                case :
                    ExpressionTree expr = ((CaseTreeleaf).getExpression();
                    StringBuilder sb = new StringBuilder(expr == null ? "default:" : "case "); //NOI18N
                    if (expr != null) {
                        sb.append("<font color=").append().append(">"); // NOI18N
                        sb.append(escape(((CaseTreeleaf).getExpression().toString()));
                        sb.append(":"); //NOI18N
                        sb.append("</font>"); //NOI18N
                    }
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("catch "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append(escape(((CatchTreeleaf).getParameter().toString()));
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("do ... while "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append(escape(((DoWhileLoopTreeleaf).getCondition().toString()));
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphnullsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("for "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append("("); //NOI18N
                    sb.append(escape(((EnhancedForLoopTreeleaf).getVariable().toString()));
                    sb.append(" : "); //NOI18N
                    sb.append(escape(((EnhancedForLoopTreeleaf).getExpression().toString()));
                    sb.append(")"); //NOI18N
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("for "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append("("); //NOI18N
                    sb.append(escape(((ForLoopTreeleaf).getInitializer().toString()));
                    sb.append("; "); //NOI18N
                    sb.append(escape(((ForLoopTreeleaf).getCondition().toString()));
                    sb.append("; "); //NOI18N
                    sb.append(escape(((ForLoopTreeleaf).getUpdate().toString()));
                    sb.append(")"); //NOI18N
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder(""); //NOI18N
                    Tree last = leaf;
                    while (path != null && path.getLeaf().getKind() == .) {
                        StringBuilder temp = new StringBuilder(""); //NOI18N
                        temp.append("if "); //NOI18N
                        temp.append("<font color=").append().append(">"); // NOI18N
                        temp.append(escape(((IfTreepath.getLeaf()).getCondition().toString()));
                        temp.append("</font>"); //NOI18N
                        
                        if (((IfTreepath.getLeaf()).getElseStatement() == last || (path.getLeaf() == leaf && elseSection)) {
                            temp.append(" else");
                        }
                        temp.append(" ");
                        sb.insert(0, temp.toString());
                        last = path.getLeaf();
                        path = path.getParentPath();
                    }
                    sb.delete(sb.length() - 1, sb.length());
                    IfTree it = (IfTreeleaf;
                    int elseStart = pos[1] + 1;
                    if (it.getElseStatement() != null) {
                        TokenSequence<JavaTokenIdts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
                        ts.move(elseStart = (intsp.getStartPosition(path.getCompilationUnit(), it.getElseStatement()));
                        boolean success;
                        while ((success = ts.movePrevious()) && ts.token().id() != .)
                            ;
                        elseStart = success ? Math.min(ts.offset(), elseStart) : elseStart;
                    }
                    if (elseSection) {
                        int endPos = (intsp.getEndPosition(path.getCompilationUnit(), it.getElseStatement());
                        if (it.getElseStatement().getKind() == .) {
                            endPos = (intsp.getStartPosition(path.getCompilationUnit(), it.getElseStatement()) - 1;
                        }
                        pos = new int[] {elseStartendPos};
                    } else {
                        pos[1] = elseStart - 1;
                    }
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("switch "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append(escape(((SwitchTreeleaf).getExpression().toString()));
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("synchronized "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append(escape(((SynchronizedTreeleaf).getExpression().toString()));
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("try"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    sb = new StringBuilder("while "); //NOI18N
                    sb.append("<font color=").append().append(">"); // NOI18N
                    sb.append(escape(((WhileLoopTreeleaf).getCondition().toString()));
                    sb.append("</font>"); //NOI18N
                    return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                case :
                    if (path.getParentPath().getLeaf().getKind() == . && ((TryTreepath.getParentPath().getLeaf()).getFinallyBlock() == leaf) {
                        sb = new StringBuilder("finally"); //NOI18N
                        return new BreadCrumbsNodeImpl(tphsb.toString(), info.getFileObject(), pos);
                    }
                    break;
            }
            return null;
    }
    static String escape(String s) {
        if (s != null) {
            try {
                return XMLUtil.toAttributeValue(s);
            } catch (CharConversionException ex) {
            }
        }
        return null;
    }
    
    private static final Image DEFAULT_ICON = .;
    
    private static Image iconFor(CompilationInfo infoTreePath path) {
        Element el = info.getTrees().getElement(path);
        if (el == nullreturn ;
        Icon icon = ElementIcons.getElementIcon(el.getKind(), el.getModifiers());
        if (icon == nullreturn ;
        return ImageUtilities.icon2Image(icon);
    }
    
    private static String className(TreePath path) {
        ClassTree ct = (ClassTreepath.getLeaf();
        
        if (path.getParentPath().getLeaf().getKind() == .) {
            NewClassTree nct = (NewClassTreepath.getParentPath().getLeaf();
            
            if (nct.getClassBody() == ct) {
                return simpleName(nct.getIdentifier());
            }
        }
        
        return ct.getSimpleName().toString();
    }
    
    private static String simpleName(Tree t) {
        switch (t.getKind()) {
            case :
                return simpleName(((ParameterizedTypeTreet).getType());
            case :
                return ((IdentifierTreet).getName().toString();
            case :
                return ((MemberSelectTreet).getIdentifier().toString();
            default:
                return "";//XXX
        }
    }
    
    private static final class ChildrenFactoryImpl extends ChildFactory<Node> {
        private final TreePathHandle tph;
        public ChildrenFactoryImpl(TreePathHandle tph) {
            this. = tph;
        }
        @Override
        protected boolean createKeys(final List<NodetoPopulate) {
            try {
                JavaSource.forFileObject(.getFileObject()).runUserActionTask(new Task<CompilationController>() {
                    @Override public void run(final CompilationController ccthrows Exception {
                        cc.toPhase(.); //XXX: resolved?
                        final TreePath tp = .resolve(cc);
                        
                        if (tp == null) {
                             //XXX: log
                            return;
                        }
                        tp.getLeaf().accept(new TreeScanner<VoidTreePath>() {
                            @Override public Void scan(Tree nodeTreePath p) {
                                if (node == nullreturn null;
                                if (node.getKind() == .) {
                                    IfTree it = (IfTreenode;
                                    Node n = createBreadcrumbs(ccnew TreePath(pnode), false);
                                    assert n != null;
                                    toPopulate.add(n);
                                    if (it.getElseStatement() != null) {
                                        n = createBreadcrumbs(ccnew TreePath(pnode), true);
                                        assert n != null;
                                        toPopulate.add(n);
                                        
                                        if (it.getElseStatement().getKind() == .) {
                                            scan((IfTreeit.getElseStatement(), new TreePath(pnode));
                                        }
                                    }
                                    return null;
                                }
                                p = new TreePath(pnode);
                                if (cc.getTreeUtilities().isSynthetic(p)) return null;
                                Node n = createBreadcrumbs(ccpfalse);
                                if (n != null) {
                                    toPopulate.add(n);
                                } else {
                                    return super.scan(nodep);
                                }
                                return null;
                            }
                            @Override public Void visitMethod(MethodTree nodeTreePath p) {
                                return scan(node.getBody(), p);
                            }
                        }, tp);
                    }
                }, true);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            return true;
        }
        @Override
        protected Node createNodeForKey(Node key) {
            return key;
        }
        
    }
    
    private static final class OpenableImpl implements OpenableOpenCookie {
        private final FileObject file;
        private final int pos;
        public OpenableImpl(FileObject fileint pos) {
            this. = file;
            this. = pos;
        }
        
        @Override
        public void open() {
            UiUtils.open();
        }
        
    }
New to GrepCode? Check out our FAQ X