You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateDialog.java 8.3KB

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