Browse Source

refactor dialog and created constructor for dbo

tags/v1.1.0
Robin Thoni 7 years ago
parent
commit
fb146826b1

+ 10
- 0
src/com/rthoni/intellij/codefromds/dbo/ColumnSelection.java View File

@@ -6,6 +6,16 @@ import com.intellij.database.model.DasColumn;
6 6
  * Created by robin on 11/15/16.
7 7
  */
8 8
 public class ColumnSelection {
9
+
10
+    public ColumnSelection() {
11
+
12
+    }
13
+
14
+    public ColumnSelection(DasColumn column) {
15
+        _column = column;
16
+        _selected = true;
17
+    }
18
+
9 19
     private DasColumn _column;
10 20
 
11 21
     private boolean _selected;

+ 20
- 0
src/com/rthoni/intellij/codefromds/dbo/DataSourceSelection.java View File

@@ -1,13 +1,33 @@
1 1
 package com.rthoni.intellij.codefromds.dbo;
2 2
 
3
+import com.intellij.database.model.DasTable;
3 4
 import com.intellij.database.psi.DbDataSource;
5
+import com.intellij.database.util.DasUtil;
4 6
 
5 7
 import java.util.List;
8
+import java.util.Vector;
9
+import java.util.stream.Collectors;
6 10
 
7 11
 /**
8 12
  * Created by robin on 11/15/16.
9 13
  */
10 14
 public class DataSourceSelection {
15
+
16
+    public DataSourceSelection() {
17
+    }
18
+
19
+    public DataSourceSelection(DbDataSource source) {
20
+        _source = source;
21
+        final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
22
+        _tables = new Vector<>();
23
+        for (DasTable table : tables) {
24
+            TableSelection tableSelection = new TableSelection();
25
+            tableSelection.setTable(table);
26
+            tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(ColumnSelection::new).collect(Collectors.toList()));
27
+            _tables.add(tableSelection);
28
+        }
29
+    }
30
+
11 31
     private DbDataSource _source;
12 32
 
13 33
     private List<TableSelection> _tables;

+ 36
- 0
src/com/rthoni/intellij/codefromds/dbo/GenerateOptions.java View File

@@ -1,9 +1,45 @@
1 1
 package com.rthoni.intellij.codefromds.dbo;
2 2
 
3
+import com.intellij.database.model.DasTable;
4
+import com.intellij.database.psi.DbDataSource;
5
+import com.intellij.database.util.DasUtil;
6
+
7
+import java.io.File;
8
+import java.util.List;
9
+import java.util.Vector;
10
+import java.util.stream.Collectors;
11
+
3 12
 /**
4 13
  * Created by robin on 11/15/16.
5 14
  */
6 15
 public class GenerateOptions {
16
+
17
+    public GenerateOptions() {
18
+    }
19
+
20
+    public GenerateOptions(DbDataSource source) {
21
+        _modelsPath = source.getProject().getBasePath() + File.separator + "Models";
22
+        _dataSourceTemplatePath = source.getProject().getBasePath() + File.separator + "Templates" + File.separator + "DataSource.twig";
23
+        _modelsTemplatePath = source.getProject().getBasePath() + File.separator + "Templates" + File.separator + "Models.twig";
24
+        _configPath = source.getProject().getBasePath() + File.separator + "code-from-ds.json";
25
+        _selection = new DataSourceSelection(source);
26
+        _selection.setSource(source);
27
+        final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
28
+        List<TableSelection> tableSelections = new Vector<>();
29
+        for (DasTable table : tables) {
30
+            TableSelection tableSelection = new TableSelection();
31
+            tableSelection.setTable(table);
32
+            tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(c -> {
33
+                ColumnSelection selection = new ColumnSelection();
34
+                selection.setColumn(c);
35
+                selection.setSelected(true);
36
+                return selection;
37
+            }).collect(Collectors.toList()));
38
+            tableSelections.add(tableSelection);
39
+        }
40
+        _selection.setTables(tableSelections);
41
+    }
42
+
7 43
     private DataSourceSelection _selection;
8 44
 
9 45
     private String _modelsPath;

+ 3
- 1
src/com/rthoni/intellij/codefromds/ui/actions/GenerateAction.java View File

@@ -25,7 +25,9 @@ public class GenerateAction extends AnAction {
25 25
         int res = dlg.getExitCode();
26 26
         if (res == DialogWrapper.OK_EXIT_CODE) {
27 27
             GenerateOptions options = dlg.getOptions();
28
-            options.getSelection();
28
+            dlg = new GenerateDialog(null);
29
+            dlg.setOptions(options);
30
+            dlg.show();
29 31
         }
30 32
     }
31 33
 

+ 66
- 50
src/com/rthoni/intellij/codefromds/ui/dialogs/GenerateDialog.java View File

@@ -61,6 +61,33 @@ public class GenerateDialog extends DialogWrapper {
61 61
         return _options;
62 62
     }
63 63
 
64
+    public void setOptions(GenerateOptions options) {
65
+        _options = options;
66
+        if (_options != null) {
67
+            showSource(_options.getSelection());
68
+            Project project = _options.getSelection().getSource().getProject();
69
+            setupTextField(_textModels, project);
70
+            setupTextField(_textDataSOurceTemplate, project);
71
+            setupTextField(_textModelsTemplate, project);
72
+            setupTextField(_textConfigPath, project);
73
+            _textModels.setText(_options.getModelsPath());
74
+            _textDataSOurceTemplate.setText(_options.getDataSourceTemplatePath());
75
+            _textModelsTemplate.setText(_options.getModelsTemplatePath());
76
+            _textConfigPath.setText(_options.getConfigPath());
77
+        }
78
+        else {
79
+            showSource(null);
80
+            setupTextField(_textModels, null);
81
+            setupTextField(_textDataSOurceTemplate, null);
82
+            setupTextField(_textModelsTemplate, null);
83
+            setupTextField(_textConfigPath, null);
84
+            _textModels.setText("");
85
+            _textDataSOurceTemplate.setText("");
86
+            _textModelsTemplate.setText("");
87
+            _textConfigPath.setText("");
88
+        }
89
+    }
90
+
64 91
     @Nullable
65 92
     @Override
66 93
     protected ValidationInfo doValidate() {
@@ -89,10 +116,10 @@ public class GenerateDialog extends DialogWrapper {
89 116
             if (!e.getValueIsAdjusting()) {
90 117
                 int index = _listDatasources.getSelectedIndex();
91 118
                 if (index == -1) {
92
-                    showSource(null);
119
+                    setOptions(null);
93 120
                 }
94 121
                 else {
95
-                    showSource(dataSources.get(index));
122
+                    setOptions(new GenerateOptions(dataSources.get(index)));
96 123
                 }
97 124
             }
98 125
         });
@@ -128,6 +155,23 @@ public class GenerateDialog extends DialogWrapper {
128 155
             }
129 156
         });
