Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2012 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.views;
  
 import static org.eclipse.jface.resource.JFaceResources.*;
 import static org.eclipse.xtext.ui.util.DisplayRunHelper.*;
 
 
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.jface.resource.JFaceResources;
 import  org.eclipse.jface.text.Document;
 import  org.eclipse.jface.text.IDocument;
 import  org.eclipse.jface.text.ITextSelection;
 import  org.eclipse.jface.text.TextSelection;
 import  org.eclipse.jface.text.source.AnnotationModel;
 import  org.eclipse.jface.text.source.SourceViewer;
 import  org.eclipse.jface.util.IPropertyChangeListener;
 import  org.eclipse.jface.util.PropertyChangeEvent;
 import  org.eclipse.jface.viewers.ISelection;
 import  org.eclipse.jface.viewers.ISelectionProvider;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.custom.StyledText;
 import  org.eclipse.swt.graphics.Color;
 import  org.eclipse.swt.graphics.Font;
 import  org.eclipse.swt.graphics.Point;
 import  org.eclipse.swt.graphics.RGB;
 import  org.eclipse.swt.widgets.Composite;
 import  org.eclipse.swt.widgets.Control;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.swt.widgets.Shell;
 import  org.eclipse.ui.IPartListener2;
 import  org.eclipse.ui.ISelectionListener;
 import  org.eclipse.ui.IWorkbenchPart;
 import  org.eclipse.ui.IWorkbenchPartReference;
 import  org.eclipse.ui.part.ViewPart;
 

