KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > model > ResourceFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.model;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IWorkspaceRoot;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.IAdaptable;
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 /**
23  * The ResourceFactory is used to save and recreate an IResource object.
24  * As such, it implements the IPersistableElement interface for storage
25  * and the IElementFactory interface for recreation.
26  *
27  * @see IMemento
28  * @see IPersistableElement
29  * @see IElementFactory
30  */

31 public class ResourceFactory implements IElementFactory, IPersistableElement {
32
33     // These persistence constants are stored in XML. Do not
34
// change them.
35
private static final String JavaDoc TAG_PATH = "path";//$NON-NLS-1$
36

37     private static final String JavaDoc TAG_TYPE = "type";//$NON-NLS-1$
38

39     private static final String JavaDoc FACTORY_ID = "org.eclipse.ui.internal.model.ResourceFactory";//$NON-NLS-1$
40

41     // IPersistable data.
42
private IResource res;
43
44     /**
45      * Create a ResourceFactory. This constructor is typically used
46      * for our IElementFactory side.
47      */

48     public ResourceFactory() {
49     }
50
51     /**
52      * Create a ResourceFactory. This constructor is typically used
53      * for our IPersistableElement side.
54      */

55     public ResourceFactory(IResource input) {
56         res = input;
57     }
58
59     /**
60      * @see IElementFactory
61      */

62     public IAdaptable createElement(IMemento memento) {
63         // Get the file name.
64
String JavaDoc fileName = memento.getString(TAG_PATH);
65         if (fileName == null) {
66             return null;
67         }
68
69         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
70         String JavaDoc type = memento.getString(TAG_TYPE);
71         if (type == null) {
72             // Old format memento. Create an IResource using findMember.
73
// Will return null for resources in closed projects.
74
res = root.findMember(new Path(fileName));
75         } else {
76             int resourceType = Integer.parseInt(type);
77
78             if (resourceType == IResource.ROOT) {
79                 res = root;
80             } else if (resourceType == IResource.PROJECT) {
81                 res = root.getProject(fileName);
82             } else if (resourceType == IResource.FOLDER) {
83                 res = root.getFolder(new Path(fileName));
84             } else if (resourceType == IResource.FILE) {
85                 res = root.getFile(new Path(fileName));
86             }
87         }
88         return res;
89     }
90
91     /**
92      * @see IPersistableElement
93      */

94     public String JavaDoc getFactoryId() {
95         return FACTORY_ID;
96     }
97
98     /**
99      * @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 }
106
Popular Tags