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

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