Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ee85cce692ea875c41a54f787e5902cf1d51725 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 * William Chen (Wind River)- [345552] Edit the remote files with a proper editor
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.ui.internal.compare;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.ITypedElement;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.CacheManager;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener;
import org.eclipse.tcf.te.tcf.filesystem.ui.nls.Messages;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.ide.FileStoreEditorInput;

/**
 * A <code>LocalTypedElement</code> extends <code>MergeTypedElement</code> and
 * wraps an <code>FSTreeNode</code> so that it can be used as the left element
 * of a <code>MergeEditorInput</code>. It implements the interface
 * <code>IEditableContent</code> so that it is editable.
 */
public class LocalTypedElement extends MergeTypedElement implements
		IEditableContent, IAdaptable, ISharedDocumentAdapterListener {
	// If the current edited file is dirty.
	private boolean dirty;
	// The shared document adapter
	private EditableSharedDocumentAdapter documentAdapter;
	// The shared document listener.
	private ISharedDocumentAdapterListener documentListener;

	/**
	 * Creates a <code>LocalTypedElement</code> for the given resource.
	 *
	 * @param resource
	 *            the resource
	 */
	public LocalTypedElement(FSTreeNode node) {
		super(node);
		setContent(getContent());
		dirty = false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == ISharedDocumentAdapter.class) {
			if (documentAdapter == null)
				documentAdapter = new EditableSharedDocumentAdapter(this);
			return documentAdapter;
		}
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.BufferedContent#setContent(byte[])
	 */
	@Override
	public void setContent(byte[] contents) {
		dirty = true;
		super.setContent(contents);
	}

	/**
	 * Set the document listener.
	 *
	 * @param documentListener
	 *            the document listener.
	 */
	public void setDocumentListener(
			ISharedDocumentAdapterListener documentListener) {
		this.documentListener = documentListener;
	}

	/**
	 * Return an input stream that opens that locally cached file to provide the
	 * content.
	 *
	 * @return a buffered input stream containing the contents of this file
	 * @exception CoreException
	 *                if the contents of this storage could not be accessed
	 */
	@Override
	protected InputStream createStream() throws CoreException {
		try {
			IPath cachePath = CacheManager.getCachePath(node);
			File cacheFile = cachePath.toFile();
			return new BufferedInputStream(new FileInputStream(cacheFile));
		} catch (FileNotFoundException e) {
			IStatus error = new Status(IStatus.ERROR,
					UIPlugin.getUniqueIdentifier(), e.getMessage(), e);
			throw new CoreException(error);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IEditableContent#isEditable()
	 */
	@Override
	public boolean isEditable() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IEditableContent#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement)
	 */
	@Override
	public ITypedElement replace(ITypedElement dest, ITypedElement src) {
		return dest;
	}

	/**
	 * Save the shared document for this element. The save can only be performed
	 * if the element is connected to a shared document. If the element is not
	 * connected, <code>false</code> is returned.
	 *
	 * @param overwrite
	 *            indicates whether overwrite should be performed while saving
	 *            the given element if necessary
	 * @param monitor
	 *            a progress monitor
	 * @throws CoreException
	 */
	public boolean store2Document(IProgressMonitor monitor)
			throws CoreException {
		if (isConnected()) {
			IEditorInput input = documentAdapter.getDocumentKey(this);
			documentAdapter.saveDocument(input, monitor);
			return true;
		}
		return false;
	}

	/**
	 * Judges whether the content has been changed.
	 *
	 * @return
	 */
	public boolean isDirty() {
		return dirty
				|| (documentAdapter != null && documentAdapter
						.hasBufferedContents());
	}

	/**
	 * Set the dirty action.
	 *
	 * @param dirty
	 *            The dirty action.
	 */
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

	/**
	 * If the document adapter has been connected.
	 *
	 * @return true if it is not null and connected.
	 */
	public boolean isConnected() {
		return documentAdapter != null && documentAdapter.isConnected();
	}

	/**
	 * Return the path to the local file of this node. It is used to compute its
	 * hash code and as the title of the comparison editor.
	 */
	@Override
	public String toString() {
		File cacheFile = CacheManager.getCacheFile(node);
		return cacheFile.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener#handleDocumentConnected()
	 */
	@Override
	public void handleDocumentConnected() {
		if (documentListener != null)
			documentListener.handleDocumentConnected();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener#handleDocumentDeleted()
	 */
	@Override
	public void handleDocumentDeleted() {
		if (documentListener != null)
			documentListener.handleDocumentDeleted();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener#handleDocumentDisconnected()
	 */
	@Override
	public void handleDocumentDisconnected() {
		if (documentListener != null)
			documentListener.handleDocumentDisconnected();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener#handleDocumentFlushed()
	 */
	@Override
	public void handleDocumentFlushed() {
		fireContentChanged();
		if (documentListener != null)
			documentListener.handleDocumentFlushed();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener#handleDocumentSaved()
	 */
	@Override
	public void handleDocumentSaved() {
		if (documentListener != null)
			documentListener.handleDocumentSaved();
	}

	/**
	 * Get an editor input for this file.
	 *
	 * @return The editor input.
	 */
	public IEditorInput getEditorInput() {
		IPath path = CacheManager.getCachePath(node);
		IFileStore fileStore = EFS.getLocalFileSystem().getStore(path);
		return new FileStoreEditorInput(fileStore);
	}

	/**
	 * Save to its local file when the document has not been connected yet.
	 *
	 * @param monitor
	 *            The monitor that reports the progress.
	 */
	public void store2Cache(IProgressMonitor monitor) throws CoreException {
		File cacheFile = CacheManager.getCacheFile(node);
		monitor.beginTask(NLS.bind(Messages.LocalTypedElement_SavingFile, cacheFile.getName()), 100);
		InputStream is = getContents();
		BufferedOutputStream bos = null;
		try {
			long total = cacheFile.length();
			bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
			byte[] data = new byte[10 * 1024];
			int length;
			long current = 0;
			int currProgress = 0;
			while ((length = is.read(data)) > 0) {
				bos.write(data, 0, length);
				bos.flush();
				current += length;
				int progress = (int) (current * 100 / total);
				if (currProgress != progress) {
					monitor.worked(progress - currProgress);
					currProgress = progress;
				}
			}
			setDirty(false);
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR,
					UIPlugin.getUniqueIdentifier(), e.getMessage(), e));
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (IOException ex) {
				}
			if (bos != null) {
				try {
					bos.close();
				} catch (Exception e) {
				}
			}
			// Notify the local file element that the document has changed.
			fireContentChanged();
		}
	}
}

Back to the top