1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import org.eclipse.compare.ITypedElement; 14 import org.eclipse.compare.structuremergeviewer.*; 15 import org.eclipse.core.resources.IEncodedStorage; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.team.core.TeamException; 19 import org.eclipse.team.core.synchronize.SyncInfo; 20 import org.eclipse.team.core.variants.IResourceVariant; 21 import org.eclipse.team.internal.ui.Policy; 22 import org.eclipse.team.internal.ui.TeamUIPlugin; 23 24 37 public class SyncInfoModelElement extends SynchronizeModelElement { 38 39 private ITypedElement ancestor; 40 private SyncInfo info; 41 42 48 public SyncInfoModelElement(IDiffContainer parent, SyncInfo info) { 49 super(parent); 50 51 Assert.isNotNull(info); 52 this.info = info; 53 setKind(info.getKind()); 55 setLeft(createLocalTypeElement(info)); 57 setRight(createRemoteTypeElement(info)); 59 setAncestor(createBaseTypeElement(info)); 61 62 fireChange(); 63 } 64 65 71 public void update(SyncInfo info) { 72 this.info = info; 73 setKind(info.getKind()); 75 76 RemoteResourceTypedElement rightEl = (RemoteResourceTypedElement)getRight(); 78 IResourceVariant remote = info.getRemote(); 79 if(rightEl == null && remote != null) { 80 setRight(createRemoteTypeElement(info)); 81 } else if(rightEl != null) { 82 if(remote == null) { 83 setRight(null); 84 } else { 85 setRight(createRemoteTypeElement(info)); 86 } 87 } 88 RemoteResourceTypedElement ancestorEl = (RemoteResourceTypedElement)getAncestor(); 90 IResourceVariant base = info.getBase(); 91 if(ancestorEl == null && base != null) { 92 setAncestor(createBaseTypeElement(info)); 93 } else if(ancestorEl != null) { 94 if(base == null) { 95 setAncestor(null); 96 } else { 97 setAncestor(createBaseTypeElement(info)); 98 } 99 } 100 101 fireChange(); 102 } 103 104 107 public int getKind() { 108 SyncInfo info = getSyncInfo(); 109 if (info != null) { 110 return info.getKind(); 111 } else { 112 return SyncInfo.IN_SYNC; 113 } 114 } 115 116 121 public void setAncestor(ITypedElement ancestor) { 122 this.ancestor = ancestor; 123 } 124 125 128 public ITypedElement getAncestor() { 129 return this.ancestor; 130 } 131 132 135 public String getName() { 136 IResource resource = getResource(); 137 if(resource != null) { 138 return resource.getName(); 139 } else { 140 return super.getName(); 141 } 142 } 143 144 147 public Object getAdapter(Class adapter) { 148 if(adapter == SyncInfo.class) { 149 return getSyncInfo(); 150 } 151 return super.getAdapter(adapter); 152 } 153 154 160 public IResource getResource() { 161 return info.getLocal(); 162 } 163 164 167 public String toString() { 168 return getResource().getFullPath().toString(); 169 } 170 171 175 public void cacheContents(IProgressMonitor monitor) throws TeamException { 176 ITypedElement base = getAncestor(); 177 ITypedElement remote = getRight(); 178 int work = Math.min((remote== null ? 0 : 50) + (base == null ? 0 : 50), 10); 179 monitor.beginTask(null, work); 180 try { 181 if (base != null && base instanceof RemoteResourceTypedElement) { 182 ((RemoteResourceTypedElement)base).cacheContents(Policy.subMonitorFor(monitor, 50)); 183 } 184 if (remote != null && remote instanceof RemoteResourceTypedElement) { 185 ((RemoteResourceTypedElement)remote).cacheContents(Policy.subMonitorFor(monitor, 50)); 186 } 187 } catch (CoreException e) { 188 throw TeamException.asTeamException(e); 189 } finally { 190 monitor.done(); 191 } 192 } 193 194 public SyncInfo getSyncInfo() { 195 return info; 196 } 197 198 202 private static ITypedElement createTypeElement(final IResource resource, final int kind) { 203 if(resource != null) { 204 return new LocalResourceTypedElement(resource); 205 } 206 return null; 207 } 208 209 213 protected static ITypedElement createTypeElement(IResourceVariant remoteResource, String encoding) { 214 return new RemoteResourceTypedElement(remoteResource,encoding); 215 } 216 217 protected static ITypedElement createRemoteTypeElement(SyncInfo info) { 218 if(info != null && info.getRemote() != null) { 219 return createTypeElement(info.getRemote(), getEncoding(info.getLocal())); 220 } 221 return null; 222 } 223 224 private static String getEncoding(IResource local) { 225 if (local instanceof IEncodedStorage) { 226 IEncodedStorage es = (IEncodedStorage) local; 227 try { 228 return es.getCharset(); 229 } catch (CoreException e) { 230 TeamUIPlugin.log(e); 231 } 232 } 233 return null; 234 } 235 236 protected static ITypedElement createLocalTypeElement(SyncInfo info) { 237 if(info != null && info.getLocal() != null) { 238 return createTypeElement(info.getLocal(), info.getKind()); 239 } 240 return null; 241 } 242 243 protected static ITypedElement createBaseTypeElement(SyncInfo info) { 244 if(info != null && info.getBase() != null) { 245 return createTypeElement(info.getBase(), getEncoding(info.getLocal())); 246 } 247 return null; 248 } 249 } 250 | Popular Tags |