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

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