tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/source/SourceEngine.java
changeset 56 aa2539c91954
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     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 * Source engine manages source documents that are opened to Eclipse UI
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine.source;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Iterator;
       
    23 
       
    24 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    25 import com.nokia.tracecompiler.model.TraceModel;
       
    26 import com.nokia.tracecompiler.source.SourceDocumentInterface;
       
    27 import com.nokia.tracecompiler.source.SourceDocumentMonitor;
       
    28 import com.nokia.tracecompiler.source.SourceDocumentProcessor;
       
    29 import com.nokia.tracecompiler.utils.DocumentFactory;
       
    30 
       
    31 /**
       
    32  * Source engine manages source documents that are opened to Eclipse UI.
       
    33  * 
       
    34  */
       
    35 public class SourceEngine implements SourceDocumentProcessor,
       
    36 		Iterable<SourceProperties> {
       
    37 
       
    38 	/**
       
    39 	 * Document monitor
       
    40 	 */
       
    41 	private SourceDocumentMonitor documentMonitor;
       
    42 
       
    43 	/**
       
    44 	 * Trace model listener implementation
       
    45 	 */
       
    46 	private SourceEngineModelListener modelListener = new SourceEngineModelListener(
       
    47 			this);
       
    48 
       
    49 	/**
       
    50 	 * Trace model extension listener
       
    51 	 */
       
    52 	private SourceEngineModelExtensionListener extensionListener = new SourceEngineModelExtensionListener(
       
    53 			this);
       
    54 
       
    55 	/**
       
    56 	 * The callback interfaces are notified about source file changes
       
    57 	 */
       
    58 	private ArrayList<SourceListener> listeners = new ArrayList<SourceListener>();
       
    59 
       
    60 	/**
       
    61 	 * Running flag
       
    62 	 */
       
    63 	private boolean running;
       
    64 
       
    65 	/**
       
    66 	 * Trace model
       
    67 	 */
       
    68 	private TraceModel model;
       
    69 
       
    70 	/**
       
    71 	 * Source list
       
    72 	 */
       
    73 	private ArrayList<SourceProperties> tempList = new ArrayList<SourceProperties>();
       
    74 
       
    75 	/**
       
    76 	 * Read-only files
       
    77 	 */
       
    78 	private String[] READ_ONLY = { ".h" //$NON-NLS-1$
       
    79 	};
       
    80 
       
    81 	/**
       
    82 	 * Non-source file list
       
    83 	 */
       
    84 	private ArrayList<String> nonSourceFiles = new ArrayList<String>();
       
    85 
       
    86 	/**
       
    87 	 * Constructor
       
    88 	 * 
       
    89 	 * @param model
       
    90 	 *            the trace model
       
    91 	 */
       
    92 	public SourceEngine(TraceModel model) {
       
    93 		this.model = model;
       
    94 	}
       
    95 
       
    96 	/**
       
    97 	 * Starts this engine. Does nothing if already running
       
    98 	 * @throws Exception 
       
    99 	 */
       
   100 	public void start() throws Exception {
       
   101 		if (!running) {
       
   102 			documentMonitor = DocumentFactory.getDocumentMonitor();
       
   103 			documentMonitor.startMonitor(this);
       
   104 			running = true;
       
   105 			model.addModelListener(modelListener);
       
   106 			model.addExtensionListener(extensionListener);
       
   107 		}
       
   108 	}
       
   109 
       
   110 	/**
       
   111 	 * Shuts down the source engine. Does nothing if already stopped
       
   112 	 */
       
   113 	public void shutdown() {
       
   114 		if (running) {
       
   115 			documentMonitor.stopMonitor();
       
   116 			documentMonitor = null;
       
   117 			running = false;
       
   118 			model.removeModelListener(modelListener);
       
   119 			model.removeExtensionListener(extensionListener);
       
   120 		}
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Adds source listener callback interface
       
   125 	 * 
       
   126 	 * @param listener
       
   127 	 *            the new listener
       
   128 	 */
       
   129 	public void addSourceListener(SourceListener listener) {
       
   130 		listeners.add(listener);
       
   131 	}
       
   132 
       
   133 	/**
       
   134 	 * Removes a source listener
       
   135 	 * 
       
   136 	 * @param listener
       
   137 	 *            the listener to be removed
       
   138 	 */
       
   139 	public void removeSourceListener(SourceListener listener) {
       
   140 		listeners.remove(listener);
       
   141 	}
       
   142 
       
   143 	/**
       
   144 	 * Gets the sources
       
   145 	 * 
       
   146 	 * @return the sources
       
   147 	 */
       
   148 	public Iterator<SourceProperties> getSources() {
       
   149 		tempList.clear();
       
   150 		for (SourceDocumentInterface doc : documentMonitor) {
       
   151 			tempList.add((SourceProperties) doc.getOwner());
       
   152 		}
       
   153 		return tempList.iterator();
       
   154 	}
       
   155 
       
   156 	/*
       
   157 	 * (non-Javadoc)
       
   158 	 * 
       
   159 	 * @see java.lang.Iterable#iterator()
       
   160 	 */
       
   161 	public Iterator<SourceProperties> iterator() {
       
   162 		return getSources();
       
   163 	}
       
   164 
       
   165 	/*
       
   166 	 * (non-Javadoc)
       
   167 	 * 
       
   168 	 * @see com.nokia.tracecompiler.source.SourceDocumentProcessor#
       
   169 	 *      sourceOpened(com.nokia.tracecompiler.source.SourceDocumentInterface)
       
   170 	 */
       
   171 	public void sourceOpened(SourceDocumentInterface source) throws TraceCompilerException {
       
   172 		SourceProperties properties = new SourceProperties(model,
       
   173 				documentMonitor.getFactory(), source);
       
   174 		// Headers are marked read-only
       
   175 		for (String s : READ_ONLY) {
       
   176 			String fileName = properties.getFileName();
       
   177 			if (fileName != null && fileName.endsWith(s)) {
       
   178 				properties.setReadOnly(true);
       
   179 				break;
       
   180 			}
       
   181 		}
       
   182 		properties.sourceOpened();
       
   183 		source.setOwner(properties);
       
   184 		for (SourceListener l : listeners) {
       
   185 			l.sourceOpened(properties);
       
   186 		}
       
   187 	}
       
   188 
       
   189 	/**
       
   190 	 * Adds a non-source file to this list.
       
   191 	 * 
       
   192 	 * @param filePath
       
   193 	 *            the non-source file path to added
       
   194 	 */
       
   195 	public void addNonSourceFile(String filePath) {
       
   196 		nonSourceFiles.add(filePath);
       
   197 	}
       
   198 
       
   199 	/**
       
   200 	 * Removes a non-source file from this list
       
   201 	 * 
       
   202 	 * @param filePath
       
   203 	 *            the non-source file path to be removed
       
   204 	 * @return true if removed
       
   205 	 */
       
   206 	public boolean removeNonSourceFile(String filePath) {
       
   207 		boolean retVal = nonSourceFiles.remove(filePath);
       
   208 		return retVal;
       
   209 	}
       
   210 
       
   211 	/**
       
   212 	 * Gets list of non-source files
       
   213 	 * 
       
   214 	 * @return the list of non-source file paths
       
   215 	 */
       
   216 	public ArrayList<String> getNonSourceFiles() {
       
   217 		return nonSourceFiles;
       
   218 	}
       
   219 
       
   220 }