KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > SynchronizeModelElement


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.team.internal.ui.synchronize;
12
13 import org.eclipse.compare.structuremergeviewer.*;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.team.core.synchronize.SyncInfo;
20 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
21 import org.eclipse.ui.model.IWorkbenchAdapter;
22
23 /**
24  * A model element that can be shown in viewers.
25  *
26  * @since 3.0
27  */

28 public abstract class SynchronizeModelElement extends DiffNode implements IAdaptable, ISynchronizeModelElement {
29
30     /*
31      * Internal flags bits for storing properties in the flags variable
32      */

33     private static final int BUSY_FLAG = 0x01;
34     private static final int PROPAGATED_CONFLICT_FLAG = 0x02;
35     private static final int PROPAGATED_ERROR_FLAG = 0x04;
36     private static final int PROPAGATED_WARNING_FLAG =0x08;
37
38     // Instance variable containing the flags for this node
39
private int flags;
40     private ListenerList listeners;
41     
42     // Parent is required to ensure that busy (and other) state is cleared.
43
// This is needed as DiffContainer#remove() will null the parent
44
private SynchronizeModelElement parent;
45     
46     public SynchronizeModelElement(IDiffContainer parent) {
47         super(parent, SyncInfo.IN_SYNC);
48         internalSetParent(parent);
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
53      */

54     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
55         return Platform.getAdapterManager().getAdapter(this, adapter);
56     }
57
58     public synchronized void addPropertyChangeListener(IPropertyChangeListener listener) {
59         if (listeners == null) {
60             listeners = new ListenerList(ListenerList.IDENTITY);
61         }
62         listeners.add(listener);
63     }
64     
65     public synchronized void removePropertyChangeListener(IPropertyChangeListener listener) {
66         if (listeners != null) {
67             listeners.remove(listener);
68             if (listeners.isEmpty()) {
69                 listeners = null;
70             }
71         }
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.compare.structuremergeviewer.IDiffElement#setParent(org.eclipse.compare.structuremergeviewer.IDiffContainer)
76      */

77     public void setParent(IDiffContainer parent) {
78         super.setParent(parent);
79         internalSetParent(parent);
80     }
81     
82     /**
83      * Return whether this node has the given property set.
84      * @param propertyName the flag to test
85      * @return <code>true</code> if the property is set
86      */

87     public boolean getProperty(String JavaDoc propertyName) {
88         return (getFlags() & getFlag(propertyName)) > 0;
89     }
90     
91     /**
92      * Add the flag to the flags for this node
93      * @param propertyName the flag to add
94      */

95     public void setProperty(String JavaDoc propertyName, boolean value) {
96         if (value) {
97             if (!getProperty(propertyName)) {
98                 int flag = getFlag(propertyName);
99                 flags |= flag;
100                 firePropertyChange(propertyName);
101             }
102         } else {
103             if (getProperty(propertyName)) {
104                 int flag = getFlag(propertyName);
105                 flags ^= flag;
106                 firePropertyChange(propertyName);
107             }
108         }
109     }
110     
111     public void setPropertyToRoot(String JavaDoc propertyName, boolean value) {
112         if (value) {
113             addToRoot(propertyName);
114         } else {
115             removeToRoot(propertyName);
116         }
117     }
118     
119     public void fireChanges() {
120         fireChange();
121     }
122     
123     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
124         IResource resource = getResource();
125         if(resource != null) {
126             IWorkbenchAdapter adapter = (IWorkbenchAdapter)((IAdaptable) resource).getAdapter(IWorkbenchAdapter.class);
127             return adapter.getImageDescriptor(resource);
128         }
129         return null;
130     }
131     
132     public abstract IResource getResource();
133
134     private void addToRoot(String JavaDoc flag) {
135         setProperty(flag, true);
136         if (parent != null) {
137             if (parent.getProperty(flag)) return;
138             parent.addToRoot(flag);
139         }
140     }
141
142     private void firePropertyChange(String JavaDoc propertyName) {
143         Object JavaDoc[] allListeners;
144         synchronized(this) {
145             if (listeners == null) return;
146             allListeners = listeners.getListeners();
147         }
148         boolean set = getProperty(propertyName);
149         final PropertyChangeEvent event = new PropertyChangeEvent(this, propertyName, Boolean.valueOf(!set), Boolean.valueOf(set));
150         for (int i = 0; i < allListeners.length; i++) {
151             Object JavaDoc object = allListeners[i];
152             if (object instanceof IPropertyChangeListener) {
153                 final IPropertyChangeListener listener = (IPropertyChangeListener)object;
154                 Platform.run(new ISafeRunnable() {
155                     public void handleException(Throwable JavaDoc exception) {
156                         // Exceptions logged by the platform
157
}
158                     public void run() throws Exception JavaDoc {
159                         listener.propertyChange(event);
160                     }
161                 });
162             }
163         }
164     }
165     
166     private int getFlag(String JavaDoc propertyName) {
167         if (propertyName == BUSY_PROPERTY) {
168             return BUSY_FLAG;
169         } else if (propertyName == PROPAGATED_CONFLICT_PROPERTY) {
170             return PROPAGATED_CONFLICT_FLAG;
171         } else if(propertyName == PROPAGATED_ERROR_MARKER_PROPERTY) {
172             return PROPAGATED_ERROR_FLAG;
173         } else if(propertyName == PROPAGATED_WARNING_MARKER_PROPERTY) {
174             return PROPAGATED_WARNING_FLAG;
175         }
176         return 0;
177     }
178     
179     private int getFlags() {
180         return flags;
181     }
182     
183     private boolean hasChildWithFlag(String JavaDoc flag) {
184         IDiffElement[] childen = getChildren();
185         for (int i = 0; i < childen.length; i++) {
186             IDiffElement element = childen[i];
187             if (((SynchronizeModelElement)element).getProperty(flag)) {
188                 return true;
189             }
190         }
191         return false;
192     }
193     
194     private void removeToRoot(String JavaDoc flag) {
195         boolean hasProperty = getProperty(flag);
196         if(hasProperty) {
197             setProperty(flag, false);
198             if (parent != null) {
199                 // If the parent doesn't have the tag, no recalculation is required
200
// Also, if the parent still has a child with the tag, no recalculation is needed
201
if (parent.getProperty(flag) && !parent.hasChildWithFlag(flag)) {
202                     // The parent no longer has the flag so propogate the recalculation
203
parent.removeToRoot(flag);
204                 }
205             }
206         }
207     }
208     
209     private void internalSetParent(IDiffContainer parent) {
210         if (parent != null && parent instanceof SynchronizeModelElement) {
211             this.parent = (SynchronizeModelElement)parent;
212         }
213     }
214     
215     /**
216      * Synchronize model elements are not copied so use identity as the
217      * equality check.
218      * @param object The object to test
219      * @return true if the objects are identical
220      */

221     public boolean equals(Object JavaDoc object) {
222         return this==object;
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.compare.structuremergeviewer.DiffNode#hashCode()
227      */

228     public int hashCode() {
229         // Use the name to get the hashCode to ensure that we can find equal elements.
230
// (The inherited hashCode uses the path which can change when items are removed)
231
return getName().hashCode();
232     }
233 }
234
Popular Tags