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.editor.embedded;
  
 import java.util.List;
 
 import  org.eclipse.core.commands.operations.IOperationHistory;
 import  org.eclipse.core.commands.operations.IUndoContext;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.jface.text.IDocument;
 import  org.eclipse.jface.text.IDocumentPartitioner;
 import  org.eclipse.jface.text.ITextListener;
 import  org.eclipse.jface.text.TextEvent;
 import  org.eclipse.jface.text.contentassist.ContentAssistEvent;
 import  org.eclipse.jface.text.contentassist.ICompletionListener;
 import  org.eclipse.jface.text.contentassist.ICompletionProposal;
 import  org.eclipse.jface.text.source.Annotation;
 import  org.eclipse.jface.text.source.AnnotationRulerColumn;
 import  org.eclipse.jface.text.source.CompositeRuler;
 import  org.eclipse.jface.text.source.IAnnotationAccessExtension;
 import  org.eclipse.jface.text.source.IAnnotationModel;
 import  org.eclipse.jface.text.source.ICharacterPairMatcher;
 import  org.eclipse.jface.text.source.ISharedTextColors;
 import  org.eclipse.jface.text.source.SourceViewer;
 import  org.eclipse.jface.viewers.ISelectionChangedListener;
 import  org.eclipse.jface.viewers.SelectionChangedEvent;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.DisposeEvent;
 import  org.eclipse.swt.events.DisposeListener;
 import  org.eclipse.swt.layout.GridData;
 import  org.eclipse.swt.widgets.Button;
 import  org.eclipse.swt.widgets.Composite;
 import  org.eclipse.swt.widgets.Control;
 import  org.eclipse.text.undo.DocumentUndoManagerRegistry;
 import  org.eclipse.text.undo.IDocumentUndoManager;
 import  org.eclipse.ui.PlatformUI;
 import  org.eclipse.ui.editors.text.EditorsUI;
 import  org.eclipse.ui.texteditor.AnnotationPreference;
 import  org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
 import  org.eclipse.ui.texteditor.ITextEditorActionConstants;
 import  org.eclipse.ui.texteditor.IUpdate;
 import  org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
 import  org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
 
Factory to create embedded editors for arbitrary resources. A typical usage pattern looks like this:
 EmbeddedEditorFactory editorFactory;
 
 EmbeddedEditor editor = editorFactory
    .newEditor(resourceProvider)
    .showErrorAndWarningAnnotations()
    .withParent(parentComposite);
 EmbeddedEditorModelAccess model = editor.createPartialEditor();
 // work with the model 
 

Author(s):
Sebastian Zarnekow - Initial contribution and API
Since:
2.2
 
 public class EmbeddedEditorFactory {
 
 	
 	public static class Builder {

The width of the vertical ruler.
		protected static final int VERTICAL_RULER_WIDTH = 12;
		protected Provider<IDocumentPartitioner> documentPartitionerProvider;
		protected ICharacterPairMatcher characterPairMatcher;
		protected String[] annotationTypes;
//		protected Boolean lineNumbers;
//		protected Boolean folding;
		protected Boolean readonly;
		protected boolean editorBuild;
			return showAnnotations("org.eclipse.xtext.ui.editor.error""org.eclipse.xtext.ui.editor.warning");
		}
		public Builder showAnnotations(String... annotationTypes) {
			if (this. != null)
				throw new IllegalStateException();
			if (annotationTypes == null)
			this. = annotationTypes;
			return this;
		}
//		public Builder showLineNumbers() {
//			if (lineNumbers != null)
//				throw new IllegalStateException();
//			this.lineNumbers = true;
//			return this;
//		}
			if (this. != null)
				throw new IllegalStateException();
			if (issueProcessor == null)
			this. = issueProcessor;
			return this;
		}
