001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.model;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.InputStream;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.core.runtime.NullProgressMonitor;
019:
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.IStorage;
023: import org.eclipse.core.resources.mapping.ResourceMapping;
024: import org.eclipse.core.resources.mapping.ResourceMappingContext;
025: import org.eclipse.core.resources.mapping.ResourceTraversal;
026:
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.widgets.Composite;
029:
030: import org.eclipse.jface.viewers.ITreeContentProvider;
031:
032: import org.eclipse.ui.navigator.CommonViewer;
033: import org.eclipse.ui.navigator.INavigatorContentExtension;
034: import org.eclipse.ui.navigator.INavigatorContentServiceListener;
035: import org.eclipse.ui.part.ViewPart;
036:
037: import org.eclipse.team.core.diff.IDiff;
038: import org.eclipse.team.core.diff.provider.ThreeWayDiff;
039: import org.eclipse.team.core.history.IFileRevision;
040: import org.eclipse.team.core.history.provider.FileRevision;
041: import org.eclipse.team.core.mapping.ISynchronizationContext;
042: import org.eclipse.team.core.mapping.provider.ResourceDiff;
043: import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
044: import org.eclipse.team.core.mapping.provider.SynchronizationContext;
045: import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager;
046:
047: import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
048:
049: import org.eclipse.jdt.core.IJavaProject;
050:
051: public class MockPluginView extends ViewPart implements
052: INavigatorContentServiceListener {
053:
054: private static final String VIEWER_ID = "org.eclipse.jdt.tests.ui.model.mockViewer";
055: private static final String JAVA_CONTENT_PROVIDER_ID = "org.eclipse.jdt.ui.javaModelContent";
056:
057: private CommonViewer fViewer;
058: private INavigatorContentExtension fExtension;
059: private ISynchronizationContext fContext;
060:
061: public MockPluginView() {
062: // Nothing to do
063: }
064:
065: public void createPartControl(Composite parent) {
066: fViewer = new CommonViewer(VIEWER_ID, parent, SWT.MULTI
067: | SWT.H_SCROLL | SWT.V_SCROLL);
068: // Only enable the Java model content
069: fViewer.getNavigatorContentService().bindExtensions(
070: new String[] { JAVA_CONTENT_PROVIDER_ID }, true);
071: fViewer
072: .getNavigatorContentService()
073: .getActivationService()
074: .activateExtensions(
075: new String[] { JAVA_CONTENT_PROVIDER_ID }, true);
076: fViewer.getNavigatorContentService().addListener(this );
077: if (fContext != null)
078: setInput(fContext);
079: }
080:
081: public void setFocus() {
082: // Nothing to do
083: }
084:
085: public void setInput(Object input) {
086: fViewer.setInput(input);
087: }
088:
089: public ITreeContentProvider getContentProvider() {
090: return (ITreeContentProvider) fViewer.getContentProvider();
091: }
092:
093: public void onLoad(INavigatorContentExtension anExtension) {
094: this .fExtension = anExtension;
095: setContext(fContext);
096: }
097:
098: public void setProject(IJavaProject project) throws CoreException {
099: ISynchronizationContext context = createContext(project);
100: setContext(context);
101: }
102:
103: private void setContext(ISynchronizationContext context) {
104: if (fContext != context) {
105: if (fContext != null) {
106: fContext.dispose();
107: }
108: fContext = context;
109: }
110: if (fExtension != null) {
111: fExtension
112: .getStateModel()
113: .setProperty(
114: ITeamContentProviderManager.P_SYNCHRONIZATION_SCOPE,
115: context.getScope());
116: fExtension
117: .getStateModel()
118: .setProperty(
119: ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT,
120: context);
121: }
122: setInput(fContext);
123: }
124:
125: public void dispose() {
126: super .dispose();
127: if (fContext != null) {
128: fContext.dispose();
129: }
130: }
131:
132: private ISynchronizationContext createContext(IJavaProject project)
133: throws CoreException {
134: ResourceDiffTree tree = new ResourceDiffTree();
135: SynchronizationScopeManager manager = new SynchronizationScopeManager(
136: "Java Model Tests", getResourceMappings(project),
137: ResourceMappingContext.LOCAL_CONTEXT, true);
138: manager.initialize(new NullProgressMonitor());
139: SynchronizationContext context = new SynchronizationContext(
140: manager, ISynchronizationContext.THREE_WAY, tree) {
141: public void refresh(ResourceTraversal[] traversals,
142: int flags, IProgressMonitor monitor)
143: throws CoreException {
144: // Nothing to do
145: }
146: };
147: return context;
148: }
149:
150: private ResourceMapping[] getResourceMappings(IJavaProject project) {
151: ResourceMapping mapping = (ResourceMapping) project
152: .getResource().getAdapter(ResourceMapping.class);
153: return new ResourceMapping[] { mapping };
154: }
155:
156: private IResource getResource(IProject project, String path) {
157: if (path.endsWith("/"))
158: return project.getFolder(path);
159: return project.getFile(path);
160: }
161:
162: public IDiff createOutgoingDeletion(IProject project, String path) {
163: final IResource resource = getResource(project, path);
164: ResourceDiff diff = new ResourceDiff(resource, IDiff.REMOVE, 0,
165: new FileRevision() {
166: public String getName() {
167: return resource.getName();
168: }
169:
170: public IStorage getStorage(IProgressMonitor monitor)
171: throws CoreException {
172: return new IStorage() {
173: public Object getAdapter(Class adapter) {
174: return null;
175: }
176:
177: public boolean isReadOnly() {
178: return true;
179: }
180:
181: public String getName() {
182: return resource.getName();
183: }
184:
185: public IPath getFullPath() {
186: return resource.getFullPath();
187: }
188:
189: public InputStream getContents()
190: throws CoreException {
191: return new ByteArrayInputStream(""
192: .getBytes());
193: }
194: };
195: }
196:
197: public boolean isPropertyMissing() {
198: return false;
199: }
200:
201: public IFileRevision withAllProperties(
202: IProgressMonitor monitor)
203: throws CoreException {
204: return this ;
205: }
206: }, null);
207: return new ThreeWayDiff(diff, null);
208: }
209:
210: public void addOutgoingDeletion(IProject project, String path) {
211: IDiff diff = createOutgoingDeletion(project, path);
212: add(diff);
213: }
214:
215: public void addIncomingAddition(IProject project, String path) {
216: IDiff diff = createIncomingAddition(project, path);
217: add(diff);
218: }
219:
220: private IDiff createIncomingAddition(IProject project, String path) {
221: final IResource resource = getResource(project, path);
222: ResourceDiff diff = new ResourceDiff(resource, IDiff.ADD, 0,
223: new FileRevision() {
224: public String getName() {
225: return resource.getName();
226: }
227:
228: public IStorage getStorage(IProgressMonitor monitor)
229: throws CoreException {
230: return new IStorage() {
231: public Object getAdapter(Class adapter) {
232: return null;
233: }
234:
235: public boolean isReadOnly() {
236: return true;
237: }
238:
239: public String getName() {
240: return resource.getName();
241: }
242:
243: public IPath getFullPath() {
244: return resource.getFullPath();
245: }
246:
247: public InputStream getContents()
248: throws CoreException {
249: return new ByteArrayInputStream(""
250: .getBytes());
251: }
252: };
253: }
254:
255: public boolean isPropertyMissing() {
256: return false;
257: }
258:
259: public IFileRevision withAllProperties(
260: IProgressMonitor monitor)
261: throws CoreException {
262: return this ;
263: }
264: }, null);
265: return new ThreeWayDiff(null, diff);
266: }
267:
268: private void add(IDiff diff) {
269: ((ResourceDiffTree) fContext.getDiffTree()).add(diff);
270: }
271:
272: }
|