cdt/cdt_6_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelManager.java
author timkelly
Wed, 19 Aug 2009 14:42:39 -0500
changeset 73 82f772574273
parent 37 c2bce6dd59e7
child 75 689d22da9f18
permissions -rw-r--r--
fix 9513, indexer refresh problems with nested source paths
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) 2000, 2008 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
 *     James Blackburn - Modified patch for 149428
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
 *     Anton Leherbauer (Wind River Systems)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    13
 *     Warren Paul (Nokia)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    14
 *	   IBM Corporation (EFS Support)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    15
 *******************************************************************************/
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    16
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    17
package org.eclipse.cdt.internal.core.model;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    18
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    19
import java.io.File;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    20
import java.io.IOException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    21
import java.io.InputStream;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    22
import java.net.URI;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    23
import java.util.ArrayList;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    24
import java.util.Collection;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    25
import java.util.Collections;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    26
import java.util.HashMap;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    27
import java.util.HashSet;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    28
import java.util.List;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    29
import java.util.Map;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    30
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    31
import org.eclipse.cdt.core.CCProjectNature;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    32
import org.eclipse.cdt.core.CCorePlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    33
import org.eclipse.cdt.core.CDescriptorEvent;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    34
import org.eclipse.cdt.core.CProjectNature;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    35
import org.eclipse.cdt.core.IBinaryParser;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    36
import org.eclipse.cdt.core.ICDescriptor;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    37
import org.eclipse.cdt.core.ICDescriptorListener;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    38
import org.eclipse.cdt.core.ICExtensionReference;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    39
import org.eclipse.cdt.core.IBinaryParser.IBinaryArchive;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    40
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    41
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    42
import org.eclipse.cdt.core.model.CModelException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    43
import org.eclipse.cdt.core.model.CoreModel;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    44
import org.eclipse.cdt.core.model.ElementChangedEvent;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    45
import org.eclipse.cdt.core.model.ICContainer;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    46
import org.eclipse.cdt.core.model.ICElement;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    47
import org.eclipse.cdt.core.model.ICElementDelta;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    48
import org.eclipse.cdt.core.model.ICModel;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    49
import org.eclipse.cdt.core.model.ICProject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    50
import org.eclipse.cdt.core.model.IElementChangedListener;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    51
import org.eclipse.cdt.core.model.IIncludeReference;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    52
import org.eclipse.cdt.core.model.IParent;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    53
import org.eclipse.cdt.core.model.ISourceRoot;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    54
import org.eclipse.cdt.core.model.ITranslationUnit;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    55
import org.eclipse.cdt.core.model.IWorkingCopy;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    56
import org.eclipse.cdt.internal.core.CCoreInternals;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    57
import org.eclipse.cdt.internal.core.LocalProjectScope;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    58
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    59
import org.eclipse.core.filesystem.EFS;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    60
import org.eclipse.core.filesystem.IFileInfo;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    61
import org.eclipse.core.filesystem.IFileStore;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    62
import org.eclipse.core.filesystem.URIUtil;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    63
import org.eclipse.core.resources.IFile;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    64
import org.eclipse.core.resources.IFolder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    65
import org.eclipse.core.resources.IProject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    66
import org.eclipse.core.resources.IResource;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    67
import org.eclipse.core.resources.IResourceChangeEvent;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    68
import org.eclipse.core.resources.IResourceChangeListener;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    69
import org.eclipse.core.resources.IResourceDelta;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    70
import org.eclipse.core.resources.IWorkspace;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    71
import org.eclipse.core.resources.IWorkspaceRoot;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    72
import org.eclipse.core.resources.ResourcesPlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    73
import org.eclipse.core.runtime.CoreException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    74
import org.eclipse.core.runtime.IPath;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    75
import org.eclipse.core.runtime.ISafeRunnable;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    76
import org.eclipse.core.runtime.Platform;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    77
import org.eclipse.core.runtime.SafeRunner;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    78
import org.eclipse.core.runtime.content.IContentType;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    79
import org.eclipse.core.runtime.content.IContentTypeManager;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    80
import org.eclipse.core.runtime.content.IContentTypeManager.ContentTypeChangeEvent;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    81
import org.eclipse.core.runtime.content.IContentTypeManager.IContentTypeChangeListener;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    82
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    83
public class CModelManager implements IResourceChangeListener, ICDescriptorListener, IContentTypeChangeListener {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    84
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    85
	public static boolean VERBOSE = false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    86
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    87
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    88
	 * Unique handle onto the CModel
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    89
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    90
	final CModel cModel = new CModel();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    91
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    92
	public static HashSet<String> OptionNames = new HashSet<String>(20);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    93
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    94
	public static final int DEFAULT_CHANGE_EVENT = 0; // must not collide with ElementChangedEvent event masks
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
	 * Used to convert <code>IResourceDelta</code>s into <code>ICElementDelta</code>s.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    98
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    99
	protected final DeltaProcessor fDeltaProcessor = new DeltaProcessor();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   100
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   101
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   102
	 * Queue of deltas created explicitly by the C Model that
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   103
	 * have yet to be fired.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   104
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   105
	List<ICElementDelta> fCModelDeltas = Collections.synchronizedList(new ArrayList<ICElementDelta>());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   106
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   107
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   108
	 * Queue of reconcile deltas on working copies that have yet to be fired.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   109
	 * This is a table form IWorkingCopy to ICElementDelta
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   110
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   111
	HashMap<IWorkingCopy, ICElementDelta> reconcileDeltas = new HashMap<IWorkingCopy, ICElementDelta>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   112
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   113
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   114
	 * Turns delta firing on/off. By default it is on.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   115
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   116
	protected boolean fFire = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   117
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   118
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   119
	 * Collection of listeners for C element deltas
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   120
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   121
	protected List<IElementChangedListener> fElementChangedListeners = Collections.synchronizedList(new ArrayList<IElementChangedListener>());
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
	 * A map from ITranslationUnit to IWorkingCopy of the shared working copies.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   125
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   126
	public Map<IBufferFactory, Map<ITranslationUnit, WorkingCopy>> sharedWorkingCopies = new HashMap<IBufferFactory, Map<ITranslationUnit, WorkingCopy>>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   127
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   128
	 * Set of elements which are out of sync with their buffers.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   129
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   130
	protected Map<ICElement,ICElement> elementsOutOfSynchWithBuffers = new HashMap<ICElement, ICElement>(11);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   131
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   132
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   133
	 * Temporary cache of newly opened elements
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   134
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   135
	private ThreadLocal<Map<ICElement, CElementInfo>> temporaryCache = new ThreadLocal<Map<ICElement, CElementInfo>>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   136
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   137
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   138
	 * Infos cache.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   139
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   140
	protected CModelCache cache = new CModelCache();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   141
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   142
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   143
	 * This is a cache of the projects before any project addition/deletion has started.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   144
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   145
	public ICProject[] cProjectsCache;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   146
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   147
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   148
	 * The list of started BinaryRunners on projects.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   149
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   150
	private HashMap<IProject, BinaryRunner> binaryRunners = new HashMap<IProject, BinaryRunner>();
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
	 * Map of the binary parser for each project.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   154
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   155
	private HashMap<IProject, BinaryParserConfig[]> binaryParsersMap = new HashMap<IProject, BinaryParserConfig[]>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   156
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   157
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   158
	 * The lis of the SourceMappers on projects.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   159
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   160
	private HashMap<ICProject, SourceMapper> sourceMappers = new HashMap<ICProject, SourceMapper>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   161
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   162
	public static final IWorkingCopy[] NoWorkingCopy = new IWorkingCopy[0];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   163
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   164
	static volatile CModelManager factory = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   165
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   166
	private CModelManager() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   167
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   168
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   169
	public static CModelManager getDefault() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   170
		if (factory == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   171
			synchronized (CModelManager.class) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   172
				if (factory != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   173
					return factory;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   174
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   175
				factory = new CModelManager();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   176
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   177
				// Register to the workspace;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   178
				ResourcesPlugin.getWorkspace().addResourceChangeListener(factory,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   179
																			IResourceChangeEvent.POST_CHANGE
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   180
																					| IResourceChangeEvent.PRE_DELETE
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   181
																					| IResourceChangeEvent.PRE_CLOSE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   182
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   183
				// Register the Core Model on the Descriptor
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   184
				// Manager, it needs to know about changes.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   185
				CCorePlugin.getDefault().getCDescriptorManager().addDescriptorListener(factory);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   186
				// Register the Core Model on the ContentTypeManager
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   187
				// it needs to know about changes.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   188
				Platform.getContentTypeManager().addContentTypeChangeListener(factory);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   189
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   190
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   191
		return factory;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   192
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   193
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   194
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   195
	 * Returns the CModel for the given workspace, creating
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   196
	 * it if it does not yet exist.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   197
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   198
	public ICModel getCModel(IWorkspaceRoot root) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   199
		return getCModel();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   200
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   201
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   202
	public CModel getCModel() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   203
		return cModel;
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
	public ICElement create(IPath path) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   207
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   208
		// Assume it is fullpath relative to workspace
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   209
		IResource res = root.findMember(path);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   210
		if (res == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   211
			IPath rootPath = root.getLocation();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   212
			if (path.equals(rootPath)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   213
				return getCModel(root);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   214
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   215
			res = root.getContainerForLocation(path);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   216
			if (res == null || !res.exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   217
				res = root.getFileForLocation(path);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   218
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   219
			if (res != null && !res.exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   220
				res = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   221
			}			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   222
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   223
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   224
		// In case this is an external resource see if we can find
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   225
		// a file for it.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   226
		if (res == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   227
			res= ResourceLookup.selectFileForLocation(path, null);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   228
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   229
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   230
		return create(res, null);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   231
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   232
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   233
	public ICElement create(IResource resource, ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   234
		if (resource == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   235
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   236
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   237
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   238
		int type = resource.getType();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   239
		switch (type) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   240
			case IResource.PROJECT :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   241
				return create((IProject)resource);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   242
			case IResource.FILE :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   243
				return create((IFile)resource, cproject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   244
			case IResource.FOLDER :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   245
				return create((IFolder)resource, cproject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   246
			case IResource.ROOT :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   247
				return getCModel((IWorkspaceRoot)resource);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   248
			default :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   249
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   250
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   251
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   252
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   253
	public ICProject create(IProject project) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   254
		if (project == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   255
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   256
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   257
		return cModel.getCProject(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   258
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   259
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   260
	public ICContainer create(IFolder folder, ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   261
		if (folder == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   262
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   263
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   264
		if (cproject == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   265
			cproject = create(folder.getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   266
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   267
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   268
			ICElement[] children = cproject.getChildren();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   269
			for (int i = 0; i < children.length; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   270
				if (children[i] instanceof ISourceRoot) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   271
					ISourceRoot root = (ISourceRoot)children[i];
73
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
   272
					if (root.isOnSourceEntry(folder) || isParentOfSourceRoot(root, folder)) {
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   273
						// Get the container
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   274
						IPath path = folder.getFullPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   275
						path = path.removeFirstSegments(root.getPath().segmentCount());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   276
						String[] segments = path.segments();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   277
						ICContainer cfolder = root;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   278
						for (int j = 0; j < segments.length; ++j) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   279
							cfolder = cfolder.getCContainer(segments[j]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   280
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   281
						return cfolder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   282
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   283
				} else if (children[i] instanceof ICContainer) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   284
					ICContainer root = (ICContainer)children[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   285
					IPath rootPath = root.getPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   286
					IPath path = folder.getFullPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   287
					if (rootPath.isPrefixOf(path) && cproject.isOnOutputEntry(folder)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   288
						path = path.removeFirstSegments(root.getPath().segmentCount());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   289
						String[] segments = path.segments();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   290
						ICContainer cfolder = root;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   291
						for (int j = 0; j < segments.length; ++j) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   292
							cfolder = cfolder.getCContainer(segments[j]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   293
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   294
						return cfolder;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   295
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   296
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   297
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   298
		} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   299
			//
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   300
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   301
		return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   302
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   303
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   304
	public ICElement create(IFile file, ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   305
		if (file == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   306
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   307
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   308
		if (cproject == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   309
			cproject = create(file.getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   310
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   311
		ICElement celement = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   312
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   313
			// First look for TU's
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   314
			IPath resourcePath = file.getFullPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   315
			ISourceRoot[] roots = cproject.getSourceRoots();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   316
			for (int i = 0; i < roots.length; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   317
				ISourceRoot root = roots[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   318
				if (root.isOnSourceEntry(file)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   319
					IPath rootPath = root.getPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   320
					IPath path = resourcePath.removeFirstSegments(rootPath.segmentCount());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   321
					String fileName = path.lastSegment();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   322
					path = path.removeLastSegments(1);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   323
					String[] segments = path.segments();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   324
					ICContainer cfolder = root;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   325
					for (String segment : segments) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   326
						cfolder = cfolder.getCContainer(segment);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   327
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   328
					if (CoreModel.isValidTranslationUnitName(cproject.getProject(), fileName)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   329
						celement = cfolder.getTranslationUnit(fileName);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   330
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   331
					break;
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
			// check for binary on output entry
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   336
			if (celement == null && cproject.isOnOutputEntry(file)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   337
				IBinaryFile bin = createBinaryFile(file);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   338
				if (bin != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   339
					celement = create(file, bin, cproject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   340
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   341
		} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   342
			//
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   343
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   344
		return celement;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   345
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   346
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   347
	public ICElement create(IFile file, IBinaryFile bin, ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   348
		if (file == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   349
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   350
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   351
		if (bin == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   352
			return create(file, cproject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   353
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   354
		if (cproject == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   355
			cproject = create(file.getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   356
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   357
		ICElement celement = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   358
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   359
			if (cproject.isOnOutputEntry(file)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   360
				IPath resourcePath = file.getParent().getFullPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   361
				ICElement cfolder = cproject.findElement(resourcePath);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   362
				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   363
				// Check if folder is a source root and use that instead
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   364
				ISourceRoot sourceRoot = cproject.findSourceRoot(resourcePath);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   365
				if (sourceRoot != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   366
					cfolder = sourceRoot;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   367
				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   368
				if (bin.getType() == IBinaryFile.ARCHIVE) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   369
					ArchiveContainer vlib = (ArchiveContainer)cproject.getArchiveContainer();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   370
					celement = new Archive(cfolder, file, (IBinaryArchive)bin);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   371
					vlib.addChild(celement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   372
				} else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   373
					BinaryContainer vbin = (BinaryContainer)cproject.getBinaryContainer();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   374
					celement = new Binary(cfolder, file, (IBinaryObject)bin);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   375
					vbin.addChild(celement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   376
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   377
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   378
		} catch (CModelException e) {
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 celement;
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 ITranslationUnit createTranslationUnitFrom(ICProject cproject, IPath path) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   385
		if (path == null || cproject == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   386
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   387
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   388
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   389
		final IProject project= cproject.getProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   390
		final String contentTypeId = CoreModel.getRegistedContentTypeId(project, path.lastSegment());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   391
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   392
		if (path.isAbsolute()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   393
			if (! Util.isNonZeroLengthFile(path)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   394
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   395
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   396
			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   397
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   398
				IIncludeReference[] includeReferences = cproject.getIncludeReferences();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   399
				for (IIncludeReference includeReference : includeReferences) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   400
					if (includeReference.isOnIncludeEntry(path)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   401
						String headerContentTypeId= contentTypeId;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   402
						if (headerContentTypeId == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   403
							headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   404
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   405
						
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   406
						// TODO:  use URI
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   407
						return new ExternalTranslationUnit(includeReference, URIUtil.toURI(path), headerContentTypeId);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   408
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   409
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   410
			} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   411
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   412
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   413
			// if the file exists and it has a known C/C++ file extension then just create
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   414
			// an external translation unit for it.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   415
			if (contentTypeId != null && path.toFile().exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   416
				// TODO:  use URI
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   417
				return new ExternalTranslationUnit(cproject, URIUtil.toURI(path), contentTypeId);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   418
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   419
		} else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   420
			// !path.isAbsolute()
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   421
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   422
				IIncludeReference[] includeReferences = cproject.getIncludeReferences();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   423
				for (IIncludeReference includeReference : includeReferences) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   424
					IPath includePath = includeReference.getPath().append(path);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   425
					if (Util.isNonZeroLengthFile(includePath)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   426
						String headerContentTypeId= contentTypeId;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   427
						if (headerContentTypeId == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   428
							headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   429
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   430
						
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   431
						// TODO:  use URI
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   432
						return new ExternalTranslationUnit(includeReference, URIUtil.toURI(includePath), headerContentTypeId);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   433
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   434
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   435
			} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   436
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   437
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   438
		return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   439
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   440
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   441
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   442
	 * Creates a translation unit in the given project for the given location.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   443
	 * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   444
	 * @param cproject
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   445
	 * @param locationURI
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   446
	 * @return ITranslationUnit
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   447
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   448
	public ITranslationUnit createTranslationUnitFrom(ICProject cproject, URI locationURI) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   449
		if (locationURI == null || cproject == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   450
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   451
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   452
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   453
		if(!locationURI.isAbsolute()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   454
			throw new IllegalArgumentException();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   455
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   456
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   457
		final IProject project= cproject.getProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   458
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   459
		IFileStore fileStore = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   460
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   461
			fileStore = EFS.getStore(locationURI);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   462
		} catch (CoreException e1) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   463
			// TODO Auto-generated catch block
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   464
			e1.printStackTrace();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   465
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   466
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   467
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   468
		final String contentTypeId = CoreModel.getRegistedContentTypeId(project, fileStore.getName());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   469
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   470
			if (! Util.isNonZeroLengthFile(locationURI)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   471
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   472
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   473
			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   474
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   475
				IIncludeReference[] includeReferences = cproject.getIncludeReferences();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   476
				for (IIncludeReference includeReference : includeReferences) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   477
					
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   478
					
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   479
					// crecoskie
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   480
					// TODO FIXME:  include entries don't handle URIs yet
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   481
					IPath path = URIUtil.toPath(locationURI);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   482
					if (path != null && includeReference.isOnIncludeEntry(path)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   483
						String headerContentTypeId= contentTypeId;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   484
						if (headerContentTypeId == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   485
							headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   486
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   487
						
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   488
						return new ExternalTranslationUnit(includeReference, locationURI, headerContentTypeId);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   489
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   490
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   491
			} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   492
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   493
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   494
			// if the file exists and it has a known C/C++ file extension then just create
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   495
			// an external translation unit for it.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   496
			IFileInfo info = fileStore.fetchInfo();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   497
			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   498
			if (contentTypeId != null && info != null && info.exists()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   499
				return new ExternalTranslationUnit(cproject, locationURI, contentTypeId);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   500
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   501
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   502
		return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   503
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   504
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   505
	public void releaseCElement(ICElement celement) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   506
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   507
		// Guard.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   508
		if (celement == null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   509
			return;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   510
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   511
		//System.out.println("RELEASE " + celement.getElementName());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   512
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   513
		// Remove from the containers.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   514
		if (celement instanceof IParent) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   515
			CElementInfo info = (CElementInfo)peekAtInfo(celement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   516
			if (info != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   517
				ICElement[] children = info.getChildren();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   518
				for (ICElement element : children) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   519
					releaseCElement(element);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   520
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   521
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   522
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   523
			// Make sure any object specifics not part of the children be destroy
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   524
			// For example the CProject needs to destroy the BinaryContainer and ArchiveContainer
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   525
			if (celement instanceof CElement) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   526
				try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   527
					((CElement)celement).closing(info);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   528
				} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   529
					//
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   530
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   531
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   532
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   533
			// If an entire folder was deleted we need to update the
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   534
			// BinaryContainer/ArchiveContainer also.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   535
			if (celement.getElementType() == ICElement.C_CCONTAINER) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   536
				ICProject cproject = celement.getCProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   537
				CProjectInfo pinfo = (CProjectInfo)peekAtInfo(cproject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   538
				ArrayList<ICElement> list = new ArrayList<ICElement>(5);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   539
				if (pinfo != null && pinfo.vBin != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   540
					if (peekAtInfo(pinfo.vBin) != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   541
						try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   542
							ICElement[] bins = pinfo.vBin.getChildren();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   543
							for (ICElement bin : bins) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   544
								if (celement.getPath().isPrefixOf(bin.getPath())) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   545
									//pinfo.vBin.removeChild(bins[i]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   546
									list.add(bin);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   547
								}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   548
							}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   549
						} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   550
							// ..
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   551
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   552
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   553
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   554
				if (pinfo != null && pinfo.vLib != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   555
					if (peekAtInfo(pinfo.vLib) != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   556
						try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   557
							ICElement[] ars = pinfo.vLib.getChildren();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   558
							for (ICElement ar : ars) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   559
								if (celement.getPath().isPrefixOf(ar.getPath())) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   560
									//pinfo.vLib.removeChild(ars[i]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   561
									list.add(ar);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   562
								}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   563
							}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   564
						} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   565
							// ..
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   566
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   567
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   568
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   569
				// release any binary/archive that was in the path
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   570
				for (int i = 0; i < list.size(); i++) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   571
					ICElement b = list.get(i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   572
					releaseCElement(b);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   573
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   574
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   575
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   576
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   577
		// Remove the child from the parent list.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   578
		//Parent parent = (Parent)celement.getParent();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   579
		//if (parent != null && peekAtInfo(parent) != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   580
		//	parent.removeChild(celement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   581
		//}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   582
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   583
		removeInfo(celement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   584
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   585
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   586
	public BinaryParserConfig[] getBinaryParser(IProject project) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   587
		BinaryParserConfig[] parsers = binaryParsersMap.get(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   588
		if (parsers == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   589
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   590
				ICDescriptor cdesc = CCorePlugin.getDefault().getCProjectDescription(project, false);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   591
				if (cdesc != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   592
					ICExtensionReference[] cextensions = cdesc.get(CCorePlugin.BINARY_PARSER_UNIQ_ID, true);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   593
					if (cextensions.length > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   594
						ArrayList<BinaryParserConfig> list = new ArrayList<BinaryParserConfig>(cextensions.length);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   595
						for (ICExtensionReference cextension : cextensions) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   596
							BinaryParserConfig config = new BinaryParserConfig(cextension);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   597
							list.add(config);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   598
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   599
						parsers = new BinaryParserConfig[list.size()];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   600
						list.toArray(parsers);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   601
					} else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   602
						// no parser configured
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   603
						parsers = new BinaryParserConfig[0];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   604
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   605
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   606
			} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   607
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   608
			if (parsers == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   609
				try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   610
					BinaryParserConfig config = new BinaryParserConfig(CCorePlugin.getDefault().getDefaultBinaryParser(), CCorePlugin.DEFAULT_BINARY_PARSER_UNIQ_ID);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   611
					parsers = new BinaryParserConfig[]{config};
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   612
				} catch (CoreException e1) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   613
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   614
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   615
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   616
		if (parsers != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   617
			binaryParsersMap.put(project, parsers);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   618
			return parsers;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   619
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   620
		return new BinaryParserConfig[0];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   621
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   622
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   623
	public IBinaryFile createBinaryFile(IFile file) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   624
		BinaryParserConfig[] parsers = getBinaryParser(file.getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   625
		if (parsers.length == 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   626
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   627
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   628
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   629
		// Only if file has no extension, has an extension that is an integer
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   630
		// or is a binary file content type
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   631
		String ext = file.getFileExtension();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   632
		if (ext != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   633
			// shared libraries often have a version number
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   634
			boolean isNumber = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   635
			for (int i = 0; i < ext.length(); ++i)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   636
				if (!Character.isDigit(ext.charAt(i))) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   637
					isNumber = false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   638
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   639
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   640
			if (!isNumber) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   641
				boolean isBinary= false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   642
				final IContentTypeManager ctm = Platform.getContentTypeManager();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   643
				final IContentType ctbin = ctm.getContentType(CCorePlugin.CONTENT_TYPE_BINARYFILE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   644
				final IContentType[] cts= ctm.findContentTypesFor(file.getName());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   645
				for (int i=0; !isBinary && i < cts.length; i++) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   646
					isBinary= cts[i].isKindOf(ctbin);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   647
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   648
				if (!isBinary) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   649
					return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   650
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   651
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   652
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   653
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   654
		URI fileUri = file.getLocationURI();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   655
		//Avoid name special devices, empty files and the like
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   656
		if (! Util.isNonZeroLengthFile(fileUri)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   657
			// PR:xxx the EFS does not seem to work for newly created file
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   658
			// so before bailing out give another try?
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   659
			//Avoid name special devices, empty files and the like
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   660
			if("file".equals(fileUri.getScheme())) { //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   661
				File f = new File(fileUri);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   662
				if (f.length() == 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   663
					return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   664
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   665
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   666
			//return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   667
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   668
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   669
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   670
		int hints = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   671
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   672
		for (BinaryParserConfig parser2 : parsers) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   673
			IBinaryParser parser = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   674
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   675
				parser = parser2.getBinaryParser();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   676
				if (parser.getHintBufferSize() > hints) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   677
					hints = Math.max(hints, parser.getHintBufferSize());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   678
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   679
			} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   680
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   681
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   682
		byte[] bytes = new byte[hints];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   683
		if (hints > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   684
			InputStream is = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   685
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   686
				is = file.getContents();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   687
				int count = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   688
				// Make sure we read up to 'hints' bytes if we possibly can
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   689
				while (count < hints) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   690
					int bytesRead = is.read(bytes, count, hints - count);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   691
					if (bytesRead < 0)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   692
						break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   693
					count += bytesRead;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   694
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   695
				if (count > 0 && count < bytes.length) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   696
					byte[] array = new byte[count];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   697
					System.arraycopy(bytes, 0, array, 0, count);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   698
					bytes = array;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   699
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   700
			} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   701
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   702
			} catch (IOException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   703
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   704
			} finally {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   705
				if (is != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   706
					try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   707
						is.close();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   708
					} catch (IOException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   709
						// ignore
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   710
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   711
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   712
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   713
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   714
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   715
		IPath location = file.getLocation();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   716
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   717
		for (BinaryParserConfig parser2 : parsers) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   718
			try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   719
				IBinaryParser parser = parser2.getBinaryParser();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   720
				if (parser.isBinary(bytes, location)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   721
    			    IBinaryFile binFile = parser.getBinary(bytes, location);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   722
    			    if (binFile != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   723
    			    	return binFile;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   724
    			    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   725
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   726
			} catch (IOException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   727
			} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   728
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   729
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   730
		return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   731
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   732
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   733
	public void resetBinaryParser(IProject project) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   734
		if (project != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   735
			ICProject cproject = create(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   736
			if (cproject != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   737
				// Let the function remove the children
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   738
				// but it has the side of effect of removing the CProject also
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   739
				// so we have to recall create again.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   740
				try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   741
					cproject.close();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   742
				} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   743
					e.printStackTrace();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   744
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   745
				binaryParsersMap.remove(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   746
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   747
				// Fired and ICElementDelta.PARSER_CHANGED
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   748
				CElementDelta delta = new CElementDelta(getCModel());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   749
				delta.binaryParserChanged(cproject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   750
				registerCModelDelta(delta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   751
				fire(ElementChangedEvent.POST_CHANGE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   752
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   753
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   754
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   755
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   756
	public BinaryRunner getBinaryRunner(ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   757
		BinaryRunner runner = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   758
		synchronized (binaryRunners) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   759
			IProject project = cproject.getProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   760
			runner = binaryRunners.get(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   761
			if (runner == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   762
				runner = new BinaryRunner(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   763
				binaryRunners.put(project, runner);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   764
				runner.start();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   765
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   766
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   767
		return runner;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   768
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   769
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   770
	public void removeBinaryRunner(ICProject cproject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   771
		removeBinaryRunner(cproject.getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   772
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   773
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   774
	public void removeBinaryRunner(IProject project) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   775
		BinaryRunner runner = binaryRunners.remove(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   776
		if (runner != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   777
			runner.stop();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   778
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   779
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   780
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   781
	public SourceMapper getSourceMapper(ICProject cProject) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   782
		SourceMapper mapper = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   783
		synchronized (sourceMappers) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   784
			mapper = sourceMappers.get(cProject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   785
			if (mapper == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   786
				mapper = new SourceMapper(cProject);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   787
				sourceMappers.put(cProject, mapper);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   788
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   789
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   790
		return mapper;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   791
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   792
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   793
	 * addElementChangedListener method comment.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   794
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   795
	public void addElementChangedListener(IElementChangedListener listener) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   796
		synchronized (fElementChangedListeners) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   797
			if (!fElementChangedListeners.contains(listener)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   798
				fElementChangedListeners.add(listener);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   799
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   800
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   801
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   802
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   803
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   804
	 * removeElementChangedListener method comment.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   805
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   806
	public void removeElementChangedListener(IElementChangedListener listener) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   807
		synchronized (fElementChangedListeners) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   808
			int i = fElementChangedListeners.indexOf(listener);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   809
			if (i != -1) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   810
				fElementChangedListeners.remove(i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   811
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   812
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   813
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   814
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   815
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   816
	 * Registers the given delta with this manager. This API is to be
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   817
	 * used to registerd deltas that are created explicitly by the C
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   818
	 * Model. Deltas created as translations of <code>IResourceDeltas</code>
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   819
	 * are to be registered with <code>#registerResourceDelta</code>.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   820
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   821
	public void registerCModelDelta(ICElementDelta delta) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   822
		fCModelDeltas.add(delta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   823
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   824
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   825
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   826
	 * Notifies this C Model Manager that some resource changes have happened
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   827
	 * on the platform, and that the C Model should update any required
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   828
	 * internal structures such that its elements remain consistent.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   829
	 * Translates <code>IResourceDeltas</code> into <code>ICElementDeltas</code>.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   830
	 * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   831
	 * @see IResourceDelta
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   832
	 * @see IResource
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   833
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   834
	public void resourceChanged(IResourceChangeEvent event) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   835
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   836
		if (event.getSource() instanceof IWorkspace) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   837
			IResourceDelta delta = event.getDelta();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   838
			IResource resource = event.getResource();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   839
			switch (event.getType()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   840
				case IResourceChangeEvent.PRE_DELETE :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   841
					try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   842
						if (resource.getType() == IResource.PROJECT && 	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   843
								( ((IProject)resource).hasNature(CProjectNature.C_NATURE_ID) ||
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   844
										((IProject)resource).hasNature(CCProjectNature.CC_NATURE_ID) )){
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   845
							this.preDeleteProject((IProject) resource);}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   846
					} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   847
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   848
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   849
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   850
				case IResourceChangeEvent.PRE_CLOSE :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   851
					try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   852
						if (resource.getType() == IResource.PROJECT && 	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   853
						    ( ((IProject)resource).hasNature(CProjectNature.C_NATURE_ID) ||
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   854
						      ((IProject)resource).hasNature(CCProjectNature.CC_NATURE_ID) )){
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   855
							this.preCloseProject((IProject) resource);}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   856
					} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   857
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   858
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   859
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   860
				case IResourceChangeEvent.POST_CHANGE :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   861
					try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   862
						if (delta != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   863
							checkForProjectRename(delta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   864
							ICElementDelta[] translatedDeltas = fDeltaProcessor.processResourceDelta(delta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   865
							if (translatedDeltas.length > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   866
								for (ICElementDelta translatedDelta : translatedDeltas) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   867
									registerCModelDelta(translatedDelta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   868
								}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   869
							}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   870
							fire(ElementChangedEvent.POST_CHANGE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   871
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   872
					} catch (Exception e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   873
						e.printStackTrace();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   874
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   875
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   876
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   877
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   878
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   879
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   880
	/* (non-Javadoc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   881
	 * @see org.eclipse.cdt.core.ICDescriptorListener#descriptorChanged(org.eclipse.cdt.core.CDescriptorEvent)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   882
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   883
	public void descriptorChanged(CDescriptorEvent event) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   884
		int flags = event.getFlags();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   885
		if ( (flags & CDescriptorEvent.EXTENSION_CHANGED) != 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   886
			ICDescriptor cdesc = event.getDescriptor();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   887
			if (cdesc != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   888
				IProject project = cdesc.getProject();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   889
				try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   890
					ICExtensionReference[] newExts = CCorePlugin.getDefault().getBinaryParserExtensions(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   891
					BinaryParserConfig[] currentConfigs = getBinaryParser(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   892
					// anything added/removed
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   893
					if (newExts.length != currentConfigs.length) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   894
						resetBinaryParser(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   895
					} else { // may reorder
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   896
						for (int i = 0; i < newExts.length; i++) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   897
							if (!newExts[i].getID().equals(currentConfigs[i].getId())) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   898
								resetBinaryParser(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   899
								break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   900
							}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   901
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   902
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   903
				} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   904
					resetBinaryParser(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   905
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   906
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   907
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   908
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   909
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   910
	/* (non-Javadoc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   911
	 * @see org.eclipse.core.runtime.content.IContentTypeManager.IContentTypeListener#contentTypeChanged()
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   912
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   913
	public void contentTypeChanged(ContentTypeChangeEvent event) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   914
		ContentTypeProcessor.processContentTypeChanges(new ContentTypeChangeEvent[]{ event });
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   915
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   916
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   917
	public void contentTypeChanged(ContentTypeChangeEvent[] events) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   918
		ContentTypeProcessor.processContentTypeChanges(events);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   919
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   920
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   921
	public void fire(int eventType) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   922
		fire(null, eventType);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   923
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   924
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   925
	public void fireShift(ICElement element, int offset, int size, int lines) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   926
		ICElementDelta delta = new CShiftData(element, offset, size, lines);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   927
		fire(delta, ElementChangedEvent.POST_SHIFT); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   928
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   929
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   930
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   931
	 * Fire C Model deltas, flushing them after the fact. 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   932
	 * If the firing mode has been turned off, this has no effect. 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   933
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   934
	@SuppressWarnings("deprecation")
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   935
	void fire(ICElementDelta customDeltas, int eventType) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   936
		if (fFire) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   937
			ICElementDelta deltaToNotify;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   938
			if (customDeltas == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   939
				deltaToNotify = mergeDeltas(this.fCModelDeltas);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   940
			} else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   941
				deltaToNotify = customDeltas;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   942
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   943
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   944
			IElementChangedListener[] listeners;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   945
			int listenerCount;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   946
			int[] listenerMask;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   947
			// Notification
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   948
			synchronized (fElementChangedListeners) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   949
				listeners = new IElementChangedListener[fElementChangedListeners.size()];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   950
				fElementChangedListeners.toArray(listeners);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   951
				listenerCount = listeners.length;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   952
				listenerMask = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   953
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   954
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   955
			switch (eventType) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   956
				case DEFAULT_CHANGE_EVENT :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   957
					firePreAutoBuildDelta(deltaToNotify, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   958
					firePostChangeDelta(deltaToNotify, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   959
					fireReconcileDelta(listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   960
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   961
				case ElementChangedEvent.PRE_AUTO_BUILD :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   962
					firePreAutoBuildDelta(deltaToNotify, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   963
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   964
				case ElementChangedEvent.POST_CHANGE :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   965
					firePostChangeDelta(deltaToNotify, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   966
					fireReconcileDelta(listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   967
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   968
				case ElementChangedEvent.POST_RECONCILE :
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   969
					fireReconcileDelta(listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   970
					break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   971
				case ElementChangedEvent.POST_SHIFT:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   972
					fireShiftEvent(deltaToNotify, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   973
					return;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   974
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   975
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   976
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   977
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   978
	@SuppressWarnings("deprecation")
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   979
	private void firePreAutoBuildDelta(ICElementDelta deltaToNotify,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   980
		IElementChangedListener[] listeners, int[] listenerMask, int listenerCount) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   981
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   982
		if (Util.VERBOSE_DELTA) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   983
			System.out.println("FIRING PRE_AUTO_BUILD Delta [" + Thread.currentThread() + "]:"); //$NON-NLS-1$//$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   984
			System.out.println(deltaToNotify == null ? "<NONE>" : deltaToNotify.toString()); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   985
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   986
		if (deltaToNotify != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   987
			notifyListeners(deltaToNotify, ElementChangedEvent.PRE_AUTO_BUILD, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   988
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   989
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   990
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   991
	private void firePostChangeDelta(ICElementDelta deltaToNotify, IElementChangedListener[] listeners, int[] listenerMask, int listenerCount) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   992
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   993
		// post change deltas
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   994
		if (Util.VERBOSE_DELTA) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   995
			System.out.println("FIRING POST_CHANGE Delta [" + Thread.currentThread() + "]:"); //$NON-NLS-1$//$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   996
			System.out.println(deltaToNotify == null ? "<NONE>" : deltaToNotify.toString()); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   997
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   998
		if (deltaToNotify != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   999
				// flush now so as to keep listener reactions to post their own deltas for subsequent iteration
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1000
			this.flush();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1001
			notifyListeners(deltaToNotify, ElementChangedEvent.POST_CHANGE, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1002
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1003
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1004
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1005
	private void fireReconcileDelta(IElementChangedListener[] listeners, int[] listenerMask, int listenerCount) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1006
		ICElementDelta deltaToNotify = mergeDeltas(this.reconcileDeltas.values());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1007
		if (Util.VERBOSE_DELTA) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1008
			System.out.println("FIRING POST_RECONCILE Delta [" + Thread.currentThread() + "]:"); //$NON-NLS-1$//$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1009
			System.out.println(deltaToNotify == null ? "<NONE>" : deltaToNotify.toString()); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1010
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1011
		if (deltaToNotify != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1012
			// flush now so as to keep listener reactions to post their own deltas for subsequent iteration
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1013
			this.reconcileDeltas = new HashMap<IWorkingCopy, ICElementDelta>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1014
			notifyListeners(deltaToNotify, ElementChangedEvent.POST_RECONCILE, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1015
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1016
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1017
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1018
	private void fireShiftEvent(ICElementDelta deltaToNotify, IElementChangedListener[] listeners, int[] listenerMask, int listenerCount) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1019
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1020
		// post change deltas
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1021
		if (Util.VERBOSE_DELTA) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1022
			System.out.println("FIRING POST_SHIFT event [" + Thread.currentThread() + "]:"); //$NON-NLS-1$//$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1023
			System.out.println(deltaToNotify == null ? "<NONE>" : deltaToNotify.toString()); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1024
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1025
		if (deltaToNotify != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1026
			this.flush();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1027
			notifyListeners(deltaToNotify, ElementChangedEvent.POST_SHIFT, listeners, listenerMask, listenerCount);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1028
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1029
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1030
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1031
	public void notifyListeners(ICElementDelta deltaToNotify, int eventType,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1032
		IElementChangedListener[] listeners, int[] listenerMask, int listenerCount) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1033
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1034
		final ElementChangedEvent extraEvent = new ElementChangedEvent(deltaToNotify, eventType);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1035
		for (int i = 0; i < listenerCount; i++) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1036
			if (listenerMask == null || (listenerMask[i] & eventType) != 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1037
				final IElementChangedListener listener = listeners[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1038
				long start = -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1039
				if (Util.VERBOSE_DELTA) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1040
					System.out.print("Listener #" + (i + 1) + "=" + listener.toString());//$NON-NLS-1$//$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1041
					start = System.currentTimeMillis();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1042
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1043
				// wrap callbacks with Safe runnable for subsequent listeners to be called when some are causing grief
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1044
				SafeRunner.run(new ISafeRunnable() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1045
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1046
					public void handleException(Throwable exception) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1047
						//CCorePlugin.log(exception, "Exception occurred in listener of C element change notification"); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1048
						CCorePlugin.log(exception);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1049
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1050
					public void run() throws Exception {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1051
						listener.elementChanged(extraEvent);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1052
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1053
				});
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1054
				if (Util.VERBOSE_DELTA) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1055
					System.out.println(" -> " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1056
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1057
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1058
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1059
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1060
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1061
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1062
	 * Flushes all deltas without firing them.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1063
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1064
	protected void flush() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1065
		fCModelDeltas.clear();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1066
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1067
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1068
	private ICElementDelta mergeDeltas(Collection<ICElementDelta> deltas) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1069
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1070
		synchronized (deltas) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1071
			if (deltas.size() == 0)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1072
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1073
			if (deltas.size() == 1)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1074
				return deltas.iterator().next();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1075
			if (deltas.size() <= 1)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1076
				return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1077
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1078
			ICElement cRoot = getCModel();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1079
			CElementDelta rootDelta = new CElementDelta(cRoot);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1080
			boolean insertedTree = false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1081
			for (ICElementDelta delta : deltas) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1082
				ICElement element = delta.getElement();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1083
				if (cRoot.equals(element)) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1084
					for (ICElementDelta child : delta.getAffectedChildren()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1085
						CElementDelta projectDelta = (CElementDelta)child;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1086
						rootDelta.insertDeltaTree(projectDelta.getElement(), projectDelta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1087
						insertedTree = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1088
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1089
					IResourceDelta[] resourceDeltas = delta.getResourceDeltas();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1090
					if (resourceDeltas != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1091
						for (IResourceDelta resourceDelta : resourceDeltas) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1092
							rootDelta.addResourceDelta(resourceDelta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1093
							insertedTree = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1094
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1095
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1096
				} else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1097
					rootDelta.insertDeltaTree(element, (CElementDelta)delta);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1098
					insertedTree = true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1099
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1100
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1101
			if (insertedTree) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1102
				return rootDelta;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1103
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1104
			return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1105
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1106
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1107
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1108
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1109
	 * Returns the set of elements which are out of synch with their buffers.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1110
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1111
	protected Map<ICElement,ICElement> getElementsOutOfSynchWithBuffers() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1112
		return this.elementsOutOfSynchWithBuffers;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1113
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1114
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1115
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1116
	 * Returns the info for the element.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1117
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1118
	public synchronized Object getInfo(ICElement element) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1119
		Map<ICElement, CElementInfo> tempCache = this.temporaryCache.get();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1120
		if (tempCache != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1121
			Object result = tempCache.get(element);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1122
			if (result != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1123
				return result;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1124
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1125
		return this.cache.getInfo(element);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1126
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1127
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1128
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1129
	 *  Returns the info for this element without
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1130
	 *  disturbing the cache ordering.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1131
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1132
	protected synchronized Object peekAtInfo(ICElement element) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1133
		Map<ICElement, CElementInfo> tempCache = this.temporaryCache.get();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1134
		if (tempCache != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1135
			Object result = tempCache.get(element);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1136
			if (result != null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1137
				return result;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1138
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1139
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1140
		return this.cache.peekAtInfo(element);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1141
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1142
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1143
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1144
	 * Puts the infos in the given map (keys are ICElements and values are CElementInfos)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1145
	 * in the C model cache in an atomic way.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1146
	 * First checks that the info for the opened element (or one of its ancestors) has not been 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1147
	 * added to the cache. If it is the case, another thread has opened the element (or one of
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1148
	 * its ancestors). So returns without updating the cache.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1149
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1150
	protected synchronized void putInfos(ICElement openedElement, Map<ICElement, CElementInfo> newElements) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1151
		// remove children
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1152
		Object existingInfo = this.cache.peekAtInfo(openedElement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1153
		if (openedElement instanceof IParent && existingInfo instanceof CElementInfo) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1154
			ICElement[] children = ((CElementInfo)existingInfo).getChildren();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1155
			for (int i = 0, size = children.length; i < size; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1156
				CElement child = (CElement)children[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1157
				try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1158
					child.close();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1159
				} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1160
					// ignore
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1161
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1162
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1163
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1164
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1165
		for (Map.Entry<ICElement, CElementInfo> element : newElements.entrySet())
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1166
			this.cache.putInfo(element.getKey(), element.getValue());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1167
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1168
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1169
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1170
	 * Removes all cached info from the C Model, including all children,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1171
	 * but does not close this element.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1172
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1173
	protected synchronized void removeChildrenInfo(ICElement openedElement) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1174
		// remove children
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1175
		Object existingInfo = this.cache.peekAtInfo(openedElement);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1176
		if (openedElement instanceof IParent && existingInfo instanceof CElementInfo) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1177
			ICElement[] children = ((CElementInfo)existingInfo).getChildren();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1178
			for (int i = 0, size = children.length; i < size; ++i) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1179
				CElement child = (CElement)children[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1180
				try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1181
					child.close();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1182
				} catch (CModelException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1183
					// ignore
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1184
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1185
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1186
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1187
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1188
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1189
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1190
	 * Removes the info of this model element.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1191
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1192
	protected synchronized void removeInfo(ICElement element) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1193
		this.cache.removeInfo(element);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1194
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1195
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1196
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1197
	 * Returns the temporary cache for newly opened elements for the current thread.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1198
	 * Creates it if not already created.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1199
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1200
	public Map<ICElement, CElementInfo> getTemporaryCache() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1201
		Map<ICElement, CElementInfo> result = this.temporaryCache.get();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1202
		if (result == null) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1203
			result = new HashMap<ICElement, CElementInfo>();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1204
			this.temporaryCache.set(result);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1205
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1206
		return result;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1207
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1208
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1209
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1210
	 * Returns whether there is a temporary cache for the current thread.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1211
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1212
	public boolean hasTemporaryCache() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1213
		return this.temporaryCache.get() != null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1214
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1215
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1216
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1217
	 * Resets the temporary cache for newly created elements to null.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1218
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1219
	public void resetTemporaryCache() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1220
		this.temporaryCache.set(null);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1221
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1222
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1223
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1224
	 * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1225
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1226
	public void startup() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1227
		// Initialization is performed on the first getDefault()...
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1228
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1229
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1230
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1231
	 *  
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1232
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1233
	public void shutdown() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1234
		// Remove ourself from the DescriptorManager.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1235
		CCorePlugin.getDefault().getCDescriptorManager().removeDescriptorListener(factory);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1236
		// Remove ourself from the ContentTypeManager
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1237
		Platform.getContentTypeManager().removeContentTypeChangeListener(factory);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1238
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1239
		// Do any shutdown of services.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1240
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(factory);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1241
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1242
		BinaryRunner[] runners = binaryRunners.values().toArray(new BinaryRunner[0]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1243
		for (BinaryRunner runner : runners) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1244
			runner.stop();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1245
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1246
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1247
		// Nullify the static factory
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1248
		factory = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1249
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1250
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1251
	private void checkForProjectRename(IResourceDelta delta) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1252
		IResourceDelta[] rem= delta.getAffectedChildren(IResourceDelta.REMOVED);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1253
		for (IResourceDelta element : rem) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1254
			delta = element;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1255
			IResource res= delta.getResource();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1256
			if (res.getType() == IResource.PROJECT) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1257
				IPath movedTo= null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1258
				if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1259
					movedTo= delta.getMovedToPath();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1260
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1261
				LocalProjectScope.deletePreferences(res.getFullPath(), movedTo);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1262
			}				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1263
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1264
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1265
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1266
	private void preDeleteProject(IProject project) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1267
		// stop the binary runner for this project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1268
		removeBinaryRunner(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1269
		// stop indexing jobs for this project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1270
		CCoreInternals.getPDOMManager().preDeleteProject(create(project));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1271
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1272
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1273
	private void preCloseProject(IProject project) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1274
		// stop the binary runner for this project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1275
		removeBinaryRunner(project);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1276
		// stop indexing jobs for this project
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1277
		CCoreInternals.getPDOMManager().preCloseProject(create(project));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1278
	}
73
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1279
	
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1280
	/**
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1281
	 * For nested source roots, checks whether or not a source root is a parent of a given folder
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1282
	 * @param root - The source root to check against
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1283
	 * @param folder - Folder to check whether or not the given root is a child
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1284
	 * @return true is the IFolder is a parent of the ISourceRoot
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1285
	 */
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1286
	public boolean isParentOfSourceRoot(ISourceRoot root, IFolder folder) {
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1287
		if (CCorePlugin.showSourceRootsAtTopOfProject()){
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1288
			// not nested source roots so no need to check
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1289
			return false;
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1290
		}
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1291
		
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1292
		return folder.getFullPath().isPrefixOf(root.getPath());
82f772574273 fix 9513, indexer refresh problems with nested source paths
timkelly
parents: 37
diff changeset
  1293
	}
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1294
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
  1295
}