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
9 * Anton Leherbauer (Wind River Systems) - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.util;
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;
22 import org.eclipse.cdt.core.model.CoreModel;
23 import org.eclipse.cdt.core.model.ICProject;
24 import org.eclipse.cdt.core.model.ITranslationUnit;
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.
32 * @see IPersistableElement
33 * @see IElementFactory
37 public class ExternalEditorInputFactory implements IElementFactory {
39 public static final String ID = "org.eclipse.cdt.ui.ExternalEditorInputFactory"; //$NON-NLS-1$
41 private static final String TAG_PATH = "path";//$NON-NLS-1$
42 private static final String TAG_PROJECT = "project";//$NON-NLS-1$
45 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
47 public IAdaptable createElement(IMemento memento) {
49 String fileName = memento.getString(TAG_PATH);
50 if (fileName == null) {
54 IPath location= new Path(fileName);
55 ICProject cProject= null;
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);
64 return EditorUtility.getEditorInputForLocation(location, cProject);
68 * Save the element state.
70 * @param memento the storage
71 * @param input the element
73 static void saveState(IMemento memento, ExternalEditorInput input) {
74 IPath location= input.getPath();
75 if (location != null) {
76 memento.putString(TAG_PATH, location.toOSString());
78 IProject project= null;
79 ITranslationUnit unit= input.getTranslationUnit();
81 project= unit.getCProject().getProject();
83 if (project == null && input.getMarkerResource() instanceof IProject) {
84 project= (IProject)input.getMarkerResource();
86 if (project != null) {
87 memento.putString(TAG_PROJECT, project.getName());