KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceModelPersistenceAdapter


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.mapping;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 JavaDoc RESOURCES = "resources"; //$NON-NLS-1$
28
private static final String JavaDoc RESOURCE_PATH = "resourcePath"; //$NON-NLS-1$
29
private static final String JavaDoc RESOURCE_TYPE = "resourceType"; //$NON-NLS-1$
30
private static final String JavaDoc WORKING_SETS = "workingSets"; //$NON-NLS-1$
31
private static final String JavaDoc WORKING_SET_NAME = "workingSetName"; //$NON-NLS-1$
32
private static final String JavaDoc MODEL_PROVIDERS = "modelProviders"; //$NON-NLS-1$
33
private static final String JavaDoc MODEL_PROVIDER_ID = "modelProviderId"; //$NON-NLS-1$
34

35     public ResourceModelPersistenceAdapter() {
36     }
37
38     /* (non-Javadoc)
39      * @see org.eclipse.team.ui.mapping.SynchronizationCompareAdapter#save(org.eclipse.core.resources.mapping.ResourceMapping[], org.eclipse.ui.IMemento)
40      */

41     public void save(ResourceMapping[] mappings, IMemento memento) {
42         for (int i = 0; i < mappings.length; i++) {
43             ResourceMapping mapping = mappings[i];
44             Object JavaDoc 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     /* (non-Javadoc)
63      * @see org.eclipse.team.ui.mapping.SynchronizationCompareAdapter#restore(org.eclipse.ui.IMemento)
64      */

65     public ResourceMapping[] restore(IMemento memento) {
66         IMemento[] children = memento.getChildren(RESOURCES);
67         List JavaDoc result = new ArrayList JavaDoc();
68         for (int i = 0; i < children.length; i++) {
69             IMemento child = children[i];
70             Integer JavaDoc typeInt = child.getInteger(RESOURCE_TYPE);
71             if (typeInt == null)
72                 continue;
73             int type = typeInt.intValue();
74             String JavaDoc 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 JavaDoc 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 JavaDoc 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