KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > EditableSharedDocumentAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.synchronize;
12
13 import org.eclipse.compare.SharedDocumentAdapter;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.ui.IEditorInput;
18 import org.eclipse.ui.texteditor.IDocumentProvider;
19 import org.eclipse.ui.texteditor.IElementStateListener;
20
21 /**
22  * A shared document adapter that tracks whether the element is
23  * connected to a shared document and whether the contents have been
24  * flushed from a compare viewer. When contents are flushed, this
25  * adapter will connect to the document provider to ensure that
26  * the changes are not lost (see {@link #hasBufferedContents()}.
27  * In order to avoid a leak, the buffer must
28  * either be saved (see {@link #saveDocument(IEditorInput, boolean, IProgressMonitor)}
29  * or released (see {@link #releaseBuffer()}.
30  * <p>
31  * This adapter must have a one-to-one correspondence to
32  * a typed element.
33  */

34 public class EditableSharedDocumentAdapter extends
35         SharedDocumentAdapter implements IElementStateListener {
36     
37     private int connectionCount;
38     private final ISharedDocumentAdapterListener listener;
39     private IEditorInput bufferedKey;
40
41     /**
42      * Interface that provides this adapter with the state of the typed element
43      * and supports call backs to the element when the adapter state changes.
44      */

45     public interface ISharedDocumentAdapterListener {
46         
47         /**
48          * Method that is invoked when the adapter connects to the
49          * document provider. This method is only invoked when the
50          * adapter first connects to the document.
51          */

52         void handleDocumentConnected();
53         
54         /**
55          * Method that is invoked when the adapter disconnects from the
56          * document provider. This method is only invoked when the
57          * adapter no longer has any connection to the document provider.
58          */

59         void handleDocumentDisconnected();
60         
61         /**
62          * Method invoked when changes in the document are flushed to the adapter.
63          */

64         void handleDocumentFlushed();
65         
66         /**
67          * Method invoked when the file behind the shared document is deleted.
68          */

69         void handleDocumentDeleted();
70         
71         /**
72          * Method invoked when the document dirty state changes from dirty to clean.
73          */

74         void handleDocumentSaved();
75     }
76     
77     /**
78      * Create the shared document adapter for the given element.
79      * @param listener access to element internals
80      */

81     public EditableSharedDocumentAdapter(ISharedDocumentAdapterListener listener) {
82         super();
83         this.listener = listener;
84     }
85     
86     /* (non-Javadoc)
87      * @see org.eclipse.compare.SharedDocumentAdapter#connect(org.eclipse.ui.texteditor.IDocumentProvider, org.eclipse.ui.IEditorInput)
88      */

89     public void connect(IDocumentProvider provider, IEditorInput documentKey)
90             throws CoreException {
91         super.connect(provider, documentKey);
92         connectionCount++;
93         if (connectionCount == 1) {
94             provider.addElementStateListener(this);
95             listener.handleDocumentConnected();
96         }
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.compare.SharedDocumentAdapter#disconnect(org.eclipse.ui.texteditor.IDocumentProvider, org.eclipse.ui.IEditorInput)
101      */

102     public void disconnect(IDocumentProvider provider,
103             IEditorInput documentKey) {
104         try {
105             super.disconnect(provider, documentKey);
106         } finally {
107             if (connectionCount > 0)
108                 connectionCount--;
109             if (connectionCount == 0) {
110                 provider.removeElementStateListener(this);
111                 listener.handleDocumentDisconnected();
112             }
113         }
114     }
115
116     /**
117      * Return whether the element is connected to a shared document.
118      * @return whether the element is connected to a shared document
119      */

120     public boolean isConnected() {
121         return connectionCount > 0;
122     }
123     
124     /**
125      * Save the shared document of the element of this adapter.
126      * @param input the document key of the element.
127      * @param overwrite indicates whether overwrite should be performed
128      * while saving the given element if necessary
129      * @param monitor a progress monitor
130      * @return whether the save succeeded or not
131      * @throws CoreException
132      */

133     public boolean saveDocument(IEditorInput input, boolean overwrite, IProgressMonitor monitor) throws CoreException {
134         if (isConnected()) {
135             IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input);
136             try {
137                 saveDocument(provider, input, provider.getDocument(input), overwrite, monitor);
138             } finally {
139                 // When we write the document, remove out hold on the buffer
140
releaseBuffer();
141             }
142             return true;
143         }
144         return false;
145     }
146
147     /**
148      * Release the buffer if this adapter has buffered the contents in response to
149      * a {@link #flushDocument(IDocumentProvider, IEditorInput, IDocument, boolean)}.
150      */

151     public void releaseBuffer() {
152         if (bufferedKey != null) {
153             IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(bufferedKey);
154             provider.disconnect(bufferedKey);
155             bufferedKey = null;
156         }
157     }
158     
159     /* (non-Javadoc)
160      * @see org.eclipse.compare.SharedDocumentAdapter#flushDocument(org.eclipse.ui.texteditor.IDocumentProvider, org.eclipse.ui.IEditorInput, org.eclipse.jface.text.IDocument, boolean, org.eclipse.core.runtime.IProgressMonitor)
161      */

162     public void flushDocument(IDocumentProvider provider,
163             IEditorInput documentKey, IDocument document,
164             boolean overwrite)
165             throws CoreException {
166         if (!hasBufferedContents()) {
167             // On a flush, make an extra connection to the shared document so it will be kept even
168
// if it is no longer being viewed.
169
bufferedKey = documentKey;
170             provider.connect(bufferedKey);
171         }
172         this.listener.handleDocumentFlushed();
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.ui.texteditor.IElementStateListener#elementContentAboutToBeReplaced(java.lang.Object)
177      */

178     public void elementContentAboutToBeReplaced(Object JavaDoc element) {
179         // Nothing to do
180
}
181
182     /* (non-Javadoc)
183      * @see org.eclipse.ui.texteditor.IElementStateListener#elementContentReplaced(java.lang.Object)
184      */

185     public void elementContentReplaced(Object JavaDoc element) {
186         // Nothing to do
187
}
188
189     /* (non-Javadoc)
190      * @see org.eclipse.ui.texteditor.IElementStateListener#elementDeleted(java.lang.Object)
191      */

192     public void elementDeleted(Object JavaDoc element) {
193         listener.handleDocumentDeleted();
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.ui.texteditor.IElementStateListener#elementDirtyStateChanged(java.lang.Object, boolean)
198      */

199     public void elementDirtyStateChanged(Object JavaDoc element, boolean isDirty) {
200         if (!isDirty) {
201             this.listener.handleDocumentSaved();
202         }
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.ui.texteditor.IElementStateListener#elementMoved(java.lang.Object, java.lang.Object)
207      */

208     public void elementMoved(Object JavaDoc originalElement, Object JavaDoc movedElement) {
209         // Nothing to do
210
}
211
212     /**
213      * Return whether the adapter has buffered contents. The adapter
214      * buffers contents by connecting to the document through the document
215      * provider. This means that the adapter must be disconnected either by saving
216      * or discarding the buffer.
217      * @return whether the adapter has buffered contents
218      */

219     public boolean hasBufferedContents() {
220         return bufferedKey != null;
221     }
222 }
Popular Tags