001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.plugin.quickdiff;
034:
035: import org.eclipse.core.resources.IFile;
036: import org.eclipse.core.runtime.IProgressMonitor;
037:
038: import org.eclipse.jface.text.Document;
039: import org.eclipse.jface.text.IDocument;
040:
041: import org.eclipse.ui.IEditorInput;
042: import org.eclipse.ui.IFileEditorInput;
043: import org.eclipse.ui.editors.text.IStorageDocumentProvider;
044: import org.eclipse.ui.texteditor.IDocumentProvider;
045: import org.eclipse.ui.texteditor.ITextEditor;
046: import org.eclipse.ui.texteditor.quickdiff.IQuickDiffReferenceProvider;
047:
048: import org.libresource.so6.plugin.core.So6Util;
049:
050: /**
051: * @author Guillaume Bort
052: */
053: import java.io.BufferedReader;
054: import java.io.IOException;
055: import java.io.InputStream;
056: import java.io.InputStreamReader;
057: import java.io.Reader;
058:
059: /**
060: * DOCUMENT ME!
061: *
062: * @author $author$
063: * @version $Revision: 1.3 $
064: */
065: public class QuickDiffProvider implements IQuickDiffReferenceProvider {
066: private Document fReference;
067: private IDocumentProvider fDocumentProvider;
068: private IEditorInput fEditorInput;
069: private String id;
070: private boolean enabled;
071: private boolean fDocumentRead;
072:
073: /**
074: * DOCUMENT ME!
075: */
076: public void dispose() {
077: fEditorInput = null;
078: fDocumentProvider = null;
079: fReference = null;
080: fDocumentRead = false;
081: enabled = false;
082: }
083:
084: /**
085: * DOCUMENT ME!
086: *
087: * @return DOCUMENT ME!
088: */
089: public String getId() {
090: return id;
091: }
092:
093: /**
094: * DOCUMENT ME!
095: *
096: * @param monitor DOCUMENT ME!
097: *
098: * @return DOCUMENT ME!
099: */
100: public IDocument getReference(IProgressMonitor monitor) {
101: if (!fDocumentRead) {
102: readDocument();
103: }
104:
105: return fReference;
106: }
107:
108: /**
109: * DOCUMENT ME!
110: *
111: * @return DOCUMENT ME!
112: */
113: public boolean isEnabled() {
114: return enabled;
115: }
116:
117: /**
118: * DOCUMENT ME!
119: *
120: * @param editor DOCUMENT ME!
121: */
122: public void setActiveEditor(ITextEditor editor) {
123: IDocumentProvider provider = null;
124: IEditorInput input = null;
125:
126: if (editor != null) {
127: provider = editor.getDocumentProvider();
128: input = editor.getEditorInput();
129: }
130:
131: // dispose if the editor input or document provider have changed
132: // note that they may serve multiple editors
133: if ((provider != fDocumentProvider) || (input != fEditorInput)) {
134: dispose();
135: fDocumentProvider = provider;
136: fEditorInput = input;
137: }
138:
139: setEnabled();
140: }
141:
142: /**
143: * DOCUMENT ME!
144: */
145: public void setEnabled() {
146: if ((fEditorInput != null)
147: && fEditorInput instanceof IFileEditorInput
148: && (fDocumentProvider != null)) {
149: IFileEditorInput input = (IFileEditorInput) fEditorInput;
150: IFile src = input.getFile();
151:
152: try {
153: enabled = So6Util.existRefCopy(src);
154: } catch (Exception e) {
155: enabled = false;
156: }
157: }
158: }
159:
160: /**
161: * DOCUMENT ME!
162: *
163: * @param id DOCUMENT ME!
164: */
165: public void setId(String id) {
166: this .id = id;
167: }
168:
169: private static Document createDocument(InputStream contentStream)
170: throws IOException {
171: Document result = new Document();
172: Reader in = null;
173:
174: try {
175: final int DEFAULT_FILE_SIZE = 15 * 1024;
176:
177: in = new BufferedReader(
178: new InputStreamReader(contentStream),
179: DEFAULT_FILE_SIZE);
180:
181: StringBuffer buffer = new StringBuffer(DEFAULT_FILE_SIZE);
182: char[] readBuffer = new char[2048];
183: int n = in.read(readBuffer);
184:
185: while (n > 0) {
186: buffer.append(readBuffer, 0, n);
187: n = in.read(readBuffer);
188: }
189:
190: result.set(buffer.toString());
191: } finally {
192: if (in != null) {
193: try {
194: in.close();
195: } catch (IOException x) {
196: ;
197: }
198: }
199: }
200:
201: return result;
202: }
203:
204: private void readDocument() {
205: if (fDocumentProvider instanceof IStorageDocumentProvider
206: && fEditorInput instanceof IFileEditorInput) {
207: IFileEditorInput input = (IFileEditorInput) fEditorInput;
208: IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider;
209:
210: IFile src = input.getFile();
211:
212: try {
213: InputStream stream = So6Util.getRefCopyContent(src);
214: fReference = createDocument(stream);
215: } catch (Exception e) {
216: e.printStackTrace();
217: }
218: }
219: }
220: }
|