KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > ActiveChangeSet


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.internal.core.subscribers;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.team.core.diff.IDiff;
18 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
19 import org.eclipse.team.internal.core.TeamPlugin;
20 import org.osgi.service.prefs.Preferences;
21
22 /**
23  * An active change set represents a set of local resource changes
24  * that are grouped together as a single logical change.
25  * @since 3.1
26  */

27 public class ActiveChangeSet extends DiffChangeSet {
28     
29     private static final String JavaDoc CTX_TITLE = "title"; //$NON-NLS-1$
30
private static final String JavaDoc CTX_COMMENT = "comment"; //$NON-NLS-1$
31
private static final String JavaDoc CTX_RESOURCES = "resources"; //$NON-NLS-1$
32
private static final String JavaDoc CTX_USER_CREATED = "userCreated"; //$NON-NLS-1$
33

34     private final ActiveChangeSetManager manager;
35     private String JavaDoc comment;
36     private boolean userCreated = true;
37     
38     /**
39      * Create a change set with the given title
40      * @param manager the manager that owns this set
41      * @param title the title of the set
42      */

43     public ActiveChangeSet(ActiveChangeSetManager manager, String JavaDoc title) {
44         super(title);
45         this.manager = manager;
46     }
47
48     /**
49      * Get the title of the change set. The title is used
50      * as the comment when the set is checking in if no comment
51      * has been explicitly set using <code>setComment</code>.
52      * @return the title of the set
53      */

54     public String JavaDoc getTitle() {
55         return getName();
56     }
57     
58     /**
59      * Set the title of the set. The title is used
60      * as the comment when the set is committed if no comment
61      * has been explicitly set using <code>setComment</code>.
62      * @param title the title of the set
63      */

64     public void setTitle(String JavaDoc title) {
65         setName(title);
66         getManager().fireNameChangedEvent(this);
67     }
68
69     /**
70      * Get the comment of this change set. If the comment
71      * as never been set, the title is returned as the comment
72      * @return the comment to be used when the set is committed
73      */

74     public String JavaDoc getComment() {
75         if (comment == null) {
76             return getTitle();
77         }
78         return comment;
79     }
80     
81     /**
82      * Set the comment to be used when the change set is committed.
83      * If <code>null</code> is passed, the title of the set
84      * will be used as the comment.
85      * @param comment the comment for the set or <code>null</code>
86      * if the title should be the comment
87      */

88     public void setComment(String JavaDoc comment) {
89         if (comment != null && comment.equals(getTitle())) {
90             this.comment = null;
91         } else {
92             this.comment = comment;
93         }
94     }
95
96     /*
97      * Override inherited method to only include outgoing changes
98      */

99     protected boolean isValidChange(IDiff diff) {
100         return getManager().isModified(diff);
101     }
102
103     private void addResource(IResource resource) throws CoreException {
104         IDiff diff = getManager().getDiff(resource);
105         if (diff != null) {
106             add(diff);
107         }
108     }
109
110     private ActiveChangeSetManager getManager() {
111         return manager;
112     }
113
114     /**
115      * Return whether the set has a comment that differs from the title.
116      * @return whether the set has a comment that differs from the title
117      */

118     public boolean hasComment() {
119         return comment != null;
120     }
121     
122     public void save(Preferences prefs) {
123         prefs.put(CTX_TITLE, getTitle());
124         if (comment != null) {
125             prefs.put(CTX_COMMENT, comment);
126         }
127         if (!isEmpty()) {
128             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
129             IResource[] resources = getResources();
130             for (int i = 0; i < resources.length; i++) {
131                 IResource resource = resources[i];
132                 buffer.append(resource.getFullPath().toString());
133                 buffer.append('\n');
134             }
135             prefs.put(CTX_RESOURCES, buffer.toString());
136         }
137         prefs.putBoolean(CTX_USER_CREATED, isUserCreated());
138     }
139
140     public void init(Preferences prefs) {
141         setName(prefs.get(CTX_TITLE, "")); //$NON-NLS-1$
142
comment = prefs.get(CTX_COMMENT, null);
143         String JavaDoc resourcePaths = prefs.get(CTX_RESOURCES, null);
144         if (resourcePaths != null) {
145             ResourceDiffTree tree = internalGetDiffTree();
146             try {
147                 tree.beginInput();
148                 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
149                 StringTokenizer tokenizer = new StringTokenizer(resourcePaths, "\n"); //$NON-NLS-1$
150
while (tokenizer.hasMoreTokens()) {
151                     String JavaDoc next = tokenizer.nextToken();
152                     if (next.trim().length() > 0) {
153                         IResource resource = getResource(root, next);
154                         // Only include the resource if it is out-of-sync
155
try {
156                             if (resource != null && getManager().getDiff(resource) != null) {
157                                 addResource(resource);
158                             }
159                         } catch (CoreException e) {
160                             TeamPlugin.log(e);
161                         }
162                     }
163                 }
164             } finally {
165                 tree.endInput(null);
166             }
167         }
168         userCreated = prefs.getBoolean(CTX_USER_CREATED, true);
169     }
170
171     private IResource getResource(IWorkspaceRoot root, String JavaDoc next) {
172         IResource resource = root.findMember(next);
173         if (resource == null) {
174             // May be an outgoing deletion
175
Path path = new Path(null, next);
176             if (next.charAt(next.length()-1) == IPath.SEPARATOR) {
177                 if (path.segmentCount() == 1) {
178                     // resource is a project
179
resource = root.getProject(path.lastSegment());
180                 } else {
181                     // resource is a folder
182
resource = root.getFolder(path);
183                 }
184             } else {
185                 // resource is a file
186
resource = root.getFile(path);
187             }
188         }
189         return resource;
190     }
191
192     /**
193      * Add the resources to the change set if they are outgoing changes.
194      * @param resources the resources to add.
195      * @throws CoreException
196      */

197     public void add(IResource[] resources) throws CoreException {
198         List toAdd = new ArrayList();
199         for (int i = 0; i < resources.length; i++) {
200             IResource resource = resources[i];
201             IDiff diff = getManager().getDiff(resource);
202             if (diff != null) {
203                 toAdd.add(diff);
204             }
205         }
206         if (!toAdd.isEmpty()) {
207             add((IDiff[]) toAdd.toArray(new IDiff[toAdd.size()]));
208         }
209     }
210
211     /**
212      * Set whether this set was created by the user.
213      * @param userCreated whether this set was created by the user
214      */

215     public void setUserCreated(boolean userCreated) {
216         this.userCreated = userCreated;
217     }
218
219     /**
220      * Return whether this set was created by the user.
221      * @return whether this set was created by the user
222      */

223     public boolean isUserCreated() {
224         return userCreated;
225     }
226 }
227
Popular Tags