1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.resources.mapping.*; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.team.internal.ui.TeamUIPlugin; 21 import org.eclipse.team.internal.ui.Utils; 22 import org.eclipse.team.ui.mapping.SynchronizationCompareAdapter; 23 import org.eclipse.ui.*; 24 25 public class ResourceModelPersistenceAdapter extends SynchronizationCompareAdapter { 26 27 private static final String RESOURCES = "resources"; private static final String RESOURCE_PATH = "resourcePath"; private static final String RESOURCE_TYPE = "resourceType"; private static final String WORKING_SETS = "workingSets"; private static final String WORKING_SET_NAME = "workingSetName"; private static final String MODEL_PROVIDERS = "modelProviders"; private static final String MODEL_PROVIDER_ID = "modelProviderId"; 35 public ResourceModelPersistenceAdapter() { 36 } 37 38 41 public void save(ResourceMapping[] mappings, IMemento memento) { 42 for (int i = 0; i < mappings.length; i++) { 43 ResourceMapping mapping = mappings[i]; 44 Object object = mapping.getModelObject(); 45 if (object instanceof IResource) { 46 IResource resource = (IResource) object; 47 IMemento child = memento.createChild(RESOURCES); 48 child.putInteger(RESOURCE_TYPE, resource.getType()); 49 child.putString(RESOURCE_PATH, resource.getFullPath().toString()); 50 } else if (object instanceof IWorkingSet) { 51 IWorkingSet ws = (IWorkingSet) object; 52 IMemento child = memento.createChild(WORKING_SETS); 53 child.putString(WORKING_SET_NAME, ws.getName()); 54 } else if (object instanceof ModelProvider) { 55 ModelProvider provider = (ModelProvider) object; 56 IMemento child = memento.createChild(MODEL_PROVIDERS); 57 child.putString(MODEL_PROVIDER_ID, provider.getId()); 58 } 59 } 60 } 61 62 65 public ResourceMapping[] restore(IMemento memento) { 66 IMemento[] children = memento.getChildren(RESOURCES); 67 List result = new ArrayList (); 68 for (int i = 0; i < children.length; i++) { 69 IMemento child = children[i]; 70 Integer typeInt = child.getInteger(RESOURCE_TYPE); 71 if (typeInt == null) 72 continue; 73 int type = typeInt.intValue(); 74 String pathString = child.getString(RESOURCE_PATH); 75 if (pathString == null) 76 continue; 77 IPath path = new Path(pathString); 78 IResource resource; 79 switch (type) { 80 case IResource.ROOT: 81 resource = ResourcesPlugin.getWorkspace().getRoot(); 82 break; 83 case IResource.PROJECT: 84 resource = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment()); 85 break; 86 case IResource.FILE: 87 resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 88 break; 89 case IResource.FOLDER: 90 resource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path); 91 break; 92 default: 93 resource = null; 94 break; 95 } 96 if (resource != null) { 97 ResourceMapping mapping = Utils.getResourceMapping(resource); 98 if (mapping != null) { 99 result.add(mapping); 100 } 101 } 102 } 103 children = memento.getChildren(WORKING_SETS); 104 for (int i = 0; i < children.length; i++) { 105 IMemento child = children[i]; 106 String name = child.getString(WORKING_SET_NAME); 107 if (name == null) 108 continue; 109 IWorkingSet set = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name); 110 if (set != null) { 111 ResourceMapping mapping = Utils.getResourceMapping(set); 112 if (mapping != null) 113 result.add(mapping); 114 } 115 } 116 children = memento.getChildren(MODEL_PROVIDERS); 117 for (int i = 0; i < children.length; i++) { 118 IMemento child = children[i]; 119 String id = child.getString(MODEL_PROVIDER_ID); 120 if (id == null) 121 continue; 122 IModelProviderDescriptor desc = ModelProvider.getModelProviderDescriptor(id); 123 if (desc == null) 124 continue; 125 try { 126 ModelProvider provider = desc.getModelProvider(); 127 if (provider != null) { 128 ResourceMapping mapping = Utils.getResourceMapping(provider); 129 if (mapping != null) 130 result.add(mapping); 131 } 132 } catch (CoreException e) { 133 TeamUIPlugin.log(e); 134 } 135 } 136 return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]); 137 } 138 139 } 140 | Popular Tags |