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.5KB

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