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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.DataSourcesBusiness;
  13. import com.rthoni.intellij.codefromds.business.Helper;
  14. import com.rthoni.intellij.codefromds.dbo.options.*;
  15. import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
  16. import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
  17. import com.rthoni.intellij.codefromds.dbo.template.StoredProcedureDbo;
  18. import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
  19. import org.jetbrains.annotations.NotNull;
  20. import org.jetbrains.annotations.Nullable;
  21. import javax.swing.*;
  22. import javax.swing.event.DocumentEvent;
  23. import javax.swing.event.DocumentListener;
  24. import javax.swing.event.ListSelectionListener;
  25. import java.awt.event.ActionListener;
  26. import java.io.File;
  27. import java.util.List;
  28. import java.util.Vector;
  29. import java.util.function.Consumer;
  30. import java.util.stream.Collectors;
  31. /**
  32. * Created by robin on 11/14/16.
  33. */
  34. public class GenerateDialog extends DialogWrapper {
  35. private JPanel _panel;
  36. private JBList _listDatasources;
  37. private JBList _listTables;
  38. private JBList _listColumns;
  39. private TextFieldWithBrowseButton _textModels;
  40. private TextFieldWithBrowseButton _textDataSource;
  41. private TextFieldWithBrowseButton _textDataSourceTemplate;
  42. private TextFieldWithBrowseButton _textModelsTemplate;
  43. private TextFieldWithBrowseButton _textConfigPath;
  44. private JTextField _textFilesExtension;
  45. private JLabel _lblModelsPath;
  46. private JLabel _lblDataSourcePath;
  47. private JLabel _lblDataSourceTemplatePath;
  48. private JLabel _lblModelsTemplatePath;
  49. private TextFieldWithBrowseButton _textCastFile;
  50. private JLabel _lblCastFile;
  51. private JBList _listStoredProcedure;
  52. private GenerateOptions _options;
  53. private Project _project;
  54. public GenerateDialog(Project project) {
  55. super(project);
  56. _project = project;
  57. setTitle("Code FROM data source");
  58. setOptions(null);
  59. init();
  60. }
  61. public GenerateOptions getOptions() {
  62. return _options;
  63. }
  64. public void setOptions(GenerateOptions options) {
  65. _options = options;
  66. if (_options != null) {
  67. showSource(_options.getDataSource());
  68. _textModels.setText(Helper.getAbsolutePath(_project, _options.getModelsRelativePath()));
  69. _textDataSource.setText(Helper.getAbsolutePath(_project, _options.getDataSourceRelativePath()));
  70. _textDataSourceTemplate.setText(Helper.getAbsolutePath(_project, _options.getDataSourceTemplateRelativePath()));
  71. _textModelsTemplate.setText(Helper.getAbsolutePath(_project, _options.getModelsTemplateRelativePath()));
  72. _textFilesExtension.setText(_options.getFilesExtension());
  73. _textCastFile.setText(Helper.getAbsolutePath(_project, _options.getCastFileRelativePath()));
  74. _textConfigPath.setText(_options.getConfigAbsolutePath());
  75. }
  76. else {
  77. showSource(null);
  78. _textModels.setText("");
  79. _textDataSource.setText("");
  80. _textDataSourceTemplate.setText("");
  81. _textModelsTemplate.setText("");
  82. _textFilesExtension.setText("");
  83. _textCastFile.setText("");
  84. _textConfigPath.setText("");
  85. }
  86. }
  87. @Nullable
  88. @Override
  89. protected ValidationInfo doValidate() {
  90. ValidationInfo info = null;
  91. File modelDir = _options == null ? null : new File(Helper.getAbsolutePath(_project, _options.getModelsRelativePath()));
  92. File dataSourcePath = _options == null ? null : new File(Helper.getAbsolutePath(_project, _options.getDataSourceRelativePath()));
  93. File dataSourceDir = _options == null ? null : new File(dataSourcePath.getParent());
  94. File dataSourceTemplatePath = _options == null ? null : new File(Helper.getAbsolutePath(_project, _options.getDataSourceTemplateRelativePath()));
  95. File modelsTemplatePath = _options == null ? null : new File(Helper.getAbsolutePath(_project, _options.getModelsTemplateRelativePath()));
  96. File configPath = _options == null ? null : new File(_options.getConfigAbsolutePath());
  97. File configDir = _options == null ? null : new File(configPath.getParent());
  98. String extension = _options == null ? null : _options.getFilesExtension();
  99. File castFilePath = _options == null ? null : new File(Helper.getAbsolutePath(_project, _options.getCastFileRelativePath()));
  100. if (_options == null) {
  101. info = new ValidationInfo("No Data Source Selected", _listDatasources);
  102. }
  103. else if (!modelDir.exists() || !modelDir.isDirectory()) {
  104. info = new ValidationInfo("Models folder does not exists", _textModels.getTextField());
  105. }
  106. else if (!dataSourceDir.exists() || !dataSourceDir.isDirectory()) {
  107. info = new ValidationInfo("Data source folder does not exists", _textDataSource.getTextField());
  108. }
  109. else if (!dataSourceTemplatePath.exists() || !dataSourceTemplatePath.isFile()) {
  110. info = new ValidationInfo("Data source template file does not exists", _textDataSourceTemplate.getTextField());
  111. }
  112. else if (!modelsTemplatePath.exists() || !modelsTemplatePath.isFile()) {
  113. info = new ValidationInfo("Models template file does not exists", _textModelsTemplate.getTextField());
  114. }
  115. else if (!configDir.exists() || !configDir.isDirectory()) {
  116. info = new ValidationInfo("Configuration file parent folder does not exists", _textConfigPath.getTextField());
  117. }
  118. else if (extension == null || extension.isEmpty()) {
  119. info = new ValidationInfo("Files extension is required", _textFilesExtension);
  120. }
  121. else if (extension.startsWith(".")) {
  122. info = new ValidationInfo("Files extension must not include dot (.)", _textFilesExtension);
  123. }
  124. else if (!castFilePath.exists() || !castFilePath.isFile()) {
  125. info = new ValidationInfo("Cast file does not exists", _textCastFile.getTextField());
  126. }
  127. return info;
  128. }
  129. @Nullable
  130. @Override
  131. protected JComponent createCenterPanel() {
  132. List<DbDataSource> dataSources = Helper.getDataSources();
  133. _listDatasources.setListData(dataSources.stream().map(DasObject::getName).toArray(String[]::new));
  134. _listDatasources.addListSelectionListener(e -> {
  135. if (!e.getValueIsAdjusting()) {
  136. int index = _listDatasources.getSelectedIndex();
  137. if (index == -1) {
  138. setOptions(null);
  139. }
  140. else {
  141. setOptions(new GenerateOptions(DataSourcesBusiness.getDataSourcesDbo().get(index),
  142. "code-from-ds" + File.separator + "code-from-ds.json"));
  143. }
  144. }
  145. });
  146. _listTables.addListSelectionListener(e -> {
  147. if (!e.getValueIsAdjusting()) {
  148. int index = _listTables.getSelectedIndex();
  149. if (index == -1) {
  150. showTable(null);
  151. }
  152. else {
  153. showTable(_options.getDataSource().getTables().get(index));
  154. }
  155. }
  156. });
  157. setupTextField(_textModels, null, true, "Models");
  158. setupTextField(_textDataSource, null, false, "Data Source");
  159. setupTextField(_textDataSourceTemplate, null, false, "Data Source Template");
  160. setupTextField(_textModelsTemplate, null, false, "Models Template");
  161. setupTextField(_textConfigPath, null, false, "Configuration");
  162. setupTextField(_textCastFile, null, false, "Types Cast");
  163. setupTextFieldListener(_textModels.getTextField(), s -> {
  164. _options.setModelsRelativePath(Helper.getRelativePath(_project, s));
  165. _lblModelsPath.setText("$ProjectRoot/" + _options.getModelsRelativePath());
  166. });
  167. setupTextFieldListener(_textDataSource.getTextField(), s -> {
  168. _options.setDataSourceRelativePath(Helper.getRelativePath(_project, s));
  169. _lblDataSourcePath.setText("$ProjectRoot/" + _options.getDataSourceRelativePath());
  170. });
  171. setupTextFieldListener(_textDataSourceTemplate.getTextField(), s -> {
  172. _options.setDataSourceTemplateRelativePath(Helper.getRelativePath(_project, s));
  173. _lblDataSourceTemplatePath.setText("$ProjectRoot/" + _options.getDataSourceTemplateRelativePath());
  174. });
  175. setupTextFieldListener(_textModelsTemplate.getTextField(), s -> {
  176. _options.setModelsTemplateRelativePath(Helper.getRelativePath(_project, s));
  177. _lblModelsTemplatePath.setText("$ProjectRoot/" + _options.getModelsTemplateRelativePath());
  178. });
  179. setupTextFieldListener(_textFilesExtension, s -> _options.setFilesExtension(s));
  180. setupTextFieldListener(_textConfigPath.getTextField(), s -> _options.setConfigAbsolutePath(s));
  181. setupTextFieldListener(_textCastFile.getTextField(), s -> {
  182. _options.setCastFileRelativePath(Helper.getRelativePath(_project, s));
  183. _lblCastFile.setText("$ProjectRoot/" + _options.getCastFileRelativePath());
  184. });
  185. _listTables.setCellRenderer(new ColoredListCellRenderer() {
  186. @Override
  187. protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
  188. TableDataSourceDbo tableSelection = _options.getDataSource().getTables().get(i);
  189. if (tableSelection.hasAll()) {
  190. setBackground(JBColor.GREEN);
  191. }
  192. else if (!tableSelection.hasAny()) {
  193. setBackground(JBColor.RED);
  194. }
  195. else {
  196. setBackground(JBColor.ORANGE);
  197. }
  198. append(tableSelection.getName());
  199. }
  200. });
  201. // _listStoredProcedure.setCellRenderer(new ColoredListCellRenderer() {
  202. // @Override
  203. // protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
  204. // StoredProcedureSelection spSelection = _options.getDataSource().getStoredProcedures().get(i);
  205. // if (spSelection.isSelected()) {
  206. // setBackground(JBColor.GREEN);
  207. // }
  208. // else {
  209. // setBackground(JBColor.RED);
  210. // }
  211. // append(spSelection.getStoredProcedure().getText());
  212. // }
  213. // });
  214. if (dataSources.size() > 0) {
  215. _listDatasources.setSelectedIndices(new int[]{0});
  216. }
  217. return _panel;
  218. }
  219. private void setupTextFieldListener(JTextField field, Consumer<String> consumer)
  220. {
  221. field.getDocument().addDocumentListener(new DocumentListener() {
  222. @Override
  223. public void insertUpdate(DocumentEvent e) {
  224. changed();
  225. }
  226. @Override
  227. public void removeUpdate(DocumentEvent e) {
  228. changed();
  229. }
  230. @Override
  231. public void changedUpdate(DocumentEvent e) {
  232. changed();
  233. }
  234. public void changed() {
  235. consumer.accept(field.getText());
  236. }
  237. });
  238. }
  239. private void setupTextField(TextFieldWithBrowseButton field, Project project, boolean dirsOnly, String title)
  240. {
  241. for (ActionListener l : field.getButton().getActionListeners()) {
  242. field.getButton().removeActionListener(l);
  243. }
  244. field.addBrowseFolderListener("Choose " + title + " " + (dirsOnly ? "Folder" : "File"),
  245. "Choose " + (dirsOnly ? "Folder" : "File"), project,
  246. dirsOnly ? FileChooserDescriptorFactory.createSingleFolderDescriptor() : FileChooserDescriptorFactory.createSingleFileDescriptor());
  247. }
  248. private void showSource(DataSourceDbo source)
  249. {
  250. for (ListSelectionListener e : _listStoredProcedure.getListSelectionListeners()) {
  251. _listStoredProcedure.removeListSelectionListener(e);
  252. }
  253. if (source != null) {
  254. List<String> tables = source.getTables().stream().map(t -> t.getName()).collect(Collectors.toList());
  255. _listTables.setListData(tables.toArray(new String[tables.size()]));
  256. if (tables.size() > 0) {
  257. showTable(source.getTables().get(0));
  258. }
  259. else {
  260. showTable(null);
  261. }
  262. List<String> sps = source.getStoredProcedures().stream().map(t -> t.getFullName()).collect(Collectors.toList());
  263. _listStoredProcedure.setListData(sps.toArray(new String[sps.size()]));
  264. int[] indices = getSpSelectedIndices(source);
  265. _listStoredProcedure.setSelectedIndices(indices);
  266. _listStoredProcedure.addListSelectionListener(e -> {
  267. if (!e.getValueIsAdjusting()) {
  268. _listStoredProcedure.updateUI();
  269. updateSpSelection();
  270. }
  271. });
  272. }
  273. else {
  274. _listTables.setListData(new String[]{});
  275. _listStoredProcedure.setListData(new String[]{});
  276. showTable(null);
  277. }
  278. }
  279. private int[] getSpSelectedIndices(final DataSourceDbo dataSource)
  280. {
  281. List<StoredProcedureDbo> columns = dataSource.getStoredProcedures();
  282. List<Integer> indices = new Vector<>();
  283. for (int i = 0; i < columns.size(); ++i) {
  284. if (columns.get(i).isSelected()) {
  285. indices.add(i);
  286. }
  287. }
  288. return indices.stream().mapToInt(Integer::intValue).toArray();
  289. }
  290. private int[] getColumnsSelectedIndices(final TableDataSourceDbo table)
  291. {
  292. List<ColumnDataSourceDbo> columns = table.getColumns();
  293. List<Integer> indices = new Vector<>();
  294. for (int i = 0; i < columns.size(); ++i) {
  295. if (columns.get(i).isSelected()) {
  296. indices.add(i);
  297. }
  298. }
  299. return indices.stream().mapToInt(Integer::intValue).toArray();
  300. }
  301. private void updateColumnSelection(final TableDataSourceDbo table)
  302. {
  303. List<ColumnDataSourceDbo> columns = table.getColumns();
  304. for (int i = 0; i < columns.size(); ++i) {
  305. columns.get(i).setSelected(_listColumns.isSelectedIndex(i));
  306. }
  307. }
  308. private void updateSpSelection()
  309. {
  310. List<StoredProcedureDbo> sps = _options.getDataSource().getStoredProcedures();
  311. for (int i = 0; i < sps.size(); ++i) {
  312. sps.get(i).setSelected(_listStoredProcedure.isSelectedIndex(i));
  313. }
  314. }
  315. private void showTable(TableDataSourceDbo table)
  316. {
  317. for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
  318. _listColumns.removeListSelectionListener(e);
  319. }
  320. if (table != null) {
  321. _listColumns.setListData(table.getColumns().stream().map(c -> c.getName()).toArray(String[]::new));
  322. int[] indices = getColumnsSelectedIndices(table);
  323. _listColumns.setSelectedIndices(indices);
  324. _listColumns.addListSelectionListener(e -> {
  325. if (!e.getValueIsAdjusting()) {
  326. _listTables.updateUI();
  327. updateColumnSelection(table);
  328. }
  329. });
  330. }
  331. else {
  332. _listColumns.setListData(new String[]{});
  333. }
  334. }
  335. }