KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > AbstractCompareInput


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.mapping;
12
13 import org.eclipse.compare.ITypedElement;
14 import org.eclipse.compare.structuremergeviewer.ICompareInput;
15 import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.swt.graphics.Image;
18
19 /**
20  * An abstract compare input whose purpose is to support change notification
21  * through a {@link CompareInputChangeNotifier}.
22  */

23 public abstract class AbstractCompareInput implements ICompareInput {
24
25     private ITypedElement ancestor;
26     private ITypedElement left;
27     private ITypedElement right;
28     private int kind;
29     private final ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
30     
31     public AbstractCompareInput(int kind,
32             ITypedElement ancestor,
33             ITypedElement left,
34             ITypedElement right) {
35                 this.kind = kind;
36                 this.ancestor = ancestor;
37                 this.left = left;
38                 this.right = right;
39     }
40
41     /* (non-Javadoc)
42      * @see org.eclipse.compare.structuremergeviewer.DiffNode#addCompareInputChangeListener(org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener)
43      */

44     public void addCompareInputChangeListener(
45             ICompareInputChangeListener listener) {
46         if (!containsListener(listener)) {
47             listeners.add(listener);
48             getChangeNotifier().connect(this);
49         }
50     }
51     
52     /* (non-Javadoc)
53      * @see org.eclipse.compare.structuremergeviewer.DiffNode#removeCompareInputChangeListener(org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener)
54      */

55     public void removeCompareInputChangeListener(
56             ICompareInputChangeListener listener) {
57         if (containsListener(listener)) {
58             listeners.remove(listener);
59             getChangeNotifier().disconnect(this);
60         }
61     }
62     
63     /**
64      * Fire a compare input change event.
65      * This method must be called from the UI thread.
66      */

67     protected void fireChange() {
68         if (!listeners.isEmpty()) {
69             Object JavaDoc[] allListeners = listeners.getListeners();
70             for (int i = 0; i < allListeners.length; i++) {
71                 final ICompareInputChangeListener listener = (ICompareInputChangeListener)allListeners[i];
72                 SafeRunner.run(new ISafeRunnable() {
73                     public void run() throws Exception JavaDoc {
74                         listener.compareInputChanged(AbstractCompareInput.this);
75                     }
76                     public void handleException(Throwable JavaDoc exception) {
77                         // Logged by the safe runner
78
}
79                 });
80             }
81         }
82     }
83     
84     private boolean containsListener(ICompareInputChangeListener listener) {
85         if (listeners.isEmpty())
86             return false;
87         Object JavaDoc[] allListeners = listeners.getListeners();
88         for (int i = 0; i < allListeners.length; i++) {
89             Object JavaDoc object = allListeners[i];
90             if (object == listener)
91                 return true;
92         }
93         return false;
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#copy(boolean)
98      */

99     public void copy(boolean leftToRight) {
100         Assert.isTrue(false, "Copy is not support by this type of compare input"); //$NON-NLS-1$
101
}
102
103     /* (non-Javadoc)
104      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#getAncestor()
105      */

106     public ITypedElement getAncestor() {
107         return ancestor;
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#getImage()
112      */

113     public Image getImage() {
114         return getMainElement().getImage();
115     }
116     
117     /**
118      * Return the main non-null element that identifies
119      * this input. By default, the left is returned if non-null.
120      * If the left is null, the right is returned. If both the
121      * left and right are null the ancestor is returned.
122      * @return the main non-null element that identifies
123      * this input
124      */

125     private ITypedElement getMainElement() {
126         if (left != null)
127             return left;
128         if (right != null)
129             return right;
130         return ancestor;
131     }
132
133     /* (non-Javadoc)
134      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#getKind()
135      */

136     public int getKind() {
137         return kind;
138     }
139
140     /**
141      * Set the kind of this compare input
142      * @param kind the new kind
143      */

144     public void setKind(int kind) {
145         this.kind = kind;
146     }
147     
148     /* (non-Javadoc)
149      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#getLeft()
150      */

151     public ITypedElement getLeft() {
152         return left;
153     }
154
155     /* (non-Javadoc)
156      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#getName()
157      */

158     public String JavaDoc getName() {
159         return getMainElement().getName();
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.compare.structuremergeviewer.ICompareInput#getRight()
164      */

165     public ITypedElement getRight() {
166         return right;
167     }
168     
169     /**
170      * Return the change notifier that will call {@link #fireChange()}
171      * when the state of the compare input changes.
172      * @return the change notifier
173      */

174     protected abstract CompareInputChangeNotifier getChangeNotifier();
175
176     /**
177      * Set the ancestor of this compare input.
178      * @param ancestor the ancestor
179      */

180     public void setAncestor(ITypedElement ancestor) {
181         this.ancestor = ancestor;
182     }
183
184     /**
185      * Set the left element of this compare input.
186      * @param left the left element
187      */

188     public void setLeft(ITypedElement left) {
189         this.left = left;
190     }
191
192     /**
193      * Set the right element of this compare input.
194      * @param right the right element
195      */

196     public void setRight(ITypedElement right) {
197         this.right = right;
198     }
199
200     /**
201      * Update the compare input and fire change notification.
202      */

203     public abstract void update();
204
205     /**
206      * Return whether this compare input needs to be updated.
207      * @return whether this compare input needs to be updated
208      */

209     public abstract boolean needsUpdate();
210
211
212 }
213
Popular Tags