//		public Builder enableFolding() {
//			if (folding != null)
//				throw new IllegalStateException();
//			this.folding = true;
//			return this;
//		}
		public Builder readOnly() {
			if ( != null)
				throw new IllegalStateException();
			this. = true;
			return this;
		}
		public Builder withResourceValidator(IResourceValidator resourceValidator) {
			this. = resourceValidator;
			return this;
		}
		public EmbeddedEditor withParent(final Composite parent) {
				throw new IllegalStateException();
			 = true;
//			/*fProjectionSupport =*/installProjectionSupport(this.fSourceViewer);
			final CompositeRuler annotationRuler;
			if ( != null && . != 0) {
				annotationRuler = new CompositeRuler();
else {
				annotationRuler = null;
			}
					parent
					annotationRuler
					null// overviewRuler
					false// showAnnotationOverview 
					SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
			viewer.configure(viewerConfiguration);
			// squiggles for markers and other decorations
			final SourceViewerDecorationSupport viewerDecorationSupport = new SourceViewerDecorationSupport(
					viewer
					null// overviewRuler 
					new DefaultMarkerAnnotationAccess() {
						public int getLayer(Annotation annotation) {
							if (annotation.isMarkedDeleted()) {
								return IAnnotationAccessExtension.DEFAULT_LAYER;
							}
							return super.getLayer(annotation);
						}
					}, 
			MarkerAnnotationPreferences annotationPreferences = new MarkerAnnotationPreferences();
			Iterator<AnnotationPreference> e = Iterators.filter(annotationPreferences.getAnnotationPreferences().iterator(), AnnotationPreference.class);
			while (e.hasNext()) {
				viewerDecorationSupport.setAnnotationPreference(e.next());
			}
			if ( != null) {
				viewerDecorationSupport.setCharacterPairMatcher();
				viewerDecorationSupport.setMatchingCharacterPainterPreferenceKeys(.,
			}
			viewerDecorationSupport.install(this..getPreferenceStore());
			final XtextDocument document = this..get();
			IDocumentPartitioner partitioner = this..get();
			partitioner.connect(document);
			document.setDocumentPartitioner(partitioner);
			final EmbeddedEditorActions actions = initializeActions(viewer);
			parent.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
					viewerDecorationSupport.dispose();
				}
			});
			final EmbeddedEditor result = new EmbeddedEditor(
					documentviewerviewerConfigurationnew Runnable() {
						public void run() {
							afterCreatePartialEditor(viewerdocumentannotationRuleractions);
							.install(viewerConfigurationviewer);
						}
					});
			viewer.getContentAssistantFacade().addCompletionListener(new ICompletionListener() {
				private Button defaultButton;
				public void selectionChanged(ICompletionProposal proposalboolean smartToggle) {
				}
				public void assistSessionStarted(ContentAssistEvent event) {
					 = parent.getShell().getDefaultButton();
					parent.getShell().setDefaultButton(null);
				}
				public void assistSessionEnded(ContentAssistEvent event) {
					parent.getShell().setDefaultButton();
					 = null;
				}
			});
				public void processIssues(List<Issueissues, IProgressMonitor monitor) {
					if (issueProcessor != null) {
						issueProcessor.processIssues(issuesmonitor);
					}
					IAnnotationModel annotationModel = viewer.getAnnotationModel();
					if (annotationModel != null) {
						if (this. == null) {
							this. = new AnnotationIssueProcessor(documentannotationModelnew IssueResolutionProvider() {
								public boolean hasResolutionFor(String issueCode) {
								}
									List<IssueResolutionresult = Lists.transform(resolutionsnew Function<IssueResolutionIssueResolution>() {
										public IssueResolution apply(final IssueResolution input) {
													input.getLabel(), 
													input.getDescription(), 
													input.getImage(), 
															if (uri.trimFragment().equals(document.getResourceURI()))
																return document;
														}
															if (original instanceof IssueModificationContext) {
																return getXtextDocument(uri);
															}
															return original.getXtextDocument();
														}
													}, 
											return result;
										}
									});
									return result;
								}
							});
						}
						if (this. != null) {
						}
					}
				}
			document.setValidationJob(job);
			Control control = viewer.getControl();
			GridData data = new GridData(SWT.FILL, SWT.FILL, truetrue);
			control.setLayoutData(data);
			return result;
		}
		protected void afterCreatePartialEditor(XtextSourceViewer viewerXtextDocument document, CompositeRuler verticalRuler
				final EmbeddedEditorActions actions) {
			if (verticalRuler != null &&  != null && . > 0) {
				AnnotationRulerColumn annotationRulerColumn = new AnnotationRulerColumn(viewer.getAnnotationModel(), new DefaultMarkerAnnotationAccess() {
					public int getLayer(Annotation annotation) {
						if (annotation.isMarkedDeleted()) {
							return IAnnotationAccessExtension.DEFAULT_LAYER;
						}
						return super.getLayer(annotation);
					}
				});
				for(String annotationType)
					annotationRulerColumn.addAnnotationType(annotationType);
				verticalRuler.addDecorator(1, annotationRulerColumn);
			}
			final OperationHistoryListener listener = installUndoRedoSupport(viewerdocumentactions);
			viewer.getControl().addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
				}
			});
			viewer.addTextListener(new ITextListener() {
				public void textChanged(TextEvent event) {
					if (event.getDocumentEvent() != null) {
					}
				}
			});
			viewer.addSelectionChangedListener(new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent event) {
				}
			});
			viewer.getUndoManager().reset();
		}
		protected void updateUndoAction(EmbeddedEditorActions actions) {
			actions.updateAction(ITextEditorActionConstants.UNDO);
		}
			IOperationHistory operationHistory = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory();
			operationHistory.removeOperationHistoryListener(listener);
		}
		protected OperationHistoryListener installUndoRedoSupport(SourceViewer viewer, IDocument documentfinal EmbeddedEditorActions actions) {
			IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
			final IUndoContext context = undoManager.getUndoContext();
			IOperationHistory operationHistory = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory();
			OperationHistoryListener operationHistoryListener = new OperationHistoryListener(contextnew IUpdate() {
				public void update() {
					actions.updateAction(ITextEditorActionConstants.REDO);
					actions.updateAction(ITextEditorActionConstants.UNDO);
				}
			});
			operationHistory.addOperationHistoryListener(operationHistoryListener);
			return operationHistoryListener;
		}
		protected void setResourceProvider(IEditedResourceProvider resourceProvider) {
			this. = resourceProvider;
		}
		protected ISharedTextColors getSharedColors() {
			return EditorsUI.getSharedTextColors();
		}
		protected EmbeddedEditorActions initializeActions(final SourceViewer viewer) {
		}
	}
	public Builder newEditor(IEditedResourceProvider resourceProvider) {
		if (resourceProvider == null)
			throw new IllegalArgumentException("resourceProvider may not be null");
		result.setResourceProvider(resourceProvider);
		return result;
	}
	}
	public void setBuilderProvider(Provider<BuilderbuilderProvider) {
		this. = builderProvider;
	}
New to GrepCode? Check out our FAQ X