source: java/FramclipsePlugin/src/main/java/com/framsticks/framclipse/editors/hover/FramscriptTextHover.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 6.1 KB
Line 
1package com.framsticks.framclipse.editors.hover;
2
3import java.util.List;
4
5import org.eclipse.jface.internal.text.html.BrowserInformationControl;
6import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
7import org.eclipse.jface.resource.JFaceResources;
8import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
9import org.eclipse.jface.text.BadLocationException;
10import org.eclipse.jface.text.DefaultInformationControl;
11import org.eclipse.jface.text.IDocument;
12import org.eclipse.jface.text.IInformationControl;
13import org.eclipse.jface.text.IInformationControlCreator;
14import org.eclipse.jface.text.IInformationControlExtension4;
15import org.eclipse.jface.text.IRegion;
16import org.eclipse.jface.text.ITextHover;
17import org.eclipse.jface.text.ITextHoverExtension;
18import org.eclipse.jface.text.ITextViewer;
19import org.eclipse.jface.text.Region;
20import org.eclipse.jface.text.information.IInformationProviderExtension2;
21import org.eclipse.swt.SWT;
22import org.eclipse.swt.widgets.Shell;
23import org.eclipse.ui.editors.text.EditorsUI;
24import org.jdom.Document;
25import org.jdom.Element;
26import org.jdom.JDOMException;
27import org.jdom.xpath.XPath;
28
29import com.framsticks.framclipse.Framclipse;
30import com.framsticks.framclipse.editors.codeCompletion.SyntaxUtils;
31
32
33public class FramscriptTextHover implements ITextHover, ITextHoverExtension, IInformationProviderExtension2 {
34       
35        private static final class PresenterControlCreator extends AbstractReusableInformationControlCreator {
36
37                public IInformationControl doCreateInformationControl(Shell parent) {
38//                      int shellStyle= SWT.RESIZE | SWT.TOOL;
39//                      int style= SWT.V_SCROLL | SWT.H_SCROLL;
40//                      if (BrowserInformationControl.isAvailable(parent)) {
41//                              return new BrowserInformationControl(parent, JFaceResources.DIALOG_FONT, true);
42//                              return new BrowserInformationControl(parent, shellStyle, style);
43//                      } else
44//                              return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
45                                return new DefaultInformationControl(parent, true);
46                }
47        }
48
49        private static final class HoverControlCreator extends AbstractReusableInformationControlCreator {
50               
51                public IInformationControl doCreateInformationControl(Shell parent) {
52//                      if (BrowserInformationControl.isAvailable(parent)) {
53//                              return new BrowserInformationControl(parent, JFaceResources.DIALOG_FONT, true);
54//                              return new BrowserInformationControl(parent, SWT.TOOL | SWT.NO_TRIM, SWT.NONE, EditorsUI.getTooltipAffordanceString());
55//                      } else {
56//                              return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), EditorsUI.getTooltipAffordanceString());
57                                return new DefaultInformationControl(parent, true);
58//                      }
59                }
60
61                /*
62                 * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#canReuse(org.eclipse.jface.text.IInformationControl)
63                 */
64                public boolean canReuse(IInformationControl control) {
65                        if (!super.canReuse(control))
66                                return false;
67                       
68                        if (control instanceof IInformationControlExtension4)
69                                ((IInformationControlExtension4)control).setStatusText(EditorsUI.getTooltipAffordanceString());
70                       
71                        return true;
72                }
73        }
74
75
76
77        private static final int DESCRIPTION_LENGTH = 60;
78       
79        private IInformationControlCreator fHoverControlCreator;
80       
81        private IInformationControlCreator fPresenterControlCreator;
82       
83        public IInformationControlCreator getInformationPresenterControlCreator() {
84                if (fPresenterControlCreator == null)
85                        fPresenterControlCreator= new PresenterControlCreator();
86                return fPresenterControlCreator;
87        }
88
89        public IInformationControlCreator getHoverControlCreator() {
90                if (fHoverControlCreator == null)
91                        fHoverControlCreator= new HoverControlCreator();
92                return fHoverControlCreator;
93        }
94
95        public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
96                int offset = hoverRegion.getOffset() + hoverRegion.getLength();
97                IDocument document = textViewer.getDocument();
98
99                List<String> elements = SyntaxUtils.getElementsAt(document, offset);
100
101                if (elements.size() == 0) {
102                        return null;
103                }
104
105                String name = elements.remove(elements.size() - 1);
106                String elementFormat = "%s[@" + SyntaxUtils.NAME_ATTRIBUTE + "='%s']/";
107                String query = SyntaxUtils.ROOT + "/";
108
109                if (elements.size() == 0) {
110                        query += String.format(elementFormat, SyntaxUtils.TYPE_ELEMENT, name);
111                } else { // elements.size() > 1
112                        String parent;
113                        try {
114                                parent = SyntaxUtils.getLastElementType(elements);
115                        } catch (JDOMException e) {
116                                e.printStackTrace();
117                                return null;
118                        }
119                        query += String.format(elementFormat, SyntaxUtils.TYPE_ELEMENT, parent)
120                                        + String.format(elementFormat, SyntaxUtils.ELEMENT_ELEMENT, name);
121                }
122                query += SyntaxUtils.DESCRIPTION_ELEMENT;
123
124                Document syntax = Framclipse.getDefault().getFramscriptSyntax();
125                try {
126                        XPath xpath = XPath.newInstance(query);
127                        Element element = (Element) xpath.selectSingleNode(syntax);
128                        if (element == null) {
129                                return null;
130                        }
131
132                        return formatDescription(element.getValue());
133                } catch (Exception e) {
134                        e.printStackTrace();
135                }
136
137                return null;
138        }
139
140        public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
141                IDocument document = textViewer.getDocument();
142                String after = SyntaxUtils.getElementAfter(document, offset);
143
144                int line = 0;
145                try {
146                        line = document.getLineOfOffset(offset);
147                } catch (BadLocationException e) {
148                        e.printStackTrace();
149                }
150
151                int lineStart = offset;
152                try {
153                        lineStart = document.getLineInformation(line).getOffset();
154                } catch (BadLocationException e) {
155                        e.printStackTrace();
156                }
157                IRegion region = new Region(lineStart, offset + after.length() - lineStart);
158
159                return region;
160        }
161
162        private static String formatDescription(String description) {
163                String result = "";
164                int offset = 0;
165                while (offset < description.length()) {
166                        int length = DESCRIPTION_LENGTH;
167                        if (offset + length > description.length()) {
168                                result += description.substring(offset);
169                                return result;
170                        }
171
172                        for (int i = length; i > 0; i--) {
173                                if (Character.isWhitespace(description.charAt(offset + i))) {
174                                        length = i;
175                                        i = 0;
176                                }
177                        }
178
179                        result += description.substring(offset, offset + length) + "\n";
180                        offset += length + 1;
181                }
182                return result;
183        }
184}
Note: See TracBrowser for help on using the repository browser.