1 /******************************************************************************* 2 * Copyright (c) 2005, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.ui.tests.model; 15 16 import java.io.ByteArrayInputStream; 17 import java.io.InputStream; 18 19 import org.eclipse.team.core.diff.IDiff; 20 import org.eclipse.team.core.diff.provider.ThreeWayDiff; 21 import org.eclipse.team.core.history.IFileRevision; 22 import org.eclipse.team.core.history.provider.FileRevision; 23 import org.eclipse.team.core.mapping.ISynchronizationContext; 24 import org.eclipse.team.core.mapping.provider.ResourceDiff; 25 import org.eclipse.team.core.mapping.provider.ResourceDiffTree; 26 import org.eclipse.team.core.mapping.provider.SynchronizationContext; 27 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager; 28 import org.eclipse.team.ui.mapping.ITeamContentProviderManager; 29 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.widgets.Composite; 32 33 import org.eclipse.core.runtime.CoreException; 34 import org.eclipse.core.runtime.IPath; 35 import org.eclipse.core.runtime.IProgressMonitor; 36 import org.eclipse.core.runtime.NullProgressMonitor; 37 38 import org.eclipse.core.resources.IProject; 39 import org.eclipse.core.resources.IResource; 40 import org.eclipse.core.resources.IStorage; 41 import org.eclipse.core.resources.mapping.ResourceMapping; 42 import org.eclipse.core.resources.mapping.ResourceMappingContext; 43 import org.eclipse.core.resources.mapping.ResourceTraversal; 44 45 import org.eclipse.jface.viewers.ITreeContentProvider; 46 47 import org.eclipse.ui.navigator.CommonViewer; 48 import org.eclipse.ui.navigator.INavigatorContentExtension; 49 import org.eclipse.ui.navigator.INavigatorContentServiceListener; 50 import org.eclipse.ui.part.ViewPart; 51 52 import org.eclipse.jdt.core.IJavaProject; 53 54 public class MockPluginView extends ViewPart implements INavigatorContentServiceListener { 55 56 private static final String VIEWER_ID = "org.eclipse.jdt.tests.ui.model.mockViewer"; 57 private static final String JAVA_CONTENT_PROVIDER_ID = "org.eclipse.jdt.ui.javaModelContent"; 58 59 private CommonViewer fViewer; 60 private INavigatorContentExtension fExtension; 61 private ISynchronizationContext fContext; 62 MockPluginView()63 public MockPluginView() { 64 // Nothing to do 65 } 66 67 @Override createPartControl(Composite parent)68 public void createPartControl(Composite parent) { 69 fViewer = new CommonViewer(VIEWER_ID, parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 70 // Only enable the Java model content 71 fViewer.getNavigatorContentService().bindExtensions(new String[] { JAVA_CONTENT_PROVIDER_ID }, true); 72 fViewer.getNavigatorContentService().getActivationService().activateExtensions(new String[] { JAVA_CONTENT_PROVIDER_ID }, true); 73 fViewer.getNavigatorContentService().addListener(this); 74 if (fContext != null) 75 setInput(fContext); 76 } 77 78 @Override setFocus()79 public void setFocus() { 80 // Nothing to do 81 } 82 setInput(Object input)83 public void setInput(Object input) { 84 fViewer.setInput(input); 85 } 86 getContentProvider()87 public ITreeContentProvider getContentProvider() { 88 return (ITreeContentProvider)fViewer.getContentProvider(); 89 } 90 91 @Override onLoad(INavigatorContentExtension anExtension)92 public void onLoad(INavigatorContentExtension anExtension) { 93 this.fExtension = anExtension; 94 setContext(fContext); 95 } 96 setProject(IJavaProject project)97 public void setProject(IJavaProject project) throws CoreException { 98 ISynchronizationContext context = createContext(project); 99 setContext(context); 100 } 101 setContext(ISynchronizationContext context)102 private void setContext(ISynchronizationContext context) { 103 if (fContext != context) { 104 if (fContext != null) { 105 fContext.dispose(); 106 } 107 fContext = context; 108 } 109 if (fExtension != null) { 110 fExtension.getStateModel().setProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_SCOPE, context.getScope()); 111 fExtension.getStateModel().setProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT, context); 112 } 113 setInput(fContext); 114 } 115 116 @Override dispose()117 public void dispose() { 118 super.dispose(); 119 if (fContext != null) { 120 fContext.dispose(); 121 } 122 } 123 createContext(IJavaProject project)124 private ISynchronizationContext createContext(IJavaProject project) throws CoreException { 125 ResourceDiffTree tree = new ResourceDiffTree(); 126 SynchronizationScopeManager manager = new SynchronizationScopeManager("Java Model Tests", 127 getResourceMappings(project), 128 ResourceMappingContext.LOCAL_CONTEXT, 129 true); 130 manager.initialize(new NullProgressMonitor()); 131 SynchronizationContext context = new SynchronizationContext(manager, ISynchronizationContext.THREE_WAY, tree) { 132 @Override 133 public void refresh(ResourceTraversal[] traversals, int flags, IProgressMonitor monitor) throws CoreException { 134 // Nothing to do 135 } 136 }; 137 return context; 138 } 139 getResourceMappings(IJavaProject project)140 private ResourceMapping[] getResourceMappings(IJavaProject project) { 141 ResourceMapping mapping = project.getResource().getAdapter(ResourceMapping.class); 142 return new ResourceMapping[] { mapping }; 143 } 144 getResource(IProject project, String path)145 private IResource getResource(IProject project, String path) { 146 if (path.endsWith("/")) 147 return project.getFolder(path); 148 return project.getFile(path); 149 } 150 createResourceDiff(IProject project, String path, int kind)151 private ResourceDiff createResourceDiff(IProject project, String path, int kind) { 152 final IResource resource = getResource(project, path); 153 ResourceDiff diff = new ResourceDiff(resource, kind, 0, new FileRevision() { 154 @Override 155 public String getName() { 156 return resource.getName(); 157 } 158 @Override 159 public IStorage getStorage(IProgressMonitor monitor) throws CoreException { 160 return new IStorage() { 161 @Override 162 public <T> T getAdapter(Class<T> adapter) { 163 return null; 164 } 165 @Override 166 public boolean isReadOnly() { 167 return true; 168 } 169 @Override 170 public String getName() { 171 return resource.getName(); 172 } 173 @Override 174 public IPath getFullPath() { 175 return resource.getFullPath(); 176 } 177 @Override 178 public InputStream getContents() throws CoreException { 179 return new ByteArrayInputStream("".getBytes()); 180 } 181 }; 182 } 183 @Override 184 public boolean isPropertyMissing() { 185 return false; 186 } 187 @Override 188 public IFileRevision withAllProperties(IProgressMonitor monitor) throws CoreException { 189 return this; 190 }}, null); 191 return diff; 192 } 193 addOutgoingDeletion(IProject project, String path)194 public void addOutgoingDeletion(IProject project, String path) { 195 IDiff diff = createOutgoingDeletion(project, path); 196 add(diff); 197 } 198 createOutgoingDeletion(IProject project, String path)199 private IDiff createOutgoingDeletion(IProject project, String path) { 200 ResourceDiff diff= createResourceDiff(project, path, IDiff.REMOVE); 201 return new ThreeWayDiff(diff, null); 202 } 203 addIncomingAddition(IProject project, String path)204 public void addIncomingAddition(IProject project, String path){ 205 IDiff diff = createIncomingAddition(project,path); 206 add(diff); 207 } 208 createIncomingAddition(IProject project, String path)209 private IDiff createIncomingAddition(IProject project, String path) { 210 ResourceDiff diff= createResourceDiff(project, path, IDiff.ADD); 211 return new ThreeWayDiff(null, diff); 212 } 213 addOutgoingChange(IProject project, String path)214 public void addOutgoingChange(IProject project, String path){ 215 IDiff diff= createOutgoingChange(project,path); 216 add(diff); 217 } 218 createOutgoingChange(IProject project, String path)219 private IDiff createOutgoingChange(IProject project, String path) { 220 ResourceDiff diff= createResourceDiff(project, path, IDiff.CHANGE); 221 return new ThreeWayDiff(diff, null); 222 } 223 add(IDiff diff)224 private void add(IDiff diff) { 225 ((ResourceDiffTree)fContext.getDiffTree()).add(diff); 226 } 227 228 } 229