Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 17835febcddb87ffbcc828adb04a34b747ffc6ff (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize;

import org.eclipse.compare.SharedDocumentAdapter;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.IElementStateListener;

/**
 * A shared document adapter that tracks whether the element is connected to a
 * shared document and whether the contents have been flushed from a compare
 * viewer. When contents are flushed, this adapter will connect to the document
 * provider to ensure that the changes are not lost (see
 * {@link #hasBufferedContents()}). In order to avoid a leak, the buffer must
 * either be saved (see
 * {@link #saveDocument(IEditorInput, boolean, IProgressMonitor)}) or released
 * (see {@link #releaseBuffer()}).
 * <p>
 * This adapter must have a one-to-one correspondence to a typed element.
 */
public class EditableSharedDocumentAdapter extends
		SharedDocumentAdapter implements IElementStateListener {

	private int connectionCount;
	private final ISharedDocumentAdapterListener listener;
	private IEditorInput bufferedKey;

	/**
	 * Interface that provides this adapter with the state of the typed element
	 * and supports call backs to the element when the adapter state changes.
	 */
	public interface ISharedDocumentAdapterListener {

		/**
		 * Method that is invoked when the adapter connects to the
		 * document provider. This method is only invoked when the
		 * adapter first connects to the document.
		 */
		void handleDocumentConnected();

		/**
		 * Method that is invoked when the adapter disconnects from the
		 * document provider. This method is only invoked when the
		 * adapter no longer has any connection to the document provider.
		 */
		void handleDocumentDisconnected();

		/**
		 * Method invoked when changes in the document are flushed to the adapter.
		 */
		void handleDocumentFlushed();

		/**
		 * Method invoked when the file behind the shared document is deleted.
		 */
		void handleDocumentDeleted();

		/**
		 * Method invoked when the document dirty state changes from dirty to clean.
		 */
		void handleDocumentSaved();
	}

	/**
	 * Create the shared document adapter for the given element.
	 * @param listener access to element internals
	 */
	public EditableSharedDocumentAdapter(ISharedDocumentAdapterListener listener) {
		super();
		this.listener = listener;
	}

	@Override
	public void connect(IDocumentProvider provider, IEditorInput documentKey)
			throws CoreException {
		super.connect(provider, documentKey);
		connectionCount++;
		if (connectionCount == 1) {
			provider.addElementStateListener(this);
			listener.handleDocumentConnected();
		}
	}

	@Override
	public void disconnect(IDocumentProvider provider,
			IEditorInput documentKey) {
		try {
			super.disconnect(provider, documentKey);
		} finally {
			if (connectionCount > 0)
				connectionCount--;
			if (connectionCount == 0) {
				provider.removeElementStateListener(this);
				listener.handleDocumentDisconnected();
			}
		}
	}

	/**
	 * Return whether the element is connected to a shared document.
	 * @return whether the element is connected to a shared document
	 */
	public boolean isConnected() {
		return connectionCount > 0;
	}

	/**
	 * Save the shared document of the element of this adapter.
	 * @param input the document key of the element.
	 * @param overwrite indicates whether overwrite should be performed
	 * 			while saving the given element if necessary
	 * @param monitor a progress monitor
	 * @return whether the save succeeded or not
	 * @throws CoreException
	 */
	public boolean saveDocument(IEditorInput input, boolean overwrite, IProgressMonitor monitor) throws CoreException {
		if (isConnected()) {
			IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input);
			try {
				saveDocument(provider, input, provider.getDocument(input), overwrite, monitor);
			} finally {
				// When we write the document, remove out hold on the buffer
				releaseBuffer();
			}
			return true;
		}
		return false;
	}

	/**
	 * Release the buffer if this adapter has buffered the contents in response to
	 * a {@link #flushDocument(IDocumentProvider, IEditorInput, IDocument, boolean)}.
	 */
	public void releaseBuffer() {
		if (bufferedKey != null) {
			IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(bufferedKey);
			provider.disconnect(bufferedKey);
			bufferedKey = null;
		}
	}

	@Override
	public void flushDocument(IDocumentProvider provider,
			IEditorInput documentKey, IDocument document,
			boolean overwrite)
			throws CoreException {
		if (!hasBufferedContents()) {
			// On a flush, make an extra connection to the shared document so it will be kept even
			// if it is no longer being viewed.
			bufferedKey = documentKey;
			provider.connect(bufferedKey);
		}
		this.listener.handleDocumentFlushed();
	}

	@Override
	public void elementContentAboutToBeReplaced(Object element) {
		// Nothing to do
	}

	@Override
	public void elementContentReplaced(Object element) {
		// Nothing to do
	}

	@Override
	public void elementDeleted(Object element) {
		listener.handleDocumentDeleted();
	}

	@Override
	public void elementDirtyStateChanged(Object element, boolean isDirty) {
		if (!isDirty) {
			this.listener.handleDocumentSaved();
		}
	}

	@Override
	public void elementMoved(Object originalElement, Object movedElement) {
		// Nothing to do
	}

	/**
	 * Return whether the adapter has buffered contents. The adapter
	 * buffers contents by connecting to the document through the document
	 * provider. This means that the adapter must be disconnected either by saving
	 * or discarding the buffer.
	 * @return whether the adapter has buffered contents
	 */
	public boolean hasBufferedContents() {
		return bufferedKey != null;
	}
}

Back to the top