1- package io .github .linyimin .plugin .view ;
1+ package io .github .linyimin .plugin .ui ;
22
3- import com .intellij .json .JsonFileType ;
4- import com .intellij .json .JsonLanguage ;
5- import com .intellij .openapi .fileTypes .PlainTextFileType ;
6- import com .intellij .openapi .fileTypes .PlainTextLanguage ;
73import com .intellij .openapi .project .Project ;
84import com .intellij .openapi .ui .Messages ;
95import com .intellij .openapi .ui .SimpleToolWindowPanel ;
106import com .intellij .openapi .wm .ToolWindow ;
7+ import com .intellij .util .ui .JBUI ;
118import io .github .linyimin .plugin .constant .Constant ;
129import io .github .linyimin .plugin .service .MybatisSqlStateComponent ;
1310import io .github .linyimin .plugin .service .SqlParamGenerateService ;
1411import io .github .linyimin .plugin .service .model .MybatisSqlConfiguration ;
1512import io .github .linyimin .plugin .utils .MybatisSqlUtils ;
16- import org .jetbrains .annotations .NotNull ;
13+ import org .fife .ui .rsyntaxtextarea .RSyntaxTextArea ;
14+ import org .fife .ui .rtextarea .RTextScrollPane ;
1715
1816import javax .swing .*;
17+ import javax .swing .border .EmptyBorder ;
1918import javax .swing .event .DocumentEvent ;
2019import javax .swing .event .DocumentListener ;
20+ import java .awt .*;
2121import java .sql .SQLException ;
2222
2323
2626 * @date 2022/02/01 12:31 下午
2727 **/
2828public class MybatisSqlViewerToolWindow extends SimpleToolWindowPanel {
29+
2930 private JTextField methodName ;
3031 private JTabbedPane tabbedPane ;
3132 private JTextField host ;
@@ -37,11 +38,17 @@ public class MybatisSqlViewerToolWindow extends SimpleToolWindowPanel {
3738 private JButton connectionTestButton ;
3839 private JPanel root ;
3940 private JTextArea connectionInfoTextArea ;
40- private JPanel params ;
41+
4142 private JTextArea result ;
42- private JPanel sql ;
43- private JScrollPane sqlScroll ;
44- private JScrollPane paramsScroll ;
43+
44+ private final RSyntaxTextArea sqlText ;
45+ private JPanel sqlPanel ;
46+ private final RTextScrollPane sqlScroll ;
47+
48+ private JPanel paramsPanel ;
49+ private final RTextScrollPane paramsScroll ;
50+ private final RSyntaxTextArea paramsText ;
51+
4552 private JScrollPane resultScroll ;
4653
4754 private final Project myProject ;
@@ -61,9 +68,29 @@ public JPanel getContent() {
6168 }
6269
6370 public MybatisSqlViewerToolWindow (ToolWindow toolWindow , Project project ) {
71+
6472 super (true , false );
6573 this .myProject = project ;
74+
75+ paramsText = CustomTextField .createArea ("json" );
76+ sqlText = CustomTextField .createArea ("sql" );
77+
78+ sqlPanel .setLayout (new BorderLayout ());
79+
80+ sqlScroll = new RTextScrollPane (sqlText );
81+ sqlScroll .setBorder (new EmptyBorder (JBUI .emptyInsets ()));
82+ sqlPanel .add (sqlScroll );
83+
84+ paramsPanel .setLayout (new BorderLayout ());
85+
86+ paramsScroll = new RTextScrollPane (paramsText );
87+ paramsScroll .setBorder (new EmptyBorder (JBUI .emptyInsets ()));
88+ paramsPanel .add (paramsScroll );
89+
90+ setScrollUnitIncrement ();
91+
6692 addComponentListener ();
93+
6794 }
6895
6996
@@ -75,34 +102,43 @@ public void refresh(Project project) {
75102 assert config != null ;
76103
77104 methodName .setText (config .getMethod ());
78- result .setText (config .getResult ());
79- ((MyTextField ) params ).setText (config .getParams ());
80105
81- (( MyTextField ) sql ) .setText (config .getSql ());
106+ paramsText .setText (config .getParams ());
82107
83- // 默认每次打开,都展示第一个tab
84- tabbedPane .setSelectedIndex (0 );
108+ sqlText .setText (config .getSql ());
85109
86- setScrollUnitIncrement ( );
110+ result . setText ( config . getResult () );
87111
88- }
112+ // 默认每次打开,都展示第一个tab
113+ tabbedPane .setSelectedIndex (0 );
89114
90- private void createUIComponents () {
91- params = new MyTextField (this .myProject , JsonLanguage .INSTANCE , JsonFileType .INSTANCE );
92- sql = new MyTextField (this .myProject , PlainTextLanguage .INSTANCE , PlainTextFileType .INSTANCE );
93115 }
94116
95117 private void addComponentListener () {
96118 host .getDocument ().addDocumentListener (new DatasourceChangeListener ());
97119 port .getDocument ().addDocumentListener (new DatasourceChangeListener ());
98120 database .getDocument ().addDocumentListener (new DatasourceChangeListener ());
99121
100- ((MyTextField ) params ).addDocumentListener (new com .intellij .openapi .editor .event .DocumentListener () {
122+ paramsText .getDocument ().addDocumentListener (new DocumentListener () {
123+ @ Override
124+ public void insertUpdate (DocumentEvent e ) {
125+ updateParams ();
126+ }
127+
101128 @ Override
102- public void documentChanged (com .intellij .openapi .editor .event .@ NotNull DocumentEvent event ) {
129+ public void removeUpdate (DocumentEvent e ) {
130+ updateParams ();
131+ }
132+
133+ @ Override
134+ public void changedUpdate (DocumentEvent e ) {
135+ updateParams ();
136+ }
137+
138+ private void updateParams () {
103139 MybatisSqlConfiguration config = myProject .getService (MybatisSqlStateComponent .class ).getState ();
104140 assert config != null ;
105- config .setParams ((( MyTextField ) params ) .getText ());
141+ config .setParams (paramsText .getText ());
106142 }
107143 });
108144
@@ -128,7 +164,7 @@ public void documentChanged(com.intellij.openapi.editor.event.@NotNull DocumentE
128164
129165 // 点击sql tab时生成sql
130166 if (selectedIndex == TabbedComponentType .sql .index ) {
131- (( MyTextField ) sql ) .setText ("Loading..." );
167+ sqlText .setText ("Loading..." );
132168 generateSql ();
133169 }
134170
@@ -151,7 +187,7 @@ private void generateSql() {
151187 String sqlStr = generateService .generateSql (myProject , sqlConfig .getMethod (), sqlConfig .getParams ());
152188 sqlConfig .setSql (sqlStr );
153189
154- (( MyTextField ) sql ) .setText (sqlStr );
190+ sqlText .setText (sqlStr );
155191 } catch (Throwable e ) {
156192 Messages .showInfoMessage ("generate sql error. err: " + e .getMessage (), Constant .APPLICATION_NAME );
157193 }
@@ -163,7 +199,7 @@ private void executeSql() {
163199 String passwordText = String .valueOf (password .getPassword ());
164200 String resultText ;
165201 try {
166- resultText = MybatisSqlUtils .executeSql (urlText , user .getText (), passwordText , (( MyTextField ) sql ) .getText ());
202+ resultText = MybatisSqlUtils .executeSql (urlText , user .getText (), passwordText , sqlText .getText ());
167203 } catch (SQLException e ) {
168204 resultText = "Execute Sql Failed. err: " + e .getMessage ();
169205 }
@@ -202,6 +238,10 @@ private void updateUrlTextField() {
202238 }
203239 }
204240
241+ private void scrollPanelConfig () {
242+
243+ }
244+
205245 private void setScrollUnitIncrement () {
206246 int unit = 16 ;
207247 this .sqlScroll .getVerticalScrollBar ().setUnitIncrement (unit );
0 commit comments