1
1
package com .neueda .jetbrains .plugin .graphdb .jetbrains .ui .console .params ;
2
2
3
+ import com .google .common .base .Throwables ;
3
4
import com .intellij .codeInsight .hint .HintManager ;
4
5
import com .intellij .json .JsonFileType ;
5
6
import com .intellij .openapi .application .ApplicationManager ;
6
7
import com .intellij .openapi .components .ServiceManager ;
7
8
import com .intellij .openapi .editor .Document ;
9
+ import com .intellij .openapi .editor .Editor ;
10
+ import com .intellij .openapi .editor .EditorFactory ;
8
11
import com .intellij .openapi .fileEditor .FileDocumentManager ;
9
12
import com .intellij .openapi .project .Project ;
10
13
import com .intellij .openapi .vfs .VirtualFile ;
11
- import com .intellij .ui .EditorTextField ;
12
14
import com .intellij .util .messages .MessageBus ;
13
15
import com .neueda .jetbrains .plugin .graphdb .jetbrains .ui .console .GraphConsoleView ;
14
16
import com .neueda .jetbrains .plugin .graphdb .jetbrains .ui .console .event .QueryParametersRetrievalErrorEvent ;
15
17
import com .neueda .jetbrains .plugin .graphdb .jetbrains .util .FileUtil ;
16
18
19
+ import javax .swing .*;
17
20
import java .awt .*;
18
- import java .io .IOException ;
19
21
20
22
import static com .neueda .jetbrains .plugin .graphdb .jetbrains .ui .console .event .QueryParametersRetrievalErrorEvent .*;
21
23
22
24
public class ParametersPanel implements ParametersProvider {
23
25
24
26
private static final FileDocumentManager FILE_DOCUMENT_MANAGER = FileDocumentManager .getInstance ();
25
27
26
- private EditorTextField editor ;
28
+ private Editor editor ;
29
+ private GraphConsoleView graphConsoleView ;
30
+ private MessageBus messageBus ;
31
+ private ParametersService service ;
27
32
28
33
public void initialize (GraphConsoleView graphConsoleView , Project project ) {
29
- MessageBus messageBus = project .getMessageBus ();
30
-
31
- editor = getJsonEditorFromScratchFile (project );
32
- setInitialContent (editor .getDocument ());
34
+ this .graphConsoleView = graphConsoleView ;
35
+ this .messageBus = project .getMessageBus ();
36
+ this .service = ServiceManager .getService (project , ParametersService .class );
37
+ setupEditor (project );
38
+ }
33
39
34
- graphConsoleView .getParametersTab ().add (editor , BorderLayout .CENTER );
40
+ public String getParametersJson () {
41
+ return editor .getDocument ().getText ();
42
+ }
35
43
36
- ParametersService service = ServiceManager .getService (project , ParametersService .class );
44
+ private void initializeUi () {
45
+ graphConsoleView .getParametersTab ().add (editor .getComponent (), BorderLayout .CENTER );
37
46
service .registerParametersProvider (this );
38
47
39
48
messageBus .connect ().subscribe (QueryParametersRetrievalErrorEvent .QUERY_PARAMETERS_RETRIEVAL_ERROR_EVENT_TOPIC ,
@@ -48,28 +57,31 @@ public void initialize(GraphConsoleView graphConsoleView, Project project) {
48
57
});
49
58
}
50
59
51
- private static EditorTextField getJsonEditorFromScratchFile (Project project ) {
52
- VirtualFile file ;
53
- try {
54
- file = FileUtil .getScratchFile (project , "Neo4jGraphDbConsoleParametersPanel" );
55
- } catch (IOException e ) {
56
- throw new RuntimeException (e );
57
- }
58
- Document document = FILE_DOCUMENT_MANAGER .getDocument (file );
59
-
60
- return new EditorTextField (document , project , new JsonFileType (), false , false );
60
+ private void setupEditor (Project project ) {
61
+ ApplicationManager .getApplication ().runWriteAction (() -> {
62
+ try {
63
+ VirtualFile file = FileUtil .getScratchFile (project , "Neo4jGraphDbConsoleParametersPanel.json" );
64
+ Document document = FILE_DOCUMENT_MANAGER .getDocument (file );
65
+ editor = createEditor (project , document );
66
+ editor .setHeaderComponent (new JLabel ("Provide query parameters in JSON format here:" ));
67
+ setInitialContent (document );
68
+ initializeUi ();
69
+ } catch (Throwable e ) {
70
+ throw Throwables .propagate (e );
71
+ }
72
+ });
61
73
}
62
74
63
75
private void setInitialContent (Document document ) {
64
76
if (document .getText ().isEmpty ()) {
65
- final Runnable setTextRunner = () -> document .setText ("// Provide query parameters in JSON format here: " );
77
+ final Runnable setTextRunner = () -> document .setText ("{} " );
66
78
ApplicationManager .getApplication ()
67
79
.invokeLater (() -> ApplicationManager .getApplication ().runWriteAction (setTextRunner ));
68
80
}
69
81
}
70
82
71
- public String getParametersJson ( ) {
72
- return editor . getText ( );
83
+ private static Editor createEditor ( Project project , Document document ) {
84
+ return EditorFactory . getInstance (). createEditor ( document , project , new JsonFileType (), false );
73
85
}
74
86
75
87
}
0 commit comments