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.cnd.classview;
043:
044: import java.util.HashMap;
045: import org.netbeans.modules.cnd.api.model.CsmClass;
046: import org.netbeans.modules.cnd.api.model.CsmCompoundClassifier;
047: import org.netbeans.modules.cnd.api.model.CsmEnum;
048: import org.netbeans.modules.cnd.api.model.CsmEnumerator;
049: import org.netbeans.modules.cnd.api.model.CsmFriend;
050: import org.netbeans.modules.cnd.api.model.CsmFriendClass;
051: import org.netbeans.modules.cnd.api.model.CsmFriendFunction;
052: import org.netbeans.modules.cnd.api.model.CsmFunction;
053: import org.netbeans.modules.cnd.api.model.CsmIdentifiable;
054: import org.netbeans.modules.cnd.api.model.CsmMember;
055: import org.netbeans.modules.cnd.api.model.CsmNamespace;
056: import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
057: import org.netbeans.modules.cnd.api.model.CsmType;
058: import org.netbeans.modules.cnd.api.model.CsmTypedef;
059: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
060: import org.netbeans.modules.cnd.classview.model.ClassNode;
061: import org.netbeans.modules.cnd.classview.model.EnumNode;
062: import org.netbeans.modules.cnd.classview.model.EnumeratorNode;
063: import org.netbeans.modules.cnd.classview.model.FriendClassNode;
064: import org.netbeans.modules.cnd.classview.model.FriendFunctionNode;
065: import org.netbeans.modules.cnd.classview.model.GlobalFuncNode;
066: import org.netbeans.modules.cnd.classview.model.MemberNode;
067: import org.openide.nodes.Node;
068:
069: /**
070: *
071: * @author Alexander Simon
072: */
073: public class ClassifierKeyArray extends HostKeyArray implements
074: UpdatebleHost {
075: private static final boolean traceEvents = Boolean
076: .getBoolean("cnd.classview.key-events"); // NOI18N
077:
078: public ClassifierKeyArray(ChildrenUpdater childrenUpdater,
079: CsmCompoundClassifier classifier) {
080: super (childrenUpdater, classifier.getContainingFile()
081: .getProject(), PersistentKey.createKey(classifier));
082: }
083:
084: public ClassifierKeyArray(ChildrenUpdater childrenUpdater,
085: CsmTypedef typedef, CsmCompoundClassifier classifier) {
086: super (childrenUpdater, classifier.getContainingFile()
087: .getProject(), PersistentKey.createKey(typedef));
088: }
089:
090: @Override
091: public boolean newNamespsce(CsmNamespace ns) {
092: return false;
093: }
094:
095: @Override
096: public boolean removeNamespsce(CsmNamespace ns) {
097: return false;
098: }
099:
100: protected boolean canCreateNode(CsmOffsetableDeclaration d) {
101: return true;
102: }
103:
104: protected java.util.Map<PersistentKey, SortedName> getMembers() {
105: java.util.Map<PersistentKey, SortedName> res = new HashMap<PersistentKey, SortedName>();
106: try {
107: CsmCompoundClassifier classifier = getClassifier();
108: if (classifier != null) {
109: if (CsmKindUtilities.isClass(classifier)) {
110: initClass((CsmClass) classifier, res);
111: } else if (CsmKindUtilities.isEnum(classifier)) {
112: initEnum((CsmEnum) classifier, res);
113: }
114: }
115: } catch (AssertionError ex) {
116: ex.printStackTrace();
117: } catch (Exception ex) {
118: ex.printStackTrace();
119: }
120: return res;
121: }
122:
123: private void initClass(CsmClass cls,
124: java.util.Map<PersistentKey, SortedName> res) {
125: for (CsmMember member : cls.getMembers()) {
126: PersistentKey key = PersistentKey.createKey(member);
127: if (key != null) {
128: res.put(key, getSortedName(member));
129: }
130: }
131: for (CsmFriend friend : cls.getFriends()) {
132: PersistentKey key = PersistentKey.createKey(friend);
133: if (key != null) {
134: res.put(key, getSortedName(friend));
135: }
136: }
137: }
138:
139: private void initEnum(CsmEnum en,
140: java.util.Map<PersistentKey, SortedName> res) {
141: for (CsmEnumerator val : en.getEnumerators()) {
142: PersistentKey key = PersistentKey.createKey(val);
143: if (key != null) {
144: res.put(key, new SortedName(0, val.getName(), 0));
145: }
146: }
147: }
148:
149: private CsmCompoundClassifier getClassifier() {
150: CsmIdentifiable object = getHostId().getObject();
151: if (object instanceof CsmCompoundClassifier) {
152: return (CsmCompoundClassifier) object;
153: } else {
154: CsmTypedef def = (CsmTypedef) object;
155: CsmType type = def.getType();
156: if (type != null) {
157: return (CsmCompoundClassifier) type.getClassifier();
158: }
159: }
160: return null;
161: }
162:
163: protected CsmOffsetableDeclaration findDeclaration(
164: PersistentKey declId) {
165: CsmOffsetableDeclaration res = (CsmOffsetableDeclaration) declId
166: .getObject();
167: return res;
168: }
169:
170: private CsmNamespace findNamespace(String nsId) {
171: return getProject().findNamespace(nsId);
172: }
173:
174: protected Node createNode(PersistentKey key) {
175: ChildrenUpdater updater = getUpdater();
176: Node node = null;
177: if (updater != null) {
178: try {
179: CsmOffsetableDeclaration member = findDeclaration(key);
180: if (member != null) {
181: if (CsmKindUtilities.isClass(member)) {
182: node = new ClassNode((CsmClass) member,
183: new ClassifierKeyArray(updater,
184: (CsmClass) member));
185: } else if (CsmKindUtilities.isEnum(member)) {
186: node = new EnumNode((CsmEnum) member,
187: new ClassifierKeyArray(updater,
188: (CsmEnum) member));
189: } else if (CsmKindUtilities.isEnumerator(member)) {
190: node = new EnumeratorNode(
191: (CsmEnumerator) member);
192: } else if (CsmKindUtilities.isFriendClass(member)) {
193: node = new FriendClassNode(
194: (CsmFriendClass) member);
195: } else if (CsmKindUtilities.isFriendMethod(member)) {
196: node = new FriendFunctionNode(
197: (CsmFriendFunction) member);
198: } else if (CsmKindUtilities.isClassMember(member)) {
199: node = new MemberNode((CsmMember) member);
200: } else if (CsmKindUtilities.isFunction(member)) {
201: if (traceEvents) {
202: System.out.println("It should be member:"
203: + member.getUniqueName()); // NOI18N
204: }
205: node = new GlobalFuncNode((CsmFunction) member);
206: } else {
207: if (traceEvents) {
208: System.out.println("It should be member:"
209: + member.getUniqueName()); // NOI18N
210: }
211: }
212: }
213: } catch (AssertionError ex) {
214: ex.printStackTrace();
215: } catch (Exception ex) {
216: ex.printStackTrace();
217: }
218: }
219: return node;
220: }
221: }
|