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