KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > InformationDispatchAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.actions;
12
13 import java.util.ResourceBundle JavaDoc;
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     /** The wrapped text operation action. */
40     private TextOperationAction fTextOperationAction;
41     
42      /** The information presenter that shows all information with scroll bars */
43     private InformationPresenter fInformationPresenter;
44     
45     /**
46      * Creates a dispatch action.
47      *
48      * @param resourceBundle the resource bundle
49      * @param prefix the prefix
50      * @param textOperationAction the text operation action
51      */

52     public InformationDispatchAction(ResourceBundle JavaDoc resourceBundle, String JavaDoc prefix, TextOperationAction textOperationAction, AntEditor editor) {
53         super(resourceBundle, prefix, editor);
54         if (textOperationAction == null) {
55             throw new IllegalArgumentException JavaDoc();
56         }
57         fTextOperationAction= textOperationAction;
58     }
59     
60     /*
61      * @see org.eclipse.jface.action.IAction#run()
62      */

63     public void run() {
64
65         /**
66          * Information provider used to present the information.
67          *
68          * @since 3.0
69          */

70         class InformationProvider implements IInformationProvider, IInformationProviderExtension2 {
71
72             private IRegion fHoverRegion;
73             private String JavaDoc fHoverInfo;
74             private IInformationControlCreator fControlCreator;
75             
76             InformationProvider(IRegion hoverRegion, String JavaDoc hoverInfo, IInformationControlCreator controlCreator) {
77                 fHoverRegion= hoverRegion;
78                 fHoverInfo= hoverInfo;
79                 fControlCreator= controlCreator;
80             }
81             /*
82              * @see org.eclipse.jface.text.information.IInformationProvider#getSubject(org.eclipse.jface.text.ITextViewer, int)
83              */

84             public IRegion getSubject(ITextViewer textViewer, int invocationOffset) {
85                 return fHoverRegion;
86             }
87             /*
88              * @see org.eclipse.jface.text.information.IInformationProvider#getInformation(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
89              */

90             public String JavaDoc getInformation(ITextViewer textViewer, IRegion subject) {
91                 return fHoverInfo;
92             }
93             /*
94              * @see org.eclipse.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
95              * @since 3.0
96              */

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         // does a text hover exist?
123
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             // get the text hover content
138
String JavaDoc 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 JavaDoc 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     // modified version from TextViewer
162
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 JavaDoc 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