Author: dazarov
Date: 2012-10-09 19:13:56 -0400 (Tue, 09 Oct 2012)
New Revision: 44388
Added:
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddAttributeMarkerResolution.java
Modified:
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddTLDMarkerResolution.java
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/JSPProblemMarkerResolutionGenerator.java
Log:
"Add missing required attribute(s)" quick fix for "Missing required
attribute" WTP problem marker
https://issues.jboss.org/browse/JBIDE-12390
Added:
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddAttributeMarkerResolution.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddAttributeMarkerResolution.java
(rev 0)
+++
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddAttributeMarkerResolution.java 2012-10-09
23:13:56 UTC (rev 44388)
@@ -0,0 +1,137 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.jst.web.ui.action;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.ui.texteditor.DocumentProviderRegistry;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.jboss.tools.common.model.ui.ModelUIImages;
+import org.jboss.tools.common.quickfix.IQuickFix;
+import org.jboss.tools.common.ui.CommonUIPlugin;
+import org.jboss.tools.jst.web.ui.Messages;
+import org.jboss.tools.jst.web.ui.WebUiPlugin;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * The Marker Resolution that adds missing attribute to the tag in jsp or xhtml file
+ *
+ * @author Daniel Azarov
+ *
+ */
+public class AddAttributeMarkerResolution implements IQuickFix{
+ private IFile file;
+
+ private int start, end;
+ private String attributeName;
+ private Node node;
+
+ public AddAttributeMarkerResolution(IFile file, Node node, String attributeName, int
start, int end){
+ this.file = file;
+ this.node = node;
+ this.attributeName = attributeName;
+ this.start = start;
+ this.end = end;
+ }
+
+ @Override
+ public String getLabel() {
+ return NLS.bind(Messages.AddAttributeMarkerResolution_Name, attributeName,
node.getNodeName());
+ }
+
+ @Override
+ public void run(IMarker marker) {
+ FileEditorInput input = new FileEditorInput(file);
+ IDocumentProvider provider =
DocumentProviderRegistry.getDefault().getDocumentProvider(input);
+ try {
+ provider.connect(input);
+
+ boolean dirty = provider.canSaveDocument(input);
+
+ IDocument document = provider.getDocument(input);
+
+ apply(document);
+
+ if(!dirty){
+ provider.aboutToChange(input);
+ provider.saveDocument(new NullProgressMonitor(), input, document, true);
+ provider.changed(input);
+ }
+
+ provider.disconnect(input);
+ }catch(CoreException ex){
+ WebUiPlugin.getPluginLog().logError(ex);
+ }
+ }
+
+ @Override
+ public String getDescription() {
+ return getLabel();
+ }
+
+ @Override
+ public Image getImage() {
+ return
CommonUIPlugin.getImageDescriptorRegistry().get(ModelUIImages.getImageDescriptor(ModelUIImages.TAGLIB_FILE));
+ }
+
+ @Override
+ public void apply(IDocument document) {
+ String text = "<"+node.getNodeName()+" ";
+
+ NamedNodeMap attributes = node.getAttributes();
+ for(int i = 0; i < attributes.getLength(); i++){
+ Node att = attributes.item(i);
+ text += att.getNodeName()+"=\""+att.getNodeValue()+"\"
";
+ }
+
+ text += attributeName+"=\"\">";
+ try {
+ document.replace(start, end-start, text);
+ } catch (BadLocationException ex) {
+ WebUiPlugin.getPluginLog().logError(ex);
+ }
+ }
+
+ @Override
+ public Point getSelection(IDocument document) {
+ return null;
+ }
+
+ @Override
+ public String getAdditionalProposalInfo() {
+ return getDescription();
+ }
+
+ @Override
+ public String getDisplayString() {
+ return getLabel();
+ }
+
+ @Override
+ public IContextInformation getContextInformation() {
+ return null;
+ }
+
+ @Override
+ public int getRelevance() {
+ return 0;
+ }
+}
\ No newline at end of file
Property changes on:
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddAttributeMarkerResolution.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddTLDMarkerResolution.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddTLDMarkerResolution.java 2012-10-09
21:24:50 UTC (rev 44387)
+++
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/AddTLDMarkerResolution.java 2012-10-09
23:13:56 UTC (rev 44388)
@@ -16,7 +16,6 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.contentassist.IContextInformation;
@@ -31,7 +30,7 @@
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.jboss.tools.common.model.ui.ModelUIImages;
import org.jboss.tools.common.model.ui.views.palette.PaletteInsertHelper;
-import org.jboss.tools.common.quickfix.IBaseMarkerResolution;
+import org.jboss.tools.common.quickfix.IQuickFix;
import org.jboss.tools.common.ui.CommonUIPlugin;
import org.jboss.tools.jst.jsp.jspeditor.dnd.JSPPaletteInsertHelper;
import org.jboss.tools.jst.jsp.jspeditor.dnd.PaletteTaglibInserter;
@@ -44,7 +43,7 @@
* @author Daniel Azarov
*
*/
-public class AddTLDMarkerResolution implements IBaseMarkerResolution,
IJavaCompletionProposal{
+public class AddTLDMarkerResolution implements IQuickFix{
private IFile file;
private String resolutionName;
Modified:
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/JSPProblemMarkerResolutionGenerator.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/JSPProblemMarkerResolutionGenerator.java 2012-10-09
21:24:50 UTC (rev 44387)
+++
trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/action/JSPProblemMarkerResolutionGenerator.java 2012-10-09
23:13:56 UTC (rev 44388)
@@ -24,6 +24,9 @@
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator2;
+import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.ui.texteditor.DocumentProviderRegistry;
+import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
@@ -33,6 +36,8 @@
import org.jboss.tools.common.el.core.resolver.ELContext;
import org.jboss.tools.common.quickfix.IQuickFixGenerator;
import org.jboss.tools.common.refactoring.MarkerResolutionUtils;
+import org.jboss.tools.common.text.ext.util.StructuredModelWrapper;
+import org.jboss.tools.common.text.ext.util.Utils;
import org.jboss.tools.jst.web.kb.IKbProject;
import org.jboss.tools.jst.web.kb.KbProjectFactory;
import org.jboss.tools.jst.web.kb.PageContextFactory;
@@ -41,6 +46,9 @@
import org.jboss.tools.jst.web.kb.taglib.INameSpace;
import org.jboss.tools.jst.web.kb.taglib.ITagLibrary;
import org.jboss.tools.jst.web.ui.WebUiPlugin;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
/**
* Shows the Marker Resolutions for Unknown tag JSP Problem Marker
@@ -55,10 +63,12 @@
private static final String UNKNOWN_TAG = "Unknown tag"; //$NON-NLS-1$
+ private static final String MISSING_ATTRIBUTE = "Missing required attribute";
//$NON-NLS-1$
+
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
try{
- return isOurCase(marker);
+ return findResolutions(marker);
}catch(CoreException ex){
WebUiPlugin.getPluginLog().logError(ex);
}
@@ -99,38 +109,49 @@
return true;
}
- private IJavaCompletionProposal[] isOurCase(Annotation annotation, Position position){
- ArrayList<IJavaCompletionProposal> proposals = new
ArrayList<IJavaCompletionProposal>();
- if(!(annotation instanceof TemporaryAnnotation)){
- return new IJavaCompletionProposal[]{};
- }
- TemporaryAnnotation ta = (TemporaryAnnotation)annotation;
+ private void getAddAttribute(ArrayList<IJavaCompletionProposal> proposals,
TemporaryAnnotation ta, String message, int start, int end){
+ String attributeName = getAttributeName(message);
- String message = annotation.getText();
- if(ta.getPosition() == null)
- return new IJavaCompletionProposal[]{};
- final int start = position.getOffset();
-
- final int end = position.getOffset()+position.getLength();
-
- if(!message.startsWith(UNKNOWN_TAG))
- return new IJavaCompletionProposal[]{};
-
+ IFile file = MarkerResolutionUtils.getFile();
+ if(file == null)
+ return;
+
+ Object additionalInfo = ta.getAdditionalFixInfo();
+ if(additionalInfo instanceof IDocument){
+ IStructuredModel model =
StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument)additionalInfo);
+ IDOMDocument xmlDocument = (model instanceof IDOMModel) ? ((IDOMModel)
model).getDocument() : null;
+
+ Node n = Utils.findNodeForOffset(xmlDocument, start);
+
+ if (n == null || !(n instanceof Attr))
+ return;
+
+ Node node = ((Attr)n).getOwnerElement();
+
+ String prefix = node.getPrefix();
+ String tagName = node.getNodeName();
+
+ proposals.add(new AddAttributeMarkerResolution(file, node, attributeName, start,
end));
+
+ }
+ }
+
+ private void getAddTLD(ArrayList<IJavaCompletionProposal> proposals,
TemporaryAnnotation ta, String message, int start, int end){
String prefix = getPrifix(message);
if(prefix == null)
- return new IJavaCompletionProposal[]{};
+ return;
String tagName = getTagName(message);
if(tagName == null)
- return new IJavaCompletionProposal[]{};
+ return;
IFile file = MarkerResolutionUtils.getFile();
if(file == null)
- return new IJavaCompletionProposal[]{};
+ return;
if(!validatePrefix(file, start, prefix)){
- return new IJavaCompletionProposal[]{};
+ return;
}
Object additionalInfo = ta.getAdditionalFixInfo();
@@ -139,6 +160,9 @@
IDOMDocument xmlDocument = (model instanceof IDOMModel) ? ((IDOMModel)
model).getDocument() : null;
IKbProject kbProject = KbProjectFactory.getKbProject(file.getProject(), true);
+ if(kbProject == null){
+ return;
+ }
List<ITagLibrary> libraries = kbProject.getAllTagLibraries();
ArrayList<String> names = new ArrayList<String>();
@@ -171,7 +195,28 @@
}
}
+ }
+
+ private IJavaCompletionProposal[] findProposals(Annotation annotation, Position
position){
+ ArrayList<IJavaCompletionProposal> proposals = new
ArrayList<IJavaCompletionProposal>();
+ if(!(annotation instanceof TemporaryAnnotation)){
+ return new IJavaCompletionProposal[]{};
+ }
+ TemporaryAnnotation ta = (TemporaryAnnotation)annotation;
+ String message = annotation.getText();
+ if(ta.getPosition() == null)
+ return new IJavaCompletionProposal[]{};
+
+ final int start = position.getOffset();
+
+ final int end = position.getOffset()+position.getLength();
+
+ if(message.startsWith(UNKNOWN_TAG)){
+ getAddTLD(proposals, ta, message, start, end);
+ }else if(message.startsWith(MISSING_ATTRIBUTE)){
+ getAddAttribute(proposals, ta, message, start, end);
+ }
return proposals.toArray(new IJavaCompletionProposal[]{});
}
@@ -184,35 +229,45 @@
return null;
}
- private IMarkerResolution[] isOurCase(IMarker marker) throws CoreException{
- ArrayList<IMarkerResolution> resolutions = new
ArrayList<IMarkerResolution>();
- String message = (String)marker.getAttribute(IMarker.MESSAGE);
+ private void getAddAttribute(ArrayList<IMarkerResolution> resolutions, IMarker
marker, String message, int start, int end) throws CoreException{
+ String attributeName = getAttributeName(message);
- Integer attribute = ((Integer)marker.getAttribute(IMarker.CHAR_START));
- if(attribute == null)
- return new IMarkerResolution[]{};
- final int start = attribute.intValue();
+ IFile file = (IFile)marker.getResource();
- attribute = ((Integer)marker.getAttribute(IMarker.CHAR_END));
- if(attribute == null)
- return new IMarkerResolution[]{};
- final int end = attribute.intValue();
-
- if(!message.startsWith(UNKNOWN_TAG))
- return new IMarkerResolution[]{};
-
+ FileEditorInput input = new FileEditorInput(file);
+ IDocumentProvider provider =
DocumentProviderRegistry.getDefault().getDocumentProvider(input);
+ IDocument document = provider.getDocument(input);
+ StructuredModelWrapper smw = new StructuredModelWrapper();
+ try {
+ smw.init(document);
+ Document xmlDocument = smw.getDocument();
+ if (xmlDocument == null) return;
+
+ Node node = Utils.findNodeForOffset(xmlDocument, start);
+
+ if (node == null) return;
+
+ String tagName = node.getNodeName();
+
+ resolutions.add(new AddAttributeMarkerResolution(file, node, attributeName, start,
end));
+ } finally {
+ smw.dispose();
+ }
+ }
+
+ private void getAddTLD(ArrayList<IMarkerResolution> resolutions, IMarker marker,
String message, int start, int end) throws CoreException{
String prefix = getPrifix(message);
if(prefix == null)
- return new IMarkerResolution[]{};
+ return;
String tagName = getTagName(message);
if(tagName == null)
- return new IMarkerResolution[]{};
+ return;
IFile file = (IFile)marker.getResource();
if(!validatePrefix(file, start, prefix)){
- return new IMarkerResolution[]{};
+ return;
}
IKbProject kbProject = KbProjectFactory.getKbProject(file.getProject(), true);
@@ -245,7 +300,28 @@
names.add(resolutionName);
}
}
+ }
+
+ private IMarkerResolution[] findResolutions(IMarker marker) throws CoreException{
+ ArrayList<IMarkerResolution> resolutions = new
ArrayList<IMarkerResolution>();
+ String message = (String)marker.getAttribute(IMarker.MESSAGE);
+ Integer attribute = ((Integer)marker.getAttribute(IMarker.CHAR_START));
+ if(attribute == null)
+ return new IMarkerResolution[]{};
+ final int start = attribute.intValue();
+
+ attribute = ((Integer)marker.getAttribute(IMarker.CHAR_END));
+ if(attribute == null)
+ return new IMarkerResolution[]{};
+ final int end = attribute.intValue();
+
+ if(message.startsWith(UNKNOWN_TAG)){
+ getAddTLD(resolutions, marker, message, start, end);
+ }else if(message.startsWith(MISSING_ATTRIBUTE)){
+ getAddAttribute(resolutions, marker, message, start, end);
+ }
+
return resolutions.toArray(new IMarkerResolution[]{});
}
@@ -280,6 +356,22 @@
return tagName;
}
+
+ public static String getAttributeName(String message){
+ String attributeName=""; //$NON-NLS-1$
+
+ int start = message.indexOf("\""); //$NON-NLS-1$
+ if(start < 0)
+ return null;
+
+ int end = message.lastIndexOf("\""); //$NON-NLS-1$
+ if(end < 0)
+ return null;
+
+ attributeName = message.substring(start+1, end);
+
+ return attributeName;
+ }
@Override
public boolean hasResolutions(IMarker marker) {
@@ -300,6 +392,6 @@
@Override
public IJavaCompletionProposal[] getProposals(Annotation annotation, Position position)
{
- return isOurCase(annotation, position);
+ return findProposals(annotation, position);
}
}
\ No newline at end of file