130 157
 
158
+        _listTables.setCellRenderer(new ColoredListCellRenderer() {
159
+            @Override
160
+            protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
161
+                TableSelection tableSelection = _options.getSelection().getTables().get(i);
162
+                if (tableSelection.hasAll()) {
163
+                    setBackground(JBColor.GREEN);
164
+                }
165
+                else if (tableSelection.hasNone()) {
166
+                    setBackground(JBColor.RED);
167
+                }
168
+                else {
169
+                    setBackground(JBColor.ORANGE);
170
+                }
171
+                append(tableSelection.getTable().getName());
172
+            }
173
+        });
174
+
131 175
         if (dataSources.size() > 0) {
132 176
             _listDatasources.setSelectedIndices(new int[]{0});
133 177
         }
@@ -135,58 +179,30 @@ public class GenerateDialog extends DialogWrapper {
135 179
         return _panel;
136 180
     }
137 181
 
138
-    private void showSource(DbDataSource source)
182
+    private void setupTextField(TextFieldWithBrowseButton field, Project project)
139 183
     {
140
-        showTable(null);
141
-        for (ActionListener l : _textModels.getButton().getActionListeners()) {
142
-            _textModels.getButton().removeActionListener(l);
184
+        for (ActionListener l : field.getButton().getActionListeners()) {
185
+            field.getButton().removeActionListener(l);
143 186
         }
144
-        if (source != null) {
145
-            _options = new GenerateOptions();
146
-
147
-            _listTables.setCellRenderer(new ColoredListCellRenderer() {
148
-                @Override
149
-                protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
150
-                    TableSelection tableSelection = _options.getSelection().getTables().get(i);
151
-                    if (tableSelection.hasAll()) {
152
-                        setBackground(JBColor.GREEN);
153
-                    }
154
-                    else if (tableSelection.hasNone()) {
155
-                        setBackground(JBColor.RED);
156
-                    }
157
-                    else {
158
-                        setBackground(JBColor.ORANGE);
159
-                    }
160
-                    append(tableSelection.getTable().getName());
161
-                }
162
-            });
187
+        field.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder",
188
+                project, FileChooserDescriptorFactory.createSingleFolderDescriptor());
189
+    }
163 190
 
164
-            _textModels.setText(source.getProject().getBasePath() + File.separator + "Models");
165
-            _textModels.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder",
166
-                    source.getProject(), FileChooserDescriptorFactory.createSingleFolderDescriptor());
167
-
168
-            _options.setSelection(new DataSourceSelection());
169
-            _options.getSelection().setSource(source);
170
-            final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
171
-            List<TableSelection> tableSelections = new Vector<>();
172
-            for (DasTable table : tables) {
173
-                TableSelection tableSelection = new TableSelection();
174
-                tableSelection.setTable(table);
175
-                tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(c -> {
176
-                    ColumnSelection selection = new ColumnSelection();
177
-                    selection.setColumn(c);
178
-                    selection.setSelected(true);
179
-                    return selection;
180
-                }).collect(Collectors.toList()));
181
-                tableSelections.add(tableSelection);
191
+    private void showSource(DataSourceSelection source)
192
+    {
193
+        if (source != null) {
194
+            List<String> tables = source.getTables().stream().map(t -> t.getTable().getName()).collect(Collectors.toList());
195
+            _listTables.setListData(tables.toArray(new String[tables.size()]));
196
+            if (tables.size() > 0) {
197
+                showTable(source.getTables().get(0));
198
+            }
199
+            else {
200
+                showTable(null);
182 201
             }
183
-            _options.getSelection().setTables(tableSelections);
184
-
185
-            _listTables.setListData(tables.stream().map(DasObject::getName).toArray(String[]::new));
186 202
         }
187 203
         else {
188
-            _options = null;
189 204
             _listTables.setListData(new String[]{});
205
+            showTable(null);
190 206
         }
191 207
     }
192 208
 
@@ -212,10 +228,10 @@ public class GenerateDialog extends DialogWrapper {
212 228
 
213 229
     private void showTable(TableSelection table)
214 230
     {
231
+        for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
232
+            _listColumns.removeListSelectionListener(e);
233
+        }
215 234
         if (table != null) {
216
-            for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
217
-                _listColumns.removeListSelectionListener(e);
218
-            }
219 235
             _listColumns.setListData(table.getColumns().stream().map(c -> c.getColumn().getName()).toArray(String[]::new));
220 236
             int[] indices = getSelectedIndices(table);
221 237
             _listColumns.setSelectedIndices(indices);

Loading…
Cancel
Save