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

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