trace/tracebuilder/com.nokia.tracebuilder.eclipse/src/com/nokia/tracebuilder/eclipse/TraceProjectMonitor.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2  * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  * Workbench monitor which opens / closes the trace project
       
    17  *
       
    18  */
       
    19 package com.nokia.tracebuilder.eclipse;
       
    20 
       
    21 import java.io.BufferedReader;
       
    22 import java.io.BufferedWriter;
       
    23 import java.io.DataInputStream;
       
    24 import java.io.File;
       
    25 import java.io.FileInputStream;
       
    26 import java.io.FileNotFoundException;
       
    27 import java.io.FileOutputStream;
       
    28 import java.io.FilenameFilter;
       
    29 import java.io.IOException;
       
    30 import java.io.InputStream;
       
    31 import java.io.InputStreamReader;
       
    32 import java.io.OutputStreamWriter;
       
    33 import java.util.ArrayList;
       
    34 import java.util.List;
       
    35 import java.util.Vector;
       
    36 
       
    37 import org.eclipse.core.resources.IFile;
       
    38 import org.eclipse.core.resources.IProject;
       
    39 import org.eclipse.core.resources.IWorkspace;
       
    40 import org.eclipse.core.resources.ResourcesPlugin;
       
    41 import org.eclipse.core.runtime.CoreException;
       
    42 import org.eclipse.core.runtime.IPath;
       
    43 import org.eclipse.core.runtime.Path;
       
    44 import org.eclipse.jface.text.Document;
       
    45 import org.eclipse.jface.text.IDocument;
       
    46 import org.eclipse.ui.IEditorReference;
       
    47 import org.eclipse.ui.IWorkbenchPage;
       
    48 import org.eclipse.ui.IWorkbenchWindow;
       
    49 import org.eclipse.ui.PlatformUI;
       
    50 import org.eclipse.ui.texteditor.ITextEditor;
       
    51 import org.osgi.framework.Version;
       
    52 
       
    53 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
       
    54 import com.nokia.carbide.cdt.builder.DefaultMMPViewConfiguration;
       
    55 import com.nokia.carbide.cdt.builder.EMMPPathContext;
       
    56 import com.nokia.carbide.cdt.builder.EpocEngineHelper;
       
    57 import com.nokia.carbide.cdt.builder.ICarbideBuildManager;
       
    58 import com.nokia.carbide.cdt.builder.InvalidDriveInMMPPathException;
       
    59 import com.nokia.carbide.cdt.builder.MMPViewPathHelper;
       
    60 import com.nokia.carbide.cdt.builder.project.ICarbideBuildConfiguration;
       
    61 import com.nokia.carbide.cdt.builder.project.ICarbideProjectInfo;
       
    62 import com.nokia.carbide.cpp.epoc.engine.EpocEnginePlugin;
       
    63 import com.nokia.carbide.cpp.epoc.engine.MMPDataRunnableAdapter;
       
    64 import com.nokia.carbide.cpp.epoc.engine.MMPViewRunnableAdapter;
       
    65 import com.nokia.carbide.cpp.epoc.engine.model.mmp.EMMPStatement;
       
    66 import com.nokia.carbide.cpp.epoc.engine.model.mmp.IMMPData;
       
    67 import com.nokia.carbide.cpp.epoc.engine.model.mmp.IMMPView;
       
    68 import com.nokia.carbide.cpp.epoc.engine.preprocessor.AcceptedNodesViewFilter;
       
    69 import com.nokia.tracebuilder.engine.TraceBuilderConfiguration;
       
    70 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    71 import com.nokia.tracebuilder.engine.TraceBuilderView;
       
    72 import com.nokia.tracebuilder.engine.TraceLocationConverter;
       
    73 import com.nokia.tracebuilder.engine.TraceLocationMap;
       
    74 import com.nokia.tracebuilder.engine.TraceProjectMonitorInterface;
       
    75 import com.nokia.tracebuilder.engine.TraceBuilderErrorCodes.FileErrorParameters;
       
    76 import com.nokia.tracebuilder.engine.TraceBuilderErrorCodes.TraceBuilderErrorCode;
       
    77 import com.nokia.tracebuilder.engine.project.ProjectEngine;
       
    78 import com.nokia.tracebuilder.engine.source.SourceProperties;
       
    79 import com.nokia.tracebuilder.file.FileUtils;
       
    80 import com.nokia.tracebuilder.model.TraceBuilderException;
       
    81 import com.nokia.tracebuilder.project.GroupNameHandlerBase;
       
    82 import com.nokia.tracebuilder.project.GroupNameHandlerOSTv1;
       
    83 import com.nokia.tracebuilder.project.GroupNameHandlerOSTv2;
       
    84 import com.nokia.tracebuilder.source.SourceConstants;
       
    85 import com.nokia.tracebuilder.source.SourceDocumentInterface;
       
    86 import com.nokia.tracebuilder.utils.DocumentFactory;
       
    87 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    88 import com.nokia.carbide.cpp.sdk.core.ISDKManager;
       
    89 
       
    90 /**
       
    91  * Workbench monitor which opens / closes the trace project
       
    92  * 
       
    93  */
       
    94 class TraceProjectMonitor implements WorkbenchListenerCallback,
       
    95 		TraceProjectMonitorInterface {
       
    96 
       
    97 	/**
       
    98 	 * OST_INSTRUMENTATION_API_VERSION text
       
    99 	 */
       
   100 	private static final String OST_INSTRUMENTATION_API_VERSION_TEXT = "OST_INSTRUMENTATION_API_VERSION"; //$NON-NLS-1$
       
   101 
       
   102 	/**
       
   103 	 * Regular expression for OST_INSTRUMENTATION_API_VERSION macro check
       
   104 	 */
       
   105 	private static final String OST_INSTRUMENTATION_API_VERSION_REGEX = "#define\\s+OST_INSTRUMENTATION_API_VERSION.*"; //$NON-NLS-1$
       
   106 
       
   107 	/**
       
   108 	 * OSTv1 version text
       
   109 	 */
       
   110 	private static final String OST_VERSION_1_X_X = "1.x.x"; //$NON-NLS-1$
       
   111 
       
   112 	/**
       
   113 	 * OSTv2 version text
       
   114 	 */
       
   115 	private static final String OST_VERSION_2_X_X = "2.x.x"; //$NON-NLS-1$
       
   116 
       
   117 	/**
       
   118 	 * OpenSystemTrace.h file name
       
   119 	 */
       
   120 	private static final String OPEN_SYSTEM_TRACE_H = "opensystemtrace.h"; //$NON-NLS-1$
       
   121 
       
   122 	/**
       
   123 	 * epoc32\include\include path as string
       
   124 	 */
       
   125 	private static final String EPOC32_INCLUDE_INTERNAL = "epoc32" + File.separator + "include" + File.separator + "internal"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   126 
       
   127 	/**
       
   128 	 * epoc32\include\platform path as string
       
   129 	 */
       
   130 	private static final String EPOC32_INCLUDE_PLATFORM = "epoc32" + File.separator + "include" + File.separator + "platform"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   131 
       
   132 	/**
       
   133 	 * PLATFORM_PATHS.HRH
       
   134 	 */
       
   135 	private static final String PLATFORM_PATHS_HRH = "PLATFORM_PATHS.HRH"; //$NON-NLS-1$
       
   136 
       
   137 	/**
       
   138 	 * #INCLUDE
       
   139 	 */
       
   140 	private static final String INCLUDE = "#INCLUDE"; //$NON-NLS-1$
       
   141 
       
   142 	/**
       
   143 	 * Multiline comment start
       
   144 	 */
       
   145 	private static final String MULTILINE_COMMENT_START = "/*"; //$NON-NLS-1$
       
   146 
       
   147 	/**
       
   148 	 * Comment
       
   149 	 */
       
   150 	private static final String COMMENT = "//"; //$NON-NLS-1$	
       
   151 
       
   152 	/**
       
   153 	 * Multiline comment end
       
   154 	 */
       
   155 	private static final String MULTILINE_COMMENT_END = "*/"; //$NON-NLS-1$
       
   156 
       
   157 	/**
       
   158 	 * ncp sw folder name
       
   159 	 */
       
   160 	private static final String NCP_SW_FOLDER_NAME = "ncp_sw"; //$NON-NLS-1$
       
   161 
       
   162 	/**
       
   163 	 * os folder name
       
   164 	 */
       
   165 	private static final String OS_FOLDER_NAME = "os"; //$NON-NLS-1$
       
   166 
       
   167 	/**
       
   168 	 * mw folder name
       
   169 	 */
       
   170 	private static final String MW_FOLDER_NAME = "mw"; //$NON-NLS-1$
       
   171 
       
   172 	/**
       
   173 	 * sf folder name
       
   174 	 */
       
   175 	private static final String SF_FOLDER_NAME = "sf"; //$NON-NLS-1$
       
   176 
       
   177 	/**
       
   178 	 * platform_paths.hrh include
       
   179 	 */
       
   180 	private static final String INCLUDE_PLATFORM_PATHS_HRH = "#include \"platform_paths.hrh\""; //$NON-NLS-1$
       
   181 
       
   182 	/**
       
   183 	 * OS layer system include macro
       
   184 	 */
       
   185 	private static final String OS_LAYER_SYSTEMINCLUDE_MACRO = "OS_LAYER_SYSTEMINCLUDE"; //$NON-NLS-1$
       
   186 
       
   187 	/**
       
   188 	 * MW layer system include macro
       
   189 	 */
       
   190 	private static final String MW_LAYER_SYSTEMINCLUDE_MACRO = "MW_LAYER_SYSTEMINCLUDE"; //$NON-NLS-1$
       
   191 
       
   192 	/**
       
   193 	 * APP layer system include macro
       
   194 	 */
       
   195 	private static final String APP_LAYER_SYSTEMINCLUDE_MACRO = "APP_LAYER_SYSTEMINCLUDE"; //$NON-NLS-1$
       
   196 
       
   197 	/**
       
   198 	 * Group directory
       
   199 	 */
       
   200 	private static final String GROUP_DIRECTORY = "group"; //$NON-NLS-1$	
       
   201 
       
   202 	/**
       
   203 	 * Traces directory
       
   204 	 */
       
   205 	public static final String TRACES_DIRECTORY = "traces"; //$NON-NLS-1$
       
   206 
       
   207 	/**
       
   208 	 * MmpFiles directory
       
   209 	 */
       
   210 	private static final String MMPFILES_DIRECTORY = "mmpfiles"; //$NON-NLS-1$
       
   211 
       
   212 	/**
       
   213 	 * Underscore character
       
   214 	 */
       
   215 	public static final String UNDERSCORE = "_"; //$NON-NLS-1$
       
   216 
       
   217 	/**
       
   218 	 * The workbench listener
       
   219 	 */
       
   220 	private WorkbenchListener listener;
       
   221 
       
   222 	/**
       
   223 	 * Project path that is currently open
       
   224 	 */
       
   225 	private String openProjectPath;
       
   226 
       
   227 	/**
       
   228 	 * Project object that is currently open
       
   229 	 */
       
   230 	private IProject openProjectObject;
       
   231 
       
   232 	/**
       
   233 	 * Trace project include added to MMP files
       
   234 	 */
       
   235 	private boolean mmpFileModified;
       
   236 
       
   237 	/**
       
   238 	 * Name of the "traces_<component name>" folder
       
   239 	 */
       
   240 	private String tracesComponentNameFolder;
       
   241 
       
   242 	/**
       
   243 	 * Name of the "traces_<target_name>_<target_type>" folder
       
   244 	 */
       
   245 	private String tracesTargetNameTargetTypeFolder;
       
   246 
       
   247 	/**
       
   248 	 * Name of the "traces\<target_name>_<target_ext>" folder
       
   249 	 */
       
   250 	private String tracesDirTargetNameTargetExtFolder;
       
   251 
       
   252 	/**
       
   253 	 * Trace folder name used in SBSv2
       
   254 	 */
       
   255 	private String sbsv2TraceFolderName;
       
   256 
       
   257 	/**
       
   258 	 * Select software component flag
       
   259 	 */
       
   260 	private boolean selectSoftwareComponent = true;
       
   261 
       
   262 	/**
       
   263 	 * Select software component check needed flag
       
   264 	 */
       
   265 	private boolean selectSoftwareComponentCheckNeeded = false;
       
   266 
       
   267 	/**
       
   268 	 * Trace folder changed flag
       
   269 	 */
       
   270 	private boolean traceFolderChanged = false;
       
   271 
       
   272 	/**
       
   273 	 * Previous file included more than once flag
       
   274 	 */
       
   275 	private boolean previousFileIncludedMoreThanOnce = false;
       
   276 
       
   277 	/**
       
   278 	 * Editor opened flag
       
   279 	 */
       
   280 	private boolean editorOpened = false;
       
   281 
       
   282 	/**
       
   283 	 * File object that is currently visible
       
   284 	 */
       
   285 	IFile openFileObject = null;
       
   286 
       
   287 	/**
       
   288 	 * Trace folder name of the project that is currently visible
       
   289 	 */
       
   290 	String openProjectTraceFolderName = null;
       
   291 
       
   292 	/**
       
   293 	 * OSTv1 start text
       
   294 	 */
       
   295 	private static final String OSTV1_START_TEXT = "1."; //$NON-NLS-1$	
       
   296 
       
   297 	/**
       
   298 	 * OSTv2 start text
       
   299 	 */
       
   300 	private static final String OSTV2_START_TEXT = "2."; //$NON-NLS-1$		
       
   301 
       
   302 	/**
       
   303 	 * Constructor
       
   304 	 */
       
   305 	TraceProjectMonitor() {
       
   306 		listener = new WorkbenchListener(this);
       
   307 	}
       
   308 
       
   309 	/*
       
   310 	 * (non-Javadoc)
       
   311 	 * 
       
   312 	 * @see
       
   313 	 * com.nokia.tracebuilder.eclipse.TraceProjectMonitorInterface#startMonitor
       
   314 	 * ()
       
   315 	 */
       
   316 	public void startMonitor() {
       
   317 		listener.startListener();
       
   318 	}
       
   319 
       
   320 	/*
       
   321 	 * (non-Javadoc)
       
   322 	 * 
       
   323 	 * @see
       
   324 	 * com.nokia.tracebuilder.eclipse.TraceProjectMonitorInterface#stopMonitor()
       
   325 	 */
       
   326 	public void stopMonitor() {
       
   327 		listener.stopListener();
       
   328 	}
       
   329 
       
   330 	/*
       
   331 	 * (non-Javadoc)
       
   332 	 * 
       
   333 	 * @see com.nokia.tracebuilder.eclipse.WorkbenchListenerCallback#
       
   334 	 * editorActivated(org.eclipse.ui.texteditor.ITextEditor,
       
   335 	 * org.eclipse.core.resources.IFile)
       
   336 	 */
       
   337 	public void editorActivated(ITextEditor editor, IFile file) {
       
   338 		TraceBuilderView view = TraceBuilderGlobals.getView();
       
   339 		view.refresh();
       
   340 		if (editorOpened) {
       
   341 			view.expandTraceGroupsBranch();
       
   342 			editorOpened = false;
       
   343 		}
       
   344 	}
       
   345 
       
   346 	/*
       
   347 	 * (non-Javadoc)
       
   348 	 * 
       
   349 	 * @see com.nokia.tracebuilder.eclipse.WorkbenchListenerCallback#
       
   350 	 * editorClosed(org.eclipse.ui.texteditor.ITextEditor,
       
   351 	 * org.eclipse.core.resources.IFile)
       
   352 	 */
       
   353 	public void editorClosed(ITextEditor editor, IFile file) {
       
   354 		if (!WorkbenchUtils.isEditorsOpen()) {
       
   355 			if (openProjectPath != null) {
       
   356 				TraceBuilderGlobals.getTraceBuilder().closeProject();
       
   357 				openProjectPath = null;
       
   358 			}
       
   359 		}
       
   360 	}
       
   361 
       
   362 	/*
       
   363 	 * (non-Javadoc)
       
   364 	 * 
       
   365 	 * @see com.nokia.tracebuilder.eclipse.WorkbenchListenerCallback#
       
   366 	 * editorOpened(org.eclipse.ui.texteditor.ITextEditor,
       
   367 	 * org.eclipse.core.resources.IFile)
       
   368 	 */
       
   369 	public void editorOpened(ITextEditor editor, IFile file) {
       
   370 		openFileObject = file;
       
   371 		updateProject(file);
       
   372 		editorOpened = true;
       
   373 	}
       
   374 
       
   375 	/*
       
   376 	 * (non-Javadoc)
       
   377 	 * 
       
   378 	 * @see com.nokia.tracebuilder.eclipse.WorkbenchListenerCallback#
       
   379 	 * editorHidden(org.eclipse.ui.texteditor.ITextEditor,
       
   380 	 * org.eclipse.core.resources.IFile)
       
   381 	 */
       
   382 	public void editorHidden(ITextEditor editor, IFile file) {
       
   383 	}
       
   384 
       
   385 	/*
       
   386 	 * (non-Javadoc)
       
   387 	 * 
       
   388 	 * @see com.nokia.tracebuilder.eclipse.WorkbenchListenerCallback#
       
   389 	 * editorVisible(org.eclipse.ui.texteditor.ITextEditor,
       
   390 	 * org.eclipse.core.resources.IFile)
       
   391 	 */
       
   392 	public void editorVisible(ITextEditor editor, IFile file) {
       
   393 		if (!file.equals(openFileObject)) {
       
   394 			updateProject(file);
       
   395 			openFileObject = file;
       
   396 		}
       
   397 	}
       
   398 
       
   399 	/*
       
   400 	 * (non-Javadoc)
       
   401 	 * 
       
   402 	 * @see com.nokia.tracebuilder.eclipse.WorkbenchListenerCallback#
       
   403 	 * editorReplaced(org.eclipse.ui.texteditor.ITextEditor,
       
   404 	 * org.eclipse.core.resources.IFile)
       
   405 	 */
       
   406 	public void editorReplaced(ITextEditor editor, IFile file) {
       
   407 		updateProject(file);
       
   408 	}
       
   409 
       
   410 	/**
       
   411 	 * Opens the trace project related to given file
       
   412 	 * 
       
   413 	 * @param file
       
   414 	 *            the file
       
   415 	 */
       
   416 	private void updateProject(IFile file) {
       
   417 		IProject project = file.getProject();
       
   418 
       
   419 		// Check that project is Carbide project
       
   420 		boolean isCarbideProject = CarbideBuilderPlugin.getBuildManager()
       
   421 				.isCarbideProject(project);
       
   422 		if (isCarbideProject) {
       
   423 
       
   424 			String projectPath = getProjectPath(file);
       
   425 
       
   426 			// If trace folder name or project changes, existing project
       
   427 			// needs to be closed
       
   428 			if (traceFolderChanged == true
       
   429 					|| (openProjectPath != null && projectPath != null && !projectPath
       
   430 							.equals(openProjectPath))) {
       
   431 				TraceBuilderGlobals.getTraceBuilder().closeProject();
       
   432 				openProjectPath = null;
       
   433 				openProjectObject = null;
       
   434 			}
       
   435 			if (projectPath != null && !projectPath.equals(openProjectPath)) {
       
   436 				try {
       
   437 					mmpFileModified = false;
       
   438 					openProjectPath = projectPath;
       
   439 					openProjectObject = file.getProject();
       
   440 					ProjectEngine.traceFolderName = openProjectTraceFolderName;
       
   441 					TraceBuilderGlobals.setProjectPath(projectPath);
       
   442 					initializeDefaultGroupNames();
       
   443 
       
   444 					// Get project name
       
   445 					String projectName = getProjectName(projectPath);
       
   446 
       
   447 					// Save Epoc root of this SDK
       
   448 					saveEpocRoot(openProjectObject);
       
   449 
       
   450 					// Open the project
       
   451 					TraceBuilderGlobals.getTraceBuilder().openProject(
       
   452 							projectName);
       
   453 
       
   454 					// Parse traces from other source files in same
       
   455 					// component
       
   456 					parseOtherTracesFromComponent(file, project);
       
   457 
       
   458 				} catch (TraceBuilderException e) {
       
   459 					openProjectPath = null;
       
   460 					openProjectObject = null;
       
   461 					// If a source file is not open, this error is not
       
   462 					// relevant
       
   463 					// The project will be opened when a source is opened
       
   464 					if (e.getErrorCode() != TraceBuilderErrorCode.SOURCE_NOT_OPEN) {
       
   465 						TraceBuilderGlobals.getEvents().postError(e);
       
   466 					}
       
   467 				}
       
   468 			}
       
   469 		} else {
       
   470 			TraceBuilderGlobals.getTraceBuilder().closeProject();
       
   471 			openProjectPath = null;
       
   472 			openProjectObject = null;
       
   473 		}
       
   474 	}
       
   475 
       
   476 	/**
       
   477 	 * Initialize default group names based on used OST version
       
   478 	 * 
       
   479 	 * @throws TraceBuilderException
       
   480 	 */
       
   481 	private void initializeDefaultGroupNames() throws TraceBuilderException {
       
   482 
       
   483 		// Initialize default group names
       
   484 
       
   485 		String ostVersion = null;
       
   486 		IPath epocroot = WorkbenchUtils
       
   487 				.getEpocRootForProject(openProjectObject);
       
   488 		String epocRootAsString = epocroot.toOSString();
       
   489 		File platformDir = new File(epocRootAsString + EPOC32_INCLUDE_PLATFORM);
       
   490 		File platformOpenSystemTraceFile = new File(platformDir,
       
   491 				OPEN_SYSTEM_TRACE_H);
       
   492 		if (platformOpenSystemTraceFile.exists()
       
   493 				&& platformOpenSystemTraceFile.isFile()) {
       
   494 			ostVersion = getOstVersion(platformOpenSystemTraceFile);
       
   495 
       
   496 			// If OST version is not defined in opensystemtrace.h file then
       
   497 			// default is 1.x.x
       
   498 			if (ostVersion == null) {
       
   499 				ostVersion = OST_VERSION_1_X_X;
       
   500 			}
       
   501 
       
   502 		} else {
       
   503 			File internalDir = new File(epocRootAsString
       
   504 					+ EPOC32_INCLUDE_INTERNAL);
       
   505 			File internalOpenSystemTraceFile = new File(internalDir,
       
   506 					OPEN_SYSTEM_TRACE_H);
       
   507 
       
   508 			// If opensystemtrace.h file exist only in internal folder then
       
   509 			// version is 1.x.x. In OSTv2 case opensystemtrace.h file should
       
   510 			// newer exist only in internal folder, so it is enough that we
       
   511 			// check version only from header in platform folder, because OSTv1
       
   512 			// version of header does not contain version information.
       
   513 			if (internalOpenSystemTraceFile.exists()
       
   514 					&& internalOpenSystemTraceFile.isFile()) {
       
   515 				ostVersion = OST_VERSION_1_X_X;
       
   516 			} else {
       
   517 
       
   518 				// If opensystemtrace.h file does not exist at all then default
       
   519 				// is 2.x.x
       
   520 				ostVersion = OST_VERSION_2_X_X;
       
   521 			}
       
   522 		}
       
   523 
       
   524 		// Check is OST version 1.x.x in use. If it is then use OSTv1 otherwise
       
   525 		// use OSTv2
       
   526 		GroupNameHandlerBase groupNameHandler = TraceBuilderGlobals
       
   527 				.getGroupNameHandler();
       
   528 		if (ostVersion.startsWith(OSTV1_START_TEXT)) {
       
   529 			if (!(groupNameHandler instanceof GroupNameHandlerOSTv1)) {
       
   530 				TraceBuilderGlobals
       
   531 						.setGroupNameHandler(new GroupNameHandlerOSTv1());
       
   532 			}
       
   533 		} else if (ostVersion.startsWith(OSTV2_START_TEXT)) {
       
   534 			if (!(groupNameHandler instanceof GroupNameHandlerOSTv2)) {
       
   535 				TraceBuilderGlobals
       
   536 						.setGroupNameHandler(new GroupNameHandlerOSTv2());
       
   537 			}
       
   538 		} else {
       
   539 			throw new TraceBuilderException(
       
   540 					TraceBuilderErrorCode.UNKNOWN_OST_VERSION);
       
   541 		}
       
   542 	}
       
   543 
       
   544 	/**
       
   545 	 * Get OST version from OpenSystemTrace.h file
       
   546 	 * 
       
   547 	 * @param platformOpenSystemTraceFile
       
   548 	 *            open system trace API file
       
   549 	 * @return OST version Used OST version (e.g. 2.0.0)
       
   550 	 * @throws TraceBuilderException
       
   551 	 */
       
   552 	private String getOstVersion(File platformOpenSystemTraceFile)
       
   553 			throws TraceBuilderException {
       
   554 		String ostVersion = null;
       
   555 		FileInputStream fstream;
       
   556 		try {
       
   557 			fstream = new FileInputStream(platformOpenSystemTraceFile
       
   558 					.toString());
       
   559 			DataInputStream in = new DataInputStream(fstream);
       
   560 
       
   561 			BufferedReader br = new BufferedReader(new InputStreamReader(in));
       
   562 			String strLine;
       
   563 
       
   564 			// Read opensystemtrace.h file line by line
       
   565 			try {
       
   566 				while ((strLine = br.readLine()) != null) {
       
   567 					boolean versionDefinitionFound = strLine
       
   568 							.matches(OST_INSTRUMENTATION_API_VERSION_REGEX);
       
   569 					if (versionDefinitionFound) {
       
   570 						int versionNumberStart = strLine
       
   571 								.indexOf(OST_INSTRUMENTATION_API_VERSION_TEXT)
       
   572 								+ OST_INSTRUMENTATION_API_VERSION_TEXT.length();
       
   573 						ostVersion = strLine.substring(versionNumberStart);
       
   574 						ostVersion = ostVersion.trim();
       
   575 						break;
       
   576 					}
       
   577 				}
       
   578 			} catch (IOException e) {
       
   579 				throw new TraceBuilderException(
       
   580 						TraceBuilderErrorCode.UNEXPECTED_EXCEPTION, e);
       
   581 			} finally {
       
   582 				try {
       
   583 					in.close();
       
   584 				} catch (IOException e1) {
       
   585 					throw new TraceBuilderException(
       
   586 							TraceBuilderErrorCode.UNEXPECTED_EXCEPTION, e1);
       
   587 				}
       
   588 			}
       
   589 		} catch (FileNotFoundException e2) {
       
   590 			FileErrorParameters params = new FileErrorParameters();
       
   591 			params.file = platformOpenSystemTraceFile.toString();
       
   592 			throw new TraceBuilderException(
       
   593 					TraceBuilderErrorCode.FILE_NOT_FOUND, params);
       
   594 		}
       
   595 
       
   596 		return ostVersion;
       
   597 	}
       
   598 
       
   599 	/**
       
   600 	 * Parse traces from other source files in same component
       
   601 	 * 
       
   602 	 * @param file
       
   603 	 *            the file
       
   604 	 * @param project
       
   605 	 *            the project
       
   606 	 */
       
   607 	private void parseOtherTracesFromComponent(IFile file, IProject project) {
       
   608 
       
   609 		if (file != null && project != null) {
       
   610 			ICarbideBuildConfiguration buildConfig = getBuildConfiguration(project);
       
   611 			if (buildConfig != null) {
       
   612 
       
   613 				// Get MMP files for given source file
       
   614 				for (final IPath mmpPath : EpocEngineHelper.getMMPsForSource(
       
   615 						openProjectObject, file.getLocation())) {
       
   616 					IPath fullMmpPath = new Path(openProjectPath);
       
   617 					fullMmpPath = fullMmpPath.append(mmpPath);
       
   618 
       
   619 					String componentId = getComponentIdFromMMPFile(mmpPath,
       
   620 							project);
       
   621 
       
   622 					// Component ID in MMP file must be same as current software
       
   623 					// component ID
       
   624 					if (TraceBuilderGlobals.getCurrentSoftwareComponentId() != null
       
   625 							&& TraceBuilderGlobals
       
   626 									.getCurrentSoftwareComponentId().equals(
       
   627 											componentId)) {
       
   628 
       
   629 						// Get source files for MMP file
       
   630 						for (final IPath sourcePath : EpocEngineHelper
       
   631 								.getSourceFilesForConfiguration(buildConfig,
       
   632 										fullMmpPath)) {
       
   633 							File source = new File(sourcePath.toOSString());
       
   634 
       
   635 							// Check that source file is not same as opened file
       
   636 							if (!file.getName().equalsIgnoreCase(
       
   637 									source.getName())
       
   638 									&& !isFileOpenedInEditor(source)) {
       
   639 								IWorkspace workspace = ResourcesPlugin
       
   640 										.getWorkspace();
       
   641 								IPath location = Path.fromOSString(source
       
   642 										.getAbsolutePath());
       
   643 								IFile isource = workspace.getRoot()
       
   644 										.getFileForLocation(location);
       
   645 
       
   646 								// Parse traces from source file
       
   647 								parseTracesFromFile(isource);
       
   648 							}
       
   649 						}
       
   650 					}
       
   651 				}
       
   652 			}
       
   653 		}
       
   654 	}
       
   655 
       
   656 	/**
       
   657 	 * Get build configuration
       
   658 	 * 
       
   659 	 * @param project
       
   660 	 *            the project
       
   661 	 * @return the build configuration
       
   662 	 */
       
   663 	private ICarbideBuildConfiguration getBuildConfiguration(IProject project) {
       
   664 		ICarbideBuildManager buildMgr = CarbideBuilderPlugin.getBuildManager();
       
   665 		ICarbideBuildConfiguration buildConfig = null;
       
   666 		if (buildMgr != null) {
       
   667 
       
   668 			// We do not need to check that is project Carbide
       
   669 			// project because we have done that earlier
       
   670 			ICarbideProjectInfo cpi = buildMgr.getProjectInfo(project);
       
   671 			if (cpi != null) {
       
   672 
       
   673 				// Get the default build configuration
       
   674 				buildConfig = cpi.getDefaultConfiguration();
       
   675 			}
       
   676 		}
       
   677 		return buildConfig;
       
   678 	}
       
   679 
       
   680 	/**
       
   681 	 * Parse traces from given file
       
   682 	 * 
       
   683 	 * @param file
       
   684 	 *            the file
       
   685 	 */
       
   686 	private void parseTracesFromFile(IFile file) {
       
   687 
       
   688 		if (file != null) {
       
   689 
       
   690 			// Get file content to string variable
       
   691 			String sourceData = getFileContentAsString(file);
       
   692 
       
   693 			if (sourceData != null) {
       
   694 
       
   695 				// Create new document from source data
       
   696 				IDocument document = new Document(sourceData);
       
   697 				document
       
   698 						.addPositionCategory(JFaceDocumentWrapper.TRACEBUILDER_POSITION_CATEGORY);
       
   699 				JFaceLocationUpdater locationUpdater = new JFaceLocationUpdater(
       
   700 						JFaceDocumentWrapper.TRACEBUILDER_POSITION_CATEGORY);
       
   701 				document.addPositionUpdater(locationUpdater);
       
   702 				SourceDocumentInterface sourceDocumentInterface = new JFaceDocumentWrapper(
       
   703 						document);
       
   704 
       
   705 				// Create new source properties
       
   706 				SourceProperties properties = new SourceProperties(
       
   707 						TraceBuilderGlobals.getTraceModel(), DocumentFactory
       
   708 								.getDocumentMonitor().getFactory(),
       
   709 						sourceDocumentInterface);
       
   710 
       
   711 				// Parse trace locations from source properties
       
   712 				properties.updateTraces(0, sourceData.length());
       
   713 
       
   714 				TraceLocationMap locationMap = TraceBuilderGlobals
       
   715 						.getLocationMap();
       
   716 
       
   717 				// When we add source to location map it will go to unrelated
       
   718 				// list
       
   719 				locationMap.addSource(properties);
       
   720 
       
   721 				// Parse traces from source so those will be added to the model
       
   722 				TraceLocationConverter lc = TraceBuilderGlobals
       
   723 						.getLocationConverter();
       
   724 				lc.parseTracesFromSource(properties);
       
   725 
       
   726 				String path = file.getLocation().removeLastSegments(1)
       
   727 						.toOSString()
       
   728 						+ "\\"; //$NON-NLS-1$
       
   729 				String fileName = file.getName();
       
   730 
       
   731 				// When we remove source from location map it will go to last
       
   732 				// known locations list
       
   733 				locationMap.removeSource(properties, path, fileName);
       
   734 			}
       
   735 		}
       
   736 	}
       
   737 
       
   738 	/**
       
   739 	 * Get file content as string
       
   740 	 * 
       
   741 	 * @param file
       
   742 	 *            the file
       
   743 	 * @return file content as string
       
   744 	 */
       
   745 	private String getFileContentAsString(IFile file) {
       
   746 		String fileContentAsString = null;
       
   747 		boolean continueExecution = true;
       
   748 
       
   749 		InputStream is = null;
       
   750 		try {
       
   751 			is = file.getContents();
       
   752 		} catch (CoreException e1) {
       
   753 			continueExecution = false;
       
   754 		}
       
   755 
       
   756 		if (continueExecution) {
       
   757 			BufferedReader reader = new BufferedReader(
       
   758 					new InputStreamReader(is));
       
   759 			StringBuilder sb = new StringBuilder();
       
   760 
       
   761 			String line = null;
       
   762 			try {
       
   763 
       
   764 				// Read file content line by line to string builder
       
   765 				while ((line = reader.readLine()) != null) {
       
   766 					sb.append(line + SourceConstants.LINE_FEED);
       
   767 				}
       
   768 			} catch (IOException e) {
       
   769 				continueExecution = false;
       
   770 			} finally {
       
   771 				try {
       
   772 					is.close();
       
   773 				} catch (IOException e) {
       
   774 					continueExecution = false;
       
   775 				}
       
   776 			}
       
   777 
       
   778 			// If everything went OK, get content from string builder to string
       
   779 			// variable
       
   780 			if (continueExecution) {
       
   781 				fileContentAsString = sb.toString();
       
   782 			}
       
   783 		}
       
   784 
       
   785 		return fileContentAsString;
       
   786 	}
       
   787 
       
   788 	/**
       
   789 	 * Check is file opened in editor
       
   790 	 * 
       
   791 	 * @param source
       
   792 	 *            .getAbsolutePath() the file
       
   793 	 * @return true if file is opened in editor, otherwise false
       
   794 	 */
       
   795 	private boolean isFileOpenedInEditor(File source) {
       
   796 		boolean fileAlredyOpened = false;
       
   797 		IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
       
   798 				.getWorkbenchWindows();
       
   799 		for (IWorkbenchWindow window : windows) {
       
   800 			IWorkbenchPage[] pages = window.getPages();
       
   801 			for (IWorkbenchPage page : pages) {
       
   802 				IEditorReference[] refs = page.getEditorReferences();
       
   803 				IFile alreadyOpenedFile = null;
       
   804 				for (IEditorReference ref : refs) {
       
   805 					alreadyOpenedFile = WorkbenchUtils.getEditorFile(ref);
       
   806 					if (alreadyOpenedFile != null
       
   807 							&& alreadyOpenedFile.getLocation().toOSString()
       
   808 									.equals(source.getAbsolutePath())) {
       
   809 						fileAlredyOpened = true;
       
   810 						break;
       
   811 					}
       
   812 				}
       
   813 				if (fileAlredyOpened) {
       
   814 					break;
       
   815 				}
       
   816 			}
       
   817 			if (fileAlredyOpened) {
       
   818 				break;
       
   819 			}
       
   820 		}
       
   821 		return fileAlredyOpened;
       
   822 	}
       
   823 
       
   824 	/**
       
   825 	 * Gets project name from project path
       
   826 	 * 
       
   827 	 * @param projectPath
       
   828 	 *            the project path
       
   829 	 * @return project name
       
   830 	 */
       
   831 	private String getProjectName(String projectPath) {
       
   832 		String separator = System.getProperty("file.separator"); //$NON-NLS-1$
       
   833 		String projectName = projectPath.substring(projectPath
       
   834 				.lastIndexOf(separator) + 1);
       
   835 		return projectName;
       
   836 	}
       
   837 
       
   838 	/**
       
   839 	 * Gets project path from file
       
   840 	 * 
       
   841 	 * @param file
       
   842 	 *            the file
       
   843 	 * @return the project path
       
   844 	 */
       
   845 	private String getProjectPath(IFile file) {
       
   846 		String projectPath = null;
       
   847 
       
   848 		// Initial project path is the file's parent folder
       
   849 		IPath path = file.getLocation();
       
   850 		IProject project = file.getProject();
       
   851 
       
   852 		// Check if file is source file
       
   853 		if (isSourceFile(file)) {
       
   854 			projectPath = getProjectPathToSourceFile(project, path);
       
   855 
       
   856 			// It could be that source file is not included to MMP file and
       
   857 			// because that we did not find project path. So if source file
       
   858 			// project path is null, try to find project path to source file
       
   859 			// same way than non source file.
       
   860 			if (projectPath == null) {
       
   861 				projectPath = getProjetcPathToNonSourceFile(project, path);
       
   862 			}
       
   863 		} else {
       
   864 			projectPath = getProjetcPathToNonSourceFile(project, path);
       
   865 		}
       
   866 
       
   867 		return projectPath;
       
   868 	}
       
   869 
       
   870 	/**
       
   871 	 * Get project path to source file
       
   872 	 * 
       
   873 	 * @param project
       
   874 	 *            project
       
   875 	 * @param path
       
   876 	 *            path where to start find project path
       
   877 	 * @return the project path
       
   878 	 */
       
   879 	private String getProjectPathToSourceFile(IProject project, IPath path) {
       
   880 		String projectPath = null;
       
   881 		String mmpFilePath = null;
       
   882 		IPath currentSoftwareComponentMMPPath = null;
       
   883 		List<IPath> mmpPaths = EpocEngineHelper.getMMPsForSource(project, path);
       
   884 		if (mmpPaths.size() > 0) {
       
   885 
       
   886 			// Check that was source file included to more than one MMP file
       
   887 			if (mmpPaths.size() > 1) {
       
   888 
       
   889 				// User need to select software component to work with, if
       
   890 				// previous file was also included to more than one MMP file or
       
   891 				// if some of the previous files belonged to different
       
   892 				// component.
       
   893 				if (selectSoftwareComponent || previousFileIncludedMoreThanOnce) {
       
   894 
       
   895 					// Clear old software component list
       
   896 					TraceBuilderGlobals.clearSoftwareComponents();
       
   897 
       
   898 					// Add all software components to software component list
       
   899 					for (int i = 0; i < mmpPaths.size(); i++) {
       
   900 						IPath mmpPath = mmpPaths.get(i);
       
   901 						String mmpPathAsString = mmpPath.toString();
       
   902 						String componentId = getComponentIdFromMMPFile(mmpPath,
       
   903 								project);
       
   904 						String componentName = getComponentNameFromMMPFileName(mmpPathAsString);
       
   905 						TraceBuilderGlobals.addSoftwareComponent(componentId,
       
   906 								componentName, mmpPathAsString);
       
   907 					}
       
   908 
       
   909 					// Ask user to select software component to be used
       
   910 					try {
       
   911 						TraceBuilderGlobals.getTraceBuilder().selectComponent();
       
   912 					} catch (TraceBuilderException e) {
       
   913 						TraceBuilderGlobals.getEvents().postError(e);
       
   914 					}
       
   915 
       
   916 					selectSoftwareComponentCheckNeeded = true;
       
   917 				}
       
   918 
       
   919 				currentSoftwareComponentMMPPath = new Path(TraceBuilderGlobals
       
   920 						.getCurrentSoftwareComponentMMPPath());
       
   921 				String currentSoftwareComponentName = TraceBuilderGlobals
       
   922 						.getCurrentSoftwareComponentName();
       
   923 				setPossibleTraceFolderNames(project,
       
   924 						currentSoftwareComponentName,
       
   925 						currentSoftwareComponentMMPPath);
       
   926 
       
   927 				// Remove mmp file name from mmpPathAsString variable
       
   928 				IPath projectLocation = project.getLocation();
       
   929 				String mmpPathAsString = TraceBuilderGlobals
       
   930 						.getCurrentSoftwareComponentMMPPath();
       
   931 				int lastIndexOfForwardSlashChar = mmpPathAsString
       
   932 						.lastIndexOf(SourceConstants.FORWARD_SLASH_CHAR);
       
   933 
       
   934 				if (lastIndexOfForwardSlashChar != -1) {
       
   935 					mmpPathAsString = mmpPathAsString.substring(0,
       
   936 							lastIndexOfForwardSlashChar);
       
   937 				}
       
   938 
       
   939 				// Append mmpPathAsString to Project path to get real
       
   940 				// mmp file path
       
   941 				mmpFilePath = projectLocation.append(mmpPathAsString)
       
   942 						.toString();
       
   943 
       
   944 				previousFileIncludedMoreThanOnce = true;
       
   945 			} else {
       
   946 				currentSoftwareComponentMMPPath = mmpPaths.get(0);
       
   947 				String mmpPathAsString = currentSoftwareComponentMMPPath
       
   948 						.toString();
       
   949 
       
   950 				String componentId = getComponentIdFromMMPFile(
       
   951 						currentSoftwareComponentMMPPath, project);
       
   952 				String componentName = getComponentNameFromMMPFileName(mmpPathAsString);
       
   953 
       
   954 				if (selectSoftwareComponentCheckNeeded) {
       
   955 					if (TraceBuilderGlobals.getCurrentSoftwareComponentId() != null
       
   956 							&& TraceBuilderGlobals
       
   957 									.getCurrentSoftwareComponentId().equals(
       
   958 											componentId)) {
       
   959 						selectSoftwareComponent = false;
       
   960 					} else {
       
   961 						selectSoftwareComponent = true;
       
   962 						selectSoftwareComponentCheckNeeded = false;
       
   963 					}
       
   964 				}
       
   965 
       
   966 				// Clear old software component list
       
   967 				TraceBuilderGlobals.clearSoftwareComponents();
       
   968 
       
   969 				// Add software component to software component list
       
   970 				TraceBuilderGlobals.addSoftwareComponent(componentId,
       
   971 						componentName, mmpPathAsString);
       
   972 
       
   973 				// Because there is only one component current component index
       
   974 				// is 0
       
   975 				TraceBuilderGlobals.setCurrentSoftwareComponentIndex(0);
       
   976 
       
   977 				setPossibleTraceFolderNames(project, componentName,
       
   978 						currentSoftwareComponentMMPPath);
       
   979 
       
   980 				// Remove mmp file name from mmpPathAsString variable
       
   981 				IPath projectLocation = project.getLocation();
       
   982 				int lastIndexOfForwardSlashChar = mmpPathAsString
       
   983 						.lastIndexOf(SourceConstants.FORWARD_SLASH_CHAR);
       
   984 
       
   985 				if (lastIndexOfForwardSlashChar != -1) {
       
   986 					mmpPathAsString = mmpPathAsString.substring(0,
       
   987 							lastIndexOfForwardSlashChar);
       
   988 				}
       
   989 
       
   990 				// Append mmpPathAsString to Project path to get real
       
   991 				// mmp file path
       
   992 				mmpFilePath = projectLocation.append(mmpPathAsString)
       
   993 						.toString();
       
   994 
       
   995 				previousFileIncludedMoreThanOnce = false;
       
   996 			}
       
   997 			projectPath = findProjectRootAndSetTracesFolderName(
       
   998 					currentSoftwareComponentMMPPath, project, mmpFilePath);
       
   999 		}
       
  1000 		return projectPath;
       
  1001 	}
       
  1002 
       
  1003 	/**
       
  1004 	 * Find project root and set traces folder name
       
  1005 	 * 
       
  1006 	 * @param mmpFilePath
       
  1007 	 *            MPP file path
       
  1008 	 * @return Project root
       
  1009 	 */
       
  1010 	private String findProjectRootAndSetTracesFolderName(IPath mmpPath,
       
  1011 			IProject project, String mmpFilePath) {
       
  1012 		String projectPath;
       
  1013 		projectPath = findProjectRoot(mmpFilePath);
       
  1014 
       
  1015 		// Store previous trace folder name
       
  1016 		String previousTraceFolderName = ProjectEngine.traceFolderName;
       
  1017 		openProjectTraceFolderName = findTraceFolderName(mmpPath, project,
       
  1018 				projectPath);
       
  1019 
       
  1020 		// Check has trace folder name changed
       
  1021 		traceFolderChanged = false;
       
  1022 		if (previousTraceFolderName != null
       
  1023 				&& !previousTraceFolderName
       
  1024 						.equalsIgnoreCase(openProjectTraceFolderName)) {
       
  1025 			traceFolderChanged = true;
       
  1026 		}
       
  1027 		return projectPath;
       
  1028 	}
       
  1029 
       
  1030 	/**
       
  1031 	 * Set possible trace folder names
       
  1032 	 * 
       
  1033 	 * @param carbideProject
       
  1034 	 *            Current Carbide.c++ project
       
  1035 	 * @param componentName
       
  1036 	 *            Current component name
       
  1037 	 * @param relativeMMPPath
       
  1038 	 *            Relative path to current MMP file
       
  1039 	 */
       
  1040 	private void setPossibleTraceFolderNames(IProject carbideProject,
       
  1041 			String componentName, IPath relativeMMPPath) {
       
  1042 		String targetName = null;
       
  1043 		String targetExt = null;
       
  1044 		tracesComponentNameFolder = TRACES_DIRECTORY + UNDERSCORE
       
  1045 				+ componentName;
       
  1046 
       
  1047 		// Append relative MMP path to the project location to get location of
       
  1048 		// the MMP file
       
  1049 		IPath mmpPath = carbideProject.getLocation().append(relativeMMPPath);
       
  1050 
       
  1051 		ICarbideBuildManager buildMgr = CarbideBuilderPlugin.getBuildManager();
       
  1052 		ICarbideProjectInfo cpi = buildMgr.getProjectInfo(carbideProject);
       
  1053 
       
  1054 		// Get the default build configuration
       
  1055 		ICarbideBuildConfiguration defultConfig = cpi.getDefaultConfiguration();
       
  1056 		IPath pathForExcutable = EpocEngineHelper.getHostPathForExecutable(
       
  1057 				defultConfig, mmpPath);
       
  1058 		if (pathForExcutable != null) {
       
  1059 			String fileName = pathForExcutable.lastSegment();
       
  1060 			int lastIndexOfDot = fileName.lastIndexOf("."); //$NON-NLS-1$
       
  1061 			targetName = fileName.substring(0, lastIndexOfDot);
       
  1062 			targetExt = fileName.substring(lastIndexOfDot + 1);
       
  1063 		}
       
  1064 
       
  1065 		Object data = EpocEnginePlugin.runWithMMPData(mmpPath,
       
  1066 				new DefaultMMPViewConfiguration(carbideProject, defultConfig,
       
  1067 						new AcceptedNodesViewFilter()),
       
  1068 				new MMPDataRunnableAdapter() {
       
  1069 					public Object run(IMMPData mmpData) {
       
  1070 						// The real return value, getting a single argument
       
  1071 						// setting
       
  1072 						return mmpData.getSingleArgumentSettings().get(
       
  1073 								EMMPStatement.TARGETTYPE);
       
  1074 					}
       
  1075 				});
       
  1076 
       
  1077 		String targetType = (String) data;
       
  1078 
       
  1079 		if (targetName != null && targetType != null) {
       
  1080 			tracesTargetNameTargetTypeFolder = TRACES_DIRECTORY + UNDERSCORE
       
  1081 					+ targetName + UNDERSCORE + targetType;
       
  1082 		} else {
       
  1083 			tracesTargetNameTargetTypeFolder = null;
       
  1084 		}
       
  1085 
       
  1086 		if (targetName != null && targetExt != null) {
       
  1087 			tracesDirTargetNameTargetExtFolder = TRACES_DIRECTORY
       
  1088 					+ File.separator + targetName + UNDERSCORE + targetExt;
       
  1089 		} else {
       
  1090 			tracesDirTargetNameTargetExtFolder = null;
       
  1091 		}
       
  1092 
       
  1093 		setSBSv2TraceFolderName(carbideProject);
       
  1094 	}
       
  1095 
       
  1096 	/**
       
  1097 	 * Get component id from MMP file
       
  1098 	 * 
       
  1099 	 * @param mmpPath
       
  1100 	 *            the mmp file path
       
  1101 	 * @param project
       
  1102 	 *            the project
       
  1103 	 * @return the component id as string
       
  1104 	 */
       
  1105 	private String getComponentIdFromMMPFile(IPath mmpPath, IProject project) {
       
  1106 		String uid = null;
       
  1107 		if (mmpPath != null) {
       
  1108 			ICarbideBuildConfiguration buildConfig = getBuildConfiguration(project);
       
  1109 			if (buildConfig != null) {
       
  1110 				Object data = EpocEnginePlugin.runWithMMPData(project
       
  1111 						.getLocation().append(mmpPath),
       
  1112 						new DefaultMMPViewConfiguration(buildConfig
       
  1113 								.getCarbideProject().getProject(), buildConfig,
       
  1114 								new AcceptedNodesViewFilter()),
       
  1115 						new MMPDataRunnableAdapter() {
       
  1116 							public Object run(IMMPData mmpData) {
       
  1117 
       
  1118 								// Try first get value of UID3
       
  1119 								String uid = mmpData.getUid3();
       
  1120 
       
  1121 								// If value of UID3 is null then
       
  1122 								// try get value of UID2
       
  1123 								if (uid == null) {
       
  1124 									uid = mmpData.getUid2();
       
  1125 								}
       
  1126 
       
  1127 								// return value could be null if
       
  1128 								// UID2 is not defined
       
  1129 								return uid; // CodForChk_Dis_Exits
       
  1130 							}
       
  1131 						});
       
  1132 				uid = (String) data;
       
  1133 			}
       
  1134 		}
       
  1135 		return uid;
       
  1136 	}
       
  1137 
       
  1138 	/**
       
  1139 	 * Get trace folder name from MMP file
       
  1140 	 * 
       
  1141 	 * @param mmpPath
       
  1142 	 *            the mmp file path
       
  1143 	 * @param project
       
  1144 	 *            the project
       
  1145 	 * @return the trace folder name
       
  1146 	 */
       
  1147 	private String getTraceFolderFromMMPFile(IPath mmpPath, IProject project) {
       
  1148 		String traceFolder = null;
       
  1149 		if (mmpPath != null) {
       
  1150 			ICarbideBuildConfiguration buildConfig = getBuildConfiguration(project);
       
  1151 			if (buildConfig != null) {
       
  1152 				Object data = EpocEnginePlugin.runWithMMPData(project
       
  1153 						.getLocation().append(mmpPath),
       
  1154 						new DefaultMMPViewConfiguration(buildConfig
       
  1155 								.getCarbideProject().getProject(), buildConfig,
       
  1156 								new AcceptedNodesViewFilter()),
       
  1157 						new MMPDataRunnableAdapter() {
       
  1158 							public Object run(IMMPData mmpData) {
       
  1159 
       
  1160 								String traceFolder = null;
       
  1161 
       
  1162 								// Get the list of user include
       
  1163 								// paths
       
  1164 								List<IPath> userIncludes = mmpData
       
  1165 										.getUserIncludes();
       
  1166 
       
  1167 								// Go through existing userincludes
       
  1168 								// to check if the path to add is
       
  1169 								// already present
       
  1170 								for (int i = 0; i < userIncludes.size(); i++) {
       
  1171 									String userIncludeAsOSString = userIncludes
       
  1172 											.get(i).toOSString();
       
  1173 									if (userIncludeAsOSString
       
  1174 											.equalsIgnoreCase(TRACES_DIRECTORY)
       
  1175 											|| userIncludeAsOSString
       
  1176 													.equalsIgnoreCase(tracesComponentNameFolder)
       
  1177 											|| userIncludeAsOSString
       
  1178 													.equalsIgnoreCase(tracesTargetNameTargetTypeFolder)
       
  1179 											|| userIncludeAsOSString
       
  1180 													.equalsIgnoreCase(tracesDirTargetNameTargetExtFolder)) {
       
  1181 										traceFolder = userIncludeAsOSString;
       
  1182 										break;
       
  1183 									}
       
  1184 								}
       
  1185 								return traceFolder; // CodForChk_Dis_Exits
       
  1186 							}
       
  1187 						});
       
  1188 				traceFolder = (String) data;
       
  1189 			}
       
  1190 		}
       
  1191 		return traceFolder;
       
  1192 	}
       
  1193 
       
  1194 	/**
       
  1195 	 * Get component name from MMP file name
       
  1196 	 * 
       
  1197 	 * @param mmpPathAsString
       
  1198 	 *            the mmp file path as string
       
  1199 	 * @return the component name
       
  1200 	 */
       
  1201 	private String getComponentNameFromMMPFileName(String mmpPathAsString) {
       
  1202 		String mmpFileName = null;
       
  1203 		if (mmpPathAsString != null) {
       
  1204 			int lastIndexOfForwardSlashChar = mmpPathAsString
       
  1205 					.lastIndexOf(SourceConstants.FORWARD_SLASH_CHAR);
       
  1206 			int lastIndexOfPeriod = mmpPathAsString.lastIndexOf(FileUtils.MMP);
       
  1207 			mmpFileName = mmpPathAsString.substring(
       
  1208 					lastIndexOfForwardSlashChar + 1, lastIndexOfPeriod);
       
  1209 		}
       
  1210 
       
  1211 		return mmpFileName;
       
  1212 	}
       
  1213 
       
  1214 	/**
       
  1215 	 * Get project path to non source file
       
  1216 	 * 
       
  1217 	 * @param project
       
  1218 	 *            project
       
  1219 	 * @param path
       
  1220 	 *            path where to start find project path
       
  1221 	 * @return the project path
       
  1222 	 */
       
  1223 	private String getProjetcPathToNonSourceFile(IProject project, IPath path) {
       
  1224 		String projectPath = null;
       
  1225 
       
  1226 		// We can not parse software components, because file is not source file
       
  1227 		// and it is not listed in MMP files. So just clear software components
       
  1228 		// list.
       
  1229 		TraceBuilderGlobals.clearSoftwareComponents();
       
  1230 
       
  1231 		// Check that is file part of open project
       
  1232 		if (project.equals(openProjectObject) && openProjectPath != null) {
       
  1233 
       
  1234 			// File part of open project, so use same project path
       
  1235 			projectPath = openProjectPath;
       
  1236 
       
  1237 			// Because project path is same, also trace folder is same
       
  1238 			traceFolderChanged = false;
       
  1239 		} else {
       
  1240 
       
  1241 			// File not part of open project, so find project path
       
  1242 			projectPath = findProjectRootAndSetTracesFolderName(path, project,
       
  1243 					path.toOSString());
       
  1244 		}
       
  1245 		return projectPath;
       
  1246 	}
       
  1247 
       
  1248 	/**
       
  1249 	 * Find files project root
       
  1250 	 * 
       
  1251 	 * @param path
       
  1252 	 *            path where to start find project root
       
  1253 	 * @return the project root
       
  1254 	 */
       
  1255 	private String findProjectRoot(String path) {
       
  1256 		boolean found = false;
       
  1257 		String root = path;
       
  1258 		File file = new File(root);
       
  1259 		while (!found && file != null) {
       
  1260 			if (isProjectRoot(file)) {
       
  1261 				root = file.getPath();
       
  1262 				found = true;
       
  1263 			} else {
       
  1264 				file = file.getParentFile();
       
  1265 			}
       
  1266 		}
       
  1267 		return root;
       
  1268 	}
       
  1269 
       
  1270 	/**
       
  1271 	 * Find trace folder name
       
  1272 	 * 
       
  1273 	 * @param mmpFilePath
       
  1274 	 *            MMP file path
       
  1275 	 * @param projectRoot
       
  1276 	 *            project root
       
  1277 	 * @return the trace folder name
       
  1278 	 */
       
  1279 	private String findTraceFolderName(IPath mmpPath, IProject project,
       
  1280 			String projectRoot) {
       
  1281 		String traceFolderName = null;
       
  1282 
       
  1283 		traceFolderName = getTraceFolderFromMMPFile(mmpPath, project);
       
  1284 
       
  1285 		if (traceFolderName == null) {
       
  1286 			traceFolderName = findTraceFolderNameFromFileSystem(projectRoot);
       
  1287 		}
       
  1288 		return traceFolderName;
       
  1289 	}
       
  1290 
       
  1291 	/**
       
  1292 	 * Find trace folder name from file system
       
  1293 	 * 
       
  1294 	 * @param projectRoot
       
  1295 	 *            project root
       
  1296 	 * @return the trace folder name
       
  1297 	 */
       
  1298 	private String findTraceFolderNameFromFileSystem(String projectRoot) {
       
  1299 		String traceFolderName = null;
       
  1300 		File file = new File(projectRoot);
       
  1301 
       
  1302 		File[] children = file.listFiles();
       
  1303 
       
  1304 		// Check that does subdirectory that name is one of the possible trace
       
  1305 		// folder name exist in this directory. If it exist use that as traces
       
  1306 		// directory name.
       
  1307 		for (int i = 0; i < children.length; i++) {
       
  1308 			File child = children[i];
       
  1309 			String childName = child.getName();
       
  1310 			if (child.isDirectory()) {
       
  1311 				if (childName
       
  1312 						.equalsIgnoreCase(tracesDirTargetNameTargetExtFolder)) {
       
  1313 					traceFolderName = tracesDirTargetNameTargetExtFolder;
       
  1314 					break;
       
  1315 				} else if (childName
       
  1316 						.equalsIgnoreCase(tracesTargetNameTargetTypeFolder)) {
       
  1317 					traceFolderName = tracesTargetNameTargetTypeFolder;
       
  1318 					break;
       
  1319 				} else if (childName
       
  1320 						.equalsIgnoreCase(tracesComponentNameFolder)) {
       
  1321 					traceFolderName = tracesComponentNameFolder;
       
  1322 					break;
       
  1323 				} else if (childName.equalsIgnoreCase(TRACES_DIRECTORY)) {
       
  1324 					traceFolderName = TRACES_DIRECTORY;
       
  1325 					break;
       
  1326 				}
       
  1327 			}
       
  1328 		}
       
  1329 
       
  1330 		// If trace folder name is "traces" we need to check that does it
       
  1331 		// include folder "<target name>_<target ext>". If it includes then
       
  1332 		// that folder need to be as traces folder name
       
  1333 		if (traceFolderName != null && traceFolderName.equals(TRACES_DIRECTORY) && tracesDirTargetNameTargetExtFolder != null) {
       
  1334 			file = new File(projectRoot + File.separator + TRACES_DIRECTORY);
       
  1335 			children = file.listFiles();
       
  1336 			int startIndex = tracesDirTargetNameTargetExtFolder
       
  1337 					.lastIndexOf(File.separator);
       
  1338 			String targetNameTargetExtFolder = tracesDirTargetNameTargetExtFolder
       
  1339 					.substring(startIndex + 1);
       
  1340 			for (int i = 0; i < children.length; i++) {
       
  1341 				File child = children[i];
       
  1342 				String childName = child.getName();
       
  1343 				if (child.isDirectory()) {
       
  1344 					if (childName.equalsIgnoreCase(targetNameTargetExtFolder)) {
       
  1345 						traceFolderName = tracesDirTargetNameTargetExtFolder;
       
  1346 						break;
       
  1347 					}
       
  1348 				}
       
  1349 			}
       
  1350 		}
       
  1351 
       
  1352 		// If trace folder is still null set default value based on used SBS
       
  1353 		// version
       
  1354 		if (traceFolderName == null) {
       
  1355 			if (sbsv2TraceFolderName != null) {
       
  1356 				traceFolderName = sbsv2TraceFolderName;
       
  1357 			} else {
       
  1358 				traceFolderName = tracesComponentNameFolder;
       
  1359 			}
       
  1360 		}
       
  1361 		return traceFolderName;
       
  1362 	}
       
  1363 
       
  1364 	/**
       
  1365 	 * Set SBSv2 trace folder name
       
  1366 	 * 
       
  1367 	 * @param project
       
  1368 	 *            Carbide project
       
  1369 	 */
       
  1370 	private void setSBSv2TraceFolderName(IProject project) {
       
  1371 		ICarbideBuildManager buildMgr = CarbideBuilderPlugin.getBuildManager();
       
  1372 
       
  1373 		boolean isSBSv2Project = buildMgr.isCarbideSBSv2Project(project);
       
  1374 
       
  1375 		if (isSBSv2Project) {
       
  1376 			ISDKManager sdkMgr = SDKCorePlugin.getSDKManager();
       
  1377 			Version sbsv2Version = sdkMgr.getSBSv2Version(true);
       
  1378 			int major = sbsv2Version.getMajor();
       
  1379 			int minor = sbsv2Version.getMinor();
       
  1380 			int micro = sbsv2Version.getMicro();
       
  1381 
       
  1382 			if ((major < 2) || (major == 2 && minor < 10)
       
  1383 					|| (major == 2 && minor == 10 && micro == 0)) {
       
  1384 				sbsv2TraceFolderName = tracesComponentNameFolder;
       
  1385 			} else if (major == 2) {
       
  1386 				if ((major == 2 && minor < 14)) {
       
  1387 					sbsv2TraceFolderName = tracesTargetNameTargetTypeFolder;
       
  1388 				} else {
       
  1389 					sbsv2TraceFolderName = tracesDirTargetNameTargetExtFolder;
       
  1390 				}
       
  1391 			} else {
       
  1392 				sbsv2TraceFolderName = tracesDirTargetNameTargetExtFolder;
       
  1393 			}
       
  1394 		} else {
       
  1395 			sbsv2TraceFolderName = null;
       
  1396 		}
       
  1397 	}
       
  1398 
       
  1399 	/**
       
  1400 	 * Checks if this folder is the project root
       
  1401 	 * 
       
  1402 	 * @param parentFolder
       
  1403 	 *            parent folder
       
  1404 	 * @return true if this folder is the project root
       
  1405 	 */
       
  1406 	private boolean isProjectRoot(File mmpFile) {
       
  1407 		// Try to find traces, group or mmpfiles folder from this folder
       
  1408 		boolean isProjectRoot = false;
       
  1409 
       
  1410 		FilenameFilter filter = new FilenameFilter() {
       
  1411 			public boolean accept(File file, String name) {
       
  1412 				boolean retval = false;
       
  1413 
       
  1414 				if (file.isDirectory()) {
       
  1415 					if (name
       
  1416 							.equalsIgnoreCase(tracesDirTargetNameTargetExtFolder)
       
  1417 							|| name
       
  1418 									.equalsIgnoreCase(tracesTargetNameTargetTypeFolder)
       
  1419 							|| name.equalsIgnoreCase(tracesComponentNameFolder)
       
  1420 							|| name.equalsIgnoreCase(TRACES_DIRECTORY)
       
  1421 							|| name.equalsIgnoreCase(GROUP_DIRECTORY)
       
  1422 							|| name.equalsIgnoreCase(MMPFILES_DIRECTORY)) {
       
  1423 						retval = true;
       
  1424 					}
       
  1425 				}
       
  1426 				return retval; // CodForChk_Dis_Exits
       
  1427 			}
       
  1428 		};
       
  1429 
       
  1430 		String[] children = mmpFile.list(filter);
       
  1431 
       
  1432 		if (children != null && children.length != 0) {
       
  1433 			isProjectRoot = true;
       
  1434 		}
       
  1435 
       
  1436 		return isProjectRoot; // CodForChk_Dis_Exits
       
  1437 	}
       
  1438 
       
  1439 	/**
       
  1440 	 * Saves Epoc root to Trace Builder Configuration
       
  1441 	 * 
       
  1442 	 * @param project
       
  1443 	 *            the project
       
  1444 	 */
       
  1445 	private void saveEpocRoot(IProject project) {
       
  1446 		IPath epocRoot = WorkbenchUtils.getEpocRootForProject(project);
       
  1447 
       
  1448 		if (epocRoot != null) {
       
  1449 			TraceBuilderGlobals.getConfiguration().setText(
       
  1450 					TraceBuilderConfiguration.ENVIRONMENT_ROOT,
       
  1451 					epocRoot.toOSString());
       
  1452 		} else {
       
  1453 			TraceBuilderGlobals.getConfiguration().setText(
       
  1454 					TraceBuilderConfiguration.ENVIRONMENT_ROOT, ""); //$NON-NLS-1$
       
  1455 		}
       
  1456 	}
       
  1457 
       
  1458 	/**
       
  1459 	 * Checks that the given file belongs to active project
       
  1460 	 * 
       
  1461 	 * @param file
       
  1462 	 *            the file
       
  1463 	 * @return true if valid, false if not
       
  1464 	 */
       
  1465 	boolean isFileActive(IFile file) {
       
  1466 		boolean isActive = false;
       
  1467 		IProject project = file.getProject();
       
  1468 		if (project.equals(openProjectObject) == true) {
       
  1469 
       
  1470 			// Check that project is Carbide project
       
  1471 			boolean isCarbideProject = CarbideBuilderPlugin.getBuildManager()
       
  1472 					.isCarbideProject(project);
       
  1473 			if (isCarbideProject) {
       
  1474 
       
  1475 				// If file is source file then we need to check that trace
       
  1476 				// folcer name is same than open project trace folder name
       
  1477 				if (isSourceFile(file)) {
       
  1478 					IPath path = file.getLocation();
       
  1479 					List<IPath> mmpPaths = EpocEngineHelper.getMMPsForSource(
       
  1480 							project, path);
       
  1481 					if (mmpPaths.size() > 0) {
       
  1482 						for (int i = 0; i < mmpPaths.size(); i++) {
       
  1483 							IPath mmpPath = mmpPaths.get(i);
       
  1484 							String mmpPathAsString = mmpPath.toString();
       
  1485 							String componentName = getComponentNameFromMMPFileName(mmpPathAsString);
       
  1486 							String currentComponentName = TraceBuilderGlobals
       
  1487 									.getCurrentSoftwareComponentName();
       
  1488 							if (componentName != null
       
  1489 									&& componentName
       
  1490 											.equalsIgnoreCase(currentComponentName)) {
       
  1491 								isActive = true;
       
  1492 								break;
       
  1493 							}
       
  1494 						}
       
  1495 					}
       
  1496 				} else {
       
  1497 					isActive = true;
       
  1498 				}
       
  1499 			}
       
  1500 		}
       
  1501 		return isActive;
       
  1502 	}
       
  1503 
       
  1504 	/**
       
  1505 	 * Check that is file source file
       
  1506 	 * 
       
  1507 	 * @param file
       
  1508 	 *            file that need to be check
       
  1509 	 * @return true if file is source file, false if not
       
  1510 	 */
       
  1511 	private boolean isSourceFile(IFile file) {
       
  1512 		boolean retVal = false;
       
  1513 
       
  1514 		String extension = file.getFileExtension();
       
  1515 
       
  1516 		if (extension != null
       
  1517 				&& (extension.equalsIgnoreCase(FileUtils.CPP_EXTENSION) || extension
       
  1518 						.equalsIgnoreCase(FileUtils.C_EXTENSION))) {
       
  1519 			retVal = true;
       
  1520 		}
       
  1521 		return retVal;
       
  1522 	}
       
  1523 
       
  1524 	/**
       
  1525 	 * Source saved
       
  1526 	 * 
       
  1527 	 * @param source
       
  1528 	 *            the source
       
  1529 	 */
       
  1530 	void sourceSaved(JFaceDocumentWrapper source) {
       
  1531 		if (!mmpFileModified && openProjectObject != null
       
  1532 				&& TraceBuilderGlobals.getTraceModel().hasTraces()) {
       
  1533 			String fileName = source.getFileName();
       
  1534 			String filepath = source.getFilePath();
       
  1535 			Path path = new Path(filepath + fileName);
       
  1536 
       
  1537 			// Add traces folder include path to MMP file
       
  1538 			addTracesFolderInclude(path);
       
  1539 
       
  1540 			// Add SYSTEMINCLUDE to MMP file if needed
       
  1541 			addOpenSystemTraceSystemInclude();
       
  1542 
       
  1543 			mmpFileModified = true;
       
  1544 		}
       
  1545 	}
       
  1546 
       
  1547 	/**
       
  1548 	 * Add layer SYSTEMINCLUDE macro to MMP file if needed
       
  1549 	 * 
       
  1550 	 */
       
  1551 	private void addOpenSystemTraceSystemInclude() {
       
  1552 
       
  1553 		if (CarbideBuilderPlugin.getBuildManager().isCarbideProject(
       
  1554 				openProjectObject)
       
  1555 				&& openProjectObject.isAccessible()) {
       
  1556 			ICarbideProjectInfo cpi = CarbideBuilderPlugin.getBuildManager()
       
  1557 					.getProjectInfo(openProjectObject);
       
  1558 			if (cpi != null) {
       
  1559 
       
  1560 				// loop through all build configurations since the list of mmp's
       
  1561 				// could be different depending on the build configuration
       
  1562 				for (ICarbideBuildConfiguration config : cpi
       
  1563 						.getBuildConfigurations()) {
       
  1564 
       
  1565 					// loop through each mmp file and add the include path if
       
  1566 					// necessary
       
  1567 					for (final IPath mmpPath : EpocEngineHelper
       
  1568 							.getMMPFilesForBuildConfiguration(config)) {
       
  1569 						final IPath epocroot = WorkbenchUtils
       
  1570 								.getEpocRootForProject(openProjectObject);
       
  1571 						String mmpFilePath = epocroot.toOSString()
       
  1572 								+ mmpPath.toOSString();
       
  1573 						try {
       
  1574 							// Open the file mmp file
       
  1575 							FileInputStream fistream = new FileInputStream(
       
  1576 									mmpFilePath);
       
  1577 
       
  1578 							// Get the object of DataInputStream
       
  1579 							DataInputStream in = new DataInputStream(fistream);
       
  1580 							BufferedReader br = new BufferedReader(
       
  1581 									new InputStreamReader(in));
       
  1582 
       
  1583 							// Read File Line By Line
       
  1584 							String strLine;
       
  1585 							int numberOfLastIncludeLine = 0;
       
  1586 							boolean headerExist = false;
       
  1587 							int headerEnd = -1;
       
  1588 							int line = 0;
       
  1589 							boolean platformPathsIncludeFound = false;
       
  1590 							boolean layerSystemIncludeMacroFound = false;
       
  1591 							Vector<String> mmpFileContent = new Vector<String>();
       
  1592 							while ((strLine = br.readLine()) != null) {
       
  1593 								mmpFileContent.add(strLine);
       
  1594 								line++;
       
  1595 								strLine = strLine.toUpperCase();
       
  1596 
       
  1597 								// Check does MMP file start with file header
       
  1598 								if (line == 1) {
       
  1599 									if (strLine
       
  1600 											.indexOf(MULTILINE_COMMENT_START) != -1) {
       
  1601 										headerExist = true;
       
  1602 									} else if (strLine.indexOf(COMMENT) != -1) {
       
  1603 										headerExist = true;
       
  1604 										headerEnd = line;
       
  1605 									}
       
  1606 								}
       
  1607 
       
  1608 								// Get header end, if MMP file starts with file
       
  1609 								// header that is made by using multiline
       
  1610 								// comment
       
  1611 								if (strLine.indexOf(MULTILINE_COMMENT_END) != -1
       
  1612 										&& headerExist && headerEnd == -1) {
       
  1613 									headerEnd = line;
       
  1614 								}
       
  1615 
       
  1616 								// Get header end, if MMP file starts with file
       
  1617 								// header that is made by using single
       
  1618 								// comments
       
  1619 								if (strLine.indexOf(COMMENT) != -1
       
  1620 										&& headerExist && headerEnd == line - 1) {
       
  1621 									headerEnd = line;
       
  1622 								}
       
  1623 
       
  1624 								// Get line of last include statement
       
  1625 								if (strLine.indexOf(INCLUDE) != -1) {
       
  1626 									numberOfLastIncludeLine = line;
       
  1627 								}
       
  1628 
       
  1629 								// Check does platform_patsh.hrh include already
       
  1630 								// exist
       
  1631 								if (strLine.indexOf(PLATFORM_PATHS_HRH) != -1) {
       
  1632 									platformPathsIncludeFound = true;
       
  1633 								}
       
  1634 
       
  1635 								// Check does layer system include macro already
       
  1636 								// exist
       
  1637 								if (strLine
       
  1638 										.indexOf(APP_LAYER_SYSTEMINCLUDE_MACRO) != -1
       
  1639 										|| strLine
       
  1640 												.indexOf(MW_LAYER_SYSTEMINCLUDE_MACRO) != -1
       
  1641 										|| strLine
       
  1642 												.indexOf(OS_LAYER_SYSTEMINCLUDE_MACRO) != -1) {
       
  1643 									layerSystemIncludeMacroFound = true;
       
  1644 									break;
       
  1645 								}
       
  1646 							}
       
  1647 
       
  1648 							// If includes did not exist, but file header exist,
       
  1649 							// then set number of last include line same as
       
  1650 							// header end.
       
  1651 							if (headerExist
       
  1652 									&& numberOfLastIncludeLine < headerEnd) {
       
  1653 								numberOfLastIncludeLine = headerEnd;
       
  1654 							}
       
  1655 
       
  1656 							// Close the buffered reader
       
  1657 							br.close();
       
  1658 
       
  1659 							// Close the input stream
       
  1660 							in.close();
       
  1661 
       
  1662 							// Close file input stream
       
  1663 							fistream.close();
       
  1664 
       
  1665 							if (!layerSystemIncludeMacroFound) {
       
  1666 								String layerSystemIncludeMacro = geLayerSystemIncludeMacro(mmpPath
       
  1667 										.toOSString());
       
  1668 								FileOutputStream fostream = new FileOutputStream(
       
  1669 										mmpFilePath);
       
  1670 								BufferedWriter bw = new BufferedWriter(
       
  1671 										new OutputStreamWriter(fostream));
       
  1672 
       
  1673 								for (line = 0; line < mmpFileContent.size(); line++) {
       
  1674 									if (line == numberOfLastIncludeLine) {
       
  1675 										if (!platformPathsIncludeFound) {
       
  1676 											bw
       
  1677 													.write(INCLUDE_PLATFORM_PATHS_HRH
       
  1678 															+ SourceConstants.LINE_FEED);
       
  1679 										}
       
  1680 										bw.write(SourceConstants.LINE_FEED
       
  1681 												+ layerSystemIncludeMacro
       
  1682 												+ SourceConstants.LINE_FEED);
       
  1683 									}
       
  1684 									bw.write(mmpFileContent.get(line)
       
  1685 											+ SourceConstants.LINE_FEED);
       
  1686 								}
       
  1687 
       
  1688 								// Close the buffered writer
       
  1689 								bw.close();
       
  1690 
       
  1691 								// Close file output stream
       
  1692 								fostream.close();
       
  1693 							}
       
  1694 						} catch (Exception e) {// Catch exception if any
       
  1695 							CarbideBuilderPlugin.log(e);
       
  1696 						}
       
  1697 					}
       
  1698 				}
       
  1699 			}
       
  1700 		}
       
  1701 	}
       
  1702 
       
  1703 	/**
       
  1704 	 * Get correct layer system include macro
       
  1705 	 * 
       
  1706 	 * @param string
       
  1707 	 *            MMP path as OS string
       
  1708 	 * 
       
  1709 	 * @return correct layer system include macro
       
  1710 	 */
       
  1711 	private String geLayerSystemIncludeMacro(String mmpPathAsOsString) {
       
  1712 		String layerSystemIncludeMacro = APP_LAYER_SYSTEMINCLUDE_MACRO;
       
  1713 		String firstDirectory = mmpPathAsOsString.substring(0,
       
  1714 				mmpPathAsOsString.indexOf(SourceConstants.BACKSLASH));
       
  1715 
       
  1716 		if (firstDirectory.equalsIgnoreCase(SF_FOLDER_NAME)) {
       
  1717 			String secondDirectory = mmpPathAsOsString.substring(firstDirectory
       
  1718 					.length() + 1, mmpPathAsOsString.indexOf(
       
  1719 					SourceConstants.BACKSLASH, firstDirectory.length() + 1));
       
  1720 			if (secondDirectory.equalsIgnoreCase(MW_FOLDER_NAME)) {
       
  1721 				layerSystemIncludeMacro = MW_LAYER_SYSTEMINCLUDE_MACRO;
       
  1722 			} else if (secondDirectory.equalsIgnoreCase(OS_FOLDER_NAME)) {
       
  1723 				layerSystemIncludeMacro = OS_LAYER_SYSTEMINCLUDE_MACRO;
       
  1724 			}
       
  1725 		} else if (firstDirectory.equalsIgnoreCase(NCP_SW_FOLDER_NAME)) {
       
  1726 			layerSystemIncludeMacro = OS_LAYER_SYSTEMINCLUDE_MACRO;
       
  1727 		}
       
  1728 
       
  1729 		return layerSystemIncludeMacro;
       
  1730 	}
       
  1731 
       
  1732 	/**
       
  1733 	 * Adds traces folder include to MMP files
       
  1734 	 * 
       
  1735 	 * @param path
       
  1736 	 *            file path
       
  1737 	 */
       
  1738 	private void addTracesFolderInclude(Path filePath) {
       
  1739 		IPath mainProjectPath = openProjectObject.getLocation();
       
  1740 		IPath traceProjectPath = new Path(openProjectPath);
       
  1741 
       
  1742 		// Calculate and remove matching segments
       
  1743 		int matchingSegs = mainProjectPath
       
  1744 				.matchingFirstSegments(traceProjectPath);
       
  1745 
       
  1746 		IPath projectRelativePath = traceProjectPath
       
  1747 				.removeFirstSegments(matchingSegs);
       
  1748 
       
  1749 		// Add Traces directory and remove device ID
       
  1750 		projectRelativePath = projectRelativePath
       
  1751 				.append(ProjectEngine.traceFolderName);
       
  1752 		projectRelativePath = projectRelativePath.setDevice(null);
       
  1753 
       
  1754 		// Add include path to the project MMP files
       
  1755 		addUserIncludeToMmpFiles(filePath, projectRelativePath);
       
  1756 	}
       
  1757 
       
  1758 	/**
       
  1759 	 * Get Carbide project info
       
  1760 	 * 
       
  1761 	 * @return Carbide project info
       
  1762 	 */
       
  1763 	private ICarbideProjectInfo getCarbideProjectInfo() {
       
  1764 		ICarbideProjectInfo cpi = null;
       
  1765 		if (CarbideBuilderPlugin.getBuildManager().isCarbideProject(
       
  1766 				openProjectObject)
       
  1767 				&& openProjectObject.isAccessible()) {
       
  1768 			cpi = CarbideBuilderPlugin.getBuildManager().getProjectInfo(
       
  1769 					openProjectObject);
       
  1770 		}
       
  1771 		return cpi;
       
  1772 	}
       
  1773 
       
  1774 	/**
       
  1775 	 * Add user include to MMP files, those uses specific source file
       
  1776 	 * 
       
  1777 	 * @param filePath
       
  1778 	 *            file path of the saved source file
       
  1779 	 * @param projectRelativeIncDirPath
       
  1780 	 *            project relative path to the include directory
       
  1781 	 */
       
  1782 	private void addUserIncludeToMmpFiles(final IPath filePath,
       
  1783 			final IPath projectRelativeIncDirPath) {
       
  1784 		if (openProjectObject != null && projectRelativeIncDirPath != null) {
       
  1785 
       
  1786 			final IPath epocroot = WorkbenchUtils
       
  1787 					.getEpocRootForProject(openProjectObject);
       
  1788 
       
  1789 			ICarbideProjectInfo cpi = getCarbideProjectInfo();
       
  1790 
       
  1791 			// Loop through all build configurations since the list of
       
  1792 			// mmp's could be different depending on the build
       
  1793 			// configuration
       
  1794 			for (ICarbideBuildConfiguration config : cpi
       
  1795 					.getBuildConfigurations()) {
       
  1796 
       
  1797 				// Get name of the MMP files where source file is used
       
  1798 				final List<IPath> mmpPaths = EpocEngineHelper.getMMPsForSource(
       
  1799 						openProjectObject, filePath);
       
  1800 				ArrayList<String> sourceMmpFileNames = new ArrayList<String>();
       
  1801 				for (int i = 0; i < mmpPaths.size(); i++) {
       
  1802 					sourceMmpFileNames.add(mmpPaths.get(i).lastSegment());
       
  1803 				}
       
  1804 
       
  1805 				// Go through build configuration's MMP files
       
  1806 				for (final IPath mmpPath : EpocEngineHelper
       
  1807 						.getMMPFilesForBuildConfiguration(config)) {
       
  1808 
       
  1809 					String mmpFileName = mmpPath.lastSegment();
       
  1810 
       
  1811 					if (sourceMmpFileNames.contains(mmpFileName)) {
       
  1812 
       
  1813 						EpocEnginePlugin.runWithMMPView(mmpPath,
       
  1814 								new DefaultMMPViewConfiguration(config,
       
  1815 										new AcceptedNodesViewFilter()),
       
  1816 								new MMPViewRunnableAdapter() {
       
  1817 
       
  1818 									public Object run(IMMPView view) {
       
  1819 										MMPViewPathHelper helper = new MMPViewPathHelper(
       
  1820 												view, epocroot);
       
  1821 										try {
       
  1822 
       
  1823 											// Convert the project relative path
       
  1824 											// to an mmp view path
       
  1825 											IPath incPath = helper
       
  1826 													.convertProjectOrFullPathToMMP(
       
  1827 															EMMPPathContext.USERINCLUDE, // CodForChk_Dis_LengthyLine
       
  1828 															projectRelativeIncDirPath); // CodForChk_Dis_LengthyLine
       
  1829 
       
  1830 											// Get the list of user include
       
  1831 											// paths and add it if it's not
       
  1832 											// already there
       
  1833 											List<IPath> userIncludes = view
       
  1834 													.getUserIncludes();
       
  1835 
       
  1836 											String incPathAsOSString = incPath
       
  1837 													.toOSString();
       
  1838 
       
  1839 											boolean includeExist = false;
       
  1840 
       
  1841 											// Go through existing userincludes
       
  1842 											// to check if the path to add is
       
  1843 											// already present
       
  1844 											for (int i = 0; i < userIncludes
       
  1845 													.size(); i++) {
       
  1846 												String pathAsOSString = userIncludes
       
  1847 														.get(i).toOSString();
       
  1848 												pathAsOSString = pathAsOSString
       
  1849 														.toLowerCase();
       
  1850 												if (pathAsOSString
       
  1851 														.equalsIgnoreCase(incPathAsOSString)) {
       
  1852 													includeExist = true;
       
  1853 													break;
       
  1854 												}
       
  1855 											}
       
  1856 
       
  1857 											if (!includeExist) {
       
  1858 												userIncludes.add(incPath);
       
  1859 												WorkbenchUtils
       
  1860 														.postUserIncludeMsg(mmpPath);
       
  1861 
       
  1862 												// Now commit the changes and
       
  1863 												// release the file
       
  1864 												while (true) {
       
  1865 													try {
       
  1866 														view.commit();
       
  1867 														break;
       
  1868 													} catch (IllegalStateException e) {
       
  1869 														CarbideBuilderPlugin
       
  1870 																.log(e);
       
  1871 														if (!view.merge()) {
       
  1872 															view.revert();
       
  1873 														}
       
  1874 													}
       
  1875 												}
       
  1876 											}
       
  1877 
       
  1878 										} catch (InvalidDriveInMMPPathException e) {
       
  1879 
       
  1880 											// Shouldn't get here; we passed in
       
  1881 											// a project-relative path
       
  1882 											CarbideBuilderPlugin.log(e);
       
  1883 										}
       
  1884 
       
  1885 										return null;
       
  1886 									}
       
  1887 								});
       
  1888 					}
       
  1889 				}
       
  1890 			}
       
  1891 		}
       
  1892 	}
       
  1893 }