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.apisupport.refactoring;
043:
044: import java.awt.Color;
045: import java.io.File;
046: import java.io.IOException;
047: import java.net.URISyntaxException;
048: import java.net.URL;
049: import java.util.ArrayList;
050: import java.util.Collection;
051: import java.util.EnumSet;
052: import java.util.Set;
053: import java.util.StringTokenizer;
054: import javax.lang.model.element.Element;
055: import javax.lang.model.element.ExecutableElement;
056: import javax.lang.model.element.TypeElement;
057: import javax.lang.model.type.DeclaredType;
058: import javax.lang.model.type.TypeKind;
059: import javax.lang.model.type.TypeMirror;
060: import javax.lang.model.util.ElementFilter;
061: import javax.swing.text.AttributeSet;
062: import javax.swing.text.StyleConstants;
063: import org.netbeans.api.java.classpath.ClassPath;
064: import org.netbeans.api.java.project.JavaProjectConstants;
065: import org.netbeans.api.java.source.ClassIndex;
066: import org.netbeans.api.java.source.CompilationInfo;
067: import org.netbeans.api.java.source.ClasspathInfo;
068: import org.netbeans.api.java.source.ElementHandle;
069: import org.netbeans.api.java.source.SourceUtils;
070: import org.netbeans.api.project.FileOwnerQuery;
071: import org.netbeans.api.project.Project;
072: import org.netbeans.api.project.ProjectUtils;
073: import org.netbeans.api.project.SourceGroup;
074: import org.netbeans.api.project.ui.OpenProjects;
075: import org.openide.filesystems.FileObject;
076: import org.openide.filesystems.FileUtil;
077: import org.openide.util.Lookup;
078: import org.openide.filesystems.FileUtil;
079: import org.openide.filesystems.URLMapper;
080: import org.openide.util.Utilities;
081:
082: /**
083: *
084: * @author Jan Becicka
085: */
086: public class RetoucheUtils {
087:
088: private static final String JAVA_MIME_TYPE = "text/x-java";
089:
090: public static String htmlize(String input) {
091: String temp = org.openide.util.Utilities.replaceString(input,
092: "<", "<"); // NOI18N
093: temp = org.openide.util.Utilities.replaceString(temp, ">",
094: ">"); // NOI18N
095: return temp;
096: }
097:
098: public static Collection<ExecutableElement> getOverridenMethods(
099: ExecutableElement e, CompilationInfo info) {
100: return getOverridenMethods(e, SourceUtils
101: .getEnclosingTypeElement(e), info);
102: }
103:
104: private static Collection<ExecutableElement> getOverridenMethods(
105: ExecutableElement e, TypeElement parent,
106: CompilationInfo info) {
107: ArrayList<ExecutableElement> result = new ArrayList<ExecutableElement>();
108:
109: TypeMirror sup = parent.getSuperclass();
110: if (sup.getKind() == TypeKind.DECLARED) {
111: TypeElement next = (TypeElement) ((DeclaredType) sup)
112: .asElement();
113: ExecutableElement overriden = getMethod(e, next, info);
114: result.addAll(getOverridenMethods(e, next, info));
115: if (overriden != null) {
116: result.add(overriden);
117: }
118: }
119: for (TypeMirror tm : parent.getInterfaces()) {
120: TypeElement next = (TypeElement) ((DeclaredType) tm)
121: .asElement();
122: ExecutableElement overriden2 = getMethod(e, next, info);
123: result.addAll(getOverridenMethods(e, next, info));
124: if (overriden2 != null) {
125: result.add(overriden2);
126: }
127: }
128: return result;
129: }
130:
131: private static ExecutableElement getMethod(
132: ExecutableElement method, TypeElement type,
133: CompilationInfo info) {
134: for (ExecutableElement met : ElementFilter.methodsIn(type
135: .getEnclosedElements())) {
136: if (info.getElements().overrides(method, met, type)) {
137: return met;
138: }
139: }
140: return null;
141: }
142:
143: public static Collection<ExecutableElement> getOverridingMethods(
144: ExecutableElement e, CompilationInfo info) {
145: Collection<ExecutableElement> result = new ArrayList();
146: TypeElement parentType = (TypeElement) e.getEnclosingElement();
147: //XXX: Fixme IMPLEMENTORS_RECURSIVE were removed
148: Set<ElementHandle<TypeElement>> subTypes = info
149: .getClasspathInfo().getClassIndex().getElements(
150: ElementHandle.create(parentType),
151: EnumSet.of(ClassIndex.SearchKind.IMPLEMENTORS),
152: EnumSet.of(ClassIndex.SearchScope.SOURCE));
153: for (ElementHandle<TypeElement> subTypeHandle : subTypes) {
154: TypeElement type = subTypeHandle.resolve(info);
155: for (ExecutableElement method : ElementFilter
156: .methodsIn(type.getEnclosedElements())) {
157: if (info.getElements().overrides(method, e, type)) {
158: result.add(method);
159: }
160: }
161: }
162: return result;
163: }
164:
165: public static boolean isJavaFile(FileObject f) {
166: return JAVA_MIME_TYPE.equals(f.getMIMEType()); //NOI18N
167: }
168:
169: private static String color(String string, AttributeSet set) {
170: if (set == null)
171: return string;
172: if (string.trim().length() == 0) {
173: return Utilities.replaceString(Utilities.replaceString(
174: string, " ", " "), "\n", "<br>"); //NOI18N
175: }
176: StringBuffer buf = new StringBuffer(string);
177: if (StyleConstants.isBold(set)) {
178: buf.insert(0, "<b>"); //NOI18N
179: buf.append("</b>"); //NOI18N
180: }
181: if (StyleConstants.isItalic(set)) {
182: buf.insert(0, "<i>"); //NOI18N
183: buf.append("</i>"); //NOI18N
184: }
185: if (StyleConstants.isStrikeThrough(set)) {
186: buf.insert(0, "<s>");
187: buf.append("</s>");
188: }
189: buf
190: .insert(0, "<font color="
191: + getHTMLColor(StyleConstants
192: .getForeground(set)) + ">"); //NOI18N
193: buf.append("</font>"); //NOI18N
194: return buf.toString();
195: }
196:
197: private static String getHTMLColor(Color c) {
198: String colorR = "0" + Integer.toHexString(c.getRed()); //NOI18N
199: colorR = colorR.substring(colorR.length() - 2);
200: String colorG = "0" + Integer.toHexString(c.getGreen()); //NOI18N
201: colorG = colorG.substring(colorG.length() - 2);
202: String colorB = "0" + Integer.toHexString(c.getBlue()); //NOI18N
203: colorB = colorB.substring(colorB.length() - 2);
204: String html_color = "#" + colorR + colorG + colorB; //NOI18N
205: return html_color;
206: }
207:
208: public static boolean isFromLibrary(Element element,
209: ClasspathInfo info) {
210: SourceUtils.getFile(element, info);
211: return FileUtil.getArchiveFile(SourceUtils.getFile(element,
212: info)) != null;
213: }
214:
215: public static boolean isValidPackageName(String name) {
216: StringTokenizer tokenizer = new StringTokenizer(name, "."); // NOI18N
217: while (tokenizer.hasMoreTokens()) {
218: if (!Utilities.isJavaIdentifier(tokenizer.nextToken())) {
219: return false;
220: }
221: }
222: return true;
223: }
224:
225: public static boolean isFileInOpenProject(FileObject file) {
226: assert file != null;
227: Project p = FileOwnerQuery.getOwner(file);
228: Project[] opened = OpenProjects.getDefault().getOpenProjects();
229: for (int i = 0; i < opened.length; i++) {
230: if (p == opened[i])
231: return true;
232: }
233: return false;
234: }
235:
236: public static boolean isOnSourceClasspath(FileObject fo) {
237: Project p = FileOwnerQuery.getOwner(fo);
238: if (p == null)
239: return false;
240: Project[] opened = OpenProjects.getDefault().getOpenProjects();
241: for (int i = 0; i < opened.length; i++) {
242: if (p == opened[i]) {
243: SourceGroup[] gr = ProjectUtils.getSources(p)
244: .getSourceGroups(
245: JavaProjectConstants.SOURCES_TYPE_JAVA);
246: for (int j = 0; j < gr.length; j++) {
247: if (fo == gr[j].getRootFolder())
248: return true;
249: if (FileUtil.isParentOf(gr[j].getRootFolder(), fo))
250: return true;
251: }
252: return false;
253: }
254: }
255: return false;
256: }
257:
258: public static boolean isClasspathRoot(FileObject fo) {
259: return fo.equals(ClassPath.getClassPath(fo, ClassPath.SOURCE)
260: .findOwnerRoot(fo));
261: }
262:
263: public static boolean isRefactorable(FileObject file) {
264: return isJavaFile(file) && isFileInOpenProject(file)
265: && isOnSourceClasspath(file);
266: }
267:
268: public static String getPackageName(FileObject folder) {
269: assert folder.isFolder() : "argument must be folder";
270: return ClassPath.getClassPath(folder, ClassPath.SOURCE)
271: .getResourceName(folder, '.', false);
272: }
273:
274: public static String getPackageName(URL url) {
275: FileObject result = URLMapper.findFileObject(url);
276: if (result != null)
277: return getPackageName(result);
278:
279: File f = new File(url.getPath());
280:
281: do {
282: FileObject fo = FileUtil.toFileObject(f);
283: if (fo != null) {
284: return getPackageName(fo);
285: }
286: f = f.getParentFile();
287: } while (f != null);
288: throw new IllegalArgumentException(
289: "Cannot create package name for url " + url);
290: }
291:
292: /**
293: * creates or finds FileObject according to
294: * @param url
295: * @return FileObject
296: */
297: public static FileObject getOrCreateFolder(URL url)
298: throws IOException {
299: try {
300: FileObject result = URLMapper.findFileObject(url);
301: if (result != null)
302: return result;
303: File f = new File(url.toURI());
304:
305: result = FileUtil.createFolder(f);
306: return result;
307: } catch (URISyntaxException ex) {
308: throw (IOException) new IOException().initCause(ex);
309: }
310: }
311:
312: }
|