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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.cnd.gotodeclaration.element.providers;
029:
030: import javax.swing.Icon;
031: import org.netbeans.modules.cnd.gotodeclaration.element.spi.ElementDescriptor;
032:
033: import org.netbeans.modules.cnd.api.model.*;
034: import org.netbeans.modules.cnd.modelutil.CsmImageLoader;
035: import org.netbeans.modules.cnd.modelutil.CsmUtilities;
036: import org.openide.util.NbBundle;
037:
038: /**
039: * Acommon base class for functions, variables, macros, etc. descriptors
040: * Based on CsmOffsetable
041: *
042: * @author Vladimir Kvashin
043: */
044: public abstract class BaseElementDescriptor implements
045: ElementDescriptor {
046:
047: private static final String contextNameFormat = ' ' + NbBundle
048: .getMessage(BaseProvider.class, "CONTEXT_NAME_FORMAT");
049:
050: protected abstract CsmOffsetable getElement();
051:
052: protected abstract String getContextNameImpl();
053:
054: public String getContextName() {
055: String contextName = getContextNameImpl();
056: if (contextName != null && contextName.length() > 0) {
057: contextName = String.format(contextNameFormat, contextName);
058: }
059: return contextName;
060: }
061:
062: public String getAbsoluteFileName() {
063: CsmOffsetable element = getElement();
064: return (element == null) ? null : element.getContainingFile()
065: .getAbsolutePath().toString();
066: }
067:
068: protected CsmProject getProject() {
069: CsmOffsetable element = getElement();
070: return (element == null) ? null : element.getContainingFile()
071: .getProject();
072: }
073:
074: public Icon getProjectIcon() {
075: return CsmImageLoader.getIcon(getProject());
076: }
077:
078: public String getProjectName() {
079: CsmProject project = getProject();
080: return (project == null) ? "" : project.getName().toString();
081: }
082:
083: public void open() {
084: CsmUtilities.openSource(getElement());
085: }
086:
087: @Override
088: public boolean equals(Object obj) {
089: if (obj == null) {
090: return false;
091: }
092: if (getClass() != obj.getClass()) {
093: return false;
094: }
095: return equals(this .getElement(), ((BaseElementDescriptor) obj)
096: .getElement());
097: }
098:
099: private static boolean equals(CsmOffsetable element1,
100: CsmOffsetable element2) {
101: if (element1 == element2) {
102: return true;
103: } else if (element1 == null) {
104: return element2 == null;
105: } else if (element2 == null) {
106: return element1 == null;
107: } else {
108: CsmFile file1 = element1.getContainingFile();
109: CsmFile file2 = element2.getContainingFile();
110: if (file1 == null || file2 == null) {
111: return element1 == element2;
112: } else if (!file1.getAbsolutePath().equals(
113: file2.getAbsolutePath())) {
114: return false;
115: } else {
116: return element1.getStartOffset() == element2
117: .getStartOffset();
118: }
119: }
120: }
121:
122: @Override
123: public int hashCode() {
124: CsmOffsetable element = getElement();
125: if (element == null) {
126: return super .hashCode();
127: } else {
128: int hash = 5;
129: String path = element.getContainingFile().getAbsolutePath()
130: .toString();
131: hash = 19 * hash + ((path == null) ? 0 : path.hashCode());
132: hash = 19 * hash + element.getStartOffset();
133: return hash;
134: }
135: }
136:
137: }
|