tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/source/SourceLocation.java
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     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 * Represents a location in source
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.source;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Collections;
       
    23 import java.util.Iterator;
       
    24 import java.util.List;
       
    25 
       
    26 /**
       
    27  * Represents a location in source
       
    28  * 
       
    29  */
       
    30 public class SourceLocation extends SourceLocationBase {
       
    31 
       
    32 	/**
       
    33 	 * Location listeners
       
    34 	 */
       
    35 	private ArrayList<SourceLocationListener> locationListeners;
       
    36 
       
    37 	/**
       
    38 	 * Temporary array for callback purposes. Prevents concurrent modifications
       
    39 	 * if listeners are removed during a callback
       
    40 	 */
       
    41 	private ArrayList<SourceLocationListener> tempListeners;
       
    42 
       
    43 	/**
       
    44 	 * Reference count
       
    45 	 */
       
    46 	private int refCount = 0;
       
    47 
       
    48 	/**
       
    49 	 * Listener change flag
       
    50 	 */
       
    51 	private boolean listenersChanged = true;
       
    52 
       
    53 	/**
       
    54 	 * Constructor
       
    55 	 * 
       
    56 	 * @param parser
       
    57 	 *            the parser owning this location
       
    58 	 * @param offset
       
    59 	 *            offset of location
       
    60 	 * @param length
       
    61 	 *            length of location
       
    62 	 */
       
    63 	public SourceLocation(SourceParser parser, int offset, int length) {
       
    64 		super(parser, offset, length);
       
    65 		parser.addLocation(this);
       
    66 		refCount = 1;
       
    67 	}
       
    68 
       
    69 	/**
       
    70 	 * Gets the name of the class which owns this location
       
    71 	 * 
       
    72 	 * @return the class name
       
    73 	 */
       
    74 	public String getClassName() {
       
    75 		String retval = null;
       
    76 		if (getParser() != null) {
       
    77 			SourceContext context = getParser().getContext(getOffset());
       
    78 			if (context != null) {
       
    79 				retval = context.getClassName();
       
    80 			}
       
    81 		}
       
    82 		return retval;
       
    83 	}
       
    84 
       
    85 	/**
       
    86 	 * Gets the name of function which owns this location
       
    87 	 * 
       
    88 	 * @return the function name
       
    89 	 */
       
    90 	public String getFunctionName() {
       
    91 		String retval = null;
       
    92 		if (getParser() != null) {
       
    93 			SourceContext context = getParser().getContext(getOffset());
       
    94 			if (context != null) {
       
    95 				retval = context.getFunctionName();
       
    96 			}
       
    97 		}
       
    98 		return retval;
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Adds a location listener to this location
       
   103 	 * 
       
   104 	 * @param listener
       
   105 	 *            the location listener
       
   106 	 */
       
   107 	public void addLocationListener(SourceLocationListener listener) {
       
   108 		if (locationListeners == null) {
       
   109 			locationListeners = new ArrayList<SourceLocationListener>();
       
   110 		}
       
   111 		locationListeners.add(listener);
       
   112 		listenersChanged = true;
       
   113 	}
       
   114 
       
   115 	/**
       
   116 	 * Removes a location listener from this location
       
   117 	 * 
       
   118 	 * @param listener
       
   119 	 *            the location listener
       
   120 	 */
       
   121 	public void removeLocationListener(SourceLocationListener listener) {
       
   122 		if (locationListeners != null) {
       
   123 			if (locationListeners.remove(listener)) {
       
   124 				listenersChanged = true;
       
   125 			}
       
   126 		}
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * Gets the listener interfaces
       
   131 	 * 
       
   132 	 * @return the listeners
       
   133 	 */
       
   134 	protected Iterator<SourceLocationListener> getListeners() {
       
   135 		List<SourceLocationListener> list;
       
   136 		if (locationListeners != null) {
       
   137 			if (listenersChanged) {
       
   138 				listenersChanged = false;
       
   139 				if (tempListeners == null) {
       
   140 					tempListeners = new ArrayList<SourceLocationListener>();
       
   141 				}
       
   142 				tempListeners.clear();
       
   143 				tempListeners.addAll(locationListeners);
       
   144 			}
       
   145 			list = tempListeners;
       
   146 		} else {
       
   147 			list = Collections.emptyList();
       
   148 		}
       
   149 		return list.iterator();
       
   150 	}
       
   151 
       
   152 	/**
       
   153 	 * Increases the reference count of this location.
       
   154 	 * 
       
   155 	 * @see #dereference()
       
   156 	 */
       
   157 	public void reference() {
       
   158 		refCount++;
       
   159 	}
       
   160 
       
   161 	/**
       
   162 	 * Decrements the reference count of this location. When reference count is
       
   163 	 * 0, this is removed from source. Note that a location can also be removed
       
   164 	 * from source even if it has outstanding references left. In that case it
       
   165 	 * can no longer be selected.
       
   166 	 */
       
   167 	public void dereference() {
       
   168 		if (--refCount <= 0) {
       
   169 			removeFromSource();
       
   170 		}
       
   171 	}
       
   172 
       
   173 	/**
       
   174 	 * Removes this location from the source
       
   175 	 */
       
   176 	private void removeFromSource() {
       
   177 		delete();
       
   178 		if (getParser() != null) {
       
   179 			getParser().removeLocation(this);
       
   180 			resetParser();
       
   181 		}
       
   182 	}
       
   183 
       
   184 }