Skip to main content
summaryrefslogtreecommitdiffstats
blob: 598833150f72924e035aeee2b39b52580dc5ffd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize;

import org.eclipse.compare.structuremergeviewer.*;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.*;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.ui.synchronize.viewers.*;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * A model element that can be shown in viewers.  
 *  
 * @since 3.0
 */
public abstract class SynchronizeModelElement extends DiffNode implements IAdaptable, ISynchronizeModelElement {

	/*
	 * Internal flags bits for stroing properties in the flags variable
	 */
	private static final int BUSY_FLAG = 1;
	private static final int PROPAGATED_CONFLICT_FLAG = 2;

	// Instance variable containing the flags for this node
	private int flags;
	private ListenerList listeners;
	
	public SynchronizeModelElement(IDiffContainer parent) {
		super(parent, SyncInfo.IN_SYNC);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class adapter) {
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	public synchronized void addPropertyChangeListener(IPropertyChangeListener listener) {
		if (listeners == null) {
			listeners = new ListenerList();
		}
		listeners.add(listener);
	}
	
	public synchronized void removePropertyChangeListener(IPropertyChangeListener listener) {
		if (listeners != null) {
			listeners.remove(listener);
			if (listeners.isEmpty()) {
				listeners = null;
			}
		}
	}
	
	/**
	 * Return whether this node has the given property set.
	 * @param propertyName the flag to test
	 * @return <code>true</code> if the property is set
	 */
	public boolean getProperty(String propertyName) {
		return (getFlags() & getFlag(propertyName)) > 0;
	}
	
	/**
	 * Add the flag to the flags for this node
	 * @param propertyName the flag to add
	 */
	public void setProperty(String propertyName, boolean value) {
		if (value) {
			if (!getProperty(propertyName)) {
				int flag = getFlag(propertyName);
				flags |= flag;
				firePropertyChange(propertyName);
			}
		} else {
			if (getProperty(propertyName)) {
				int flag = getFlag(propertyName);
				flags ^= flag;
				firePropertyChange(propertyName);
			}
		}
	}
	
	public void setPropertyToRoot(String propertyName, boolean value) {
		if (value) {
			addToRoot(propertyName);
		} else {
			removeToRoot(propertyName);
		}
	}
	
	public void fireChanges() {
		fireChange();
	}
	
	public ImageDescriptor getImageDescriptor(Object object) {
		IResource resource = getResource();
		if(resource != null) {
			IWorkbenchAdapter adapter = (IWorkbenchAdapter)((IAdaptable) resource).getAdapter(IWorkbenchAdapter.class);
			return adapter.getImageDescriptor(resource);
		}
		return null;
	}
	
	public abstract IResource getResource();

	private void addToRoot(String flag) {
		setProperty(flag, true);
		SynchronizeModelElement parent = (SynchronizeModelElement)getParent();
		if (parent != null) {
			if (parent.getProperty(flag)) return;
			parent.addToRoot(flag);
		}
	}

	private void firePropertyChange(String propertyName) {
		Object[] allListeners;
		synchronized(this) {
			if (listeners == null) return;
			allListeners = listeners.getListeners();
		}
		boolean set = getProperty(propertyName);
		final PropertyChangeEvent event = new PropertyChangeEvent(this, propertyName, Boolean.valueOf(!set), Boolean.valueOf(set));
		for (int i = 0; i < allListeners.length; i++) {
			Object object = allListeners[i];
			if (object instanceof IPropertyChangeListener) {
				final IPropertyChangeListener listener = (IPropertyChangeListener)object;
				Platform.run(new ISafeRunnable() {
					public void handleException(Throwable exception) {
						// Exceptions logged by the platform
					}
					public void run() throws Exception {
						listener.propertyChange(event);
					}
				});
			}
		}
	}
	
	private int getFlag(String propertyName) {
		if (propertyName == BUSY_PROPERTY) {
			return BUSY_FLAG;
		} else if (propertyName == PROPAGATED_CONFLICT_PROPERTY) {
			return PROPAGATED_CONFLICT_FLAG;
		}
		return 0;
	}
	
	private int getFlags() {
		return flags;
	}
	
	private boolean hasChildWithFlag(String flag) {
		IDiffElement[] childen = getChildren();
		for (int i = 0; i < childen.length; i++) {
			IDiffElement element = childen[i];
			if (((SynchronizeModelElement)element).getProperty(flag)) {
				return true;
			}
		}
		return false;
	}
	
	private void removeToRoot(String flag) {
		setProperty(flag, false);
		SynchronizeModelElement parent = (SynchronizeModelElement)getParent();
		if (parent != null) {
			// If the parent doesn't have the tag, no recalculation is required
			// Also, if the parent still has a child with the tag, no recalculation is needed
			if (parent.getProperty(flag) && !parent.hasChildWithFlag(flag)) {
				// The parent no longer has the flag so propogate the reclaculation
				parent.removeToRoot(flag);
			}
		}
	}
}

Back to the top