trace/tracebuilder/com.nokia.tracebuilder/src/com/nokia/tracebuilder/engine/source/IncludeStatementAdder.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2 * Copyright (c) 2008 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 * Queues addition of #include statement into source file
       
    17 *
       
    18 */
       
    19 package com.nokia.tracebuilder.engine.source;
       
    20 
       
    21 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    22 import com.nokia.tracebuilder.plugin.TraceFormatConstants;
       
    23 import com.nokia.tracebuilder.plugin.TraceAPIFormatter.TraceFormatType;
       
    24 import com.nokia.tracebuilder.source.SourceConstants;
       
    25 import com.nokia.tracebuilder.source.SourceEditor;
       
    26 import com.nokia.tracebuilder.source.SourceLocation;
       
    27 import com.nokia.tracebuilder.source.SourceLocationBase;
       
    28 import com.nokia.tracebuilder.source.SourceParserException;
       
    29 
       
    30 /**
       
    31  * Queues addition of <code>#include</code> statement into source file
       
    32  * 
       
    33  */
       
    34 class IncludeStatementAdder extends SourceEditorUpdater {
       
    35 
       
    36 	/**
       
    37 	 * Name of include file
       
    38 	 */
       
    39 	private String includeFile;
       
    40 
       
    41 	/**
       
    42 	 * Location (0, 0) -> this is run after other operations
       
    43 	 */
       
    44 	private SourceLocationBase location;
       
    45 
       
    46 	/**
       
    47 	 * Constructor
       
    48 	 * 
       
    49 	 * @param properties
       
    50 	 *            the source to update
       
    51 	 * @param include
       
    52 	 *            the include file to add to the source
       
    53 	 */
       
    54 	IncludeStatementAdder(SourceProperties properties, String include) {
       
    55 		super(properties);
       
    56 		includeFile = include;
       
    57 		location = properties.getSourceEditor().createHiddenLocation(0, 0);
       
    58 	}
       
    59 
       
    60 	/*
       
    61 	 * (non-Javadoc)
       
    62 	 * 
       
    63 	 * @see com.nokia.tracebuilder.engine.source.SourceEditorUpdater#runUpdate()
       
    64 	 */
       
    65 	@Override
       
    66 	protected boolean runUpdate() throws SourceParserException {
       
    67 		boolean updated = false;
       
    68 		SourceEditor sourceEditor = getSource().getSourceEditor();
       
    69 		try {
       
    70 			if (includeFile != null) {
       
    71 				
       
    72 				// Update excluded areas before finding the include
       
    73 				sourceEditor.findExcludedAreas();
       
    74 				
       
    75 				int index = sourceEditor.findInclude(includeFile);
       
    76 				if (index >= 0) {
       
    77 					// Already in the source
       
    78 					includeFile = null;
       
    79 				} else {
       
    80 					index = -1 - index;
       
    81 					String inc = createIncludeLine();
       
    82 					sourceEditor.updateSource(index, 0, inc);
       
    83 					updated = true;
       
    84 					postIncludeAddedMessage(sourceEditor, index, inc);
       
    85 				}
       
    86 			}
       
    87 		} finally {
       
    88 			// The location needs to be removed from the source
       
    89 			sourceEditor.removeHiddenLocation(location);
       
    90 		}
       
    91 		return updated;
       
    92 	}
       
    93 
       
    94 	/**
       
    95 	 * Posts an event specifying that include was added to code
       
    96 	 * 
       
    97 	 * @param sourceEditor
       
    98 	 *            the source editor
       
    99 	 * @param index
       
   100 	 *            the index where include was added
       
   101 	 * @param inc
       
   102 	 *            the include statement
       
   103 	 */
       
   104 	private void postIncludeAddedMessage(SourceEditor sourceEditor, int index,
       
   105 			String inc) {
       
   106 		// Removes white spaces from the include statement
       
   107 		String trimmed = inc.trim();
       
   108 		SourceLocation loc = sourceEditor.createLocation(index
       
   109 				+ SourceConstants.LINE_FEED.length(), trimmed.length());
       
   110 		String pref = Messages
       
   111 				.getString("IncludeStatementAdder.IncludeAddedEventPrefix"); //$NON-NLS-1$
       
   112 		String post = Messages
       
   113 				.getString("IncludeStatementAdder.IncludeAddedEventPostfix"); //$NON-NLS-1$
       
   114 		TraceBuilderGlobals.getEvents().postInfoMessage(
       
   115 				pref + includeFile + post, loc);
       
   116 		// The location reference is removed. The event handler has
       
   117 		// incremented the reference if it stored the location
       
   118 		loc.dereference();
       
   119 	}
       
   120 
       
   121 	/**
       
   122 	 * Creates the #include statement to be inserted into source
       
   123 	 * 
       
   124 	 * @return the string buffer containing the #include
       
   125 	 */
       
   126 	private String createIncludeLine() {
       
   127 		TraceFormattingRule rule = TraceBuilderGlobals.getTraceModel()
       
   128 				.getExtension(TraceFormattingRule.class);
       
   129 		String template = rule.getFormat(null, TraceFormatType.INCLUDE_FORMAT);
       
   130 		int templateIndex = template
       
   131 				.indexOf(TraceFormatConstants.INCLUDE_FORMAT);
       
   132 		StringBuffer sb = new StringBuffer();
       
   133 		sb.append(template);
       
   134 		sb.append(SourceConstants.LINE_FEED);
       
   135 		sb.replace(templateIndex, templateIndex
       
   136 				+ TraceFormatConstants.INCLUDE_FORMAT.length(), includeFile);
       
   137 		sb.insert(0, SourceConstants.LINE_FEED);
       
   138 		return sb.toString();
       
   139 	}
       
   140 
       
   141 	/*
       
   142 	 * (non-Javadoc)
       
   143 	 * 
       
   144 	 * @see com.nokia.tracebuilder.engine.source.SourceEditorUpdater#getPosition()
       
   145 	 */
       
   146 	@Override
       
   147 	protected SourceLocationBase getPosition() {
       
   148 		return location;
       
   149 	}
       
   150 
       
   151 }