Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.compare;
  
 import java.util.Map;
 
 import  org.eclipse.compare.CompareConfiguration;
 import  org.eclipse.compare.IResourceProvider;
 import  org.eclipse.compare.ISharedDocumentAdapter;
 import  org.eclipse.compare.IStreamContentAccessor;
 import  org.eclipse.compare.ITypedElement;
 import  org.eclipse.compare.contentmergeviewer.IMergeViewerContentProvider;
 import  org.eclipse.compare.contentmergeviewer.TextMergeViewer;
 import  org.eclipse.compare.structuremergeviewer.ICompareInput;
 import  org.eclipse.core.resources.IResource;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.core.runtime.IAdaptable;
 import  org.eclipse.jface.action.IAction;
 import  org.eclipse.jface.text.ITextViewer;
 import  org.eclipse.jface.text.TextViewer;
 import  org.eclipse.jface.text.source.ISourceViewer;
 import  org.eclipse.jface.text.source.SourceViewer;
 import  org.eclipse.jface.text.source.SourceViewerConfiguration;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.DisposeEvent;
 import  org.eclipse.swt.widgets.Composite;
 import  org.eclipse.ui.IEditorInput;
 import  org.eclipse.ui.IEditorSite;
 import  org.eclipse.ui.IPageListener;
 import  org.eclipse.ui.IStorageEditorInput;
 import  org.eclipse.ui.IWorkbenchPart;
 import  org.eclipse.ui.IWorkbenchPartSite;
 import  org.eclipse.ui.PartInitException;
 import  org.eclipse.ui.actions.PartEventAction;
 import  org.eclipse.ui.texteditor.IDocumentProvider;
 import  org.eclipse.ui.texteditor.ITextEditorActionConstants;
 

