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

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