Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f7dd30b1f15569789e1b341bddca91ada3c058c (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
/*******************************************************************************
 * Copyright (c) 2009 Remy Chi Jian Suen 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:
 *    Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.internal.sync.resources.core;

import java.io.ByteArrayInputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.ecf.sync.ModelUpdateException;
import org.eclipse.ecf.sync.resources.core.preferences.PreferenceConstants;

public final class FileChangeMessage extends ResourceChangeMessage {

	private static final long serialVersionUID = 1482601202361201103L;

	FileChangeMessage(String path, int kind, byte[] contents) {
		super(path, IResource.FILE, kind, contents);
	}

	public void applyToModel(Object model) throws ModelUpdateException {
		try {
			apply();
		} catch (CoreException e) {
			throw new ModelUpdateException(e, this, model);
		}
	}

	private void apply() throws CoreException {
		IPath resourcePath = new Path(path);
		final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(
				resourcePath);

		switch (kind) {
		case IResourceDelta.ADDED:
			switch (SyncResourcesCore
					.getInt(PreferenceConstants.REMOTE_RESOURCE_ADDITION)) {
			case PreferenceConstants.COMMIT_VALUE:
				if (file.exists()) {
					// file was added by a remote peer when it exists locally,
					// this is a conflict, flag it as such
					setConflicted(true);
					// change the contents
					file.setContents(new ByteArrayInputStream(contents), true,
							true, null);
				} else {
					// file doesn't exist, create it
					file.create(new ByteArrayInputStream(contents), true, null);
				}
				break;
			case PreferenceConstants.IGNORE_CONFLICTS_VALUE:
				if (file.exists()) {
					// the file exists, this is a conflict, flag it
					setConflicted(true);
					// the user has set the system to ignore conflicts, flag it
					setIgnored(true);
				} else {
					// file doesn't exist, create it
					file.create(new ByteArrayInputStream(contents), true,
							new NullProgressMonitor());
				}
				break;
			case PreferenceConstants.IGNORE_VALUE:
				setIgnored(true);
				break;
			}
			break;
		case IResourceDelta.CHANGED:
			switch (SyncResourcesCore
					.getInt(PreferenceConstants.REMOTE_RESOURCE_ADDITION)) {
			case PreferenceConstants.COMMIT_VALUE:
				if (file.exists()) {
					file.setContents(new ByteArrayInputStream(contents), true,
							true, null);
				} else {
					setConflicted(true);
					file.create(new ByteArrayInputStream(contents), true, null);
				}
				break;
			case PreferenceConstants.IGNORE_CONFLICTS_VALUE:
				if (file.exists()) {
					file.setContents(new ByteArrayInputStream(contents), true,
							true, null);
				} else {
					setConflicted(true);
					setIgnored(true);
				}
				break;
			case PreferenceConstants.IGNORE_VALUE:
				setIgnored(true);
				break;
			}
			break;
		case IResourceDelta.REMOVED:
			switch (SyncResourcesCore
					.getInt(PreferenceConstants.REMOTE_RESOURCE_DELETION)) {
			case PreferenceConstants.COMMIT_VALUE:
				if (file.exists()) {
					// the file exists, commit the change by deleting it
					file.delete(false, true, new NullProgressMonitor());
				} else {
					// if it doesn't exist, then there's nothing to delete, so
					// we flag this change as having been ignored
					setIgnored(true);
				}
				break;
			case PreferenceConstants.IGNORE_VALUE:
				setIgnored(true);
				break;
			}
		}
	}

}

Back to the top