1 11 package org.eclipse.jdt.internal.ui.model; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.Path; 19 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IWorkspaceRoot; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.resources.mapping.IModelProviderDescriptor; 24 import org.eclipse.core.resources.mapping.ModelProvider; 25 import org.eclipse.core.resources.mapping.ResourceMapping; 26 27 import org.eclipse.ui.IMemento; 28 import org.eclipse.ui.IWorkingSet; 29 import org.eclipse.ui.PlatformUI; 30 31 import org.eclipse.compare.structuremergeviewer.ICompareInput; 32 33 import org.eclipse.team.core.mapping.ISynchronizationContext; 34 35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 36 import org.eclipse.ltk.ui.refactoring.model.AbstractSynchronizationCompareAdapter; 37 38 import org.eclipse.jdt.core.IJavaElement; 39 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 42 47 public final class JavaSynchronizationCompareAdapter extends AbstractSynchronizationCompareAdapter { 48 49 50 private static final String MODEL_PROVIDER_ID= "modelProviderId"; 52 53 private static final String MODEL_PROVIDERS= "modelProviders"; 55 56 private static final String RESOURCE_PATH= "resourcePath"; 58 59 private static final String RESOURCE_TYPE= "resourceType"; 61 62 private static final String RESOURCES= "resources"; 64 65 private static final String WORKING_SET_NAME= "workingSetName"; 67 68 private static final String WORKING_SETS= "workingSets"; 70 73 public ICompareInput asCompareInput(final ISynchronizationContext context, final Object element) { 74 if (element instanceof RefactoringDescriptorProxy) 75 return super.asCompareInput(context, element); 76 final IResource resource= JavaModelProvider.getResource(element); 77 if (resource != null) 78 return super.asCompareInput(context, resource); 79 return null; 80 } 81 82 85 public ResourceMapping[] restore(final IMemento memento) { 86 IMemento[] children= memento.getChildren(RESOURCES); 87 final List result= new ArrayList (); 88 for (int index= 0; index < children.length; index++) { 89 final Integer typeInt= children[index].getInteger(RESOURCE_TYPE); 90 if (typeInt == null) 91 continue; 92 final String pathString= children[index].getString(RESOURCE_PATH); 93 if (pathString == null) 94 continue; 95 IResource resource= null; 96 final IPath path= new Path(pathString); 97 final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 98 switch (typeInt.intValue()) { 99 case IResource.ROOT: 100 resource= root; 101 break; 102 case IResource.PROJECT: 103 resource= root.getProject(path.lastSegment()); 104 break; 105 case IResource.FILE: 106 resource= root.getFile(path); 107 break; 108 case IResource.FOLDER: 109 resource= root.getFolder(path); 110 break; 111 } 112 if (resource != null) { 113 final ResourceMapping mapping= JavaSynchronizationContentProvider.getResourceMapping(resource); 114 if (mapping != null) 115 result.add(mapping); 116 } 117 } 118 children= memento.getChildren(WORKING_SETS); 119 for (int index= 0; index < children.length; index++) { 120 final String name= children[index].getString(WORKING_SET_NAME); 121 if (name == null) 122 continue; 123 final IWorkingSet set= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name); 124 if (set != null) { 125 final ResourceMapping mapping= JavaSynchronizationContentProvider.getResourceMapping(set); 126 if (mapping != null) 127 result.add(mapping); 128 } 129 } 130 children= memento.getChildren(MODEL_PROVIDERS); 131 for (int index= 0; index < children.length; index++) { 132 final String id= children[index].getString(MODEL_PROVIDER_ID); 133 if (id == null) 134 continue; 135 final IModelProviderDescriptor descriptor= ModelProvider.getModelProviderDescriptor(id); 136 if (descriptor == null) 137 continue; 138 try { 139 final ModelProvider provider= descriptor.getModelProvider(); 140 if (provider != null) { 141 final ResourceMapping mapping= JavaSynchronizationContentProvider.getResourceMapping(provider); 142 if (mapping != null) 143 result.add(mapping); 144 } 145 } catch (CoreException event) { 146 JavaPlugin.log(event); 147 } 148 } 149 return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]); 150 } 151 152 155 public void save(final ResourceMapping[] mappings, final IMemento memento) { 156 for (int index= 0; index < mappings.length; index++) { 157 final Object object= mappings[index].getModelObject(); 158 if (object instanceof IJavaElement) { 159 final IJavaElement element= (IJavaElement) object; 160 final IResource resource= (IResource) element.getAdapter(IResource.class); 161 if (resource != null) { 162 final IMemento child= memento.createChild(RESOURCES); 163 child.putInteger(RESOURCE_TYPE, resource.getType()); 164 child.putString(RESOURCE_PATH, resource.getFullPath().toString()); 165 } 166 } 167 if (object instanceof IResource) { 168 final IResource resource= (IResource) object; 169 final IMemento child= memento.createChild(RESOURCES); 170 child.putInteger(RESOURCE_TYPE, resource.getType()); 171 child.putString(RESOURCE_PATH, resource.getFullPath().toString()); 172 } else if (object instanceof IWorkingSet) 173 memento.createChild(WORKING_SETS).putString(WORKING_SET_NAME, ((IWorkingSet) object).getName()); 174 else if (object instanceof ModelProvider) 175 memento.createChild(MODEL_PROVIDERS).putString(MODEL_PROVIDER_ID, ((ModelProvider) object).getId()); 176 } 177 } 178 } 179 | Popular Tags |