project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/cpp/epoc/engine/model/StandaloneModelProvider.java
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2006-2009 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 the License "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 */
       
    17 
       
    18 package com.nokia.carbide.internal.cpp.epoc.engine.model;
       
    19 
       
    20 import com.nokia.carbide.cpp.epoc.engine.EpocEnginePlugin;
       
    21 import com.nokia.carbide.cpp.epoc.engine.model.IModelFactory;
       
    22 import com.nokia.cpp.internal.api.utils.core.*;
       
    23 
       
    24 import org.eclipse.core.runtime.*;
       
    25 
       
    26 import java.io.File;
       
    27 import java.io.IOException;
       
    28 import java.util.HashMap;
       
    29 import java.util.Map;
       
    30 
       
    31 /**
       
    32  * This is the model provider implementation that works on java.io.File.
       
    33  * <p>
       
    34  * All paths are full paths.
       
    35  * <p>
       
    36  * It does not track changes to files.  
       
    37  * <p>
       
    38  * Do not use it when talking to workspace resources, or else the workspace
       
    39  * will become out of sync.
       
    40  *
       
    41  */
       
    42 public class StandaloneModelProvider<Model, SharedModel> extends ModelProviderBase {
       
    43 
       
    44 	private Map<IPath, Long> externalFileModDateMap;
       
    45 
       
    46 	public StandaloneModelProvider(IModelFactory modelFactory) {
       
    47 		super(modelFactory);
       
    48 		this.externalFileModDateMap = new HashMap<IPath, Long>();
       
    49 	}
       
    50 
       
    51 	/* (non-Javadoc)
       
    52 	 * @see com.nokia.carbide.internal.cpp.epoc.engine.model.ModelProviderBase#getCanonicalPath(org.eclipse.core.runtime.IPath)
       
    53 	 */
       
    54 	@Override
       
    55 	protected IPath getFullPath(IPath path) {
       
    56 		File file = path.toFile();
       
    57 		try {
       
    58 			file = file.getCanonicalFile();
       
    59 		} catch (IOException e) {
       
    60 			EpocEnginePlugin.log(e);
       
    61 		}
       
    62 		return new Path(file.getAbsolutePath());
       
    63 	}
       
    64 
       
    65 	/* (non-Javadoc)
       
    66 	 * @see com.nokia.carbide.internal.cpp.epoc.engine.model.ModelProviderBase#loadDocument(org.eclipse.core.runtime.IPath)
       
    67 	 */
       
    68 	@Override
       
    69 	protected String loadStorage(IPath path) throws CoreException {
       
    70 		File file = path.toFile();
       
    71 		if (file != null && file.exists()) {
       
    72 			externalFileModDateMap.put(path, file.lastModified());
       
    73 			char[] text = FileUtils.readFileContents(file, null);
       
    74 			return new String(text);
       
    75 		} else {
       
    76 			return null;
       
    77 		}
       
    78 	}
       
    79 
       
    80 	/* (non-Javadoc)
       
    81 	 * @see com.nokia.carbide.internal.cpp.epoc.engine.model.ModelProviderBase#saveDocument(org.eclipse.core.runtime.IPath, org.eclipse.jface.text.IDocument)
       
    82 	 */
       
    83 	@Override
       
    84 	protected void saveStorage(IPath path, String text)
       
    85 			throws CoreException {
       
    86 		File file = path.toFile();
       
    87 		if (file != null) {
       
    88 			char[] chars = text.toCharArray();
       
    89 			FileUtils.writeFileContents(file, chars, null);
       
    90 			externalFileModDateMap.put(path, file.lastModified());
       
    91 		}
       
    92 	}
       
    93 	
       
    94 	/* (non-Javadoc)
       
    95 	 * @see com.nokia.carbide.internal.cpp.epoc.engine.model.ModelProviderBase#startTrackingStorage(org.eclipse.core.runtime.IPath)
       
    96 	 */
       
    97 	protected void startTrackingStorage(IPath path) {
       
    98 	}
       
    99 
       
   100 	/* (non-Javadoc)
       
   101 	 * @see com.nokia.carbide.internal.cpp.epoc.engine.model.ModelProviderBase#stopTrackingStorage(org.eclipse.core.runtime.IPath)
       
   102 	 */
       
   103 	protected void stopTrackingStorage(IPath path) {
       
   104 	}
       
   105 	
       
   106 	@Override
       
   107 	protected boolean hasTrackedStorageChanged(IPath fullPath) {
       
   108 		Long modDate = externalFileModDateMap.get(fullPath);
       
   109 		if (modDate != null && modDate.longValue() != fullPath.toFile().lastModified())
       
   110 			return true;
       
   111 		return false;
       
   112 	}
       
   113 }