Author: akazakov
Date: 2012-02-16 20:40:25 -0500 (Thu, 16 Feb 2012)
New Revision: 38852
Modified:
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/AsYouTypeValidatorManager.java
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/TempMarkerManager.java
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidatorManager.java
Log:
https://issues.jboss.org/browse/JBIDE-10738 As-you-type EL validation
Modified:
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/AsYouTypeValidatorManager.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/AsYouTypeValidatorManager.java 2012-02-17
00:30:01 UTC (rev 38851)
+++
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/AsYouTypeValidatorManager.java 2012-02-17
01:40:25 UTC (rev 38852)
@@ -11,19 +11,33 @@
package org.jboss.tools.common.validation;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.Map;
+import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.progress.UIJob;
+import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation;
import org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator;
import org.eclipse.wst.validation.internal.core.ValidationException;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;
import org.eclipse.wst.validation.internal.provisional.core.IValidationContext;
+import org.jboss.tools.common.util.EclipseUIUtil;
/**
* This Manager is responsible for as-you-type validation.
@@ -37,6 +51,8 @@
private EditorValidationContext context;
private Map<IValidator, IProject> rootProjects;
+ private static Set<IDocument> reporters = new HashSet<IDocument>();
+
/*
* (non-Javadoc)
* @see
org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator#connect(org.eclipse.jface.text.IDocument)
@@ -53,10 +69,57 @@
@Override
public void disconnect(IDocument document) {
context = null;
+ synchronized (reporters) {
+ reporters.remove(document);
+ }
}
+ /**
+ * Remove all the temporary annotations added by this validator.
+ */
+ static void removeMessages() {
+ UIJob job = new UIJob("Removing as-you-type JBT validation problems")
{
+ public IStatus runInUIThread(IProgressMonitor monitor) {
+ ITextEditor e = EclipseUIUtil.getActiveEditor();
+ if(e!=null && !e.isDirty()) {
+ IEditorInput input = e.getEditorInput();
+ IDocumentProvider dp = e.getDocumentProvider();
+ IDocument doc = dp.getDocument(input);
+ boolean ok = false;
+ synchronized (reporters) {
+ ok = reporters.contains(doc);
+ }
+ if(ok) {
+ IAnnotationModel model = dp.getAnnotationModel(input);
+ if(model instanceof AbstractMarkerAnnotationModel) {
+ AbstractMarkerAnnotationModel anModel = ((AbstractMarkerAnnotationModel)model);
+ synchronized (anModel.getLockObject()) {
+ Iterator iterator = anModel.getAnnotationIterator();
+ while (iterator.hasNext()) {
+ Object o = iterator.next();
+ if(o instanceof TemporaryAnnotation) {
+ TemporaryAnnotation annotation = (TemporaryAnnotation)o;
+ Map attributes = annotation.getAttributes();
+ if(attributes!=null &&
attributes.get(TempMarkerManager.AS_YOU_TYPE_VALIDATION_ANNOTATION_ATTRIBUTE)!=null) {
+ anModel.removeAnnotation(annotation);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return Status.OK_STATUS;
+ }
+ };
+ job.schedule();
+ }
+
private boolean init(IValidationContext helper, IReporter reporter) {
if(context==null) {
+ synchronized (reporters) {
+ reporters.add(document);
+ }
String[] uris = helper.getURIs();
if(uris.length==0) {
return false;
@@ -98,7 +161,7 @@
((IAsYouTypeValidator)validator).validate(this, rootProject, dirtyRegion, helper,
reporter, context, projectBrunch.getRootContext(), file);
}
}
-// reporter.removeAllMessages(this, file);
+// reporter.removeAllMessages(AsYouTypeValidatorManager.this, file);
}
@Override
Modified:
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/TempMarkerManager.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/TempMarkerManager.java 2012-02-17
00:30:01 UTC (rev 38851)
+++
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/TempMarkerManager.java 2012-02-17
01:40:25 UTC (rev 38852)
@@ -11,17 +11,33 @@
package org.jboss.tools.common.validation;
import java.text.MessageFormat;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.Locale;
+import java.util.Set;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.progress.UIJob;
+import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.wst.validation.internal.core.Message;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;
import org.eclipse.wst.validation.internal.provisional.core.IValidator;
import org.jboss.tools.common.CommonPlugin;
import org.jboss.tools.common.text.ITextSourceReference;
+import org.jboss.tools.common.util.EclipseUIUtil;
/**
* @author Alexey Kazakov
@@ -68,16 +84,116 @@
return message;
}
+ public static final String AS_YOU_TYPE_VALIDATION_ANNOTATION_ATTRIBUTE =
"org.jboss.tools.common.validation.asyoutype";
+
private static IMessage addMesssage(IValidator validator, IReporter reporter, int
offset, int length, IFile file, int lineNumber, int severity, String textMessage, Object[]
messageArguments, String bundleName) {
Message message = new ValidationMessage(severity, MessageFormat.format(textMessage,
messageArguments), file);
message.setOffset(offset);
message.setLength(length);
message.setLineNo(lineNumber);
message.setBundleName(bundleName);
+ message.setAttribute(AS_YOU_TYPE_VALIDATION_ANNOTATION_ATTRIBUTE, Boolean.TRUE);
reporter.addMessage(validator, message);
return message;
}
+/*
+ static class DisabledAnnotation extends Annotation implements IAnnotationPresentation {
+ private static final int WARNING_LAYER;
+ private static final int ERROR_LAYER;
+
+ static {
+ AnnotationPreferenceLookup lookup = EditorsUI.getAnnotationPreferenceLookup();
+ WARNING_LAYER = computeLayer("org.eclipse.wst.sse.ui.temp.warning",
lookup); //$NON-NLS-1$
+ ERROR_LAYER = computeLayer("org.eclipse.wst.sse.ui.temp.error",
lookup); //$NON-NLS-1$
+ }
+
+ private static int computeLayer(String annotationType, AnnotationPreferenceLookup
lookup) {
+ Annotation annotation = new Annotation(annotationType, false, null);
+ AnnotationPreference preference= lookup.getAnnotationPreference(annotation);
+ if (preference != null) {
+ return preference.getPresentationLayer() + 1;
+ } else {
+ return IAnnotationAccessExtension.DEFAULT_LAYER + 1;
+ }
+ }
+
+ public DisabledAnnotation(String type, boolean isPersistent, String text) {
+ super(type, isPersistent, text);
+ }
+
+ @Override
+ public int getLayer() {
+ return WARNING_LAYER;
+ }
+
+ @Override
+ public void paint(GC gc, Canvas canvas, Rectangle bounds) {
+ String path = WorkbenchImages.ICONS_PATH + "dlcl16/showwarn_tsk.gif";
//$NON-NLS-1$
+ URL url = BundleUtility.find(IDEWorkbenchPlugin.IDE_WORKBENCH, path);
+ ImageDescriptor descriptor = ImageDescriptor.createFromURL(url);
+ Image image = descriptor.createImage(false);
+// ImageUtilities.drawImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK),
gc, canvas, bounds, SWT.CENTER, SWT.TOP);
+ ImageUtilities.drawImage(image, gc, canvas, bounds, SWT.CENTER, SWT.TOP);
+ }
+ }
+*/
+ protected void disableProblemAnnotations(final ITextSourceReference reference) {
+ // Remove (TODO disable) all the existing problem annotations for the reference in case
of as-you-type validation
+ UIJob job = new UIJob("As-you-type JBT validation. Disabling the marker
annotations.") {
+ public IStatus runInUIThread(IProgressMonitor monitor) {
+ ITextEditor e = EclipseUIUtil.getActiveEditor();
+ if(e!=null && e.isDirty()) {
+ IEditorInput input = e.getEditorInput();
+ IDocumentProvider dp = e.getDocumentProvider();
+ if(document == dp.getDocument(input)) {
+ IAnnotationModel model = dp.getAnnotationModel(input);
+ if(model instanceof AbstractMarkerAnnotationModel) {
+ AbstractMarkerAnnotationModel anModel = ((AbstractMarkerAnnotationModel)model);
+ synchronized (anModel.getLockObject()) {
+ Iterator iterator = anModel.getAnnotationIterator(reference.getStartPosition(),
reference.getLength(), false, false);
+ Set<MarkerAnnotation> annotationsToRemove = new
HashSet<MarkerAnnotation>();
+// Map<Annotation, Position> newAnnotations = new HashMap<Annotation,
Position>();
+ while (iterator.hasNext()) {
+ Object o = iterator.next();
+ if(o instanceof MarkerAnnotation) {
+ MarkerAnnotation annotation = (MarkerAnnotation)o;
+ IMarker marker = annotation.getMarker();
+ try {
+ String type = marker.getType();
+ if(getMarkerType().equals(type)) {
+// Annotation newAnnotation = new DisabledAnnotation(annotation.getType(),
false, annotation.getText());
+// int offset = marker.getAttribute(IMarker.CHAR_START, 0);
+// int length = 0; // marker.getAttribute(IMarker.CHAR_END, 0) - offset;
+// Position p = new Position(offset, length);
+// newAnnotations.put(newAnnotation, p);
+ annotationsToRemove.add(annotation);
+ }
+ } catch (CoreException ce) {
+ CommonPlugin.getDefault().logError(ce);
+ }
+ }
+ }
+// if(!newAnnotations.isEmpty()) {
+// Annotation[] annotationsToRemoveArray = annotationsToRemove.toArray(new
Annotation[annotationsToRemove.size()]);
+// anModel.replaceAnnotations(annotationsToRemoveArray, newAnnotations);
+// }
+ for (MarkerAnnotation annotation : annotationsToRemove) {
+ anModel.removeAnnotation(annotation);
+ }
+// for (Annotation annotation : newAnnotations.keySet()) {
+// anModel.addAnnotation(annotation, newAnnotations.get(annotation));
+// }
+ }
+ }
+ }
+ }
+ return Status.OK_STATUS;
+ }
+ };
+ job.schedule();
+ }
+
static class ValidationMessage extends Message {
private String message;
Modified:
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidatorManager.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidatorManager.java 2012-02-17
00:30:01 UTC (rev 38851)
+++
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidatorManager.java 2012-02-17
01:40:25 UTC (rev 38852)
@@ -109,6 +109,7 @@
IValidationContextManager validationContextManager =
validationHelper.getValidationContextManager();
List<IValidator> validators = validationContextManager.getValidators();
removeMarkers(changedFiles);
+ AsYouTypeValidatorManager.removeMessages();
for (IValidator validator : validators) {
for (IProject rootProject : rootProjects) {
IValidatingProjectSet projectBrunch =
validationHelper.getValidationContextManager().getValidatingProjectTree(validator).getBrunches().get(rootProject);