Author(s):
Michael Clay - Initial contribution and API
Since:
2.3
 
 public abstract class AbstractSourceView extends ViewPart implements IPartListener2, ISelectionListener, IPropertyChangeListener {
 	private String languageName;
 	private int computeCount;
 	private RGB backgroundColorRGB;
 	private Color backgroundColor;
 	private SourceViewer sourceViewer;
 
 	}
 
 	protected void setWorkbenchPartSelection(IWorkbenchPartSelection workbenchPartSelection) {
 		this. = workbenchPartSelection;
 	}
 
 	protected SourceViewer getSourceViewer() {
 		return ;
 	}
 
 	public String getLanguageName() {
 		return ;
 	}
 
 	public void createPartControl(Composite parent) {
 		this. = createSourceViewer(parent);
 		getSite().getWorkbenchWindow().getPartService().addPartListener(this);
 	}
 
 	protected abstract SourceViewer createSourceViewer(Composite parent);
 
 	protected void inititalizeColors() {
 		Display display = getDisplay();
 		if (display == null) {
 			return;
 		}
 		getSourceViewer().getTextWidget().setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
 		getColorRegistry().addListener(this);
 		Color bgColor;
		if ( == null) {
			bgColor = display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);
			 = bgColor.getRGB();
else {
			bgColor = new Color(display);
			 = bgColor;
		}
		getSourceViewer().getTextWidget().setBackground(bgColor);
	}
		return null;
	}
	protected void inititalizeFont() {
		getFontRegistry().addListener(this);
	}
	private void setViewerFont() {
		Font font = getFont(getViewerFontName());
		SourceViewer viewer = getSourceViewer();
		if (viewer.getDocument() != null) {
			Point selection = viewer.getSelectedRange();
			int topIndex = viewer.getTopIndex();
			StyledText styledText = viewer.getTextWidget();
			Control parent = viewer.getControl();
			parent.setRedraw(false);
			styledText.setFont(font);
			viewer.setSelectedRange(selection.x, selection.y);
			viewer.setTopIndex(topIndex);
			if (parent instanceof Composite) {
				Composite composite = (Composite) parent;
				composite.layout(true);
			}
			parent.setRedraw(true);
else {
			StyledText styledText = viewer.getTextWidget();
			styledText.setFont(font);
		}
	}
	protected String getViewerFontName() {
		return JFaceResources.DEFAULT_FONT;
	}
	public void selectionChanged(IWorkbenchPart workbenchPart, ISelection selection) {
		computeAndSetInput(new DefaultWorkbenchPartSelection(workbenchPartselection));
	}
	public void propertyChange(PropertyChangeEvent event) {
		if (getSourceViewer() == null) {
			return;
		}
		if (event.getProperty().equals(getBackgroundColorKey())) {
else if (event.getProperty().equals(getViewerFontName())) {
		}
	}
	public void partVisible(IWorkbenchPartReference ref) {
		if (ref.getId().equals(getSite().getId())) {
			IWorkbenchPart activePart = ref.getPage().getActivePart();
			if (activePart != null) {
				selectionChanged(activePartref.getPage().getSelection());
			}
		}
	}
	public void partHidden(IWorkbenchPartReference ref) {
		if (ref.getId().equals(getSite().getId())) {
		}
	}
	public void partInputChanged(IWorkbenchPartReference ref) {
		if (!ref.getId().equals(getSite().getId())) {
			IWorkbenchPart workbenchPart = ref.getPart(false);
			ISelectionProvider provider = workbenchPart.getSite().getSelectionProvider();
			if (provider == null) {
				return;
			}
			ISelection selection = provider.getSelection();
			if (selection == null || selection.isEmpty()) {
				return;
			}
			computeAndSetInput(new DefaultWorkbenchPartSelection(ref.getPart(false), selection));
		}
	}
	public void partActivated(IWorkbenchPartReference ref) {
	}
	public void partBroughtToTop(IWorkbenchPartReference ref) {
	}
	public void partClosed(IWorkbenchPartReference ref) {
	}
	public void partDeactivated(IWorkbenchPartReference ref) {
	}
	public void partOpened(IWorkbenchPartReference ref) {
	}
	protected void addPostSelectionListener() {
		getSite().getPage().addPostSelectionListener(this);
	}
	protected void removePostSelectionListener() {
		getSite().getPage().removePostSelectionListener(this);
	}
	public void computeAndSetInput(final IWorkbenchPartSelection workbenchPartSelection) {
		computeAndSetInput(workbenchPartSelectionfalse);
	}
	public void computeAndSetInput(final IWorkbenchPartSelection workbenchPartSelectionboolean forceSelection) {
		if (!isValidSelection(workbenchPartSelection)) {
			return;
		}
		internalComputeAndSetInput(workbenchPartSelectionforceSelection);
	}
	protected void internalComputeAndSetInput(final IWorkbenchPartSelection workbenchPartSelectionboolean forceSelection) {
		if (!forceSelection && isIgnored(workbenchPartSelection)) {
			return;
		}
		this. = workbenchPartSelection;
		final int currentCount = ++;
		ThreadFactory threadFactory = Executors.defaultThreadFactory();
		Thread thread = threadFactory.newThread(new Runnable() {
			public void run() {
				if (currentCount != ) {
					return;
				}
				final String input = computeInput(workbenchPartSelection);
				if (input == null) {
					return;
				}
				Display display = getDisplay();
				if (display == null) {
					return;
				}
					public void run() {
						if ( != currentCount || getViewSite().getShell().isDisposed()) {
							return;
						}
						String description = computeDescription(workbenchPartSelection);
						setInput(input);
						selectAndReveal(workbenchPartSelection);
					}
				});
			}
		});
		thread.setDaemon(true);
		thread.start();
	}
	protected boolean isValidSelection(IWorkbenchPartSelection workbenchPartSelection) {
		return !this.equals(workbenchPartSelection.getWorkbenchPart())
				&& workbenchPartSelection.getWorkbenchPart() instanceof XtextEditor
				&& workbenchPartSelection.getSelection() instanceof ITextSelection
				&& ((XtextEditorworkbenchPartSelection.getWorkbenchPart()).getLanguageName().equalsIgnoreCase(
	}
	protected boolean isIgnored(IWorkbenchPartSelection workbenchPartSelection) {
		return workbenchPartSelection.equals(this.);
	}
	protected String computeInput(IWorkbenchPartSelection workbenchPartSelection) {
		return null;
	}
	protected String computeDescription(IWorkbenchPartSelection workbenchPartSelection) {
		return "";
	}
	protected void setInput(String input) {
		IDocument document = createDocument(input);
		getSourceViewer().setDocument(documentcreateAnnotationModel());
	}
	protected AnnotationModel createAnnotationModel() {
		return new AnnotationModel();
	}
	protected IDocument createDocument(String input) {
		IDocument document = new Document(input);
		return document;
	}
	protected void selectAndReveal(IWorkbenchPartSelection workbenchPartSelection) {
		ITextRegion textSelection = computeSelectedText(workbenchPartSelection);
		if (textSelection != null && !..equals(textSelection)) {
			setSelection(textSelectiontrue);
		}
	}
	protected void setSelection(ITextRegion textSelectionboolean reveal) {
		StyledText text = getSourceViewer().getTextWidget();
		if (text.getText().length() >= textSelection.getOffset() + textSelection.getLength()) {
			getSourceViewer().setSelection(new TextSelection(textSelection.getOffset(), textSelection.getLength()),
					reveal);
		}
	}
	protected ITextRegion computeSelectedText(IWorkbenchPartSelection workbenchPartSelection) {
	}
	protected Display getDisplay() {
		Shell shell = getSite().getShell();
		if (shell == null || shell.isDisposed()) {
			return null;
		}
		Display display = shell.getDisplay();
		if (display == null || display.isDisposed()) {
			return null;
		}
		return display;
	}
	public void setFocus() {
		StyledText styledText = getTextWidget();
		if (styledText != null) {
			styledText.setFocus();
		}
	}
	private StyledText getTextWidget() {
		SourceViewer viewer = getSourceViewer();
		if (viewer != null) {
			return viewer.getTextWidget();
		}
		return null;
	}
	public void dispose() {
		super.dispose();
		getSite().getWorkbenchWindow().getPartService().removePartListener(this);
		getColorRegistry().removeListener(this);
		getFontRegistry().removeListener(this);
		if ( != null) {
			.dispose();
		}
		 = null;
	}
	protected IStorage getEditorResource(IWorkbenchPartSelection workbenchPartSelection) {
		IWorkbenchPart workbenchPart = workbenchPartSelection.getWorkbenchPart();
		return (IStorage) (workbenchPart instanceof XtextEditor ? ((XtextEditorworkbenchPart).getResource() : null);
	}
New to GrepCode? Check out our FAQ X