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

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