Author(s):
Michael Clay - Initial contribution and API
 
 public class DefaultMergeViewer extends TextMergeViewer {
 		public Boolean exec(XtextResource xtextResourcethrows Exception {
 			return xtextResource != null;
 		}
 	};
 	private static final IResourceProvider NULL_RESOURCE_PROVIDER = new IResourceProvider() {
 		public IResource getResource() {
 			return null;
 		}
 	};
 
 	protected IDocumentProvider documentProvider;
 	protected Map<ISourceViewer, DefaultMergeEditorsourceViewerEditorMap;
 
 	private Map<Object, IStreamContentAccessor> inputObjectStreamContentAccessorMap = Maps.newHashMap();
 
 	public DefaultMergeViewer(Composite parentint styles, CompareConfiguration compareConfiguration,
 			IDocumentProvider documentProvider) {
 		super(parentstyles | SWT.LEFT_TO_RIGHT, compareConfiguration);
 		this. = documentProvider;
 	}
 
 	public DefaultMergeViewer(Composite parentint styles, CompareConfiguration compareConfiguration,
 			StreamContentDocumentProvider documentProvider,
 			Provider<XtextSourceViewerConfigurationsourceViewerConfigurationProvider) {
 		super(parentstyles | SWT.LEFT_TO_RIGHT, compareConfiguration);
 		this. = documentProvider;
 		this. = sourceViewerConfigurationProvider;
 	}
 
 	protected void updateContent(Object ancestorObject leftObject right) {
 		ancestor = updateAsDocument(ancestor);
 		left = updateAsDocument(left);
 		right = updateAsDocument(right);
 		super.updateContent(ancestorleftright);
 	}
 
 	protected Object updateAsDocument(Object object) {
 		if (object instanceof IResourceProvider && supportsSharedDocuments(object)) {
			return object;
		}
		if (object instanceof IStreamContentAccessor) {
			try {
				StreamContentAccessorDelegate streamContentAccessorDelegate = new StreamContentAccessorDelegate(
						(IStreamContentAccessor) objectcreateResourceProvider(object));
				.connect(streamContentAccessorDelegate);
				.put(objectstreamContentAccessorDelegate);
				return .getDocument(streamContentAccessorDelegate);
catch (CoreException coreException) {
				throw new WrappedException(coreException);
			}
		}
		return object;
	}
	protected IResourceProvider createResourceProvider(Object object) {
		if (getInput() instanceof ICompareInput && object instanceof ITypedElement) {
			return new CompareInputResourceProvider(((ICompareInput) getInput()), (ITypedElement) object);
		}
	}
	private boolean supportsSharedDocuments(Object object) {
		return object instanceof IAdaptable && ((IAdaptable) object).getAdapter(ISharedDocumentAdapter.class) != null;
	}
	protected void handleDispose(DisposeEvent event) {
		super.handleDispose(event);
	}
	protected void disposeEditors() {
		if ( != null) {
			for (Iterator<DefaultMergeEditoriterator = .values().iterator(); iterator.hasNext();) {
				DefaultMergeEditor mergeEditor = iterator.next();
				mergeEditor.dispose();
			}
		}
	}
	protected void disconnectDocuments() {
		IMergeViewerContentProvider contentProvider = (IMergeViewerContentProvider) getContentProvider();
		Object ancestorContent = contentProvider.getAncestorContent(getInput());
		Object leftContent = contentProvider.getLeftContent(getInput());
		Object rightContent = contentProvider.getRightContent(getInput());
		.disconnect(.get(ancestorContent));
	}
	protected void configureTextViewer(TextViewer textViewer) {
		if (textViewer instanceof SourceViewer) {
			configureSourceViewer(((SourceViewer) textViewer));
		}
	}
	protected void configureSourceViewer(SourceViewer sourceViewer) {
		IEditorInput editorInput = getEditorInput(sourceViewer);
		SourceViewerConfiguration sourceViewerConfiguration = createSourceViewerConfiguration(sourceViewereditorInput);
		sourceViewer.unconfigure();
		sourceViewer.configure(sourceViewerConfiguration);
		if (sourceViewer.getDocument() instanceof IXtextDocument) {
			IXtextDocument xtextDocument = (IXtextDocumentsourceViewer.getDocument();
			if (!xtextDocument.readOnly()) {
				String[] configuredContentTypes = sourceViewerConfiguration.getConfiguredContentTypes(sourceViewer);
				for (String contentType : configuredContentTypes) {
					sourceViewer.removeTextHovers(contentType);
				}
				sourceViewer.setHyperlinkDetectors(nullsourceViewerConfiguration.getHyperlinkStateMask(sourceViewer));
			}
		}
	}
	protected SourceViewerConfiguration createSourceViewerConfiguration(SourceViewer sourceViewer,
			IEditorInput editorInput) {
		SourceViewerConfiguration sourceViewerConfiguration = null;
		if (editorInput != null && getEditor(sourceViewer) != null) {
			DefaultMergeEditor mergeEditor = getEditor(sourceViewer);
			sourceViewerConfiguration = mergeEditor.getXtextSourceViewerConfiguration();
			try {
				mergeEditor.init((IEditorSite) mergeEditor.getSite(), editorInput);
				mergeEditor.createActions();
catch (PartInitException partInitException) {
				throw new WrappedException(partInitException);
			}
else {
			sourceViewerConfiguration = .get();
		}
		return sourceViewerConfiguration;
	}
	protected void setEditable(ISourceViewer sourceViewerboolean state) {
		super.setEditable(sourceViewerstate);
		DefaultMergeEditor mergeEditor = getEditor(sourceViewer);
		if (mergeEditor != null) {
			mergeEditor.setEditable(state);
		}
	}
	protected void setActionsActivated(SourceViewer sourceViewerboolean state) {
		DefaultMergeEditor mergeEditor = getEditor(sourceViewer);
		if (mergeEditor != null) {
			mergeEditor.setActionsActivated(state);
			IAction saveAction = mergeEditor.getAction(ITextEditorActionConstants.SAVE);
			if (saveAction instanceof IPageListener) {
				PartEventAction partEventAction = (PartEventAction) saveAction;
				IWorkbenchPart compareEditorPart = getCompareConfiguration().getContainer().getWorkbenchPart();
				if (state) {
					partEventAction.partActivated(compareEditorPart);
else {
					partEventAction.partDeactivated(compareEditorPart);
				}
			}
		}
	}
	protected IEditorInput getEditorInput(ISourceViewer sourceViewer) {
		IEditorInput editorInput = super.getEditorInput(sourceViewer);
		if (editorInput == null) {
			return null;
		}
		if (getSite() == null) {
			return null;
		}
		if (!(editorInput instanceof IStorageEditorInput)) {
			return null;
		}
		return editorInput;
	}
	protected SourceViewer createSourceViewer(Composite parentint textOrientation) {
		if (getSite() != null) {
			if ( == null) {
			}
			mergeEditor.setTextOrientation(textOrientation);
			mergeEditor.setInternalSite(getSite());
			mergeEditor.createPartControl(parent);
			SourceViewer internalSourceViewer = (SourceViewer) mergeEditor.getInternalSourceViewer();
			.put(internalSourceViewermergeEditor);
			return internalSourceViewer;
		}
		return super.createSourceViewer(parenttextOrientation);
	}
		@SuppressWarnings("unchecked")
		DefaultMergeEditor mergeEditor = mergeEditorProvider.get();
		return mergeEditor;
	}
	protected IWorkbenchPartSite getSite() {
		IWorkbenchPart workbenchPart = getCompareConfiguration().getContainer().getWorkbenchPart();
		return workbenchPart != null ? workbenchPart.getSite() : null;
	}
	protected boolean isEditorBacked(ITextViewer textViewer) {
		return getSite() != null;
	}
	protected DefaultMergeEditor getEditor(ISourceViewer sourceViewer) {
		DefaultMergeEditor mergeEditor = null;
		if ( != null) {
			mergeEditor = .get(sourceViewer);
		}
		return mergeEditor;
	}
New to GrepCode? Check out our FAQ X