1 /*******************************************************************************
2 * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.egit
.ui
.internal
;
12 import java
.io
.ByteArrayInputStream
;
13 import java
.io
.InputStream
;
15 import org
.eclipse
.compare
.IContentChangeListener
;
16 import org
.eclipse
.compare
.IContentChangeNotifier
;
17 import org
.eclipse
.compare
.IEditableContent
;
18 import org
.eclipse
.compare
.ISharedDocumentAdapter
;
19 import org
.eclipse
.compare
.ITypedElement
;
20 import org
.eclipse
.compare
.internal
.ContentChangeNotifier
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.Platform
;
23 import org
.eclipse
.team
.core
.history
.IFileRevision
;
24 import org
.eclipse
.team
.internal
.ui
.history
.FileRevisionTypedElement
;
25 import org
.eclipse
.team
.internal
.ui
.synchronize
.EditableSharedDocumentAdapter
;
31 public class EditableRevision
extends FileRevisionTypedElement
implements
32 ITypedElement
, IEditableContent
, IContentChangeNotifier
{
34 private byte[] modifiedContent
;
36 private ContentChangeNotifier fChangeNotifier
;
38 private EditableSharedDocumentAdapter sharedDocumentAdapter
;
43 public EditableRevision(IFileRevision fileRevision
) {
47 public boolean isEditable() {
51 public ITypedElement
replace(ITypedElement dest
, ITypedElement src
) {
56 public InputStream
getContents() throws CoreException
{
57 if (modifiedContent
!= null) {
58 return new ByteArrayInputStream(modifiedContent
);
60 return super.getContents();
63 public void setContent(byte[] newContent
) {
64 modifiedContent
= newContent
;
69 * @return the modified content
71 public byte[] getModifiedContent() {
72 return modifiedContent
;
75 public Object
getAdapter(Class adapter
) {
76 if (adapter
== ISharedDocumentAdapter
.class) {
77 return getSharedDocumentAdapter();
79 return Platform
.getAdapterManager().getAdapter(this, adapter
);
82 private synchronized ISharedDocumentAdapter
getSharedDocumentAdapter() {
83 if (sharedDocumentAdapter
== null)
84 sharedDocumentAdapter
= new EditableSharedDocumentAdapter(
85 new EditableSharedDocumentAdapter
.ISharedDocumentAdapterListener() {
86 public void handleDocumentConnected() {
89 public void handleDocumentFlushed() {
92 public void handleDocumentDeleted() {
95 public void handleDocumentSaved() {
98 public void handleDocumentDisconnected() {
101 return sharedDocumentAdapter
;
104 public void addContentChangeListener(IContentChangeListener listener
) {
105 if (fChangeNotifier
== null)
106 fChangeNotifier
= new ContentChangeNotifier(this);
107 fChangeNotifier
.addContentChangeListener(listener
);
110 public void removeContentChangeListener(IContentChangeListener listener
) {
111 if (fChangeNotifier
!= null) {
112 fChangeNotifier
.removeContentChangeListener(listener
);
113 if (fChangeNotifier
.isEmpty())
114 fChangeNotifier
= null;
119 * Notifies all registered <code>IContentChangeListener</code>s of a content
122 protected void fireContentChanged() {
123 if (fChangeNotifier
== null || fChangeNotifier
.isEmpty()) {
126 fChangeNotifier
.fireContentChanged();