Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 58bbfc2581a7650410819484165255411a8b6c71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core.subscribers;

import java.util.*;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
import org.eclipse.team.internal.core.TeamPlugin;
import org.osgi.service.prefs.Preferences;

/**
 * An active change set represents a set of local resource changes
 * that are grouped together as a single logical change.
 * @since 3.1
 */
public class ActiveChangeSet extends DiffChangeSet {

    private static final String CTX_TITLE = "title"; //$NON-NLS-1$
    private static final String CTX_COMMENT = "comment"; //$NON-NLS-1$
    private static final String CTX_RESOURCES = "resources"; //$NON-NLS-1$
    private static final String CTX_USER_CREATED = "userCreated"; //$NON-NLS-1$

    private final ActiveChangeSetManager manager;
    private String comment;
	private boolean userCreated = true;

	/**
	 * Create a change set with the given title
	 * @param manager the manager that owns this set
     * @param title the title of the set
     */
    public ActiveChangeSet(ActiveChangeSetManager manager, String title) {
        super(title);
        this.manager = manager;
    }

    /**
     * Get the title of the change set. The title is used
     * as the comment when the set is checking in if no comment
     * has been explicitly set using <code>setComment</code>.
     * @return the title of the set
     */
    public String getTitle() {
        return getName();
    }

    /**
     * Set the title of the set. The title is used
     * as the comment when the set is committed if no comment
     * has been explicitly set using <code>setComment</code>.
     * @param title the title of the set
     */
    public void setTitle(String title) {
        setName(title);
        getManager().fireNameChangedEvent(this);
    }

    /**
     * Get the comment of this change set. If the comment
     * as never been set, the title is returned as the comment
     * @return the comment to be used when the set is committed
     */
    @Override
	public String getComment() {
        if (comment == null) {
            return getTitle();
        }
        return comment;
    }

    /**
     * Set the comment to be used when the change set is committed.
     * If <code>null</code> is passed, the title of the set
     * will be used as the comment.
     * @param comment the comment for the set or <code>null</code>
     * if the title should be the comment
     */
    public void setComment(String comment) {
        if (comment != null && comment.equals(getTitle())) {
            this.comment = null;
        } else {
            this.comment = comment;
        }
    }

    /*
     * Override inherited method to only include outgoing changes
     */
    @Override
	protected boolean isValidChange(IDiff diff) {
        return getManager().isModified(diff);
    }

    private void addResource(IResource resource) throws CoreException {
        IDiff diff = getManager().getDiff(resource);
        if (diff != null) {
            add(diff);
        }
    }

    private ActiveChangeSetManager getManager() {
        return manager;
    }

    /**
     * Return whether the set has a comment that differs from the title.
     * @return whether the set has a comment that differs from the title
     */
    public boolean hasComment() {
        return comment != null;
    }

    public void save(Preferences prefs) {
        prefs.put(CTX_TITLE, getTitle());
        if (comment != null) {
            prefs.put(CTX_COMMENT, comment);
        }
        if (!isEmpty()) {
	        StringBuffer buffer = new StringBuffer();
	        IResource[] resources = getResources();
	        for (int i = 0; i < resources.length; i++) {
                IResource resource = resources[i];
	            buffer.append(resource.getFullPath().toString());
	            buffer.append('\n');
	        }
	        prefs.put(CTX_RESOURCES, buffer.toString());
        }
        prefs.putBoolean(CTX_USER_CREATED, isUserCreated());
    }

    public void init(Preferences prefs) {
        setName(prefs.get(CTX_TITLE, "")); //$NON-NLS-1$
        comment = prefs.get(CTX_COMMENT, null);
        String resourcePaths = prefs.get(CTX_RESOURCES, null);
        if (resourcePaths != null) {
            ResourceDiffTree tree = internalGetDiffTree();
            try {
                tree.beginInput();
	            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	            StringTokenizer tokenizer = new StringTokenizer(resourcePaths, "\n"); //$NON-NLS-1$
	            while (tokenizer.hasMoreTokens()) {
	                String next = tokenizer.nextToken();
	                if (next.trim().length() > 0) {
	                    IResource resource = getResource(root, next);
                        // Only include the resource if it is out-of-sync
                        try {
							if (resource != null && getManager().getDiff(resource) != null) {
								addResource(resource);
							}
						} catch (CoreException e) {
							TeamPlugin.log(e);
						}
	                }
	            }
            } finally {
                tree.endInput(null);
            }
        }
        userCreated = prefs.getBoolean(CTX_USER_CREATED, true);
    }

    private IResource getResource(IWorkspaceRoot root, String next) {
        IResource resource = root.findMember(next);
        if (resource == null) {
            // May be an outgoing deletion
            Path path = new Path(null, next);
            if (next.charAt(next.length()-1) == IPath.SEPARATOR) {
                if (path.segmentCount() == 1) {
                    // resource is a project
                    resource = root.getProject(path.lastSegment());
                } else {
                    // resource is a folder
                    resource = root.getFolder(path);
                }
            } else {
                // resource is a file
                resource = root.getFile(path);
            }
        }
        return resource;
    }

    /**
     * Add the resources to the change set if they are outgoing changes.
     * @param resources the resources to add.
     * @throws CoreException
     */
    public void add(IResource[] resources) throws CoreException {
        List<IDiff> toAdd = new ArrayList<>();
        for (int i = 0; i < resources.length; i++) {
            IResource resource = resources[i];
            IDiff diff = getManager().getDiff(resource);
            if (diff != null) {
                toAdd.add(diff);
            }
        }
        if (!toAdd.isEmpty()) {
            add(toAdd.toArray(new IDiff[toAdd.size()]));
        }
    }

    /**
     * Set whether this set was created by the user.
     * @param userCreated whether this set was created by the user
     */
	public void setUserCreated(boolean userCreated) {
		this.userCreated = userCreated;
	}

	/**
	 * Return whether this set was created by the user.
	 * @return whether this set was created by the user
	 */
	public boolean isUserCreated() {
		return userCreated;
	}
}

Back to the top