1 11 package org.eclipse.ant.internal.ui.editor.actions; 12 13 import java.util.ResourceBundle ; 14 15 import org.eclipse.ant.internal.ui.editor.AntEditor; 16 import org.eclipse.ant.internal.ui.editor.AntEditorSourceViewerConfiguration; 17 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant; 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IInformationControlCreator; 21 import org.eclipse.jface.text.IRegion; 22 import org.eclipse.jface.text.ITextHover; 23 import org.eclipse.jface.text.ITextViewer; 24 import org.eclipse.jface.text.ITextViewerExtension2; 25 import org.eclipse.jface.text.ITextViewerExtension4; 26 import org.eclipse.jface.text.ITextViewerExtension5; 27 import org.eclipse.jface.text.TextUtilities; 28 import org.eclipse.jface.text.information.IInformationProvider; 29 import org.eclipse.jface.text.information.IInformationProviderExtension2; 30 import org.eclipse.jface.text.information.InformationPresenter; 31 import org.eclipse.jface.text.source.ISourceViewer; 32 import org.eclipse.swt.custom.StyledText; 33 import org.eclipse.swt.graphics.Point; 34 import org.eclipse.ui.texteditor.TextEditorAction; 35 import org.eclipse.ui.texteditor.TextOperationAction; 36 37 public class InformationDispatchAction extends TextEditorAction { 38 39 40 private TextOperationAction fTextOperationAction; 41 42 43 private InformationPresenter fInformationPresenter; 44 45 52 public InformationDispatchAction(ResourceBundle resourceBundle, String prefix, TextOperationAction textOperationAction, AntEditor editor) { 53 super(resourceBundle, prefix, editor); 54 if (textOperationAction == null) { 55 throw new IllegalArgumentException (); 56 } 57 fTextOperationAction= textOperationAction; 58 } 59 60 63 public void run() { 64 65 70 class InformationProvider implements IInformationProvider, IInformationProviderExtension2 { 71 72 private IRegion fHoverRegion; 73 private String fHoverInfo; 74 private IInformationControlCreator fControlCreator; 75 76 InformationProvider(IRegion hoverRegion, String hoverInfo, IInformationControlCreator controlCreator) { 77 fHoverRegion= hoverRegion; 78 fHoverInfo= hoverInfo; 79 fControlCreator= controlCreator; 80 } 81 84 public IRegion getSubject(ITextViewer textViewer, int invocationOffset) { 85 return fHoverRegion; 86 } 87 90 public String getInformation(ITextViewer textViewer, IRegion subject) { 91 return fHoverInfo; 92 } 93 97 public IInformationControlCreator getInformationPresenterControlCreator() { 98 return fControlCreator; 99 } 100 } 101 102 ISourceViewer sourceViewer= ((AntEditor)getTextEditor()).getViewer(); 103 if (sourceViewer == null) { 104 fTextOperationAction.run(); 105 return; 106 } 107 108 if (sourceViewer instanceof ITextViewerExtension4) { 109 ITextViewerExtension4 extension4= (ITextViewerExtension4) sourceViewer; 110 if (extension4.moveFocusToWidgetToken()) { 111 return; 112 } 113 } 114 115 if (! (sourceViewer instanceof ITextViewerExtension2)) { 116 fTextOperationAction.run(); 117 return; 118 } 119 120 ITextViewerExtension2 textViewerExtension2= (ITextViewerExtension2) sourceViewer; 121 122 ITextHover textHover= textViewerExtension2.getCurrentTextHover(); 124 if (textHover == null) { 125 fTextOperationAction.run(); 126 return; 127 } 128 129 Point hoverEventLocation= textViewerExtension2.getHoverEventLocation(); 130 int offset= computeOffsetAtLocation(sourceViewer, hoverEventLocation.x, hoverEventLocation.y); 131 if (offset == -1) { 132 fTextOperationAction.run(); 133 return; 134 } 135 136 try { 137 String contentType= TextUtilities.getContentType(sourceViewer.getDocument(), AntDocumentSetupParticipant.ANT_PARTITIONING, offset, true); 139 140 IRegion hoverRegion= textHover.getHoverRegion(sourceViewer, offset); 141 if (hoverRegion == null) 142 return; 143 144 String hoverInfo= textHover.getHoverInfo(sourceViewer, hoverRegion); 145 146 IInformationControlCreator controlCreator= null; 147 if (textHover instanceof IInformationProviderExtension2) 148 controlCreator= ((IInformationProviderExtension2)textHover).getInformationPresenterControlCreator(); 149 150 IInformationProvider informationProvider= new InformationProvider(hoverRegion, hoverInfo, controlCreator); 151 InformationPresenter presenter= getInformationPresenter(); 152 presenter.setOffset(offset); 153 presenter.setDocumentPartitioning(AntDocumentSetupParticipant.ANT_PARTITIONING); 154 presenter.setInformationProvider(informationProvider, contentType); 155 presenter.showInformation(); 156 157 } catch (BadLocationException e) { 158 } 159 } 160 161 private int computeOffsetAtLocation(ITextViewer textViewer, int x, int y) { 163 164 StyledText styledText= textViewer.getTextWidget(); 165 IDocument document= textViewer.getDocument(); 166 167 if (document == null) { 168 return -1; 169 } 170 171 try { 172 int widgetLocation= styledText.getOffsetAtLocation(new Point(x, y)); 173 if (textViewer instanceof ITextViewerExtension5) { 174 ITextViewerExtension5 extension= (ITextViewerExtension5) textViewer; 175 return extension.widgetOffset2ModelOffset(widgetLocation); 176 } 177 IRegion visibleRegion= textViewer.getVisibleRegion(); 178 return widgetLocation + visibleRegion.getOffset(); 179 } catch (IllegalArgumentException e) { 180 return -1; 181 } 182 } 183 184 private InformationPresenter getInformationPresenter() { 185 if (fInformationPresenter == null) { 186 IInformationControlCreator informationControlCreator= AntEditorSourceViewerConfiguration.getInformationPresenterControlCreator(); 187 fInformationPresenter= new InformationPresenter(informationControlCreator); 188 fInformationPresenter.setSizeConstraints(60, 10, true, true); 189 fInformationPresenter.install(((AntEditor)getTextEditor()).getViewer()); 190 } 191 return fInformationPresenter; 192 } 193 } 194 | Popular Tags |