KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > tree > FolderTreeElement


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.refactoring.java.ui.tree;
21
22 import java.util.Collections JavaDoc;
23 import javax.lang.model.element.ElementKind;
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.ImageIcon JavaDoc;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.netbeans.api.java.project.JavaProjectConstants;
28 import org.netbeans.api.java.source.UiUtils;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.api.queries.VisibilityQuery;
35 import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
36 import org.netbeans.modules.refactoring.spi.ui.*;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.util.Utilities;
40
41 /**
42  *
43  * @author Jan Becicka
44  */

45 public class FolderTreeElement implements TreeElement {
46     
47     private FileObject fo;
48     FolderTreeElement(FileObject fo) {
49         this.fo = fo;
50     }
51         
52     
53     public TreeElement getParent(boolean isLogical) {
54         if (isLogical) {
55             SourceGroup sg = getSourceGroup(fo);
56             if (sg!=null) {
57                 return TreeElementFactory.getTreeElement(sg);
58             } else {
59                 return null;
60             }
61         } else {
62             Project p = FileOwnerQuery.getOwner(fo);
63             if (p!=null) {
64                 return TreeElementFactory.getTreeElement(p);
65             } else {
66                 return null;
67             }
68         }
69     }
70
71     public Icon JavaDoc getIcon() {
72         return UiUtils.getElementIcon(ElementKind.PACKAGE, null);
73     }
74
75     public String JavaDoc getText(boolean isLogical) {
76         return ClassPath.getClassPath(fo, ClassPath.SOURCE).getResourceName(fo).replace('/','.');
77     }
78
79     static SourceGroup getSourceGroup(FileObject file) {
80         Project prj = FileOwnerQuery.getOwner(file);
81         if (prj == null)
82             return null;
83         Sources src = ProjectUtils.getSources(prj);
84         SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
85         for(int i=0; i<groups.length; i++) {
86             if (groups[i].getRootFolder().equals(file) || FileUtil.isParentOf(groups[i].getRootFolder(), file))
87                 return groups[i];
88         }
89         return null;
90     }
91
92     public Object JavaDoc getUserObject() {
93         return fo;
94     }
95 }
96
Popular Tags