1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.mapping.ResourceMapping; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.ListenerList; 20 import org.eclipse.jface.util.*; 21 import org.eclipse.team.internal.ui.TeamUIPlugin; 22 import org.eclipse.ui.IMemento; 23 24 33 public abstract class AbstractSynchronizeScope implements ISynchronizeScope { 34 35 38 private static final String CTX_SUBSCRIBER_SCOPE_TYPE = TeamUIPlugin.ID + ".SCOPE_TYPE"; 40 43 private ListenerList listeners = new ListenerList(ListenerList.IDENTITY); 44 45 51 protected static void saveScope(ISynchronizeScope scope, IMemento settings) { 52 settings.putString(CTX_SUBSCRIBER_SCOPE_TYPE, getType(scope)); 53 ((AbstractSynchronizeScope)scope).saveState(settings); 54 } 55 56 62 protected static ISynchronizeScope createScope(IMemento settings) { 63 String type = settings.getString(CTX_SUBSCRIBER_SCOPE_TYPE); 64 if (type == null) { 65 return new WorkspaceScope(); 66 } 67 if (type.equals("ResourceScope")) { return new ResourceScope(settings); 69 } 70 if (type.equals("WorkingSetScope")) { return new WorkingSetScope(settings); 72 } 73 return new WorkspaceScope(); 74 } 75 76 private static String getType(ISynchronizeScope scope) { 77 String name = scope.getClass().getName(); 78 int lastDot = name.lastIndexOf("."); if (lastDot == -1) { 80 return name; 81 } 82 return name.substring(lastDot + 1); 83 } 84 85 88 protected AbstractSynchronizeScope() { 89 } 90 91 94 protected AbstractSynchronizeScope(IMemento memento) { 95 init(memento); 96 } 97 98 101 public void addPropertyChangeListener(IPropertyChangeListener listener) { 102 synchronized(listeners) { 103 listeners.add(listener); 104 } 105 } 106 107 110 public void removePropertyChangeListener(IPropertyChangeListener listener) { 111 synchronized(listeners) { 112 listeners.remove(listeners); 113 } 114 } 115 116 119 public void dispose() { 120 } 122 123 128 protected void firePropertyChangedEvent(final PropertyChangeEvent event) { 129 Object [] allListeners; 130 synchronized(listeners) { 131 allListeners = listeners.getListeners(); 132 } 133 for (int i = 0; i < allListeners.length; i++) { 134 final IPropertyChangeListener listener = (IPropertyChangeListener)allListeners[i]; 135 SafeRunner.run(new SafeRunnable() { 136 public void run() throws Exception { 137 listener.propertyChange(event); 138 } 139 }); 140 } 141 } 142 146 protected void fireRootsChanges() { 147 firePropertyChangedEvent(new PropertyChangeEvent(this, ROOTS, new IResource[0], getRoots())); 148 } 149 150 156 public void saveState(IMemento memento) { 157 } 159 160 165 protected void init(IMemento memento) { 166 } 168 169 177 public boolean contains(IResource resource) { 178 IResource[] roots = getRoots(); 179 IPath resourcePath = resource.getFullPath(); 180 for (int i = 0; i < roots.length; i++) { 181 IResource root = roots[i]; 182 if (root.getFullPath().isPrefixOf(resourcePath)) { 183 return true; 184 } 185 } 186 return false; 187 } 188 189 196 public ResourceMapping[] getMappings() { 197 List result = new ArrayList (); 198 IResource[] roots = getRoots(); 199 for (int i = 0; i < roots.length; i++) { 200 IResource resource = roots[i]; 201 result.add(resource.getAdapter(ResourceMapping.class)); 202 } 203 return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]); 204 } 205 } 206 | Popular Tags |