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

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