您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GenerateDialog.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. _listStoredProcedure.addListSelectionListener(e -> {
  152. if (!e.getValueIsAdjusting()) {
  153. updateSpSelection();
  154. }
  155. });
  156. setupTextField(_textModels, null, true, "Models");
  157. setupTextField(_textDataSource, null, false, "Data Source");
  158. setupTextField(_textDataSourceTemplate, null, false, "Data Source Template");
  159. setupTextField(_textModelsTemplate, null, false, "Models Template");
  160. setupTextField(_textConfigPath, null, false, "Configuration");
  161. setupTextField(_textCastFile, null, false, "Types Cast");
  162. setupTextFieldListener(_textModels.getTextField(), s -> {
  163. _options.setModelsRelativePath(Helper.getRelativePath(_project, s));
  164. _lblModelsPath.setText("$ProjectRoot/" + _options.getModelsRelativePath());
  165. });
  166. setupTextFieldListener(_textDataSource.getTextField(), s -> {
  167. _options.setDataSourceRelativePath(Helper.getRelativePath(_project, s));
  168. _lblDataSourcePath.setText("$ProjectRoot/" + _options.getDataSourceRelativePath());
  169. });
  170. setupTextFieldListener(_textDataSourceTemplate.getTextField(), s -> {
  171. _options.setDataSourceTemplateRelativePath(Helper.getRelativePath(_project, s));
  172. _lblDataSourceTemplatePath.setText("$ProjectRoot/" + _options.getDataSourceTemplateRelativePath());
  173. });
  174. setupTextFieldListener(_textModelsTemplate.getTextField(), s -> {
  175. _options.setModelsTemplateRelativePath(Helper.getRelativePath(_project, s));
  176. _lblModelsTemplatePath.setText("$ProjectRoot/" + _options.getModelsTemplateRelativePath());
  177. });
  178. setupTextFieldListener(_textFilesExtension, s -> _options.setFilesExtension(s));
  179. setupTextFieldListener(_textConfigPath.getTextField(), s -> _options.setConfigAbsolutePath(s));
  180. setupTextFieldListener(_textCastFile.getTextField(), s -> {
  181. _options.setCastFileRelativePath(Helper.getRelativePath(_project, s));
  182. _lblCastFile.setText("$ProjectRoot/" + _options.getCastFileRelativePath());
  183. });
  184. _listTables.setCellRenderer(new ColoredListCellRenderer() {
  185. @Override
  186. protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
  187. TableSelection tableSelection = _options.getSelection().getTables().get(i);
  188. if (tableSelection.hasAll()) {
  189. setBackground(JBColor.GREEN);
  190. }
  191. else if (tableSelection.hasNone()) {
  192. setBackground(JBColor.RED);
  193. }
  194. else {
  195. setBackground(JBColor.ORANGE);
  196. }
  197. append(tableSelection.getTable().getName());
  198. }
  199. });
  200. _listStoredProcedure.setCellRenderer(new ColoredListCellRenderer() {
  201. @Override
  202. protected void customizeCellRenderer(@NotNull JList jList, Object o, int i, boolean b, boolean b1) {
  203. StoredProcedureSelection spSelection = _options.getSelection().getStoredProcedures().get(i);
  204. if (spSelection.isSelected()) {
  205. setBackground(JBColor.GREEN);
  206. }
  207. else {
  208. setBackground(JBColor.RED);
  209. }
  210. append(spSelection.getStoredProcedure().getText());
  211. }
  212. });
  213. if (dataSources.size() > 0) {
  214. _listDatasources.setSelectedIndices(new int[]{0});
  215. }
  216. return _panel;
  217. }
  218. private void setupTextFieldListener(JTextField field, Consumer<String> consumer)
  219. {
  220. field.getDocument().addDocumentListener(new DocumentListener() {
  221. @Override
  222. public void insertUpdate(DocumentEvent e) {
  223. changed();
  224. }
  225. @Override
  226. public void removeUpdate(DocumentEvent e) {
  227. changed();
  228. }
  229. @Override
  230. public void changedUpdate(DocumentEvent e) {
  231. changed();
  232. }
  233. public void changed() {
  234. consumer.accept(field.getText());
  235. }
  236. });
  237. }
  238. private void setupTextField(TextFieldWithBrowseButton field, Project project, boolean dirsOnly, String title)
  239. {
  240. for (ActionListener l : field.getButton().getActionListeners()) {
  241. field.getButton().removeActionListener(l);
  242. }
  243. field.addBrowseFolderListener("Choose " + title + " " + (dirsOnly ? "Folder" : "File"), "Choose " + (dirsOnly ? "Folder" : "File"), project,
  244. dirsOnly ? FileChooserDescriptorFactory.createSingleFolderDescriptor() : FileChooserDescriptorFactory.createSingleFileDescriptor());
  245. }
  246. private void showSource(DataSourceSelection source)
  247. {
  248. if (source != null) {
  249. List<String> tables = source.getTables().stream().map(t -> t.getTable().getName()).collect(Collectors.toList());
  250. _listTables.setListData(tables.toArray(new String[tables.size()]));
  251. if (tables.size() > 0) {
  252. showTable(source.getTables().get(0));
  253. }
  254. else {
  255. showTable(null);
  256. }
  257. List<String> sps = source.getStoredProcedures().stream().map(t -> t.getStoredProcedure().getText()).collect(Collectors.toList());
  258. _listStoredProcedure.setListData(sps.toArray(new String[sps.size()]));
  259. }
  260. else {
  261. _listTables.setListData(new String[]{});
  262. _listStoredProcedure.setListData(new String[]{});
  263. showTable(null);
  264. }
  265. }
  266. private int[] getSelectedIndices(final TableSelection table)
  267. {
  268. List<ColumnSelection> columns = table.getColumns();
  269. List<Integer> indices = new Vector<>();
  270. for (int i = 0; i < columns.size(); ++i) {
  271. if (columns.get(i).isSelected()) {
  272. indices.add(i);
  273. }
  274. }
  275. return indices.stream().mapToInt(Integer::intValue).toArray();
  276. }
  277. private void updateColumnSelection(final TableSelection table)
  278. {
  279. List<ColumnSelection> columns = table.getColumns();
  280. for (int i = 0; i < columns.size(); ++i) {
  281. columns.get(i).setSelected(_listColumns.isSelectedIndex(i));
  282. }
  283. }
  284. private void updateSpSelection()
  285. {
  286. List<StoredProcedureSelection> sps = _options.getSelection().getStoredProcedures();
  287. for (int i = 0; i < sps.size(); ++i) {
  288. sps.get(i).setSelected(_listStoredProcedure.isSelectedIndex(i));
  289. }
  290. }
  291. private void showTable(TableSelection table)
  292. {
  293. for (ListSelectionListener e : _listColumns.getListSelectionListeners()) {
  294. _listColumns.removeListSelectionListener(e);
  295. }
  296. if (table != null) {
  297. _listColumns.setListData(table.getColumns().stream().map(c -> c.getColumn().getName()).toArray(String[]::new));
  298. int[] indices = getSelectedIndices(table);
  299. _listColumns.setSelectedIndices(indices);
  300. _listColumns.addListSelectionListener(e -> {
  301. if (!e.getValueIsAdjusting()) {
  302. _listTables.updateUI();
  303. updateColumnSelection(table);
  304. }
  305. });
  306. }
  307. else {
  308. _listColumns.setListData(new String[]{});
  309. }
  310. }
  311. }