/*******************************************************************************
* Copyright (c) 2000, 2006 IBM Corporation 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
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.onpositive.google.scrapbook;
import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.internal.debug.ui.JDIContentAssistPreference;
import org.eclipse.jdt.internal.ui.text.JavaCompositeReconcilingStrategy;
import org.eclipse.jdt.internal.ui.text.JavaReconciler;
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
import org.eclipse.jdt.ui.text.JavaTextTools;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.reconciler.DirtyRegion;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* The source viewer configuration for the Java snippet editor.
*/
@SuppressWarnings("restriction")
public class JavaSnippetViewerConfiguration extends
JavaSourceViewerConfiguration {
private final class ValidationReconcilingStrategy implements
IReconcilingStrategy, IReconcilingStrategyExtension {
private IDocument document;
public void setDocument(IDocument document) {
this.document = document;
}
public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
rec();
}
private void rec() {
CompilerFacade compilerFacade = new CompilerFacade();
CategorizedProblem[] validate = compilerFacade.validate(
document.get(), editor.getJavaProject());
IAnnotationModel annotationModel = editor.getViewer()
.getAnnotationModel();
IAnnotationModelExtension e = (IAnnotationModelExtension) annotationModel;
e.removeAllAnnotations();
if (validate != null) {
for (CategorizedProblem p : validate) {
if (p.isError()){
int length = p.getSourceEnd() - p.getSourceStart()+1;
try{
Position pa = new Position(p.getSourceStart()-compilerFacade.length,length);
annotationModel.addAnnotation(new Annotation(
"org.eclipse.ui.workbench.texteditor.spelling",
false, p.getMessage()), pa);
}catch (Exception ea) {
}
}
}
}
}
public void reconcile(IRegion partition) {
rec();
}
public void initialReconcile() {
rec();
}
public void setProgressMonitor(IProgressMonitor monitor) {
}
}
private ConsoleEditor editor;
public JavaSnippetViewerConfiguration(JavaTextTools tools,
IPreferenceStore preferenceStore, ConsoleEditor editor) {
super(tools.getColorManager(), preferenceStore, editor,
IJavaPartitions.JAVA_PARTITIONING);
this.editor = editor;
}
/**
* @see JDIViewerConfiguration#getContentAssistantProcessor()
*/
public IContentAssistProcessor getContentAssistantProcessor() {
return new JavaSnippetCompletionProcessor((ConsoleEditor) getEditor());
}
/**
* @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
*/
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
ContentAssistant assistant = new ContentAssistant();
assistant.setContentAssistProcessor(getContentAssistantProcessor(),
IDocument.DEFAULT_CONTENT_TYPE);
JDIContentAssistPreference.configure(assistant, getColorManager());
assistant
.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
assistant
.setInformationControlCreator(getInformationControlCreator(sourceViewer));
return assistant;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler
* (org.eclipse.jface.text.source.ISourceViewer)
*/
public IReconciler getReconciler(ISourceViewer sourceViewer) {
final ITextEditor editor = getEditor();
if (editor != null && editor.isEditable()) {
JavaCompositeReconcilingStrategy strategy = new JavaCompositeReconcilingStrategy(
sourceViewer, editor,
getConfiguredDocumentPartitioning(sourceViewer)) {
public void initialReconcile() {
// TODO Auto-generated method stub
super.initialReconcile();
}
};
IReconcilingStrategy[] reconcilingStrategies = strategy
.getReconcilingStrategies();
ArrayList<IReconcilingStrategy> arrayList = new ArrayList<IReconcilingStrategy>(
Arrays.asList(reconcilingStrategies));
arrayList.add(new ValidationReconcilingStrategy());
strategy.setReconcilingStrategies(arrayList
.toArray(new IReconcilingStrategy[arrayList.size()]));
JavaReconciler reconciler = new JavaReconciler(editor, strategy,
false);
reconciler.setIsAllowedToModifyDocument(false);
reconciler.setDelay(500);
return reconciler;
}
return null;
}
}