KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.compare.*;
14 import org.eclipse.compare.patch.PatchConfiguration;
15 import org.eclipse.compare.structuremergeviewer.*;
16 import org.eclipse.core.resources.IFile;
17
18 public class PatchFileDiffNode extends PatchDiffNode implements IContentChangeListener {
19
20     private final FileDiffResult result;
21
22     public static PatchFileDiffNode createDiffNode(DiffNode parent, FileDiffResult result) {
23         return new PatchFileDiffNode(result, parent, getKind(result), getAncestorElement(result), getLeftElement(result), getRightElement(result));
24     }
25     
26     private static int getKind(FileDiffResult result) {
27         if (!result.hasMatches())
28             return Differencer.NO_CHANGE;
29         return result.getDiff().getDiffType(result.getConfiguration().isReversed()) | Differencer.RIGHT;
30     }
31
32     private static ITypedElement getRightElement(FileDiffResult result) {
33         return new PatchFileTypedElement(result, true);
34     }
35
36     private static ITypedElement getLeftElement(FileDiffResult result) {
37         return new PatchFileTypedElement(result, false);
38     }
39
40     private static ITypedElement getAncestorElement(FileDiffResult result) {
41         return new PatchFileTypedElement(result, false);
42     }
43
44     public PatchFileDiffNode(FileDiffResult result, IDiffContainer parent, int kind,
45             ITypedElement ancestor, ITypedElement left, ITypedElement right) {
46         super(result.getDiff(), parent, kind, ancestor, left, right);
47         this.result = result;
48     }
49
50     public FileDiffResult getDiffResult() {
51         return result;
52     }
53     
54     protected PatchConfiguration getConfiguration() {
55         return result.getConfiguration();
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.compare.structuremergeviewer.DiffContainer#add(org.eclipse.compare.structuremergeviewer.IDiffElement)
60      */

61     public void add(IDiffElement diff) {
62         super.add(diff);
63         // Listen for content changes in unmatched children so we can fire an input change
64
if (diff instanceof HunkDiffNode) {
65             HunkDiffNode node = (HunkDiffNode) diff;
66             Object JavaDoc left = node.getLeft();
67             if (left instanceof IContentChangeNotifier) {
68                 IContentChangeNotifier notifier = (IContentChangeNotifier) left;
69                 notifier.addContentChangeListener(this);
70             }
71         }
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.compare.IContentChangeListener#contentChanged(org.eclipse.compare.IContentChangeNotifier)
76      */

77     public void contentChanged(IContentChangeNotifier source) {
78         fireChange();
79     }
80     
81     public int getKind() {
82         int kind = super.getKind();
83         if (kind == Differencer.NO_CHANGE && getPatcher().hasCachedContents(getDiffResult().getDiff())) {
84             return Differencer.CHANGE | Differencer.RIGHT;
85         }
86         return kind;
87     }
88
89     public boolean fileExists() {
90         IFile file = ((WorkspaceFileDiffResult)getDiffResult()).getTargetFile();
91         return file != null && file.isAccessible();
92     }
93
94 }
95
Popular Tags