cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardUtil.java
author wpaul
Mon, 10 Aug 2009 13:11:33 -0500
changeset 61 ff389f2cc4aa
parent 37 c2bce6dd59e7
permissions -rw-r--r--
fixed bug #9585.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     1
/*******************************************************************************
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     2
 * Copyright (c) 2005, 2009 QNX Software Systems and others.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     3
 * All rights reserved. This program and the accompanying materials
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     4
 * are made available under the terms of the Eclipse Public License v1.0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     5
 * which accompanies this distribution, and is available at
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     6
 * http://www.eclipse.org/legal/epl-v10.html
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     7
 *
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     8
 * Contributors:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     9
 *     QNX Software Systems - initial API and implementation
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    10
 *     IBM Corporation
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    11
 *     Markus Schorn (Wind River Systems)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    12
 *     Warren Paul (Nokia) - 174238
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    13
 *******************************************************************************/
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    14
package org.eclipse.cdt.internal.ui.wizards.classwizard;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    15
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    16
import java.util.ArrayList;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    17
import java.util.List;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    18
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    19
import org.eclipse.core.resources.IFile;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    20
import org.eclipse.core.resources.IProject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    21
import org.eclipse.core.resources.IResource;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    22
import org.eclipse.core.resources.IWorkspaceRoot;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    23
import org.eclipse.core.resources.ResourcesPlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    24
import org.eclipse.core.runtime.CoreException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    25
import org.eclipse.core.runtime.IAdaptable;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    26
import org.eclipse.core.runtime.IPath;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    27
import org.eclipse.core.runtime.NullProgressMonitor;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    28
import org.eclipse.core.runtime.Path;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    29
import org.eclipse.jface.operation.IRunnableContext;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    30
import org.eclipse.jface.viewers.IStructuredSelection;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    31
import org.eclipse.ui.IEditorInput;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    32
import org.eclipse.ui.IEditorPart;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    33
import org.eclipse.ui.IWorkbenchPart;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    34
import org.eclipse.ui.views.contentoutline.ContentOutline;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    35
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    36
import org.eclipse.cdt.core.CCorePlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    37
import org.eclipse.cdt.core.browser.AllTypesCache;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    38
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    39
import org.eclipse.cdt.core.browser.ITypeInfo;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    40
import org.eclipse.cdt.core.browser.ITypeReference;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    41
import org.eclipse.cdt.core.browser.TypeSearchScope;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    42
import org.eclipse.cdt.core.dom.ILinkage;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    43
import org.eclipse.cdt.core.dom.ast.DOMException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    44
import org.eclipse.cdt.core.dom.ast.IBinding;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    45
import org.eclipse.cdt.core.dom.ast.IEnumeration;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    46
import org.eclipse.cdt.core.dom.ast.ITypedef;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    47
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    48
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    49
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    50
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    51
import org.eclipse.cdt.core.index.IIndex;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    52
import org.eclipse.cdt.core.index.IndexFilter;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    53
import org.eclipse.cdt.core.model.CModelException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    54
import org.eclipse.cdt.core.model.CoreModel;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    55
import org.eclipse.cdt.core.model.ICContainer;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    56
import org.eclipse.cdt.core.model.ICElement;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    57
import org.eclipse.cdt.core.model.ICProject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    58
import org.eclipse.cdt.core.model.ISourceRoot;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    59
import org.eclipse.cdt.core.parser.IScannerInfo;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    60
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    61
import org.eclipse.cdt.ui.CUIPlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    62
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    63
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    64
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    65
import org.eclipse.cdt.internal.ui.editor.CEditor;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    66
import org.eclipse.cdt.internal.ui.viewsupport.IViewPartInputProvider;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    67
import org.eclipse.cdt.internal.ui.wizards.filewizard.NewSourceFileGenerator;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    68
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    69
public class NewClassWizardUtil {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    70
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    71
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    72
     * Returns the parent source folder of the given element. If the given
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    73
     * element is already a source folder, the element itself is returned.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    74
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    75
     * @param element the C Element
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    76
     * @return the source folder
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    77
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    78
    public static ICContainer getSourceFolder(ICElement element) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    79
        ICContainer folder = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    80
        boolean foundSourceRoot = false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    81
        ICElement curr = element;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    82
        while (curr != null && !foundSourceRoot) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    83
            if (curr instanceof ICContainer && folder == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    84
                folder = (ICContainer)curr;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    85
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    86
            foundSourceRoot = (curr instanceof ISourceRoot);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    87
            curr = curr.getParent();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    88
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    89
        if (folder == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    90
            ICProject cproject = element.getCProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    91
            folder = cproject.findSourceRoot(cproject.getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    92
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    93
        return folder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    94
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    95
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    96
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    97
     * Returns the parent source folder for the given path. If the given
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    98
     * path is already a source folder, the corresponding C element is returned.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    99
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   100
     * @param path the path
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   101
     * @return the source folder
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   102
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   103
    public static ICContainer getSourceFolder(IPath path) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   104
        if (path == null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   105
            return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   106
        while (path.segmentCount() > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   107
            IResource res = getWorkspaceRoot().findMember(path);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   108
            if (res != null && res.exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   109
                int resType = res.getType();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   110
                if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   111
                    ICElement elem = CoreModel.getDefault().create(res.getFullPath());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   112
                    if (elem != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   113
                        ICContainer sourceFolder = getSourceFolder(elem);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   114
                        if (sourceFolder != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   115
                            return sourceFolder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   116
                        if (resType == IResource.PROJECT) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   117
                        	if (elem instanceof ICContainer) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   118
                                return (ICContainer)elem;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   119
                        	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   120
                        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   121
                    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   122
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   123
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   124
            path = path.removeLastSegments(1);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   125
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   126
        return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   127
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   128
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   129
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   130
     * Returns the parent source folder for the given resource. If the given
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   131
     * resource is already a source folder, the corresponding C element is returned.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   132
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   133
     * @param resource the resource
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   134
     * @return the source folder
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   135
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   136
    public static ICContainer getSourceFolder(IResource resource) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   137
        if (resource != null && resource.exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   138
            int resType = resource.getType();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   139
            if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   140
                ICElement elem = CoreModel.getDefault().create(resource.getFullPath());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   141
                if (elem != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   142
                    ICContainer sourceFolder = getSourceFolder(elem);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   143
                    if (sourceFolder != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   144
                        return sourceFolder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   145
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   146
            } else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   147
                return getSourceFolder(resource.getParent());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   148
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   149
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   150
        return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   151
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   152
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   153
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   154
     * Returns the first source root in the given project. If the project has
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   155
     * no source roots as children, the project itself is returned.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   156
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   157
     * @param cproject
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   158
     * @return the source root
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   159
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   160
    public static ISourceRoot getFirstSourceRoot(ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   161
        ISourceRoot folder = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   162
        try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   163
            if (cproject.exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   164
                ISourceRoot[] roots = cproject.getSourceRoots();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   165
                if (roots != null && roots.length > 0)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   166
                    folder = roots[0];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   167
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   168
        } catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   169
            CUIPlugin.log(e);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   170
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   171
        if (folder == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   172
            folder = cproject.findSourceRoot(cproject.getResource());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   173
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   174
        return folder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   175
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   176
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   177
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   178
     * Returns the C Element which corresponds to the given selection.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   179
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   180
     * @param selection the selection to be inspected
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   181
     * @return a C element matching the selection, or <code>null</code>
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   182
     * if no C element exists in the given selection
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   183
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   184
    public static ICElement getCElementFromSelection(IStructuredSelection selection) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   185
        ICElement celem = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   186
        if (selection != null && !selection.isEmpty()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   187
            Object selectedElement = selection.getFirstElement();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   188
            if (selectedElement instanceof IAdaptable) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   189
                IAdaptable adaptable = (IAdaptable) selectedElement;            
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   190
                
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   191
                celem = (ICElement) adaptable.getAdapter(ICElement.class);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   192
                if (celem == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   193
                    IResource resource = (IResource) adaptable.getAdapter(IResource.class);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   194
                    if (resource != null && resource.getType() != IResource.ROOT) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   195
                        while (celem == null && resource.getType() != IResource.PROJECT) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   196
                            celem = (ICElement) resource.getAdapter(ICElement.class);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   197
                            resource = resource.getParent();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   198
                        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   199
                        if (celem == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   200
                            celem = CoreModel.getDefault().create(resource); // c project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   201
                        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   202
                    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   203
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   204
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   205
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   206
        return celem;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   207
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   208
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   209
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   210
     * Returns the C Element which corresponds to the active editor.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   211
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   212
     * @return a C element matching the active editor, or <code>null</code>
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   213
     * if no C element can be found
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   214
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   215
    public static ICElement getCElementFromEditor() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   216
        ICElement celem = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   217
        IWorkbenchPart part = CUIPlugin.getActivePage().getActivePart();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   218
        if (part instanceof ContentOutline) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   219
            part = CUIPlugin.getActivePage().getActiveEditor();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   220
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   221
        if (part instanceof IViewPartInputProvider) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   222
            Object elem = ((IViewPartInputProvider)part).getViewPartInput();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   223
            if (elem instanceof ICElement) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   224
                celem = (ICElement) elem;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   225
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   226
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   227
        if (celem == null && part instanceof CEditor) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   228
            IEditorInput input = ((IEditorPart)part).getEditorInput();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   229
            if (input != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   230
                final IResource res = (IResource) input.getAdapter(IResource.class);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   231
                if (res != null && res instanceof IFile) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   232
                    celem = CoreModel.getDefault().create((IFile)res);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   233
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   234
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   235
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   236
        return celem;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   237
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   238
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   239
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   240
     * Returns the parent namespace for the given element. If the given element is
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   241
     * already a namespace, the element itself is returned.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   242
     *
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   243
     * @param element the given C Element
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   244
     * @return the C Element for the namespace, or <code>null</code> if not found
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   245
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   246
    public static ICElement getNamespace(ICElement element) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   247
        ICElement curr = element;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   248
        while (curr != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   249
            int type = curr.getElementType();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   250
            if (type == ICElement.C_UNIT) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   251
                break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   252
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   253
            if (type == ICElement.C_NAMESPACE) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   254
                return curr;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   255
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   256
            curr = curr.getParent();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   257
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   258
        return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   259
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   260
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   261
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   262
     * Creates a header file name from the given class name. This is the file name
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   263
     * to be used when the class is created. eg. "MyClass" -> "MyClass.h"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   264
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   265
     * @param className the class name
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   266
     * @return the header file name for the given class
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   267
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   268
    public static String createHeaderFileName(String className) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   269
        return NewSourceFileGenerator.generateHeaderFileNameFromClass(className);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   270
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   271
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   272
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   273
     * Creates a source file name from the given class name. This is the file name
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   274
     * to be used when the class is created. eg. "MyClass" -> "MyClass.cpp"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   275
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   276
     * @param className the class name
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   277
     * @return the source file name for the given class
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   278
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   279
    public static String createSourceFileName(String className) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   280
        return NewSourceFileGenerator.generateSourceFileNameFromClass(className);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   281
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   282
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   283
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   284
     * Returns the workspace root.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   285
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   286
     * @return the workspace root
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   287
     */ 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   288
    public static IWorkspaceRoot getWorkspaceRoot() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   289
        return ResourcesPlugin.getWorkspace().getRoot();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   290
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   291
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   292
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   293
     * Resolve the location of the given class.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   294
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   295
     * @param type the class to resolve
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   296
     * @param context the runnable context
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   297
     * @return the class location, or <code>null</code> if not found
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   298
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   299
    public static ITypeReference resolveClassLocation(ITypeInfo type, IRunnableContext context) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   300
        return type.getResolvedReference();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   301
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   302
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   303
    private static final int[] CLASS_TYPES = { ICElement.C_CLASS, ICElement.C_STRUCT };
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   304
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   305
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   306
     * Returns all classes/structs which are accessible from the include
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   307
     * paths of the given project.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   308
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   309
     * @param cProject the given project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   310
     * @return array of classes/structs
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   311
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   312
    public static ITypeInfo[] getReachableClasses(ICProject cProject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   313
        ITypeInfo[] elements = AllTypesCache.getTypes(new TypeSearchScope(true), CLASS_TYPES);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   314
        if (elements != null && elements.length > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   315
            if (cProject != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   316
            	IProject project = cProject.getProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   317
                IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   318
                if (provider != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   319
                    //TODO get the scanner info for the actual source folder
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   320
                    IScannerInfo info = provider.getScannerInformation(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   321
                    if (info != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   322
                        String[] includePaths = info.getIncludePaths();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   323
                        List<ITypeInfo> filteredTypes = new ArrayList<ITypeInfo>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   324
                        for (int i = 0; i < elements.length; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   325
                            ITypeInfo baseType = elements[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   326
                            if (isTypeReachable(baseType, cProject, includePaths)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   327
                                filteredTypes.add(baseType);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   328
                            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   329
                        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   330
                        return filteredTypes.toArray(new ITypeInfo[filteredTypes.size()]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   331
                    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   332
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   333
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   334
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   335
        return elements;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   336
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   337
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   338
    /**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   339
     * Checks whether the given type can be found in the given project or the
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   340
     * given include paths.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   341
     * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   342
     * @param type the type
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   343
     * @param project the project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   344
     * @param includePaths the include paths
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   345
     * @return <code>true</code> if the given type is found
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   346
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   347
    public static boolean isTypeReachable(ITypeInfo type, ICProject project, String[] includePaths) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   348
        ICProject baseProject = type.getEnclosingProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   349
        
61
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   350
        if (baseProject != null && baseProject.equals(project)) {
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   351
            return true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   352
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   353
        
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   354
        // check the include paths
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   355
        ITypeReference ref = type.getResolvedReference();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   356
        IPath location = ref == null ? null : ref.getLocation();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   357
        boolean isTypeLocation= true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   358
        if (location == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   359
        	isTypeLocation= false;
61
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   360
        	
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   361
        	if (baseProject != null) {
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   362
                location = baseProject.getProject().getLocation();
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   363
        	}
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   364
ff389f2cc4aa fixed bug #9585.
wpaul
parents: 37
diff changeset
   365
        	if (location == null)
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   366
            	return false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   367
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   368
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   369
        for (int i = 0; i < includePaths.length; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   370
            IPath includePath = new Path(includePaths[i]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   371
            if (isTypeLocation) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   372
				if (includePath.isPrefixOf(location))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   373
                    return true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   374
            } else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   375
                // we don't have the real location, so just check the project path
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   376
				if (location.isPrefixOf(includePath))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   377
                    return true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   378
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   379
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   380
        
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   381
        return false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   382
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   383
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   384
	public static final int SEARCH_MATCH_ERROR = -1;  // some error, search failed
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   385
	public static final int SEARCH_MATCH_NOTFOUND = 0;  // no match found
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   386
	public static final int SEARCH_MATCH_FOUND_EXACT = 1;   // exact match
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   387
	public static final int SEARCH_MATCH_FOUND_EXACT_ANOTHER_TYPE = 2; // same name found, by different type
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   388
	public static final int SEARCH_MATCH_FOUND_ANOTHER_NAMESPACE = 3; // same type name exits in different scope
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   389
	public static final int SEARCH_MATCH_FOUND_ANOTHER_TYPE = 4; // same name used by another type in different scope
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   390
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   391
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   392
	 * Search for the given qualified name of the give
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   393
	 * @param typeName  qualified name of the type to search
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   394
	 * @param project  
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   395
	 * @param queryType Class of interface type to search for (e.g. ICPPClassType.class)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   396
	 * @return one of {@link #SEARCH_MATCH_ERROR}, 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   397
	 * {@link #SEARCH_MATCH_FOUND_ANOTHER_NAMESPACE},
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   398
	 * {@link #SEARCH_MATCH_FOUND_ANOTHER_TYPE}, 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   399
	 * {@link #SEARCH_MATCH_FOUND_EXACT_ANOTHER_TYPE},
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   400
	 * {@link #SEARCH_MATCH_FOUND_EXACT} or
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   401
	 * {@link #SEARCH_MATCH_NOTFOUND}.	 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   402
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   403
	public static int searchForCppType(IQualifiedTypeName typeName, ICProject project, Class<?> queryType) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   404
		IIndex index= null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   405
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   406
			if (project != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   407
				index = CCorePlugin.getIndexManager().getIndex(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   408
				index.acquireReadLock();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   409
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   410
		} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   411
			CUIPlugin.log(e);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   412
			return SEARCH_MATCH_ERROR;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   413
		} catch (InterruptedException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   414
			Thread.currentThread().interrupt();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   415
			return SEARCH_MATCH_NOTFOUND;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   416
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   417
		if (index == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   418
			return SEARCH_MATCH_ERROR;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   419
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   420
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   421
			String fullyQualifiedTypeName = typeName.getFullyQualifiedName();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   422
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   423
				IndexFilter filter= IndexFilter.getFilter(ILinkage.CPP_LINKAGE_ID);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   424
				//bug 165636: findBindings(char[][]...) does not find nested nodes (classes)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   425
				//therefore switching back to findBindings(Pattern...)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   426
				IBinding[] bindings = index.findBindings(typeName.getName().toCharArray(), false, filter, new NullProgressMonitor());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   427
				boolean sameTypeNameExists = false; 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   428
				boolean sameNameDifferentTypeExists = false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   429
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   430
				for (int i = 0; i < bindings.length; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   431
					ICPPBinding binding = (ICPPBinding)bindings[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   432
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   433
					//get the fully qualified name of this binding
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   434
					String bindingFullName = CPPVisitor.renderQualifiedName(binding.getQualifiedName());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   435
					Class<? extends ICPPBinding> currentNodeType = binding.getClass();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   436
					// full binding				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   437
					if (queryType.isAssignableFrom(currentNodeType)) {						
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   438
						if (bindingFullName.equals(fullyQualifiedTypeName)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   439
							//bug 165636: there is a match only if there is a definition for the binding
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   440
							//otherwise, users can create a new class definition for this binding
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   441
							if (index.findDefinitions(binding).length > 0)	{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   442
								return SEARCH_MATCH_FOUND_EXACT;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   443
							}							
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   444
						} else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   445
							// same type , same name , but different name space
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   446
							// see if there is an exact match;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   447
							sameTypeNameExists = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   448
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   449
					} else if(ICPPClassType.class.isAssignableFrom(currentNodeType) || 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   450
							IEnumeration.class.isAssignableFrom(currentNodeType) || // TODO - this should maybe be ICPPEnumeration
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   451
							ICPPNamespace.class.isAssignableFrom(currentNodeType) ||
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   452
							ITypedef.class.isAssignableFrom(currentNodeType) ||
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   453
							ICPPBasicType.class.isAssignableFrom(currentNodeType))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   454
					{						
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   455
						if (bindingFullName.equals(fullyQualifiedTypeName))	{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   456
							return SEARCH_MATCH_FOUND_EXACT_ANOTHER_TYPE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   457
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   458
						// different type , same name , but different name space
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   459
						sameNameDifferentTypeExists = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   460
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   461
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   462
				if(sameTypeNameExists){
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   463
					return SEARCH_MATCH_FOUND_ANOTHER_NAMESPACE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   464
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   465
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   466
				if(sameNameDifferentTypeExists){
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   467
					return SEARCH_MATCH_FOUND_ANOTHER_TYPE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   468
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   469
			} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   470
				return SEARCH_MATCH_ERROR;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   471
			} catch (DOMException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   472
				return SEARCH_MATCH_ERROR;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   473
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   474
			return SEARCH_MATCH_NOTFOUND;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   475
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   476
		finally {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   477
			index.releaseReadLock();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   478
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   479
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   480
}