KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > MergeViewerContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13 import org.eclipse.compare.*;
14 import org.eclipse.compare.contentmergeviewer.IMergeViewerContentProvider;
15 import org.eclipse.compare.structuremergeviewer.*;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.swt.graphics.Image;
18
19 /**
20  * Adapts any <code>ContentMergeViewer</code> to work on an <code>ICompareInput</code>
21  * e.g. a <code>DiffNode</code>.
22  */

23 public class MergeViewerContentProvider implements IMergeViewerContentProvider {
24     
25     public static final char ANCESTOR_CONTRIBUTOR = 'A';
26     public static final char RIGHT_CONTRIBUTOR = 'R';
27     public static final char LEFT_CONTRIBUTOR = 'L';
28     
29     private CompareConfiguration fCompareConfiguration;
30     private String JavaDoc fAncestorError;
31     private String JavaDoc fLeftError;
32     private String JavaDoc fRightError;
33         
34     public MergeViewerContentProvider(CompareConfiguration cc) {
35         fCompareConfiguration= cc;
36     }
37     
38     private boolean hasError() {
39         return fAncestorError != null || fLeftError != null || fRightError != null;
40     }
41     
42     public void dispose() {
43         // empty default implementation
44
}
45     
46     public void inputChanged(Viewer v, Object JavaDoc o1, Object JavaDoc o2) {
47         // we are not interested since we have no state
48
}
49     
50     //---- ancestor
51

52     public void setAncestorError(String JavaDoc errorMessage) {
53         fAncestorError= errorMessage;
54     }
55     
56     public String JavaDoc getAncestorLabel(Object JavaDoc element) {
57         if (fAncestorError != null)
58             return fAncestorError;
59         return fCompareConfiguration.getAncestorLabel(element);
60     }
61     
62     public Image getAncestorImage(Object JavaDoc element) {
63         if (fAncestorError != null)
64             return null;
65         return fCompareConfiguration.getAncestorImage(element);
66     }
67     
68     public Object JavaDoc getAncestorContent(Object JavaDoc element) {
69         if (element instanceof ICompareInput)
70             return ((ICompareInput) element).getAncestor();
71         return null;
72     }
73     
74     public boolean showAncestor(Object JavaDoc element) {
75         if (element instanceof ICompareInput)
76             return true; // fix for #45239: Show ancestor for incoming and outgoing changes
77
//return (((ICompareInput)element).getKind() & Differencer.DIRECTION_MASK) == Differencer.CONFLICTING;
78
return false;
79     }
80
81     //---- left
82

83     public void setLeftError(String JavaDoc errorMessage) {
84         fLeftError= errorMessage;
85     }
86     
87     public String JavaDoc getLeftLabel(Object JavaDoc element) {
88         if (fLeftError != null)
89             return fLeftError;
90         return fCompareConfiguration.getLeftLabel(element);
91     }
92     
93     public Image getLeftImage(Object JavaDoc element) {
94         if (fLeftError != null)
95             return null;
96         return fCompareConfiguration.getLeftImage(element);
97     }
98     
99     public Object JavaDoc getLeftContent(Object JavaDoc element) {
100         if (element instanceof ICompareInput)
101             return ((ICompareInput) element).getLeft();
102         return null;
103     }
104         
105     public boolean isLeftEditable(Object JavaDoc element) {
106         if (hasError())
107             return false;
108         if (element instanceof ICompareInput) {
109             Object JavaDoc left= ((ICompareInput) element).getLeft();
110             if (left == null && element instanceof IDiffElement) {
111                 IDiffElement parent= ((IDiffElement)element).getParent();
112                 if (parent instanceof ICompareInput)
113                     left= ((ICompareInput) parent).getLeft();
114             }
115             if (left instanceof IEditableContent)
116                 return ((IEditableContent)left).isEditable();
117         }
118         return false;
119     }
120
121     public void saveLeftContent(Object JavaDoc element, byte[] bytes) {
122         if (element instanceof ICompareInput) {
123             ICompareInput node= (ICompareInput) element;
124             if (bytes != null) {
125                 ITypedElement left= node.getLeft();
126                 // #9869: problem if left is null (because no resource exists yet) nothing is done!
127
if (left == null) {
128                     node.copy(false);
129                     left= node.getLeft();
130                 }
131                 if (left instanceof IEditableContent)
132                     ((IEditableContent)left).setContent(bytes);
133                 if (node instanceof ResourceCompareInput.MyDiffNode)
134                     ((ResourceCompareInput.MyDiffNode)node).fireChange();
135             } else {
136                 node.copy(false);
137             }
138         }
139     }
140     
141     //---- right
142

143     public void setRightError(String JavaDoc errorMessage) {
144         fRightError= errorMessage;
145     }
146     
147     public String JavaDoc getRightLabel(Object JavaDoc element) {
148         if (fRightError != null)
149             return fRightError;
150         return fCompareConfiguration.getRightLabel(element);
151     }
152     
153     public Image getRightImage(Object JavaDoc element) {
154         if (fRightError != null)
155             return null;
156         return fCompareConfiguration.getRightImage(element);
157     }
158     
159     public Object JavaDoc getRightContent(Object JavaDoc element) {
160         if (element instanceof ICompareInput)
161             return ((ICompareInput) element).getRight();
162         return null;
163     }
164     
165     public boolean isRightEditable(Object JavaDoc element) {
166         if (hasError())
167             return false;
168         if (element instanceof ICompareInput) {
169             Object JavaDoc right= ((ICompareInput) element).getRight();
170             if (right == null && element instanceof IDiffElement) {
171                 IDiffContainer parent= ((IDiffElement)element).getParent();
172                 if (parent instanceof ICompareInput)
173                     right= ((ICompareInput) parent).getRight();
174             }
175             if (right instanceof IEditableContent)
176                 return ((IEditableContent)right).isEditable();
177         }
178         return false;
179     }
180     
181     public void saveRightContent(Object JavaDoc element, byte[] bytes) {
182         if (element instanceof ICompareInput) {
183             ICompareInput node= (ICompareInput) element;
184             if (bytes != null) {
185                 ITypedElement right= node.getRight();
186                 // #9869: problem if right is null (because no resource exists yet) nothing is done!
187
if (right == null) {
188                     node.copy(true);
189                     right= node.getRight();
190                 }
191                 if (right instanceof IEditableContent)
192                     ((IEditableContent)right).setContent(bytes);
193                 if (node instanceof ResourceCompareInput.MyDiffNode)
194                     ((ResourceCompareInput.MyDiffNode)node).fireChange();
195             } else {
196                 node.copy(true);
197             }
198         }
199     }
200 }
201
202
Popular Tags