tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/TraceCompilerModelListener.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2  * Copyright (c) 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  * Model extension listener for console UI
       
    17  *
       
    18  */
       
    19 package com.nokia.tracecompiler;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Collections;
       
    23 import java.util.Comparator;
       
    24 
       
    25 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages;
       
    26 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    27 import com.nokia.tracecompiler.engine.TraceLocation;
       
    28 import com.nokia.tracecompiler.engine.TraceLocationList;
       
    29 import com.nokia.tracecompiler.engine.TraceLocationListListener;
       
    30 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
       
    31 import com.nokia.tracecompiler.model.TraceModelExtension;
       
    32 import com.nokia.tracecompiler.model.TraceModelExtensionListener;
       
    33 import com.nokia.tracecompiler.model.TraceModelListener;
       
    34 import com.nokia.tracecompiler.model.TraceObject;
       
    35 import com.nokia.tracecompiler.model.TraceProcessingListener;
       
    36 import com.nokia.tracecompiler.source.SourceConstants;
       
    37 import com.nokia.tracecompiler.source.SourceLocationListener;
       
    38 
       
    39 /**
       
    40  * Model extension listener for TraceCompiler UI
       
    41  * 
       
    42  */
       
    43 final class TraceCompilerModelListener implements TraceModelListener,
       
    44 		TraceModelExtensionListener, TraceLocationListListener,
       
    45 		SourceLocationListener, TraceProcessingListener {
       
    46 
       
    47 	/**
       
    48 	 * List of errors for a source
       
    49 	 */
       
    50 	private ArrayList<TraceLocation> errorList = new ArrayList<TraceLocation>();
       
    51 
       
    52 	/**
       
    53 	 * Name of the file being processed
       
    54 	 */
       
    55 	private String currentFileName;
       
    56 
       
    57 	/*
       
    58 	 * (non-Javadoc)
       
    59 	 * 
       
    60 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
    61 	 * objectAdded(com.nokia.tracecompiler.model.TraceObject,
       
    62 	 * com.nokia.tracecompiler.model.TraceObject)
       
    63 	 */
       
    64 	public void objectAdded(TraceObject owner, TraceObject object) {
       
    65 	}
       
    66 
       
    67 	/*
       
    68 	 * (non-Javadoc)
       
    69 	 * 
       
    70 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
    71 	 * objectCreationComplete(com.nokia.tracecompiler.model.TraceObject)
       
    72 	 */
       
    73 	public void objectCreationComplete(TraceObject object) {
       
    74 	}
       
    75 
       
    76 	/*
       
    77 	 * (non-Javadoc)
       
    78 	 * 
       
    79 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
    80 	 * objectRemoved(com.nokia.tracecompiler.model.TraceObject,
       
    81 	 * com.nokia.tracecompiler.model.TraceObject)
       
    82 	 */
       
    83 	public void objectRemoved(TraceObject owner, TraceObject object) {
       
    84 	}
       
    85 
       
    86 	/*
       
    87 	 * (non-Javadoc)
       
    88 	 * 
       
    89 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
    90 	 * propertyUpdated(com.nokia.tracecompiler.model.TraceObject, int)
       
    91 	 */
       
    92 	public void propertyUpdated(TraceObject object, int property) {
       
    93 	}
       
    94 
       
    95 	/*
       
    96 	 * (non-Javadoc)
       
    97 	 * 
       
    98 	 * @see com.nokia.tracecompiler.model.TraceModelExtensionListener#
       
    99 	 * extensionAdded(com.nokia.tracecompiler.model.TraceObject,
       
   100 	 * com.nokia.tracecompiler.model.TraceModelExtension)
       
   101 	 */
       
   102 	public void extensionAdded(TraceObject object, TraceModelExtension extension) {
       
   103 		if (extension instanceof TraceLocationList) {
       
   104 			((TraceLocationList) extension).addLocationListListener(this);
       
   105 		}
       
   106 	}
       
   107 
       
   108 	/*
       
   109 	 * (non-Javadoc)
       
   110 	 * 
       
   111 	 * @see com.nokia.tracecompiler.model.TraceModelExtensionListener#
       
   112 	 * extensionRemoved(com.nokia.tracecompiler.model.TraceObject,
       
   113 	 * com.nokia.tracecompiler.model.TraceModelExtension)
       
   114 	 */
       
   115 	public void extensionRemoved(TraceObject object,
       
   116 			TraceModelExtension extension) {
       
   117 		if (extension instanceof TraceLocationList) {
       
   118 			((TraceLocationList) extension).removeLocationListListener(this);
       
   119 		}
       
   120 	}
       
   121 
       
   122 	/*
       
   123 	 * (non-Javadoc)
       
   124 	 * 
       
   125 	 * @see com.nokia.tracecompiler.source.TraceLocationListListener#
       
   126 	 * locationAdded(com.nokia.tracecompiler.engine.TraceLocation)
       
   127 	 */
       
   128 	public void locationAdded(TraceLocation location) {
       
   129 		location.addLocationListener(this);
       
   130 	}
       
   131 
       
   132 	/*
       
   133 	 * (non-Javadoc)
       
   134 	 * 
       
   135 	 * @see com.nokia.tracecompiler.source.TraceLocationListListener#
       
   136 	 * locationRemoved(com.nokia.tracecompiler.engine.TraceLocation)
       
   137 	 */
       
   138 	public void locationRemoved(TraceLocation location) {
       
   139 		location.removeLocationListener(this);
       
   140 	}
       
   141 
       
   142 	/*
       
   143 	 * (non-Javadoc)
       
   144 	 * 
       
   145 	 * @see com.nokia.tracecompiler.source.SourceLocationListener#
       
   146 	 * locationValidityChanged(com.nokia.tracecompiler.engine.TraceLocation)
       
   147 	 */
       
   148 	public void locationValidityChanged(TraceLocation location) {
       
   149 		String fileName = location.getFileName();
       
   150 		if (currentFileName != null) {
       
   151 			if (!fileName.equals(currentFileName)) {
       
   152 				currentFileName = fileName;
       
   153 				printLocationErrors();
       
   154 			}
       
   155 		} else {
       
   156 			currentFileName = fileName;
       
   157 		}
       
   158 		TraceCompilerErrorCode code = location.getValidityCode();
       
   159 		if (code != TraceCompilerErrorCode.OK
       
   160 				&& code != TraceCompilerErrorCode.TRACE_DOES_NOT_EXIST) {
       
   161 			if (!errorList.contains(location)) {
       
   162 				errorList.add(location);
       
   163 				location.reference();
       
   164 			}
       
   165 		} else {
       
   166 			if (errorList.remove(location)) {
       
   167 				location.dereference();
       
   168 			}
       
   169 		}
       
   170 	}
       
   171 
       
   172 	/**
       
   173 	 * Prints the errors from the error list
       
   174 	 */
       
   175 	void printLocationErrors() {
       
   176 		Collections.sort(errorList, new Comparator<TraceLocation>() {
       
   177 
       
   178 			/**
       
   179 			 * Sorts the array by line number
       
   180 			 * 
       
   181 			 * @param o1
       
   182 			 *            location 1
       
   183 			 * @param o2
       
   184 			 *            location 2
       
   185 			 * @return the result
       
   186 			 */
       
   187 			public int compare(TraceLocation o1, TraceLocation o2) {
       
   188 				int line1 = o1.getLineNumber();
       
   189 				int line2 = o2.getLineNumber();
       
   190 				return line1 > line2 ? 1 : line1 == line2 ? 0 : -1;
       
   191 			}
       
   192 
       
   193 		});
       
   194 		for (TraceLocation location : errorList) {
       
   195 			printLocationError(location);
       
   196 		}
       
   197 		errorList.clear();
       
   198 	}
       
   199 
       
   200 	/**
       
   201 	 * Prints a location error
       
   202 	 * 
       
   203 	 * @param location
       
   204 	 *            the location
       
   205 	 */
       
   206 	private void printLocationError(TraceLocation location) {
       
   207 		TraceCompilerEngineGlobals.getEvents().postErrorMessage(
       
   208 				location.getFileName()
       
   209 						+ Messages.getString("TraceCompilerModelListener.lineNumberPrefix") //$NON-NLS-1$
       
   210 						+ location.getLineNumber()
       
   211 						+ SourceConstants.COLON
       
   212 						+ SourceConstants.SPACE
       
   213 						+ TraceCompilerEngineErrorMessages.getErrorMessage(
       
   214 								location.getValidityCode(), location
       
   215 										.getValidityParameters()), null, true);
       
   216 	}
       
   217 
       
   218 	/**
       
   219 	 * get list of cumulated erros.
       
   220 	 * 
       
   221 	 * @return list of TaceLocaion
       
   222 	 */
       
   223 	public ArrayList<TraceLocation> getErrors() {
       
   224 		return errorList;
       
   225 	}
       
   226 
       
   227 	/*
       
   228 	 * (non-Javadoc)
       
   229 	 * 
       
   230 	 * @see
       
   231 	 * com.nokia.tracecompiler.model.TraceProcessingListener#processingComplete
       
   232 	 * (boolean)
       
   233 	 */
       
   234 	public void processingComplete(boolean changed) {
       
   235 		printLocationErrors();
       
   236 	}
       
   237 
       
   238 	/*
       
   239 	 * (non-Javadoc)
       
   240 	 * 
       
   241 	 * @see
       
   242 	 * com.nokia.tracecompiler.model.TraceProcessingListener#processingStarted()
       
   243 	 */
       
   244 	public void processingStarted() {
       
   245 	}
       
   246 }