2
2
3
3
import java .awt .Toolkit ;
4
4
import java .awt .datatransfer .StringSelection ;
5
+ import java .util .Arrays ;
5
6
import java .util .List ;
6
7
7
8
import javax .xml .xpath .XPathExpressionException ;
8
9
9
10
import org .eclipse .core .resources .IFile ;
10
11
import org .eclipse .core .resources .IProject ;
11
- import org .eclipse .core .runtime .CoreException ;
12
12
import org .eclipse .jdt .core .IJavaProject ;
13
13
import org .eclipse .jdt .core .JavaCore ;
14
14
import org .eclipse .jdt .ui .text .java .hover .IJavaEditorTextHover ;
17
17
import org .eclipse .jface .action .ToolBarManager ;
18
18
import org .eclipse .jface .text .BadLocationException ;
19
19
import org .eclipse .jface .text .DefaultInformationControl ;
20
- import org .eclipse .jface .text .IDocument ;
21
20
import org .eclipse .jface .text .IInformationControl ;
22
21
import org .eclipse .jface .text .IInformationControlCreator ;
23
22
import org .eclipse .jface .text .IRegion ;
27
26
import org .eclipse .swt .SWT ;
28
27
import org .eclipse .swt .widgets .Shell ;
29
28
import org .eclipse .ui .IEditorPart ;
30
- import org .eclipse .ui .IWorkbenchPage ;
31
- import org .eclipse .ui .PartInitException ;
32
- import org .eclipse .ui .PlatformUI ;
33
- import org .eclipse .ui .ide .IDE ;
34
- import org .eclipse .ui .texteditor .IDocumentProvider ;
35
- import org .eclipse .ui .texteditor .ITextEditor ;
36
- import org .eclipse .wst .xml .core .internal .document .ElementImpl ;
37
- import org .eclipse .wst .xml .core .internal .provisional .document .IDOMDocument ;
38
- import org .w3c .dom .Node ;
39
29
40
30
import codeplus .Activator ;
41
31
import codeplus .util .EditorUtil ;
42
- import codeplus .util .MapperNamespaceCache ;
43
32
import codeplus .util .TextUtil ;
44
33
import codeplus .util .XmlUtil ;
45
34
import codeplus .util .XmlUtil .MatchFile ;
46
- import codeplus .util .XpathUtil ;
35
+ import codeplus .util .mapper . FindMapperXml ;
47
36
48
37
public class JavaDaoHover implements IJavaEditorTextHover , ITextHoverExtension , ITextHoverExtension2 {
49
38
private IEditorPart currentEditor ;
50
39
private ITextViewer currentViewer ;
51
40
private IRegion currentRegion ;
52
41
53
- @ Override
54
- public String getHoverInfo (ITextViewer viewer , IRegion region ) {
55
- this .currentViewer = viewer ;
56
- this .currentRegion = region ;
42
+ private String [] getNamespaceAndSqlid () throws BadLocationException {
43
+ String hoverStr = currentViewer .getDocument ().get (currentRegion .getOffset (), currentRegion .getLength ());
57
44
58
- try {
59
- String hoverStr = viewer .getDocument ().get (region .getOffset (), region .getLength ());
60
-
61
- String content = EditorUtil .getText (currentEditor );
62
- String selected = TextUtil .getSelectedWord (content , region .getOffset ());
63
- String sqlId = null ;
64
- String wholeSqlId = null ;
65
- if (selected != null && selected .contains (hoverStr ) && selected .contains ("." )) {
66
- wholeSqlId = selected ;
67
- }
45
+ String content = EditorUtil .getText (currentEditor );
46
+ String selected = TextUtil .getSelectedWord (content , currentRegion .getOffset ());
47
+ String sqlId = null ;
48
+ String wholeSqlId = null ;
49
+ if (selected != null && selected .contains (hoverStr ) && selected .contains ("." )) {
50
+ wholeSqlId = selected ;
51
+ }
68
52
69
- IFile file = currentEditor .getEditorInput ().getAdapter (IFile .class );
70
- String fileName = file .getName ();
71
- if (fileName .matches (".*(?i)(Dao|Mapper)\\ .java" )) {
72
- IProject project = EditorUtil .getCurrentProject (currentEditor );
73
- IJavaProject javaProj = JavaCore .create (project );
74
-
75
- String namespace = null ;
76
- if (wholeSqlId != null ) {
77
- int index = wholeSqlId .lastIndexOf ("." );
78
- namespace = wholeSqlId .substring (0 , index );
79
- sqlId = wholeSqlId .substring (index + 1 );
80
- } else {
53
+ IFile file = currentEditor .getEditorInput ().getAdapter (IFile .class );
54
+ String fileName = file .getName ();
55
+ if (fileName .matches (FindMapperXml .MATCH_REG )) {
56
+ String namespace = null ;
57
+ if (wholeSqlId != null ) {
58
+ int index = wholeSqlId .lastIndexOf ("." );
59
+ namespace = wholeSqlId .substring (0 , index );
60
+ if (namespace .trim ().length () == 0 ) {
81
61
String packName = TextUtil .getJavaPackage (file );
82
62
namespace = new StringBuilder (packName ).append ("." ).append (fileName .substring (0 , fileName .lastIndexOf ("." ))).toString ();
83
- sqlId = hoverStr ;
84
63
}
64
+ sqlId = wholeSqlId .substring (index + 1 );
65
+ return new String [] { namespace , sqlId };
66
+ } else {
67
+ String packName = TextUtil .getJavaPackage (file );
68
+ namespace = new StringBuilder (packName ).append ("." ).append (fileName .substring (0 , fileName .lastIndexOf ("." ))).toString ();
69
+ sqlId = hoverStr ;
70
+ return new String [] { namespace , sqlId };
71
+ }
72
+ }
73
+ return null ;
74
+ }
85
75
86
- List <IFile > lst = MapperNamespaceCache .getInstance ().get (javaProj , namespace , null );
87
- if (lst != null ) {
88
- for (IFile mapperFile : lst ) {
89
- IDOMDocument mapperDocument = XmlUtil .getMapperDocument (mapperFile );
90
- Node node = XpathUtil .xpathNode (mapperDocument , "//*[@id='" + sqlId + "']" );
91
- if (node != null ) {
92
- StringBuilder buffer = new StringBuilder ();
93
- XmlUtil .computeStatementText ((ElementImpl ) node , buffer , javaProj );
94
-
95
- String s = buffer .toString ();
96
- buffer .delete (0 , buffer .length ());
97
-
98
- buffer .append ("|  Tips: button1 open sql xml file; button2 copy sql to clipboard. <br>" );
99
- buffer .append (
100
- "|-----------------------------------------------------------------------------------------------------------------------<br>" );
101
-
102
- s = TextUtil .htmlEncode (s );
103
- s = s .replaceAll ("\t " , "  " );
104
-
105
- // s = s.replaceAll("\n", "<br>");
106
- for (String str : s .split ("\n " )) {
107
- buffer .append ("|  " ).append (str ).append ("<br>" );
108
- }
109
- s = buffer .toString ();
76
+ @ Override
77
+ public String getHoverInfo (ITextViewer viewer , IRegion region ) {
78
+ this .currentViewer = viewer ;
79
+ this .currentRegion = region ;
110
80
111
- return s ;
112
- }
81
+ try {
82
+ IProject project = EditorUtil .getCurrentProject (currentEditor );
83
+ IJavaProject javaProj = JavaCore .create (project );
84
+
85
+ String [] arr = getNamespaceAndSqlid ();
86
+ if (arr != null ) {
87
+ String namespace = arr [0 ];
88
+ String sqlId = arr [1 ];
89
+ String s = FindMapperXml .findSql (javaProj , namespace , sqlId );
90
+ if (s != null ) {
91
+ StringBuilder buffer = new StringBuilder ();
92
+ buffer .append ("|  Tips: button1 open sql xml file; button2 copy sql to clipboard. <br>" );
93
+ buffer .append (
94
+ "|-----------------------------------------------------------------------------------------------------------------------<br>" );
95
+
96
+ s = TextUtil .htmlEncode (s );
97
+ s = s .replaceAll ("\t " , "  " );
98
+
99
+ // s = s.replaceAll("\n", "<br>");
100
+ for (String str : s .split ("\n " )) {
101
+ buffer .append ("|  " ).append (str ).append ("<br>" );
113
102
}
103
+ s = buffer .toString ();
104
+ return s ;
114
105
}
115
- } else {
116
- return null ;
117
106
}
107
+ return null ;
118
108
} catch (BadLocationException e ) {
119
109
Activator .log (e );
120
110
} catch (XPathExpressionException e ) {
121
111
Activator .log (e );
112
+ } catch (InterruptedException e ) {
113
+ Activator .log (e );
122
114
}
123
115
return null ;
124
116
}
@@ -143,26 +135,32 @@ public IInformationControlCreator getInformationPresenterControlCreator() {
143
135
IAction action1 = new Action () {
144
136
public void run () {
145
137
try {
146
- String sqlId = currentViewer .getDocument ().get (currentRegion .getOffset (), currentRegion .getLength ());
147
138
IFile file = currentEditor .getEditorInput ().getAdapter (IFile .class );
148
139
String fileName = file .getName ();
149
- if (fileName .matches (".*(?i)(Dao|Mapper)\\ .java" )) {
150
- String packName = TextUtil .getJavaPackage (file );
151
- String namespace = new StringBuilder (packName ).append ("." ).append (fileName .substring (0 , fileName .lastIndexOf ("." )))
152
- .toString ();
153
-
154
- IProject project = EditorUtil .getCurrentProject (currentEditor );
155
- IJavaProject javaProj = JavaCore .create (project );
156
- List <IFile > lst = MapperNamespaceCache .getInstance ().get (javaProj , namespace , null );
157
-
158
- if (lst != null ) {
159
- MatchFile mf = XmlUtil .getMatchFileById (lst , sqlId );
160
- IFile fileToOpen = mf .getFile ();
161
- EditorUtil .openFile (fileToOpen , mf .getMatchLineNo ());
140
+ if (fileName .matches (FindMapperXml .MATCH_REG )) {
141
+ String [] arr = getNamespaceAndSqlid ();
142
+ if (arr != null ) {
143
+ String namespace = arr [0 ];
144
+ String sqlId = arr [1 ];
145
+
146
+ IProject project = EditorUtil .getCurrentProject (currentEditor );
147
+ IJavaProject javaProj = JavaCore .create (project );
148
+ IFile f = FindMapperXml .findSqlXml (javaProj , namespace , sqlId );
149
+ if (f != null ) {
150
+ List <IFile > lst = Arrays .asList (f );
151
+
152
+ MatchFile mf = XmlUtil .getMatchFileById (lst , sqlId );
153
+ IFile fileToOpen = mf .getFile ();
154
+ EditorUtil .openFile (fileToOpen , mf .getMatchLineNo ());
155
+ }
162
156
}
163
157
}
164
158
} catch (BadLocationException e ) {
165
159
Activator .log (e );
160
+ } catch (XPathExpressionException e ) {
161
+ Activator .log (e );
162
+ } catch (InterruptedException e ) {
163
+ Activator .log (e );
166
164
}
167
165
}
168
166
};
@@ -174,45 +172,32 @@ public void run() {
174
172
IAction action2 = new Action () {
175
173
public void run () {
176
174
try {
177
- String sqlId = currentViewer .getDocument ().get (currentRegion .getOffset (), currentRegion .getLength ());
178
175
IFile file = currentEditor .getEditorInput ().getAdapter (IFile .class );
179
176
String fileName = file .getName ();
180
- if (fileName .matches (".*(?i)(Dao|Mapper)\\ .java" )) {
181
- String packName = TextUtil .getJavaPackage (file );
182
- String namespace = new StringBuilder (packName ).append ("." ).append (fileName .substring (0 , fileName .lastIndexOf ("." )))
183
- .toString ();
184
-
185
- IProject project = EditorUtil .getCurrentProject (currentEditor );
186
- IJavaProject javaProj = JavaCore .create (project );
187
- List <IFile > lst = MapperNamespaceCache .getInstance ().get (javaProj , namespace , null );
188
-
189
- if (lst != null )
190
- for (IFile mapperFile : lst ) {
191
- String charset = mapperFile .getCharset ();
192
- IDOMDocument mapperDocument = XmlUtil .getMapperDocument (mapperFile );
193
- Node node = XpathUtil .xpathNode (mapperDocument , "//*[@id='" + sqlId + "']" );
194
- if (node != null ) {
195
- StringBuilder buffer = new StringBuilder ();
196
- XmlUtil .computeStatementText ((ElementImpl ) node , buffer , javaProj );
197
-
198
- String s = buffer .toString ();
199
-
200
- Toolkit toolkit = Toolkit .getDefaultToolkit ();
201
- java .awt .datatransfer .Clipboard clipboard = toolkit .getSystemClipboard ();
202
- StringSelection stringSel = new StringSelection (s );
203
- clipboard .setContents (stringSel , null );
204
- return ;
205
- }
177
+ if (fileName .matches (FindMapperXml .MATCH_REG )) {
178
+ String [] arr = getNamespaceAndSqlid ();
179
+ if (arr != null ) {
180
+ String namespace = arr [0 ];
181
+ String sqlId = arr [1 ];
182
+
183
+ IProject project = EditorUtil .getCurrentProject (currentEditor );
184
+ IJavaProject javaProj = JavaCore .create (project );
185
+ String s = FindMapperXml .findSql (javaProj , namespace , sqlId );
186
+ if (s != null ) {
187
+ Toolkit toolkit = Toolkit .getDefaultToolkit ();
188
+ java .awt .datatransfer .Clipboard clipboard = toolkit .getSystemClipboard ();
189
+ StringSelection stringSel = new StringSelection (s );
190
+ clipboard .setContents (stringSel , null );
191
+ return ;
206
192
}
193
+ }
207
194
}
208
- } catch (PartInitException e ) {
209
- Activator .log (e );
210
195
} catch (BadLocationException e ) {
211
196
Activator .log (e );
212
- } catch (CoreException e ) {
213
- Activator .log (e );
214
197
} catch (XPathExpressionException e ) {
215
198
Activator .log (e );
199
+ } catch (InterruptedException e ) {
200
+ Activator .log (e );
216
201
}
217
202
}
218
203
};
0 commit comments