trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/api/DataProcessorAdder.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-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  * Handles adding of DataProcessors to the list 
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.api;
       
    20 
       
    21 import java.util.HashMap;
       
    22 import java.util.Iterator;
       
    23 import java.util.List;
       
    24 import java.util.Map;
       
    25 
       
    26 import com.nokia.traceviewer.api.TraceViewerAPI.TVAPIError;
       
    27 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    28 import com.nokia.traceviewer.engine.TraceViewerViewInterface;
       
    29 import com.nokia.traceviewer.engine.dataprocessor.DataProcessor;
       
    30 import com.nokia.traceviewer.engine.dataprocessor.Decoder;
       
    31 import com.nokia.traceviewer.engine.dataprocessor.FilterProcessor;
       
    32 
       
    33 /**
       
    34  * Handles adding of DataProcessors to the list
       
    35  */
       
    36 final class DataProcessorAdder {
       
    37 
       
    38 	/**
       
    39 	 * Locations map
       
    40 	 */
       
    41 	private Map<DataProcessor, DataProcessorLocation> locations;
       
    42 
       
    43 	/**
       
    44 	 * Adds DataProcessor to the DataProcessors list and keeps track of the
       
    45 	 * locations
       
    46 	 * 
       
    47 	 * @param dp
       
    48 	 *            Dataprocessor
       
    49 	 * @param location
       
    50 	 *            location
       
    51 	 * @param priority
       
    52 	 *            priority of the DataProcessor
       
    53 	 * @return error code from TraceViewerAPI
       
    54 	 */
       
    55 	public TVAPIError addDataProcessor(DataProcessor dp, DPLocation location,
       
    56 			int priority) {
       
    57 		TVAPIError errorCode = TVAPIError.NONE;
       
    58 
       
    59 		// Create new dataprocessor location object
       
    60 		DataProcessorLocation newLocation = new DataProcessorLocation(location,
       
    61 				priority);
       
    62 
       
    63 		// Create the map if it doesn't exist
       
    64 		if (locations == null) {
       
    65 			locations = new HashMap<DataProcessor, DataProcessorLocation>();
       
    66 		}
       
    67 
       
    68 		List<DataProcessor> list = TraceViewerGlobals.getTraceViewer()
       
    69 				.getDataProcessorAccess().getDataProcessorList();
       
    70 
       
    71 		int decoderPos = 0;
       
    72 		int filterPos = 0;
       
    73 		int viewStartPos = 0;
       
    74 		int viewEndPos = 0;
       
    75 
       
    76 		// Loop through the list and gather the positions
       
    77 		for (int i = 0; i < list.size(); i++) {
       
    78 			if (list.get(i) instanceof Decoder) {
       
    79 				decoderPos = i;
       
    80 			} else if (list.get(i) instanceof FilterProcessor) {
       
    81 				filterPos = i;
       
    82 			} else if (list.get(i) instanceof TraceViewerViewInterface) {
       
    83 				if (viewStartPos == 0) {
       
    84 					viewStartPos = i;
       
    85 					viewEndPos = i;
       
    86 				} else {
       
    87 					viewEndPos = i;
       
    88 				}
       
    89 			}
       
    90 		}
       
    91 
       
    92 		// Check if this DataProcessor already exists in the list. If so, don't
       
    93 		// add anything and return error
       
    94 		if (locations.containsKey(dp)) {
       
    95 			errorCode = TVAPIError.DATAPROCESSOR_ALREADY_ADDED;
       
    96 
       
    97 			// If DataProcessor doesn't exist, add it to the list
       
    98 		} else {
       
    99 			// Add the dataprocessor to a right place
       
   100 			addToRightPlace(dp, location, priority, list, decoderPos,
       
   101 					filterPos, viewStartPos, viewEndPos);
       
   102 
       
   103 			// Add location to the list
       
   104 			locations.put(dp, newLocation);
       
   105 		}
       
   106 
       
   107 		return errorCode;
       
   108 
       
   109 	}
       
   110 
       
   111 	/**
       
   112 	 * Adds the dataprocessor to the right place in the DataProcessor list
       
   113 	 * 
       
   114 	 * @param dp
       
   115 	 *            the dataprocessor
       
   116 	 * @param location
       
   117 	 *            location where to add the dataprocessor
       
   118 	 * @param priority
       
   119 	 *            priority of the dataprocessor
       
   120 	 * @param list
       
   121 	 *            list of dataprocessors
       
   122 	 * @param decoderPos
       
   123 	 *            position of decoder in the list
       
   124 	 * @param filterPos
       
   125 	 *            position of filter in the list
       
   126 	 * @param viewStartPos
       
   127 	 *            position of first view in the list
       
   128 	 * @param viewEndPos
       
   129 	 *            position of last view in the list
       
   130 	 */
       
   131 	private void addToRightPlace(DataProcessor dp, DPLocation location,
       
   132 			int priority, List<DataProcessor> list, int decoderPos,
       
   133 			int filterPos, int viewStartPos, int viewEndPos) {
       
   134 
       
   135 		// Calculate how many dataprocessors there are with the same Location
       
   136 		// with smaller and bigger prioritys
       
   137 		int smallerPrioritys = 0;
       
   138 		int higherPrioritys = 0;
       
   139 
       
   140 		Iterator<DataProcessorLocation> iterator = locations.values()
       
   141 				.iterator();
       
   142 		while (iterator.hasNext()) {
       
   143 			DataProcessorLocation loc = iterator.next();
       
   144 			if (loc.getLocation() == location) {
       
   145 				if (loc.getPriority() > priority) {
       
   146 					higherPrioritys++;
       
   147 				} else {
       
   148 					smallerPrioritys++;
       
   149 				}
       
   150 			}
       
   151 		}
       
   152 
       
   153 		// Switch the location
       
   154 		switch (location) {
       
   155 
       
   156 		case BEFORE_DECODER:
       
   157 			list.add(decoderPos - smallerPrioritys, dp);
       
   158 			break;
       
   159 		case AFTER_DECODER:
       
   160 			list.add(decoderPos + 1 + higherPrioritys, dp);
       
   161 			break;
       
   162 		case BEFORE_FILTER:
       
   163 			list.add(filterPos - smallerPrioritys, dp);
       
   164 			break;
       
   165 		case AFTER_FILTER:
       
   166 			list.add(filterPos + 1 + higherPrioritys, dp);
       
   167 			break;
       
   168 		case BEFORE_VIEW:
       
   169 			list.add(viewStartPos - smallerPrioritys, dp);
       
   170 			break;
       
   171 		case AFTER_VIEW:
       
   172 			list.add(viewEndPos + 1 + higherPrioritys, dp);
       
   173 			break;
       
   174 		default:
       
   175 			break;
       
   176 		}
       
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * Removes DataProcessor from the list of DataProcessors
       
   181 	 * 
       
   182 	 * @param dataProcessor
       
   183 	 *            the DataProcessor to be removed
       
   184 	 * @return error code from TraceViewerAPI
       
   185 	 */
       
   186 	public TVAPIError removeDataProcessor(DataProcessor dataProcessor) {
       
   187 		TVAPIError errorCode = TVAPIError.DATAPROCESSOR_NOT_FOUND;
       
   188 
       
   189 		// Check if the DataProcessor can be found from the list
       
   190 		if (locations != null && locations.containsKey(dataProcessor)) {
       
   191 
       
   192 			// Remove DataProcessor from the list and from the locations
       
   193 			boolean removed = TraceViewerGlobals.getTraceViewer()
       
   194 					.getDataProcessorAccess().getDataProcessorList().remove(
       
   195 							dataProcessor);
       
   196 
       
   197 			if (removed) {
       
   198 				errorCode = TVAPIError.NONE;
       
   199 			}
       
   200 			locations.remove(dataProcessor);
       
   201 		}
       
   202 		return errorCode;
       
   203 	}
       
   204 }