tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/project/TraceProjectFile.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 * Generic trace project file properties
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.project;
       
    20 
       
    21 import java.io.File;
       
    22 
       
    23 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    24 import com.nokia.tracecompiler.file.FileUtils;
       
    25 import com.nokia.tracecompiler.model.TraceModel;
       
    26 import com.nokia.tracecompiler.model.TraceModelExtension;
       
    27 import com.nokia.tracecompiler.model.TraceModelListener;
       
    28 import com.nokia.tracecompiler.model.TraceObject;
       
    29 import com.nokia.tracecompiler.source.SourceConstants;
       
    30 
       
    31 /**
       
    32  * Generic trace project file properties
       
    33  * 
       
    34  */
       
    35 public abstract class TraceProjectFile implements TraceModelExtension,
       
    36 		TraceModelListener {
       
    37 
       
    38 	/**
       
    39 	 * The trace model
       
    40 	 */
       
    41 	private TraceModel owner;
       
    42 
       
    43 	/**
       
    44 	 * Name of the project file
       
    45 	 */
       
    46 	private String name;
       
    47 
       
    48 	/**
       
    49 	 * Project file path
       
    50 	 */
       
    51 	private String path;
       
    52 
       
    53 	/**
       
    54 	 * Project file name is based on model name and updated when model changes
       
    55 	 */
       
    56 	protected boolean hasModelName;
       
    57 
       
    58 	/**
       
    59 	 * Constructor
       
    60 	 * 
       
    61 	 * @param path
       
    62 	 *            the path to the file
       
    63 	 * @param name
       
    64 	 *            the name of the project or empty if this file does not use the
       
    65 	 *            project name
       
    66 	 */
       
    67 	protected TraceProjectFile(String path, String name) {
       
    68 		this.path = path;
       
    69 		this.name = name;
       
    70 		if (name == null || name.length() == 0) {
       
    71 			this.hasModelName = false;
       
    72 		} else {
       
    73 			this.hasModelName = true;
       
    74 		}
       
    75 	}
       
    76 
       
    77 	/**
       
    78 	 * Constructor
       
    79 	 * 
       
    80 	 * @param absolutePath
       
    81 	 *            the absolute path to the file
       
    82 	 * @param hasModelName
       
    83 	 *            flag, which tells to update the project file name if model
       
    84 	 *            name changes
       
    85 	 */
       
    86 	protected TraceProjectFile(String absolutePath, boolean hasModelName) {
       
    87 		this.hasModelName = hasModelName;
       
    88 		updatePath(absolutePath);
       
    89 	}
       
    90 
       
    91 	/**
       
    92 	 * Gets the file extension of this project file
       
    93 	 * 
       
    94 	 * @return the extension
       
    95 	 */
       
    96 	protected abstract String getFileExtension();
       
    97 
       
    98 	/**
       
    99 	 * Gets the title to be shown in UI
       
   100 	 * 
       
   101 	 * @return the title
       
   102 	 */
       
   103 	public abstract String getTitle();
       
   104 
       
   105 	/**
       
   106 	 * Gets the name of the project
       
   107 	 * 
       
   108 	 * @return the project name
       
   109 	 */
       
   110 	public final String getProjectName() {
       
   111 		return name;
       
   112 	}
       
   113 
       
   114 	/**
       
   115 	 * Gets the path of this project file
       
   116 	 * 
       
   117 	 * @return the project file path
       
   118 	 */
       
   119 	public final String getPath() {
       
   120 		return path;
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Gets the name of this project file
       
   125 	 * 
       
   126 	 * @return the file name
       
   127 	 */
       
   128 	public final String getFileName() {
       
   129 		StringBuffer sb = new StringBuffer();
       
   130 		addFileName(sb, false);
       
   131 		return sb.toString();
       
   132 	}
       
   133 
       
   134 	/**
       
   135 	 * Checks if this file is valid
       
   136 	 * 
       
   137 	 * @return true if valid, false if not
       
   138 	 */
       
   139 	public boolean isValid() {
       
   140 		return path != null && name != null;
       
   141 	}
       
   142 
       
   143 	/**
       
   144 	 * Posts a project file written event
       
   145 	 * 
       
   146 	 * @param path
       
   147 	 *            the path where file was written
       
   148 	 */
       
   149 	public void postFileWrittenEvent(String path) {
       
   150 		String msg = Messages
       
   151 				.getString("TraceProjectFile.ProjectFileWrittenMiddle"); //$NON-NLS-1$
       
   152 		TraceCompilerEngineGlobals.getEvents().postInfoMessage(
       
   153 				getTitle() + msg + path, null);
       
   154 	}
       
   155 
       
   156 	/**
       
   157 	 * Updates the project file name and path
       
   158 	 * 
       
   159 	 * @param absolutePath
       
   160 	 *            the new path including the file name
       
   161 	 */
       
   162 	public void updatePath(String absolutePath) {
       
   163 		File file = new File(absolutePath);
       
   164 		path = file.getParent();
       
   165 		name = file.getName();
       
   166 		String ext = getFileExtension();
       
   167 		if (name.endsWith(ext)) {
       
   168 			name = name.substring(0, name.length() - ext.length());
       
   169 		}
       
   170 	}
       
   171 
       
   172 	/**
       
   173 	 * Gets the path including file name
       
   174 	 * 
       
   175 	 * @return the path
       
   176 	 */
       
   177 	public final String getAbsolutePath() {
       
   178 		String retval;
       
   179 		if (isValid()) {
       
   180 			StringBuffer sb = new StringBuffer();
       
   181 			sb.append(FileUtils.convertSeparators(
       
   182 					SourceConstants.FORWARD_SLASH_CHAR, path, true));
       
   183 			addFileName(sb, false);
       
   184 			retval = sb.toString();
       
   185 		} else {
       
   186 			retval = null;
       
   187 		}
       
   188 		return retval;
       
   189 	}
       
   190 
       
   191 	/**
       
   192 	 * Gets the absolute path to this file, including the model ID in file name
       
   193 	 * 
       
   194 	 * @return the path
       
   195 	 */
       
   196 	public final String getAbsolutePathWithID() {
       
   197 		String retval;
       
   198 		if (isValid()) {
       
   199 			StringBuffer sb = new StringBuffer();
       
   200 			sb.append(FileUtils.convertSeparators(
       
   201 					SourceConstants.FORWARD_SLASH_CHAR, path, true));
       
   202 			addFileName(sb, true);
       
   203 			retval = sb.toString();
       
   204 		} else {
       
   205 			retval = null;
       
   206 		}
       
   207 		return retval;
       
   208 	}
       
   209 
       
   210 	/**
       
   211 	 * Adds the file name to the given buffer
       
   212 	 * 
       
   213 	 * @param sb
       
   214 	 *            the buffer
       
   215 	 * @param addID
       
   216 	 *            true if ID needs to be added to name
       
   217 	 */
       
   218 	private void addFileName(StringBuffer sb, boolean addID) {
       
   219 		sb.append(name);
       
   220 		if (addID) {
       
   221 			sb.append("_0x"); //$NON-NLS-1$
       
   222 			sb.append(Integer.toHexString(getOwner().getModel().getID()));
       
   223 			sb.append("_"); //$NON-NLS-1$
       
   224 		}
       
   225 		sb.append(getFileExtension());
       
   226 	}
       
   227 
       
   228 	/*
       
   229 	 * (non-Javadoc)
       
   230 	 * 
       
   231 	 * @see com.nokia.tracecompiler.model.TraceModelExtension#getOwner()
       
   232 	 */
       
   233 	public TraceObject getOwner() {
       
   234 		return owner;
       
   235 	}
       
   236 
       
   237 	/*
       
   238 	 * (non-Javadoc)
       
   239 	 * 
       
   240 	 * @see com.nokia.tracecompiler.model.TraceModelExtension#
       
   241 	 *      setOwner(com.nokia.tracecompiler.model.TraceObject)
       
   242 	 */
       
   243 	public void setOwner(TraceObject owner) {
       
   244 		if (this.owner != null) {
       
   245 			this.owner.removeModelListener(this);
       
   246 		}
       
   247 		if (owner instanceof TraceModel) {
       
   248 			this.owner = (TraceModel) owner;
       
   249 			this.owner.addModelListener(this);
       
   250 		}
       
   251 	}
       
   252 
       
   253 	/*
       
   254 	 * (non-Javadoc)
       
   255 	 * 
       
   256 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
   257 	 *      objectAdded(com.nokia.tracecompiler.model.TraceObject,
       
   258 	 *      com.nokia.tracecompiler.model.TraceObject)
       
   259 	 */
       
   260 	public void objectAdded(TraceObject owner, TraceObject object) {
       
   261 	}
       
   262 
       
   263 	/*
       
   264 	 * (non-Javadoc)
       
   265 	 * 
       
   266 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
   267 	 *      objectCreationComplete(com.nokia.tracecompiler.model.TraceObject)
       
   268 	 */
       
   269 	public void objectCreationComplete(TraceObject object) {
       
   270 	}
       
   271 
       
   272 	/*
       
   273 	 * (non-Javadoc)
       
   274 	 * 
       
   275 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
   276 	 *      objectRemoved(com.nokia.tracecompiler.model.TraceObject,
       
   277 	 *      com.nokia.tracecompiler.model.TraceObject)
       
   278 	 */
       
   279 	public void objectRemoved(TraceObject owner, TraceObject object) {
       
   280 	}
       
   281 
       
   282 	/*
       
   283 	 * (non-Javadoc)
       
   284 	 * 
       
   285 	 * @see com.nokia.tracecompiler.model.TraceModelListener#
       
   286 	 *      propertyUpdated(com.nokia.tracecompiler.model.TraceObject, int)
       
   287 	 */
       
   288 	public void propertyUpdated(TraceObject object, int property) {
       
   289 	}
       
   290 
       
   291 }