|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+package com.rthoni.intellij.codefromds.ui.dialogs;
|
|
|
2
|
+
|
|
|
3
|
+import com.intellij.database.model.DasObject;
|
|
|
4
|
+import com.intellij.database.model.DasTable;
|
|
|
5
|
+import com.intellij.database.psi.DbDataSource;
|
|
|
6
|
+import com.intellij.database.psi.DbPsiFacade;
|
|
|
7
|
+import com.intellij.database.util.DasUtil;
|
|
|
8
|
+import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
|
|
|
9
|
+import com.intellij.openapi.project.Project;
|
|
|
10
|
+import com.intellij.openapi.project.ProjectManager;
|
|
|
11
|
+import com.intellij.openapi.ui.DialogWrapper;
|
|
|
12
|
+import com.intellij.openapi.ui.TextFieldWithBrowseButton;
|
|
|
13
|
+import com.intellij.openapi.ui.ValidationInfo;
|
|
|
14
|
+import com.intellij.ui.ColoredListCellRenderer;
|
|
|
15
|
+import com.intellij.ui.JBColor;
|
|
|
16
|
+import com.intellij.ui.components.JBList;
|
|
|
17
|
+import com.rthoni.intellij.codefromds.dbo.ColumnSelection;
|
|
|
18
|
+import com.rthoni.intellij.codefromds.dbo.DataSourceSelection;
|
|
|
19
|
+import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
|
|
|
20
|
+import com.rthoni.intellij.codefromds.dbo.TableSelection;
|
|
|
21
|
+import org.jetbrains.annotations.NotNull;
|
|
|
22
|
+import org.jetbrains.annotations.Nullable;
|
|
|
23
|
+
|
|
|
24
|
+import javax.swing.*;
|
|
|
25
|
+import javax.swing.event.DocumentEvent;
|
|
|
26
|
+import javax.swing.event.DocumentListener;
|
|
|
27
|
+import javax.swing.event.ListSelectionListener;
|
|
|
28
|
+import java.awt.*;
|
|
|
29
|
+import java.awt.event.ActionListener;
|
|
|
30
|
+import java.io.File;
|
|
|
31
|
+import java.util.Arrays;
|
|
|
32
|
+import java.util.Collection;
|
|
|
33
|
+import java.util.List;
|
|
|
34
|
+import java.util.Vector;
|
|
|
35
|
+import java.util.stream.Collectors;
|
|
|
36
|
+
|
|
|
37
|
+/**
|
|
|
38
|
+ * Created by robin on 11/14/16.
|
|
|
39
|
+ */
|
|
|
40
|
+public class GenerateDialog extends DialogWrapper {
|
|
|
41
|
+
|
|
|
42
|
+ private JPanel _panel;
|
|
|
43
|
+ private JBList _listDatasources;
|
|
|
44
|
+ private JBList _listTables;
|
|
|
45
|
+ private JBList _listColumns;
|
|
|
46
|
+ private TextFieldWithBrowseButton _textModels;
|
|
|
47
|
+
|
|
|
48
|
+ private GenerateOptions _options;
|
|
|
49
|
+
|
|
|
50
|
+ public GenerateDialog(@Nullable Project project) {
|
|
|
51
|
+ super(project);
|
|
|
52
|
+ setTitle("Code FROM data source");
|
|
|
53
|
+ showSource(null);
|
|
|
54
|
+ init();
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ public GenerateOptions getOptions() {
|
|
|
58
|
+ return _options;
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ @Nullable
|
|
|
62
|
+ @Override
|
|
|
63
|
+ protected ValidationInfo doValidate() {
|
|
|
64
|
+ ValidationInfo info = null;
|
|
|
65
|
+ File modelDir = _options == null ? null : new File(_options.getModelsFolder());
|
|
|
66
|
+ if (_options == null) {
|
|
|
67
|
+ info = new ValidationInfo("No Data Source Selected", _listDatasources);
|
|
|
68
|
+ }
|
|
|
69
|
+ else if (!modelDir.exists() || !modelDir.isDirectory()) {
|
|
|
70
|
+ info = new ValidationInfo("Bad Model Folder", _textModels.getTextField());
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ return info;
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ @Nullable
|
|
|
77
|
+ @Override
|
|
|
78
|
+ protected JComponent createCenterPanel() {
|
|
|
79
|
+ ProjectManager pm = ProjectManager.getInstance();
|
|
|
80
|
+ Project[] projects = pm.getOpenProjects();
|
|
|
81
|
+
|
|
|
82
|
+ List<DbDataSource> dataSources = Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources()).flatMap(Collection::stream).collect(Collectors.toList());
|
|
|
83
|
+
|
|
|
84
|
+ _listDatasources.setListData(dataSources.stream().map(DasObject::getName).toArray(String[]::new));
|
|
|
85
|
+ _listDatasources.addListSelectionListener(e -> {
|
|
|
86
|
+ if (!e.getValueIsAdjusting()) {
|
|
|
87
|
+ int index = _listDatasources.getSelectedIndex();
|
|
|
88
|
+ if (index == -1) {
|
|
|
89
|
+ showSource(null);
|
|
|
90
|
+ }
|
|
|
91
|
+ else {
|
|
|
92
|
+ showSource(dataSources.get(index));
|
|
|
93
|
+ }
|
|
|
94
|
+ }
|
|
|
95
|
+ });
|
|
|
96
|
+ _listTables.addListSelectionListener(e -> {
|
|
|
97
|
+ if (!e.getValueIsAdjusting()) {
|
|
|
98
|
+ int index = _listTables.getSelectedIndex();
|
|
|
99
|
+ if (index == -1) {
|
|
|
100
|
+ showTable(null);
|
|
|
101
|
+ }
|
|
|
102
|
+ else {
|
|
|
103
|
+ showTable(_options.getSelection().getTables().get(index));
|
|
|
104
|
+ }
|
|
|
105
|
+ }
|
|
|
106
|
+ });
|
|
|
107
|
+ _textModels.getTextField().getDocument().addDocumentListener(new DocumentListener() {
|
|
|
108
|
+ @Override
|
|
|
109
|
+ public void insertUpdate(DocumentEvent e) {
|
|
|
110
|
+ changed();
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ @Override
|
|
|
114
|
+ public void removeUpdate(DocumentEvent e) {
|
|
|
115
|
+ changed();
|
|
|
116
|
+ }
|
|
|
117
|
+
|
|
|
118
|
+ @Override
|
|
|
119
|
+ public void changedUpdate(DocumentEvent e) {
|
|
|
120
|
+ changed();
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ public void changed() {
|
|
|
124
|
+ _options.setModelsFolder(_textModels.getText());
|
|
|
125
|
+ }
|
|
|
126
|
+ });
|
|
|
127
|
+
|
|
|
128
|
+ if (dataSources.size() > 0) {
|
|
|
129
|
+ _listDatasources.setSelectedIndices(new int[]{0});
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ return _panel;
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ private void showSource(DbDataSource source)
|
|
|
136
|
+ {
|
|
|
137
|
+ showTable(null);
|
|
|
138
|
+ for (ActionListener l : _textModels.getButton().getActionListeners()) {
|
|
|
139
|
+ _textModels.getButton().removeActionListener(l);
|
|
|
140
|
+ }
|
|
|
141
|
+ if (source != null) {
|
|
|
142
|
+ _options = new GenerateOptions();
|
|
|
143
|
+
|
|
|
144
|
+ _listTables.setCellRenderer(new ColoredListCellRenderer() {
|
|
|
145
|
+ @Override
|
|
|
146
|
+ protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
|
|
|
147
|
+ TableSelection tableSelection = _options.getSelection().getTables().get(i);
|
|
|
148
|
+ if (tableSelection.hasAll()) {
|
|
|
149
|
+ setBackground(JBColor.GREEN);
|
|
|
150
|
+ }
|
|
|
151
|
+ else if (tableSelection.hasNone()) {
|
|
|
152
|
+ setBackground(JBColor.RED);
|
|
|
153
|
+ }
|
|
|
154
|
+ else {
|
|
|
155
|
+ setBackground(JBColor.ORANGE);
|
|
|
156
|
+ }
|
|
|
157
|
+ append(tableSelection.getTable().getName());
|
|
|
158
|
+ }
|
|
|
159
|
+ });
|
|
|
160
|
+
|
|
|
161
|
+ _textModels.setText(source.getProject().getBasePath() + File.separator + "Models");
|
|
|
162
|
+ _textModels.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder",
|
|
|
163
|
+ source.getProject(), FileChooserDescriptorFactory.createSingleFolderDescriptor());
|
|
|
164
|
+
|
|
|
165
|
+ _options.setSelection(new DataSourceSelection());
|
|
|
166
|
+ _options.getSelection().setSource(source);
|
|
|
167
|
+ final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
|
|
|
168
|
+ List<TableSelection> tableSelections = new Vector<>();
|
|
|
169
|
+ for (DasTable table : tables) {
|
|
|
170
|
+ TableSelection tableSelection = new TableSelection();
|
|
|
171
|
+ tableSelection.setTable(table);
|
|
|
172
|
+ tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(c -> {
|
|
|
173
|
+ ColumnSelection selection = new ColumnSelection();
|
|
|
174
|
+ selection.setColumn(c);
|
|
|
175
|
+ selection.setSelected(true);
|
|
|
176
|
+ return selection;
|
|
|
177
|
+ }).collect(Collectors.toList()));
|
|
|
178
|
+ tableSelections.add(tableSelection);
|
|
|
179
|
+ }
|
|
|
180
|
+ _options.getSelection().setTables(tableSelections);
|
|
|
181
|
+
|
|
|
182
|
+ _listTables.setListData(tables.stream().map(DasObject::getName).toArray(String[]::new));
|
|
|
183
|
+ }
|
|
|
184
|
+ else {
|
|
|
185
|
+ _options = null;
|
|
|
186
|
+ _listTables.setListData(new String[]{});
|
|
|
187
|
+ }
|
|
|
188
|
+ }
|
|
|
189
|
+
|
|
|
190
|
+ private int[] getSelectedIndices(final TableSelection table)
|
|
|
191
|
+ {
|
|
|
192
|
+ List<ColumnSelection> columns = table.getColumns();
|
|
|
193
|
+ List<Integer> indices = new Vector<>();
|
|
|
194
|
+ for (int i = 0; i < columns.size(); ++i) {
|
|
|
195
|
+ if (columns.get(i).isSelected()) {
|
|
|
196
|
+ indices.add(i);
|
|
|
197
|
+ }
|
|
|
198
|
+ }
|
|
|
199
|
+ return indices.stream().mapToInt(Integer::intValue).toArray();
|
|
|
200
|
+ }
|
|
|
201
|
+
|
|
|
202
|
+ private void updateSelection(final TableSelection table)
|
|
|
203
|
+ {
|
|
|
204
|
+ List<ColumnSelection> columns = table.getColumns();
|
|
|
205
|
+ for (int i = 0; i < columns.size(); ++i) {
|
|
|
206
|
+ columns.get(i).setSelected(_listColumns.isSelectedIndex(i));
|
|
|
207
|
+ }
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ private void showTable(TableSelection table)
|
|
|
211
|
+ {
|
|
|
212
|
+ if (table != null) {
|
|
|
213
|
+ for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
|
|
|
214
|
+ _listColumns.removeListSelectionListener(e);
|
|
|
215
|
+ }
|
|
|
216
|
+ _listColumns.setListData(table.getColumns().stream().map(c -> c.getColumn().getName()).toArray(String[]::new));
|
|
|
217
|
+ int[] indices = getSelectedIndices(table);
|
|
|
218
|
+ _listColumns.setSelectedIndices(indices);
|
|
|
219
|
+ _listColumns.addListSelectionListener(e -> {
|
|
|
220
|
+ if (!e.getValueIsAdjusting()) {
|
|
|
221
|
+ _listTables.updateUI();
|
|
|
222
|
+ updateSelection(table);
|
|
|
223
|
+ }
|
|
|
224
|
+ });
|
|
|
225
|
+ }
|
|
|
226
|
+ else {
|
|
|
227
|
+ _listColumns.setListData(new String[]{});
|
|
|
228
|
+ }
|
|
|
229
|
+ }
|
|
|
230
|
+}
|