Browse Source

refactor dialog and created constructor for dbo

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

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

6
  * Created by robin on 11/15/16.
6
  * Created by robin on 11/15/16.
7
  */
7
  */
8
 public class ColumnSelection {
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
     private DasColumn _column;
19
     private DasColumn _column;
10
 
20
 
11
     private boolean _selected;
21
     private boolean _selected;

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

1
 package com.rthoni.intellij.codefromds.dbo;
1
 package com.rthoni.intellij.codefromds.dbo;
2
 
2
 
3
+import com.intellij.database.model.DasTable;
3
 import com.intellij.database.psi.DbDataSource;
4
 import com.intellij.database.psi.DbDataSource;
5
+import com.intellij.database.util.DasUtil;
4
 
6
 
5
 import java.util.List;
7
 import java.util.List;
8
+import java.util.Vector;
9
+import java.util.stream.Collectors;
6
 
10
 
7
 /**
11
 /**
8
  * Created by robin on 11/15/16.
12
  * Created by robin on 11/15/16.
9
  */
13
  */
10
 public class DataSourceSelection {
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
     private DbDataSource _source;
31
     private DbDataSource _source;
12
 
32
 
13
     private List<TableSelection> _tables;
33
     private List<TableSelection> _tables;

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

1
 package com.rthoni.intellij.codefromds.dbo;
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
  * Created by robin on 11/15/16.
13
  * Created by robin on 11/15/16.
5
  */
14
  */
6
 public class GenerateOptions {
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
     private DataSourceSelection _selection;
43
     private DataSourceSelection _selection;
8
 
44
 
9
     private String _modelsPath;
45
     private String _modelsPath;

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

25
         int res = dlg.getExitCode();
25
         int res = dlg.getExitCode();
26
         if (res == DialogWrapper.OK_EXIT_CODE) {
26
         if (res == DialogWrapper.OK_EXIT_CODE) {
27
             GenerateOptions options = dlg.getOptions();
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
         return _options;
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
     @Nullable
91
     @Nullable
65
     @Override
92
     @Override
66
     protected ValidationInfo doValidate() {
93
     protected ValidationInfo doValidate() {
89
             if (!e.getValueIsAdjusting()) {
116
             if (!e.getValueIsAdjusting()) {
90
                 int index = _listDatasources.getSelectedIndex();
117
                 int index = _listDatasources.getSelectedIndex();
91
                 if (index == -1) {
118
                 if (index == -1) {
92
-                    showSource(null);
119
+                    setOptions(null);
93
                 }
120
                 }
94
                 else {
121
                 else {
95
-                    showSource(dataSources.get(index));
122
+                    setOptions(new GenerateOptions(dataSources.get(index)));
96
                 }
123
                 }
97
             }
124
             }
98
         });
125
         });
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
         if (dataSources.size() > 0) {
175
         if (dataSources.size() > 0) {
132
             _listDatasources.setSelectedIndices(new int[]{0});
176
             _listDatasources.setSelectedIndices(new int[]{0});
133
         }
177
         }
135
         return _panel;
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
         else {
203
         else {
188
-            _options = null;
189
             _listTables.setListData(new String[]{});
204
             _listTables.setListData(new String[]{});
205
+            showTable(null);
190
         }
206
         }
191
     }
207
     }
192
 
208
 
212
 
228
 
213
     private void showTable(TableSelection table)
229
     private void showTable(TableSelection table)
214
     {
230
     {
231
+        for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
232
+            _listColumns.removeListSelectionListener(e);
233
+        }
215
         if (table != null) {
234
         if (table != null) {
216
-            for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
217
-                _listColumns.removeListSelectionListener(e);
218
-            }
219
             _listColumns.setListData(table.getColumns().stream().map(c -> c.getColumn().getName()).toArray(String[]::new));
235
             _listColumns.setListData(table.getColumns().stream().map(c -> c.getColumn().getName()).toArray(String[]::new));
220
             int[] indices = getSelectedIndices(table);
236
             int[] indices = getSelectedIndices(table);
221
             _listColumns.setSelectedIndices(indices);
237
             _listColumns.setSelectedIndices(indices);

Loading…
Cancel
Save