Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How do I activate the Quick Fix command in a context menu?
How do I activate the Quick Fix command in a context menu? [message #334600] Thu, 19 February 2009 18:42 Go to next message
Pepe Ciardelli is currently offline Pepe CiardelliFriend
Messages: 12
Registered: July 2009
Junior Member
Hi all,

I am attempting to add quick fix to a SourceViewer per the instructions in
http://wiki.eclipse.org/FAQ_How_do_I_implement_Quick_Fixes_f or_my_own_language%3F,
i.e.:

<extension
point="org.eclipse.ui.ide.markerResolution">
<markerResolutionGenerator
class="eu.etaxonomy.taxeditor.tests.QuickFixer"
markerType="org.eclipse.core.resources.problemmarker">
</markerResolutionGenerator>
</extension>



I have added a menu to my StyledText widget with this plugin.xml code:

<extension point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:eu.etaxonomy.taxeditor.tests.view">
<command
commandId=" org.eclipse.jdt.ui.edit.text.java.correction.assist.proposal s "
/>
</menuContribution>
</extension>

and this:

MenuManager menuMgr = new MenuManager();
menuMgr.add(new
GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
getSite().registerContextMenu(menuMgr, sourceViewer);

Control control = sourceViewer.getTextWidget();
Menu menu = menuMgr.createContextMenu(control);
control.setMenu(menu);

My StyledText widget contains an annotation that extends MarkerAnnotation
and shows its IMarker.MESSAGE attribute when hovering over the
SourceViewer's ruler.

So far so good.

But when I right-click inside the StyledText widget to look at the menu,
"Quick Fix Ctrl+1" is grayed-out / deactivated. Similarly, Ctrl+1 does
nothing.

It seems as if the SourceViewer is failing some kind of context test for
activation. I've poked around in the plugin.xml's of the required plugins,
and I can't find the appropriate handler for the assist command, nor can I
find where the marker resolution generator is called.

Any ideas?

Many thanks in advance!
Re: How do I activate the Quick Fix command in a context menu? [message #334607 is a reply to message #334600] Fri, 20 February 2009 07:23 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Pepe Ciardelli wrote:
> Hi all,
>
> I am attempting to add quick fix to a SourceViewer per the
> instructions in
> http://wiki.eclipse.org/FAQ_How_do_I_implement_Quick_Fixes_f or_my_own_language%3F,
> i.e.:
>
> <extension
> point="org.eclipse.ui.ide.markerResolution">
> <markerResolutionGenerator
> class="eu.etaxonomy.taxeditor.tests.QuickFixer"
> markerType="org.eclipse.core.resources.problemmarker">
> </markerResolutionGenerator>
> </extension>
>
>
>
> I have added a menu to my StyledText widget with this plugin.xml code:
>
> <extension point="org.eclipse.ui.menus">
> <menuContribution
> locationURI="popup:eu.etaxonomy.taxeditor.tests.view">
> <command
> commandId=" org.eclipse.jdt.ui.edit.text.java.correction.assist.proposal s "
> />
> </menuContribution>
> </extension>
>
> and this:
>
> MenuManager menuMgr = new MenuManager();
> menuMgr.add(new
> GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
> getSite().registerContextMenu(menuMgr, sourceViewer);
>
> Control control = sourceViewer.getTextWidget();
> Menu menu = menuMgr.createContextMenu(control);
> control.setMenu(menu);
>
> My StyledText widget contains an annotation that extends
> MarkerAnnotation and shows its IMarker.MESSAGE attribute when hovering
> over the SourceViewer's ruler.
>
> So far so good.
>
> But when I right-click inside the StyledText widget to look at the
> menu, "Quick Fix Ctrl+1" is grayed-out / deactivated. Similarly,
> Ctrl+1 does nothing.
>
> It seems as if the SourceViewer is failing some kind of context test
> for activation. I've poked around in the plugin.xml's of the required
> plugins, and I can't find the appropriate handler for the assist
> command, nor can I find where the marker resolution generator is called.
There's no pre-installed handler or action for the source viewer. The
editor framework does this for you if your source viewer configuration
provides a quick assist assistant (see:
org.eclipse.jface.text.source.SourceViewerConfiguration.getQ uickAssistAssistant(ISourceViewer)).

Dani
>
> Any ideas?
> Many thanks in advance!
>
>
Re: How do I activate the Quick Fix command in a context menu? [message #334610 is a reply to message #334607] Fri, 20 February 2009 12:31 Go to previous messageGo to next message
Pepe Ciardelli is currently offline Pepe CiardelliFriend
Messages: 12
Registered: July 2009
Junior Member
Daniel Megert wrote:

> There's no pre-installed handler or action for the source viewer. The
> editor framework does this for you if your source viewer configuration
> provides a quick assist assistant (see:
>
org.eclipse.jface.text.source.SourceViewerConfiguration.getQ uickAssistAssistant(ISourceViewer)).

> Dani

Thanks very much, first of all.

I have added content assistants to my SourceViewerConfiguration as follows:

private IContentAssistant contentAssistant = new ContentAssistant();

public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
return contentAssistant;
}

private IQuickAssistAssistant quickAssistAssistant = new
QuickAssistAssistant();

public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer
sourceViewer) {
return quickAssistAssistant;
}

However, "Quick Fix Ctrl+1" in the SourceViewer context menu is still
disabled. Where exactly does the editor framework check for the content
assistant?

And out of curiosity, where is the key binding to Ctrl-1 set?

Cheers,
Pepe
Re: How do I activate the Quick Fix command in a context menu? [message #334611 is a reply to message #334610] Fri, 20 February 2009 14:22 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Pepe Ciardelli wrote:
> Daniel Megert wrote:
>
>> There's no pre-installed handler or action for the source viewer. The
>> editor framework does this for you if your source viewer
>> configuration provides a quick assist assistant (see:
> org.eclipse.jface.text.source.SourceViewerConfiguration.getQ uickAssistAssistant(ISourceViewer)).
>
>
>> Dani
>
> Thanks very much, first of all.
> I have added content assistants to my SourceViewerConfiguration as
> follows:
>
> private IContentAssistant contentAssistant = new ContentAssistant();
>
> public IContentAssistant getContentAssistant(ISourceViewer
> sourceViewer) {
> return contentAssistant;
> }
>
> private IQuickAssistAssistant quickAssistAssistant = new
> QuickAssistAssistant();
>
> public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer
> sourceViewer) {
> return quickAssistAssistant;
> }
>
> However, "Quick Fix Ctrl+1" in the SourceViewer context menu is still
> disabled. Where exactly does the editor framework check for the
> content assistant?
If you use/subclass one of the editors you get it for free, otherwise
you have to create and connect an action that then invokes the
corresponding operation on the source viewer.
>
> And out of curiosity, where is the key binding to Ctrl-1 set?
In plugin.xml where the command is defined.

Dani
> Cheers,
> Pepe
>
Re: How do I activate the Quick Fix command in a context menu? [message #334618 is a reply to message #334611] Fri, 20 February 2009 15:01 Go to previous messageGo to next message
Pepe Ciardelli is currently offline Pepe CiardelliFriend
Messages: 12
Registered: July 2009
Junior Member
Daniel Megert wrote:

>> However, "Quick Fix Ctrl+1" in the SourceViewer context menu is still
>> disabled. Where exactly does the editor framework check for the
>> content assistant?
> If you use/subclass one of the editors you get it for free, otherwise
> you have to create and connect an action that then invokes the
> corresponding operation on the source viewer.

My question is then, what is the action / handler that the editors use for
quick assist? My goal is to use the exact same presentation format the IDE
uses to display the results of my custom IMarkerResolutionGenerator.

>> And out of curiosity, where is the key binding to Ctrl-1 set?
> In plugin.xml where the command is defined.

Yes, but not in my project's plugin.xml - I have looked through the
plugin.xml files of the various plugins my project references, and I can't
find it. If I could find this, then I could presumably find the action /
handler used by the default editor, and adapt accordingly.

Thanks for patiently bearing with me,
Pepe
Re: How do I activate the Quick Fix command in a context menu? [message #334656 is a reply to message #334618] Mon, 23 February 2009 15:52 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Pepe Ciardelli wrote:
> Daniel Megert wrote:
>
>>> However, "Quick Fix Ctrl+1" in the SourceViewer context menu is
>>> still disabled. Where exactly does the editor framework check for
>>> the content assistant?
>> If you use/subclass one of the editors you get it for free, otherwise
>> you have to create and connect an action that then invokes the
>> corresponding operation on the source viewer.
>
> My question is then, what is the action / handler that the editors use
> for quick assist? My goal is to use the exact same presentation format
> the IDE uses to display the results of my custom
> IMarkerResolutionGenerator.
>
>>> And out of curiosity, where is the key binding to Ctrl-1 set?
>> In plugin.xml where the command is defined.
>
> Yes, but not in my project's plugin.xml - I have looked through the
> plugin.xml files of the various plugins my project references, and I
> can't find it. If I could find this, then I could presumably find the
> action / handler used by the default editor, and adapt accordingly.
See: /org.eclipse.ui.ide/plugin.xml

Dani
>
> Thanks for patiently bearing with me,
> Pepe
>
Re: How do I activate the Quick Fix command in a context menu? [message #334673 is a reply to message #334656] Tue, 24 February 2009 12:06 Go to previous message
Pepe Ciardelli is currently offline Pepe CiardelliFriend
Messages: 12
Registered: July 2009
Junior Member
OK, finally got all this working on a very rough test basis, and thought
I'd put the code here since Lord knows there isn't much else out there!

1. Hover over ruler shows marker text "Your blablabla don't work."
2. Hover over red-squigglied annotation shows "Your blablabla don't work."
followed by "2 quick fixes available" and 2 quick fix proposals. Clicking
on either proposal shows a "not yet implemented" dialog.
3. Both Control-1 and the right-click context menu inside the SourceViewer
show the 2 quick fix proposals in a white popup and, after a slight delay,
in a yellow popup with further information ("Problem description: Your
blablabla don't work.").

Hope someone finds this useful:

************************************************************ *************************
CLASS VIEW extends VIEWPART
************************************************************ *************************

public class View extends ViewPart {

private TextViewer text;
private StyledText styledText_2;
private StyledText styledText;
public static final String ID = "eu.etaxonomy.taxeditor.tests.view";

private SourceViewer sourceViewer;

public void createPartControl(final Composite parent) {

...

// Create annotation model
AnnotationModel model = new
ResourceMarkerAnnotationModel(ResourcesPlugin.getWorkspace() .getRoot());

// Create vertical ruler
final IVerticalRuler ruler = new Ruler(16);

// Create source viewer
sourceViewer = new SourceViewer(composite, ruler, SWT.WRAP |
SWT.MULTI | SWT.RESIZE);

...

// Add model to viewer
sourceViewer.setDocument(new Document("bla bla bla bla bla bla bla
bla bla bla bla bla bla "), model);

createAnnotationPainter();


// Configure viewer
ViewerConfiguration config = new ViewerConfiguration();
sourceViewer.configure(config);

for (String contentType :
config.getConfiguredContentTypes(sourceViewer)) {
config.getConfiguredTextHoverStateMasks(sourceViewer,
contentType);
}


// Create annotation that implements IAnnotationPresentation.
MarkerAnnotation annotation = null;
try {
IMarker marker =
ResourcesPlugin.getWorkspace().getRoot().createMarker(IMarke r.PROBLEM);
marker.setAttribute(IMarker.MESSAGE, "Your blablabla don't
work.");

// Note that hover text is retrieved from marker's text, not
annotation's
annotation = new MarkerAnnotation(marker);
annotation.setQuickFixable(true);
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// Add annotation to model - it should then show up in the ruler
model.addAnnotation(annotation, new Position(0,5));


// register context menu
MenuManager menuMgr = new MenuManager();
menuMgr.add(new
GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
getSite().registerContextMenu(menuMgr, sourceViewer);

Control control = sourceViewer.getTextWidget();
Menu menu = menuMgr.createContextMenu(control);
control.setMenu(menu);

}


public void showPossibleQuickAssists() {
sourceViewer.getQuickAssistAssistant().showPossibleQuickAssi sts();
}

private void createAnnotationPainter(){
// Annotations section
IAnnotationAccess fAnnotationAccess = new
DefaultMarkerAnnotationAccess();

// To paint the annotations
AnnotationPainter annotationPainter = new
AnnotationPainter(sourceViewer, fAnnotationAccess);

// Add ability to paint red squigglies

annotationPainter.addAnnotationType(AnnotationWithMarker.INT ERNAL_ANNOTATION);

annotationPainter.setAnnotationTypeColor(AnnotationWithMarke r.INTERNAL_ANNOTATION,
new Color(Display.getDefault(), new RGB(255, 0, 0)));

sourceViewer.addPainter(annotationPainter);
}
}


************************************************************ *************************
CLASS VIEWERCONFIGURATION extends SOURCEVIEWERCONFIGURATION
************************************************************ *************************

@SuppressWarnings("restriction")
public class ViewerConfiguration extends SourceViewerConfiguration {

private IQuickAssistAssistant quickAssistAssistant = new
QuickAssistAssistant();

public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer
sourceViewer) {
quickAssistAssistant.setQuickAssistProcessor(new
IQuickAssistProcessor() {

@Override
public boolean canAssist(
IQuickAssistInvocationContext invocationContext) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean canFix(Annotation annotation) {

if (annotation instanceof MarkerAnnotation) {
return ((MarkerAnnotation)
annotation).isQuickFixable();
}
return false;
}

@Override
public ICompletionProposal[] computeQuickAssistProposals(
IQuickAssistInvocationContext invocationContext) {
/**
* TextInvocationContext - length, offset, and sourceviewer
*/
ISourceViewer viewer= invocationContext.getSourceViewer();
int documentOffset= invocationContext.getOffset();

ICompletionProposal proposal1 = null;
ICompletionProposal proposal2 = null;

Iterator<?> iter =
viewer.getAnnotationModel().getAnnotationIterator();
while (iter.hasNext()) {
Annotation annotation = (Annotation) iter.next();
if (annotation instanceof MarkerAnnotation) {
proposal1 = new MarkerResolutionProposal(
new QuickFix("proposal1"),
((MarkerAnnotation) annotation).getMarker());
proposal2 = new MarkerResolutionProposal(
new QuickFix("proposal2"),
((MarkerAnnotation) annotation).getMarker());
}
}

return new ICompletionProposal[] { proposal1, proposal2 };
}

@Override
public String getErrorMessage() {
// TODO Auto-generated method stub
return null;
}

});
quickAssistAssistant.setInformationControlCreator(
getInformationControlCreator(sourceViewer));
return quickAssistAssistant;
}

private IAnnotationHover annotationHover = new
DefaultAnnotationHover();

public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer)
{
return annotationHover;
}

public ITextHover getTextHover(ISourceViewer sourceViewer, String
contentType) {
return new AbstractAnnotationHover(true) {
public Object getHoverInfo2(ITextViewer textViewer, IRegion
hoverRegion) {
IAnnotationModel model = ((SourceViewer)
textViewer).getAnnotationModel();

Iterator parent =
((IAnnotationModelExtension2)model).getAnnotationIterator(ho verRegion.getOffset(),
hoverRegion.getLength(), true, true);
Iterator iter = new JavaAnnotationIterator(parent, false);


Annotation annotation = null;
Position position = null;
while (iter.hasNext()) {
Annotation a = (Annotation) iter.next();
Position p = model.getPosition(a);

annotation = a;
position = p;
}

return new AnnotationInfo(annotation, position,
textViewer) {
public ICompletionProposal[] getCompletionProposals() {

ICompletionProposal proposal1 = null;
ICompletionProposal proposal2 = null;

if (annotation instanceof MarkerAnnotation) {
proposal1 = new MarkerResolutionProposal(
new QuickFix("proposal1"),
((MarkerAnnotation) annotation).getMarker());
proposal2 = new MarkerResolutionProposal(
new QuickFix("proposal2"),
((MarkerAnnotation) annotation).getMarker());

}

return new ICompletionProposal[] { proposal1,
proposal2 };
}
};
}
};
}
}


************************************************************ *************************
CLASS QUICKFIX implements IMARKERRESOLUTION
************************************************************ *************************

public class QuickFix implements IMarkerResolution {

String label;
QuickFix(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void run(IMarker marker) {
MessageDialog.openInformation(null, "QuickFix Demo",
"This quick-fix is not yet implemented");
}
}

************************************************************ *************************
CLASS QUICKFIXHANDLER implements ABSTRACTHANDLER
************************************************************ *************************
public class QuickFixHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPart part = HandlerUtil.getActivePart(event);

if (part instanceof View) {
((View) part).showPossibleQuickAssists();
}
return null;
}

}



************************************************************ *************************
EXTENSION POINTS FOR CONTEXT MENU AND COMMAND HANDLER
************************************************************ *************************
<extension point="org.eclipse.ui.handlers">
<handler
class="eu.etaxonomy.taxeditor.tests.QuickFixHandler"

commandId=" org.eclipse.jdt.ui.edit.text.java.correction.assist.proposal s ">
<activeWhen>
<with variable="activePartId">
<equals value="eu.etaxonomy.taxeditor.tests.view" />
</with>
</activeWhen>
</handler>
</extension>

<extension
point="org.eclipse.ui.ide.markerResolution">
<markerResolutionGenerator
class="eu.etaxonomy.taxeditor.tests.QuickFixer"
markerType="org.eclipse.core.resources.problemmarker">
</markerResolutionGenerator>
</extension>
Previous Topic:Command Framework in custom editor
Next Topic:JFace Dialog - get ClassCastException when opening a JFace Dialog from a RCP application
Goto Forum:
  


Current Time: Thu Nov 07 12:14:28 GMT 2024

Powered by FUDForum. Page generated in 0.03327 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top