1 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 31 public class ResourceFactory implements IElementFactory, IPersistableElement { 32 33 private static final String TAG_PATH = "path"; 37 private static final String TAG_TYPE = "type"; 39 private static final String FACTORY_ID = "org.eclipse.ui.internal.model.ResourceFactory"; 41 private IResource res; 43 44 48 public ResourceFactory() { 49 } 50 51 55 public ResourceFactory(IResource input) { 56 res = input; 57 } 58 59 62 public IAdaptable createElement(IMemento memento) { 63 String fileName = memento.getString(TAG_PATH); 65 if (fileName == null) { 66 return null; 67 } 68 69 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 70 String type = memento.getString(TAG_TYPE); 71 if (type == null) { 72 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 94 public String getFactoryId() { 95 return FACTORY_ID; 96 } 97 98 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 |