1 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 28 public abstract class SynchronizeModelElement extends DiffNode implements IAdaptable, ISynchronizeModelElement { 29 30 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 private int flags; 40 private ListenerList listeners; 41 42 private SynchronizeModelElement parent; 45 46 public SynchronizeModelElement(IDiffContainer parent) { 47 super(parent, SyncInfo.IN_SYNC); 48 internalSetParent(parent); 49 } 50 51 54 public Object getAdapter(Class 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 77 public void setParent(IDiffContainer parent) { 78 super.setParent(parent); 79 internalSetParent(parent); 80 } 81 82 87 public boolean getProperty(String propertyName) { 88 return (getFlags() & getFlag(propertyName)) > 0; 89 } 90 91 95 public void setProperty(String 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 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 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 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 propertyName) { 143 Object [] 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 object = allListeners[i]; 152 if (object instanceof IPropertyChangeListener) { 153 final IPropertyChangeListener listener = (IPropertyChangeListener)object; 154 Platform.run(new ISafeRunnable() { 155 public void handleException(Throwable exception) { 156 } 158 public void run() throws Exception { 159 listener.propertyChange(event); 160 } 161 }); 162 } 163 } 164 } 165 166 private int getFlag(String 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 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 flag) { 195 boolean hasProperty = getProperty(flag); 196 if(hasProperty) { 197 setProperty(flag, false); 198 if (parent != null) { 199 if (parent.getProperty(flag) && !parent.hasChildWithFlag(flag)) { 202 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 221 public boolean equals(Object object) { 222 return this==object; 223 } 224 225 228 public int hashCode() { 229 return getName().hashCode(); 232 } 233 } 234 | Popular Tags |