001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.model;
011:
012: import org.eclipse.core.resources.IResource;
013: import org.eclipse.core.resources.IWorkspaceRoot;
014: import org.eclipse.core.resources.ResourcesPlugin;
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.Path;
017: import org.eclipse.ui.IElementFactory;
018: import org.eclipse.ui.IMemento;
019: import org.eclipse.ui.IPersistableElement;
020:
021: /**
022: * The ResourceFactory is used to save and recreate an IResource object.
023: * As such, it implements the IPersistableElement interface for storage
024: * and the IElementFactory interface for recreation.
025: *
026: * @see IMemento
027: * @see IPersistableElement
028: * @see IElementFactory
029: */
030: public class ResourceFactory implements IElementFactory,
031: IPersistableElement {
032:
033: // These persistence constants are stored in XML. Do not
034: // change them.
035: private static final String TAG_PATH = "path";//$NON-NLS-1$
036:
037: private static final String TAG_TYPE = "type";//$NON-NLS-1$
038:
039: private static final String FACTORY_ID = "org.eclipse.ui.internal.model.ResourceFactory";//$NON-NLS-1$
040:
041: // IPersistable data.
042: private IResource res;
043:
044: /**
045: * Create a ResourceFactory. This constructor is typically used
046: * for our IElementFactory side.
047: */
048: public ResourceFactory() {
049: }
050:
051: /**
052: * Create a ResourceFactory. This constructor is typically used
053: * for our IPersistableElement side.
054: */
055: public ResourceFactory(IResource input) {
056: res = input;
057: }
058:
059: /**
060: * @see IElementFactory
061: */
062: public IAdaptable createElement(IMemento memento) {
063: // Get the file name.
064: String fileName = memento.getString(TAG_PATH);
065: if (fileName == null) {
066: return null;
067: }
068:
069: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
070: String type = memento.getString(TAG_TYPE);
071: if (type == null) {
072: // Old format memento. Create an IResource using findMember.
073: // Will return null for resources in closed projects.
074: res = root.findMember(new Path(fileName));
075: } else {
076: int resourceType = Integer.parseInt(type);
077:
078: if (resourceType == IResource.ROOT) {
079: res = root;
080: } else if (resourceType == IResource.PROJECT) {
081: res = root.getProject(fileName);
082: } else if (resourceType == IResource.FOLDER) {
083: res = root.getFolder(new Path(fileName));
084: } else if (resourceType == IResource.FILE) {
085: res = root.getFile(new Path(fileName));
086: }
087: }
088: return res;
089: }
090:
091: /**
092: * @see IPersistableElement
093: */
094: public String getFactoryId() {
095: return FACTORY_ID;
096: }
097:
098: /**
099: * @see IPersistableElement
100: */
101: public void saveState(IMemento memento) {
102: memento.putString(TAG_PATH, res.getFullPath().toString());
103: memento.putString(TAG_TYPE, Integer.toString(res.getType()));
104: }
105: }
|