trace/tracebuilder/com.nokia.tracebuilder/src/com/nokia/tracebuilder/engine/source/SourceEditorUpdateQueue.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2 * Copyright (c) 2007 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 * Update queue for a source editor
       
    17 *
       
    18 */
       
    19 package com.nokia.tracebuilder.engine.source;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Collections;
       
    23 import java.util.Comparator;
       
    24 
       
    25 import com.nokia.tracebuilder.engine.TraceBuilderConfiguration;
       
    26 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    27 
       
    28 /**
       
    29  * Update queue for a source editor
       
    30  * 
       
    31  */
       
    32 final class SourceEditorUpdateQueue implements Runnable {
       
    33 
       
    34 	/**
       
    35 	 * Comparator for the update queue
       
    36 	 */
       
    37 	private Comparator<SourceEditorUpdater> updateComparator = new Comparator<SourceEditorUpdater>() {
       
    38 
       
    39 		/**
       
    40 		 * Compares the two source editors
       
    41 		 * 
       
    42 		 * @param o1
       
    43 		 *            editor 1
       
    44 		 * @param o2
       
    45 		 *            editor 2
       
    46 		 * @return result
       
    47 		 */
       
    48 		public int compare(SourceEditorUpdater o1, SourceEditorUpdater o2) {
       
    49 			int i1 = o1.getPosition().getOffset();
       
    50 			int i2 = o2.getPosition().getOffset();
       
    51 			return (i1 > i2) ? -1 : ((i1 == i2) ? 0 : 1);
       
    52 		}
       
    53 
       
    54 	};
       
    55 
       
    56 	/**
       
    57 	 * List of update operations to be run
       
    58 	 */
       
    59 	private ArrayList<SourceEditorUpdater> updateQueue;
       
    60 
       
    61 	/**
       
    62 	 * Queued updates flag
       
    63 	 */
       
    64 	private boolean hasQueuedUpdates;
       
    65 
       
    66 	/**
       
    67 	 * Source
       
    68 	 */
       
    69 	private SourceProperties properties;
       
    70 
       
    71 	/**
       
    72 	 * Constructor
       
    73 	 * 
       
    74 	 * @param properties
       
    75 	 *            the source
       
    76 	 */
       
    77 	SourceEditorUpdateQueue(SourceProperties properties) {
       
    78 		this.properties = properties;
       
    79 		updateQueue = new ArrayList<SourceEditorUpdater>();
       
    80 	}
       
    81 
       
    82 	/*
       
    83 	 * (non-Javadoc)
       
    84 	 * 
       
    85 	 * @see java.lang.Runnable#run()
       
    86 	 */
       
    87 	public void run() {
       
    88 		// Updates are copied, since an update may affect the queue
       
    89 		SourceEditorUpdater[] updaters = new SourceEditorUpdater[updateQueue
       
    90 				.size()];
       
    91 		updateQueue.toArray(updaters);
       
    92 		// Removes the operations that are run from the queue
       
    93 		resetUpdateQueue();
       
    94 		try {
       
    95 			for (int i = 0; i < updaters.length; i++) {
       
    96 				if (i == updaters.length - 1) {
       
    97 					hasQueuedUpdates = false;
       
    98 				} else {
       
    99 					hasQueuedUpdates = true;
       
   100 				}
       
   101 				boolean updated = updaters[i].runUpdate();
       
   102 				if (!updated) {
       
   103 					// Generates a dummy update, otherwise the source will be
       
   104 					// left in invalid state
       
   105 					properties.getSourceEditor().updateSource(0, 0, ""); //$NON-NLS-1$
       
   106 				}
       
   107 			}
       
   108 		} catch (Exception e) {
       
   109 			if (TraceBuilderConfiguration.ASSERTIONS_ENABLED) {
       
   110 				TraceBuilderGlobals.getEvents().postCriticalAssertionFailed(
       
   111 						"Source update failure", e); //$NON-NLS-1$
       
   112 			}
       
   113 		}
       
   114 	}
       
   115 
       
   116 	/**
       
   117 	 * Queues an asynchronous operation
       
   118 	 * 
       
   119 	 * @param updater
       
   120 	 *            the operation
       
   121 	 */
       
   122 	void queueUpdate(SourceEditorUpdater updater) {
       
   123 		if (updateQueue.isEmpty()) {
       
   124 			updateQueue.add(updater);
       
   125 			TraceBuilderGlobals.runAsyncOperation(this);
       
   126 		} else {
       
   127 			int index = Collections.binarySearch(updateQueue, updater,
       
   128 					updateComparator);
       
   129 			if (index >= 0) {
       
   130 				boolean duplicate = false;
       
   131 				// Checks backwards for duplicates
       
   132 				for (int i = index; i >= 0; i--) {
       
   133 					SourceEditorUpdater queuedUpdater = updateQueue.get(i);
       
   134 					if (queuedUpdater.getPosition().getOffset() == updater
       
   135 							.getPosition().getOffset()) {
       
   136 						if (updater.getPosition() == queuedUpdater
       
   137 								.getPosition()) {
       
   138 							duplicate = true;
       
   139 							i = -1;
       
   140 						}
       
   141 					}
       
   142 				}
       
   143 				// Also checks forwards for duplicates
       
   144 				for (int i = index; i < updateQueue.size(); i++) {
       
   145 					SourceEditorUpdater queuedUpdater = updateQueue.get(i);
       
   146 					if (queuedUpdater.getPosition().getOffset() == updater
       
   147 							.getPosition().getOffset()) {
       
   148 						if (updater.getPosition() == queuedUpdater
       
   149 								.getPosition()) {
       
   150 							duplicate = true;
       
   151 							i = updateQueue.size();
       
   152 						} else {
       
   153 							// New entry is added after others with same offset
       
   154 							index++;
       
   155 						}
       
   156 					} else {
       
   157 						i = updateQueue.size();
       
   158 					}
       
   159 				}
       
   160 				if (!duplicate) {
       
   161 					updateQueue.add(index, updater);
       
   162 				}
       
   163 			} else {
       
   164 				// Adds the update to correct position in the queue
       
   165 				// The updates are run starting from the end of file so they
       
   166 				// will not interfere each other
       
   167 				updateQueue.add(-1 - index, updater);
       
   168 			}
       
   169 		}
       
   170 	}
       
   171 
       
   172 	/**
       
   173 	 * Resets the queue when all processing has been done
       
   174 	 */
       
   175 	void resetUpdateQueue() {
       
   176 		updateQueue.clear();
       
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * Flag which determines if there are updates in the queue
       
   181 	 * 
       
   182 	 * @return true if queued, false if not
       
   183 	 */
       
   184 	boolean hasQueuedUpdates() {
       
   185 		return hasQueuedUpdates;
       
   186 	}
       
   187 
       
   188 }