upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / util / ExternalEditorInputFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     Anton Leherbauer (Wind River Systems) - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.util;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.ui.IElementFactory;
19 import org.eclipse.ui.IMemento;
20 import org.eclipse.ui.IPersistableElement;
21
22 import org.eclipse.cdt.core.model.CoreModel;
23 import org.eclipse.cdt.core.model.ICProject;
24 import org.eclipse.cdt.core.model.ITranslationUnit;
25
26 /**
27  * The ExternalEditorInputFactory is used to save and recreate an ExternalEditorInput object.
28  * As such, it implements the IPersistableElement interface for storage
29  * and the IElementFactory interface for recreation.
30  *
31  * @see IMemento
32  * @see IPersistableElement
33  * @see IElementFactory
34  *
35  * @since 4.0
36  */
37 public class ExternalEditorInputFactory implements IElementFactory {
38
39         public static final String ID = "org.eclipse.cdt.ui.ExternalEditorInputFactory"; //$NON-NLS-1$
40
41     private static final String TAG_PATH = "path";//$NON-NLS-1$
42     private static final String TAG_PROJECT = "project";//$NON-NLS-1$
43
44         /*
45          * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
46          */
47         public IAdaptable createElement(IMemento memento) {
48         // Get the file name.
49         String fileName = memento.getString(TAG_PATH);
50         if (fileName == null) {
51                         return null;
52                 }
53
54         IPath location= new Path(fileName);
55         ICProject cProject= null;
56         
57         String projectName= memento.getString(TAG_PROJECT);
58         if (projectName != null) {
59                 IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
60                 if (project.isAccessible() && CoreModel.hasCNature(project)) {
61                         cProject= CoreModel.getDefault().create(project);
62                 }
63         }
64                 return EditorUtility.getEditorInputForLocation(location, cProject);
65         }
66
67         /**
68          * Save the element state.
69          * 
70          * @param memento  the storage
71          * @param input  the element
72          */
73         static void saveState(IMemento memento, ExternalEditorInput input) {
74                 IPath location= input.getPath();
75                 if (location != null) {
76                         memento.putString(TAG_PATH, location.toOSString());
77                 }
78                 IProject project= null;
79                 ITranslationUnit unit= input.getTranslationUnit();
80                 if (unit != null) {
81                         project= unit.getCProject().getProject();
82                 }
83                 if (project == null && input.getMarkerResource() instanceof IProject) {
84                         project= (IProject)input.getMarkerResource();
85                 }
86                 if (project != null) {
87                         memento.putString(TAG_PROJECT, project.getName());
88                 }
89         }
90
91 }