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: package org.netbeans.modules.refactoring.java.api;
042:
043: import com.sun.source.tree.Tree;
044: import com.sun.source.util.TreePath;
045: import java.util.Collections;
046: import java.util.Set;
047: import java.util.Set;
048: import javax.lang.model.element.Element;
049: import javax.lang.model.element.ElementKind;
050: import javax.lang.model.element.Modifier;
051: import javax.lang.model.type.TypeMirror;
052: import javax.swing.Icon;
053: import org.netbeans.api.java.source.CompilationInfo;
054: import org.netbeans.api.java.source.ElementHandle;
055: import org.netbeans.api.java.source.TreePathHandle;
056: import org.netbeans.api.java.source.TypeMirrorHandle;
057: import org.netbeans.api.java.source.TypeMirrorHandle;
058: import org.netbeans.api.java.source.UiUtils;
059: import org.netbeans.api.java.source.UiUtils.PrintPart;
060:
061: /**
062: * Wrapper class for ElementHandles, TreePathHandles and TypeMirrorHandles.
063: * It contains referemce to appropriste handle + name and icon
064: * @param H
065: * @author Jan Becicka
066: */
067: public final class MemberInfo<H> {
068: private H member;
069: private String htmlText;
070: private Icon icon;
071: private Group group;
072: private Set<Modifier> modifiers;
073: private boolean makeAbstract;
074: private String name;
075:
076: public String getName() {
077: return name;
078: }
079:
080: public void setName(String name) {
081: this .name = name;
082: }
083:
084: public enum Group {
085: /**
086: * member is in implements clause
087: */
088: IMPLEMENTS,
089: /**
090: * member represents method
091: */
092: METHOD,
093: /**
094: * member represents field
095: */
096: FIELD,
097: /**
098: * member represents type
099: */
100: TYPE;
101: }
102:
103: /** Creates a new instance of MemberInfo describing a field
104: * to be pulled up.
105: * @param member
106: * @param name
107: * @param htmlText
108: * @param icon
109: */
110: private MemberInfo(H member, String name, String htmlText, Icon icon) {
111: this .member = member;
112: this .htmlText = htmlText;
113: this .icon = icon;
114: this .name = name;
115: }
116:
117: public H getElementHandle() {
118: return member;
119: }
120:
121: /**
122: *
123: * @return
124: */
125: public String getHtmlText() {
126: return htmlText;
127: }
128:
129: public static <T extends TypeMirror> MemberInfo<TypeMirrorHandle<T>> create(
130: T el, Tree t, CompilationInfo c) {
131: MemberInfo<TypeMirrorHandle<T>> mi = new MemberInfo<TypeMirrorHandle<T>>(
132: TypeMirrorHandle.create(el), t.toString(),
133: "implements " + t.toString(), UiUtils.getElementIcon(
134: ElementKind.INTERFACE, null)); // NOI18N
135: mi.group = Group.IMPLEMENTS;
136: return mi;
137: }
138:
139: public static <T extends Element> MemberInfo<ElementHandle<T>> create(
140: T el, CompilationInfo c) {
141: String format = PrintPart.NAME;
142: Group g = Group.TYPE;
143: if (el.getKind() == ElementKind.FIELD) {
144: format += " : " + PrintPart.TYPE; // NOI18N
145: g = Group.FIELD;
146: } else if (el.getKind() == ElementKind.METHOD) {
147: format += PrintPart.PARAMETERS + " : " + PrintPart.TYPE; // NOI18N
148: g = Group.METHOD;
149: }
150:
151: MemberInfo<ElementHandle<T>> mi = new MemberInfo<ElementHandle<T>>(
152: ElementHandle.create(el),
153: el.getSimpleName().toString(), UiUtils.getHeader(el, c,
154: format), UiUtils.getDeclarationIcon(el));
155: mi.modifiers = el.getModifiers();
156: mi.group = g;
157: return mi;
158: }
159:
160: public static <T extends Element> MemberInfo<ElementHandle<T>> create(
161: T el, CompilationInfo c, Group group) {
162: MemberInfo<ElementHandle<T>> mi = new MemberInfo<ElementHandle<T>>(
163: ElementHandle.create(el),
164: el.getSimpleName().toString(), UiUtils.getHeader(el, c,
165: UiUtils.PrintPart.NAME), UiUtils
166: .getDeclarationIcon(el));
167: mi.group = group;
168: mi.modifiers = el.getModifiers();
169: return mi;
170: }
171:
172: public static MemberInfo<TreePathHandle> create(TreePath tpath,
173: CompilationInfo c) {
174: String format = PrintPart.NAME;
175: Group g = null;
176: Element el = c.getTrees().getElement(tpath);
177: if (el.getKind() == ElementKind.FIELD) {
178: format += " : " + PrintPart.TYPE; // NOI18N
179: g = Group.FIELD;
180: } else if (el.getKind() == ElementKind.METHOD) {
181: format += PrintPart.PARAMETERS + " : " + PrintPart.TYPE; // NOI18N
182: g = Group.METHOD;
183: } else if (el.getKind().isInterface()) {
184: g = Group.IMPLEMENTS;
185: format = "implements " + format; // NOI18N
186: }
187:
188: MemberInfo<TreePathHandle> mi = new MemberInfo<TreePathHandle>(
189: TreePathHandle.create(tpath, c), el.getSimpleName()
190: .toString(), UiUtils.getHeader(el, c, format),
191: UiUtils.getDeclarationIcon(el));
192: mi.modifiers = el.getModifiers();
193: mi.group = g;
194: return mi;
195: }
196:
197: private MemberInfo(H handle, String htmlText, Icon icon,
198: String name, Group group, Set<Modifier> modifiers,
199: boolean makeAbstract) {
200: this .member = handle;
201: this .htmlText = htmlText;
202: this .icon = icon;
203: this .name = name;
204: this .group = group;
205: this .modifiers = modifiers;
206: this .makeAbstract = makeAbstract;
207: }
208:
209: public static <T extends TypeMirror> MemberInfo<TypeMirrorHandle<T>> createImplements(
210: TypeMirrorHandle handle, String htmlText, Icon icon,
211: String name) {
212: return new MemberInfo<TypeMirrorHandle<T>>(handle, htmlText,
213: icon, name, Group.IMPLEMENTS, Collections
214: .<Modifier> emptySet(), false);
215: }
216:
217: public Icon getIcon() {
218: return icon;
219: }
220:
221: public Group getGroup() {
222: return group;
223: }
224:
225: @Override
226: public boolean equals(Object o) {
227: if (o instanceof MemberInfo
228: && ((MemberInfo) o).member instanceof ElementHandle) {
229: return ((ElementHandle) ((MemberInfo) o).member)
230: .signatureEquals((ElementHandle) this .member);
231: }
232: return super .equals(o);
233: }
234:
235: @Override
236: public int hashCode() {
237: return member.hashCode();
238: }
239:
240: public Set<Modifier> getModifiers() {
241: return modifiers;
242: }
243:
244: public boolean isMakeAbstract() {
245: return makeAbstract;
246: }
247:
248: public void setMakeAbstract(Boolean b) {
249: this .makeAbstract = b;
250: }
251:
252: @Override
253: public String toString() {
254: return htmlText;
255: }
256:
257: }
|