org.symbian.tools.mtw.ui/src/org/symbian/tools/mtw/ui/ProjectMemo.java
changeset 461 7a8f9fa8d278
parent 460 c0bff5ed874c
child 462 cdc4995b1677
equal deleted inserted replaced
460:c0bff5ed874c 461:7a8f9fa8d278
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation 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  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.mtw.ui;
       
    20 
       
    21 import java.io.ByteArrayInputStream;
       
    22 import java.io.ByteArrayOutputStream;
       
    23 import java.io.IOException;
       
    24 import java.io.InputStream;
       
    25 import java.io.InputStreamReader;
       
    26 import java.io.OutputStreamWriter;
       
    27 import java.io.Writer;
       
    28 
       
    29 import org.eclipse.core.resources.IFile;
       
    30 import org.eclipse.core.runtime.CoreException;
       
    31 import org.eclipse.core.runtime.Path;
       
    32 import org.eclipse.ui.IMemento;
       
    33 import org.eclipse.ui.XMLMemento;
       
    34 import org.symbian.tools.mtw.core.projects.IMTWProject;
       
    35 import org.symbian.tools.mtw.ui.deployment.IDeploymentTarget;
       
    36 import org.symbian.tools.mtw.ui.deployment.IDeploymentTargetType;
       
    37 
       
    38 public class ProjectMemo {
       
    39     private static final String TARGET_TYPE = "typeId";
       
    40     private static final String TARGET = "targetId";
       
    41     private static final String TARGET_CONFIGURATION = "target";
       
    42     private static final String MEMO_TYPE = "deployment";
       
    43 
       
    44     private final IMTWProject project;
       
    45     private XMLMemento memento;
       
    46 
       
    47     public ProjectMemo(IMTWProject project) {
       
    48         this.project = project;
       
    49     }
       
    50 
       
    51     public synchronized void setDeploymentTarget(String providerId, IDeploymentTarget target) {
       
    52         try {
       
    53             checkMemento();
       
    54             memento.putString(TARGET_TYPE, providerId);
       
    55             memento.putString(TARGET, target.getId());
       
    56             IMemento child = null;
       
    57             IMemento[] children = memento.getChildren(TARGET_CONFIGURATION);
       
    58             for (IMemento memento : children) {
       
    59                 if (providerId.equals(memento.getString(TARGET_TYPE))
       
    60                         && target.getId().equals(memento.getString(TARGET))) {
       
    61                     child = memento;
       
    62                 }
       
    63             }
       
    64             if (child == null) {
       
    65                 child = memento.createChild(TARGET_CONFIGURATION);
       
    66                 child.putString(TARGET_TYPE, providerId);
       
    67                 child.putString(TARGET, target.getId());
       
    68             }
       
    69             target.save(child);
       
    70             saveMemento();
       
    71         } catch (CoreException e) {
       
    72             MTWCoreUI.log(e);
       
    73         } catch (IOException e) {
       
    74             MTWCoreUI.log(e);
       
    75         }
       
    76     }
       
    77 
       
    78     private void saveMemento() throws IOException, CoreException {
       
    79         ByteArrayOutputStream os = new ByteArrayOutputStream();
       
    80         final Writer writer = new OutputStreamWriter(os);
       
    81         memento.save(writer);
       
    82         writer.close();
       
    83         final InputStream stream = new ByteArrayInputStream(os.toByteArray());
       
    84         IFile file = getMemoFile();
       
    85         if (file.exists()) {
       
    86             file.setContents(stream, IFile.KEEP_HISTORY, null);
       
    87         } else {
       
    88             file.create(stream, false, null);
       
    89         }
       
    90     }
       
    91 
       
    92     private IFile getMemoFile() {
       
    93         return project.getProject().getFile(new Path(".settings").append(MTWCoreUI.PLUGIN_ID).addFileExtension("xml"));
       
    94     }
       
    95 
       
    96     private void checkMemento() throws CoreException, IOException {
       
    97         IFile memoFile = getMemoFile();
       
    98         if (memoFile.exists()) {
       
    99             memento = XMLMemento.createReadRoot(new InputStreamReader(memoFile.getContents(), memoFile.getCharset()));
       
   100         } else {
       
   101             memento = XMLMemento.createWriteRoot(MEMO_TYPE);
       
   102         }
       
   103     }
       
   104 
       
   105     public IDeploymentTarget getPreviousDeploymentTarget() {
       
   106         try {
       
   107             checkMemento();
       
   108             String type = memento.getString(TARGET_TYPE);
       
   109             if (type != null) {
       
   110                 final IDeploymentTargetType provider = MTWCoreUI.getDefault().getDeploymentTypesRegistry()
       
   111                         .getType(type);
       
   112                 if (provider != null) {
       
   113                     IDeploymentTarget target = provider.findTarget(project, memento.getString(TARGET));
       
   114                     if (target != null) {
       
   115                         return target;
       
   116                     }
       
   117                 }
       
   118             }
       
   119         } catch (CoreException e) {
       
   120             MTWCoreUI.log(e);
       
   121         } catch (IOException e) {
       
   122             MTWCoreUI.log(e);
       
   123         }
       
   124         return null;
       
   125     }
       
   126 
       
   127     public IMemento getMemo(String targetType, IDeploymentTarget target) {
       
   128         if (memento != null) {
       
   129             IMemento[] children = memento.getChildren(TARGET_CONFIGURATION);
       
   130             for (IMemento memento : children) {
       
   131                 if (targetType.equals(memento.getString(TARGET_TYPE))
       
   132                         && target.getId().equals(memento.getString(TARGET))) {
       
   133                     return memento;
       
   134                 }
       
   135             }
       
   136         }
       
   137         return null;
       
   138     }
       
   139 
       
   140 }