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

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