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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.business.Helper;
  18. import com.rthoni.intellij.codefromds.dbo.ColumnSelection;
  19. import com.rthoni.intellij.codefromds.dbo.DataSourceSelection;
  20. import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
  21. import com.rthoni.intellij.codefromds.dbo.TableSelection;
  22. import org.jetbrains.annotations.NotNull;
  23. import org.jetbrains.annotations.Nullable;
  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.event.ActionListener;
  29. import java.io.File;
  30. import java.util.Arrays;
  31. import java.util.Collection;
  32. import java.util.List;
  33. import java.util.Vector;
  34. import java.util.function.Consumer;
  35. import java.util.stream.Collectors;
  36. /**
  37. * Created by robin on 11/14/16.
  38. */
  39. public class GenerateDialog extends DialogWrapper {
  40. private JPanel _panel;
  41. private JBList _listDatasources;
  42. private JBList _listTables;
  43. private JBList _listColumns;
  44. private TextFieldWithBrowseButton _textModels;
  45. private TextFieldWithBrowseButton _textDataSOurceTemplate;
  46. private TextFieldWithBrowseButton _textModelsTemplate;
  47. private TextFieldWithBrowseButton _textConfigPath;
  48. private GenerateOptions _options;
  49. public GenerateDialog(@Nullable Project project) {
  50. super(project);
  51. setTitle("Code FROM data source");
  52. setOptions(null);
  53. init();
  54. }
  55. public GenerateOptions getOptions() {
  56. return _options;
  57. }
  58. public void setOptions(GenerateOptions options) {
  59. _options = options;
  60. if (_options != null) {
  61. showSource(_options.getSelection());
  62. _textModels.setText(_options.getModelsPath());
  63. _textDataSOurceTemplate.setText(_options.getDataSourceTemplatePath());
  64. _textModelsTemplate.setText(_options.getModelsTemplatePath());
  65. _textConfigPath.setText(_options.getConfigPath());
  66. }
  67. else {
  68. showSource(null);
  69. _textModels.setText("");
  70. _textDataSOurceTemplate.setText("");
  71. _textModelsTemplate.setText("");
  72. _textConfigPath.setText("");
  73. }
  74. }
  75. @Nullable
  76. @Override
  77. protected ValidationInfo doValidate() {
  78. ValidationInfo info = null;
  79. File modelDir = _options == null ? null : new File(_options.getModelsPath());
  80. File dataSourceTemplatePath = _options == null ? null : new File(_options.getDataSourceTemplatePath());
  81. File modelsTemplatePath = _options == null ? null : new File(_options.getModelsTemplatePath());
  82. File configPath = _options == null ? null : new File(_options.getConfigPath());
  83. File configDir = _options == null ? null : new File(configPath.getParent());
  84. if (_options == null) {
  85. info = new ValidationInfo("No Data Source Selected", _listDatasources);
  86. }
  87. else if (!modelDir.exists() || !modelDir.isDirectory()) {
  88. info = new ValidationInfo("Models folder does not exists", _textModels.getTextField());
  89. }
  90. else if (!dataSourceTemplatePath.exists() || !dataSourceTemplatePath.isFile()) {
  91. info = new ValidationInfo("Data source template file does not exists", _textDataSOurceTemplate.getTextField());
  92. }
  93. else if (!modelsTemplatePath.exists() || !modelsTemplatePath.isFile()) {
  94. info = new ValidationInfo("Models template file does not exists", _textModelsTemplate.getTextField());
  95. }
  96. else if (!configDir.exists() || !configDir.isDirectory()) {
  97. info = new ValidationInfo("Configuration file parent folder does not exists", _textConfigPath.getTextField());
  98. }
  99. return info;
  100. }
  101. @Nullable
  102. @Override
  103. protected JComponent createCenterPanel() {
  104. List<DbDataSource> dataSources = Helper.getDataSources();
  105. _listDatasources.setListData(dataSources.stream().map(DasObject::getName).toArray(String[]::new));
  106. _listDatasources.addListSelectionListener(e -> {
  107. if (!e.getValueIsAdjusting()) {
  108. int index = _listDatasources.getSelectedIndex();
  109. if (index == -1) {
  110. setOptions(null);
  111. }
  112. else {
  113. setOptions(new GenerateOptions(dataSources.get(index)));
  114. }
  115. }
  116. });
  117. _listTables.addListSelectionListener(e -> {
  118. if (!e.getValueIsAdjusting()) {
  119. int index = _listTables.getSelectedIndex();
  120. if (index == -1) {
  121. showTable(null);
  122. }
  123. else {
  124. showTable(_options.getSelection().getTables().get(index));
  125. }
  126. }
  127. });
  128. setupTextField(_textModels, null, true);
  129. setupTextField(_textDataSOurceTemplate, null, false);
  130. setupTextField(_textModelsTemplate, null, false);
  131. setupTextField(_textConfigPath, null, false);
  132. setupTextFieldListener(_textModels.getTextField(), s -> _options.setModelsPath(s));
  133. setupTextFieldListener(_textDataSOurceTemplate.getTextField(), s -> _options.setDataSourceTemplatePath(s));
  134. setupTextFieldListener(_textModelsTemplate.getTextField(), s -> _options.setModelsTemplatePath(s));
  135. setupTextFieldListener(_textConfigPath.getTextField(), s -> _options.setConfigPath(s));
  136. _listTables.setCellRenderer(new ColoredListCellRenderer() {
  137. @Override
  138. protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
  139. TableSelection tableSelection = _options.getSelection().getTables().get(i);
  140. if (tableSelection.hasAll()) {
  141. setBackground(JBColor.GREEN);
  142. }
  143. else if (tableSelection.hasNone()) {
  144. setBackground(JBColor.RED);
  145. }
  146. else {
  147. setBackground(JBColor.ORANGE);
  148. }
  149. append(tableSelection.getTable().getName());
  150. }
  151. });
  152. if (dataSources.size() > 0) {
  153. _listDatasources.setSelectedIndices(new int[]{0});
  154. }
  155. return _panel;
  156. }
  157. private void setupTextFieldListener(JTextField field, Consumer<String> consumer)
  158. {
  159. field.getDocument().addDocumentListener(new DocumentListener() {
  160. @Override
  161. public void insertUpdate(DocumentEvent e) {
  162. changed();
  163. }
  164. @Override
  165. public void removeUpdate(DocumentEvent e) {
  166. changed();
  167. }
  168. @Override
  169. public void changedUpdate(DocumentEvent e) {
  170. changed();
  171. }
  172. public void changed() {
  173. consumer.accept(field.getText());
  174. }
  175. });
  176. }
  177. private void setupTextField(TextFieldWithBrowseButton field, Project project, boolean dirsOnly)
  178. {
  179. for (ActionListener l : field.getButton().getActionListeners()) {
  180. field.getButton().removeActionListener(l);
  181. }
  182. field.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder", project,
  183. dirsOnly ? FileChooserDescriptorFactory.createSingleFolderDescriptor() : FileChooserDescriptorFactory.createSingleFileDescriptor());
  184. }
  185. private void showSource(DataSourceSelection source)
  186. {
  187. if (source != null) {
  188. List<String> tables = source.getTables().stream().map(t -> t.getTable().getName()).collect(Collectors.toList());
  189. _listTables.setListData(tables.toArray(new String[tables.size()]));
  190. if (tables.size() > 0) {
  191. showTable(source.getTables().get(0));
  192. }
  193. else {
  194. showTable(null);
  195. }
  196. }
  197. else {
  198. _listTables.setListData(new String[]{});
  199. showTable(null);
  200. }
  201. }
  202. private int[] getSelectedIndices(final TableSelection table)
  203. {
  204. List<ColumnSelection> columns = table.getColumns();
  205. List<Integer> indices = new Vector<>();
  206. for (int i = 0; i < columns.size(); ++i) {
  207. if (columns.get(i).isSelected()) {
  208. indices.add(i);
  209. }
  210. }
  211. return indices.stream().mapToInt(Integer::intValue).toArray();
  212. }
  213. private void updateSelection(final TableSelection table)
  214. {
  215. List<ColumnSelection> columns = table.getColumns();
  216. for (int i = 0; i < columns.size(); ++i) {
  217. columns.get(i).setSelected(_listColumns.isSelectedIndex(i));
  218. }
  219. }
  220. private void showTable(TableSelection table)
  221. {
  222. for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
  223. _listColumns.removeListSelectionListener(e);
  224. }
  225. if (table != null) {
  226. _listColumns.setListData(table.getColumns().stream().map(c -> c.getColumn().getName()).toArray(String[]::new));
  227. int[] indices = getSelectedIndices(table);
  228. _listColumns.setSelectedIndices(indices);
  229. _listColumns.addListSelectionListener(e -> {
  230. if (!e.getValueIsAdjusting()) {
  231. _listTables.updateUI();
  232. updateSelection(table);
  233. }
  234. });
  235. }
  236. else {
  237. _listColumns.setListData(new String[]{});
  238. }
  239. }
  240. }