KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > patch > UnmatchedHunkTypedElement


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.compare.internal.patch;
12
13 import java.io.*;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.compare.*;
17 import org.eclipse.compare.internal.CompareUIPlugin;
18 import org.eclipse.compare.internal.ContentChangeNotifier;
19 import org.eclipse.compare.patch.PatchConfiguration;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.CoreException;
22
23 public class UnmatchedHunkTypedElement extends HunkTypedElement implements IContentChangeNotifier, IEditableContent {
24
25     private ContentChangeNotifier changeNotifier;
26     
27     public UnmatchedHunkTypedElement(HunkResult result) {
28         // An unmatched hunk element is always used for the before state and is full context
29
super(result, false, true);
30     }
31
32     /* (non-Javadoc)
33      * @see org.eclipse.compare.IContentChangeNotifier#addContentChangeListener(org.eclipse.compare.IContentChangeListener)
34      */

35     public synchronized void addContentChangeListener(IContentChangeListener listener) {
36         if (changeNotifier == null)
37             changeNotifier = new ContentChangeNotifier(this);
38         changeNotifier.addContentChangeListener(listener);
39     }
40
41     /* (non-Javadoc)
42      * @see org.eclipse.compare.IContentChangeNotifier#removeContentChangeListener(org.eclipse.compare.IContentChangeListener)
43      */

44     public synchronized void removeContentChangeListener(IContentChangeListener listener) {
45         if (changeNotifier != null)
46             changeNotifier.removeContentChangeListener(listener);
47     }
48
49     /* (non-Javadoc)
50      * @see org.eclipse.compare.IEditableContent#isEditable()
51      */

52     public boolean isEditable() {
53         IFile file = ((WorkspaceFileDiffResult)getHunkResult().getDiffResult()).getTargetFile();
54         return file != null && file.isAccessible();
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.compare.IEditableContent#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement)
59      */

60     public ITypedElement replace(ITypedElement dest, ITypedElement src) {
61         // Not supported
62
return null;
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.compare.IEditableContent#setContent(byte[])
67      */

68     public void setContent(byte[] newContent) {
69         getPatcher().setManuallyMerged(getHunkResult().getHunk(), true);
70         getPatcher().cacheContents(getDiff(), newContent);
71         if (changeNotifier != null)
72             changeNotifier.fireContentChanged();
73     }
74
75     private FileDiff getDiff() {
76         return getHunkResult().getDiffResult().getDiff();
77     }
78
79     private Patcher getPatcher() {
80         return Patcher.getPatcher(getConfiguration());
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.compare.internal.patch.HunkTypedElement#getContents()
85      */

86     public InputStream getContents() throws CoreException {
87         // If there are cached contents, use them
88
if (getPatcher().hasCachedContents(getDiff()))
89             return new ByteArrayInputStream(getPatcher().getCachedContents(getDiff()));
90         // Otherwise return the after state of the diff result
91
List JavaDoc lines = getHunkResult().getDiffResult().getAfterLines();
92         String JavaDoc content = Patcher.createString(getHunkResult().getDiffResult().isPreserveLineDelimeters(), lines);
93         byte[] bytes = null;
94         if (getCharset() != null)
95             try {
96                 bytes = content.getBytes(getCharset());
97             } catch (UnsupportedEncodingException e) {
98                 CompareUIPlugin.log(e);
99             }
100         if (bytes == null)
101             bytes = content.getBytes();
102         return new ByteArrayInputStream(bytes);
103     }
104
105     private PatchConfiguration getConfiguration() {
106         return getHunkResult().getDiffResult().getConfiguration();
107     }
108 }
109
Popular Tags