KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > ResourceScope


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.synchronize;
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.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.team.internal.ui.Utils;
21 import org.eclipse.ui.IMemento;
22
23 /**
24  * A synchronize scope whose roots are a set of resources.
25  * <p>
26  * Clients are not expected to subclass this class.
27  * </p>
28  * @since 3.0
29  */

30 public class ResourceScope extends AbstractSynchronizeScope {
31     
32     /*
33      * Constants used to save and restore this scope
34      */

35     private final static String JavaDoc CTX_ROOT = "resource_scope_roots"; //$NON-NLS-1$
36
private final static String JavaDoc CTX_ROOT_PATH = "resource_scope_root_resource"; //$NON-NLS-1$
37

38     /*
39      * The resources that define this scope
40      */

41     private IResource[] resources;
42     
43     /**
44      * Create the resource scope for the given resources
45      *
46      * @param resources the resources that define this scope
47      */

48     public ResourceScope(IResource[] resources) {
49         this.resources = resources;
50     }
51     
52     /**
53      * Create this scope from it's previously saved state
54      *
55      * @param memento persisted state that can be restored
56      */

57     protected ResourceScope(IMemento memento) {
58         super(memento);
59     }
60     
61     /**
62      * Set the resources that define this scope
63      *
64      * @param resources the resources that define this scope
65      */

66     public void setResources(IResource[] resources) {
67         this.resources = resources;
68         fireRootsChanges();
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#getName()
73      */

74     public String JavaDoc getName() {
75         return Utils.convertSelection(resources);
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#getRoots()
80      */

81     public IResource[] getRoots() {
82         return resources;
83     }
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#dispose()
87      */

88     public void dispose() {
89         // Nothing to dispose
90
}
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#saveState(org.eclipse.ui.IMemento)
94      */

95     public void saveState(IMemento memento) {
96         if (resources != null) {
97             for (int i = 0; i < resources.length; i++) {
98                 IResource resource = resources[i];
99                 IMemento rootNode = memento.createChild(CTX_ROOT);
100                 rootNode.putString(CTX_ROOT_PATH, resource.getFullPath().toString());
101             }
102         }
103     }
104     
105     /* (non-Javadoc)
106      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeScope#init(org.eclipse.ui.IMemento)
107      */

108     protected void init(IMemento memento) {
109         IMemento[] rootNodes = memento.getChildren(CTX_ROOT);
110         if(rootNodes != null) {
111             List JavaDoc resources = new ArrayList JavaDoc();
112             for (int i = 0; i < rootNodes.length; i++) {
113                 IMemento rootNode = rootNodes[i];
114                 IPath path = new Path(rootNode.getString(CTX_ROOT_PATH));
115                 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path, true /* include phantoms */);
116                 if(resource != null) {
117                     resources.add(resource);
118                 }
119             }
120             this.resources = (IResource[]) resources.toArray(new IResource[resources.size()]);
121         }
122     }
123 }
124
Popular Tags