KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > model > JavaSynchronizationCompareAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.jdt.internal.ui.model;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 /**
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 JavaDoc MODEL_PROVIDER_ID= "modelProviderId"; //$NON-NLS-1$
51

52     /** The modelProviders name */
53     private static final String JavaDoc MODEL_PROVIDERS= "modelProviders"; //$NON-NLS-1$
54

55     /** The resourcePath name */
56     private static final String JavaDoc RESOURCE_PATH= "resourcePath"; //$NON-NLS-1$
57

58     /** The resourceType name */
59     private static final String JavaDoc RESOURCE_TYPE= "resourceType"; //$NON-NLS-1$
60

61     /** The resources name */
62     private static final String JavaDoc RESOURCES= "resources"; //$NON-NLS-1$
63

64     /** The workingSetName name */
65     private static final String JavaDoc WORKING_SET_NAME= "workingSetName"; //$NON-NLS-1$
66

67     /** The workingSets name */
68     private static final String JavaDoc WORKING_SETS= "workingSets"; //$NON-NLS-1$
69

70     /**
71      * {@inheritDoc}
72      */

73     public ICompareInput asCompareInput(final ISynchronizationContext context, final Object JavaDoc 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     /**
83      * {@inheritDoc}
84      */

85     public ResourceMapping[] restore(final IMemento memento) {
86         IMemento[] children= memento.getChildren(RESOURCES);
87         final List JavaDoc result= new ArrayList JavaDoc();
88         for (int index= 0; index < children.length; index++) {
89             final Integer JavaDoc typeInt= children[index].getInteger(RESOURCE_TYPE);
90             if (typeInt == null)
91                 continue;
92             final String JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
153      * {@inheritDoc}
154      */

155     public void save(final ResourceMapping[] mappings, final IMemento memento) {
156         for (int index= 0; index < mappings.length; index++) {
157             final Object JavaDoc 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