Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 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.patch; |
12 |
|
13 |
import org.eclipse.compare.*; |
14 |
import org.eclipse.compare.internal.core.patch.FileDiffResult; |
15 |
import org.eclipse.compare.internal.core.patch.FilePatch2; |
16 |
import org.eclipse.compare.internal.patch.*; |
17 |
import org.eclipse.compare.patch.PatchConfiguration; |
18 |
import org.eclipse.compare.structuremergeviewer.*; |
19 |
import org.eclipse.core.internal.runtime.AdapterManager; |
20 |
import org.eclipse.core.resources.IResource; |
21 |
import org.eclipse.core.runtime.IAdaptable; |
22 |
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement; |
23 |
|
24 |
public class PatchFileDiffNode extends PatchDiffNode implements IContentChangeListener, IAdaptable { |
25 |
|
26 |
private final FileDiffResult result; |
27 |
|
28 |
public static PatchFileDiffNode createDiffNode(DiffNode parent, FileDiffResult result) { |
29 |
return new PatchFileDiffNode(result, parent, getKind(result), getAncestorElement(result), getLeftElement(result), getRightElement(result)); |
30 |
} |
31 |
|
32 |
private static int getKind(FileDiffResult result) { |
33 |
if (!result.hasMatches()) |
34 |
return Differencer.NO_CHANGE; |
35 |
int fileDiffKind = result.getDiff().getDiffType(result.getConfiguration().isReversed()); |
36 |
int kind = convertFileDiffTypeToDifferencerType(fileDiffKind); |
37 |
return kind | Differencer.RIGHT; |
38 |
} |
39 |
|
40 |
private static int convertFileDiffTypeToDifferencerType(int fileDiffKind) { |
41 |
int kind; |
42 |
switch (fileDiffKind) { |
43 |
case FilePatch2.ADDITION: |
44 |
kind = Differencer.ADDITION; |
45 |
break; |
46 |
case FilePatch2.DELETION: |
47 |
kind = Differencer.DELETION; |
48 |
break; |
49 |
case FilePatch2.CHANGE: |
50 |
kind = Differencer.CHANGE; |
51 |
break; |
52 |
default: |
53 |
kind = Differencer.CHANGE; |
54 |
break; |
55 |
} |
56 |
return kind; |
57 |
} |
58 |
|
59 |
private static ITypedElement getRightElement(FileDiffResult result) { |
60 |
return new PatchFileTypedElement(result, true); |
61 |
} |
62 |
|
63 |
private static ITypedElement getLeftElement(FileDiffResult result) { |
64 |
return new LocalResourceTypedElement(((WorkspaceFileDiffResult)result).getTargetFile()); |
65 |
// return new PatchFileTypedElement(result, false); |
66 |
} |
67 |
|
68 |
private static ITypedElement getAncestorElement(FileDiffResult result) { |
69 |
return new PatchFileTypedElement(result, false); |
70 |
} |
71 |
|
72 |
public PatchFileDiffNode(FileDiffResult result, IDiffContainer parent, int kind, |
73 |
ITypedElement ancestor, ITypedElement left, ITypedElement right) { |
74 |
super(result.getDiff(), parent, kind, ancestor, left, right); |
75 |
this.result = result; |
76 |
} |
77 |
|
78 |
public FileDiffResult getDiffResult() { |
79 |
return result; |
80 |
} |
81 |
|
82 |
protected PatchConfiguration getConfiguration() { |
83 |
return result.getConfiguration(); |
84 |
} |
85 |
|
86 |
/* (non-Javadoc) |
87 |
* @see org.eclipse.compare.structuremergeviewer.DiffContainer#add(org.eclipse.compare.structuremergeviewer.IDiffElement) |
88 |
*/ |
89 |
public void add(IDiffElement diff) { |
90 |
super.add(diff); |
91 |
// Listen for content changes in unmatched children so we can fire an input change |
92 |
if (diff instanceof HunkDiffNode) { |
93 |
HunkDiffNode node = (HunkDiffNode) diff; |
94 |
Object left = node.getLeft(); |
95 |
if (left instanceof IContentChangeNotifier) { |
96 |
IContentChangeNotifier notifier = (IContentChangeNotifier) left; |
97 |
notifier.addContentChangeListener(this); |
98 |
} |
99 |
} |
100 |
} |
101 |
|
102 |
/* (non-Javadoc) |
103 |
* @see org.eclipse.compare.IContentChangeListener#contentChanged(org.eclipse.compare.IContentChangeNotifier) |
104 |
*/ |
105 |
public void contentChanged(IContentChangeNotifier source) { |
106 |
fireChange(); |
107 |
} |
108 |
|
109 |
public int getKind() { |
110 |
int kind = super.getKind(); |
111 |
if (kind == Differencer.NO_CHANGE && getPatcher().hasCachedContents(getDiffResult().getDiff())) { |
112 |
return Differencer.CHANGE | Differencer.RIGHT; |
113 |
} |
114 |
return kind; |
115 |
} |
116 |
|
117 |
public boolean fileExists() { |
118 |
IResource file = getResource(); |
119 |
return file != null && file.isAccessible(); |
120 |
} |
121 |
|
122 |
public IResource getResource() { |
123 |
return ((WorkspaceFileDiffResult)getDiffResult()).getTargetFile(); |
124 |
} |
125 |
|
126 |
public Object getAdapter(Class adapter) { |
127 |
if (adapter == IResource.class) |
128 |
return getResource(); |
129 |
return AdapterManager.getDefault().getAdapter(this, adapter); |
130 |
} |
131 |
} |