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.

GenerateAction.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.rthoni.intellij.codefromds.ui.actions;
  2. import com.intellij.openapi.actionSystem.AnAction;
  3. import com.intellij.openapi.actionSystem.AnActionEvent;
  4. import com.intellij.openapi.actionSystem.CommonDataKeys;
  5. import com.intellij.openapi.project.Project;
  6. import com.intellij.openapi.ui.DialogWrapper;
  7. import com.intellij.openapi.vfs.VirtualFile;
  8. import com.rthoni.intellij.codefromds.business.Generator;
  9. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  10. import com.rthoni.intellij.codefromds.ui.dialogs.GenerateDialog;
  11. import com.rthoni.intellij.codefromds.ui.others.NotificationsService;
  12. /**
  13. * Created by robin on 11/14/16.
  14. */
  15. public class GenerateAction extends AnAction {
  16. @Override
  17. public void actionPerformed(AnActionEvent e) {
  18. NotificationsService notificationsService = NotificationsService.getInstance();
  19. Project project = e.getProject();
  20. if (project == null) {
  21. notificationsService.showErrorNotification("Error", "Code FROM ds needs a current project to run");
  22. return;
  23. }
  24. GenerateDialog dlg = new GenerateDialog(project);
  25. if (!e.getPlace().equals("MainMenu")) {
  26. final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
  27. if (file != null) {
  28. try {
  29. GenerateOptions options = Generator.loadOptions(file.getPath());
  30. dlg.setOptions(options);
  31. } catch (Exception e1) {
  32. notificationsService.showErrorNotification("Error while loading configuration file", e1.toString());
  33. e1.printStackTrace();
  34. }
  35. }
  36. }
  37. dlg.show();
  38. int res = dlg.getExitCode();
  39. if (res == DialogWrapper.OK_EXIT_CODE) {
  40. GenerateOptions options = dlg.getOptions();
  41. try {
  42. Generator.saveOptions(options);
  43. } catch (Exception e1) {
  44. notificationsService.showErrorNotification("Error while saving configuration", e1.toString());
  45. e1.printStackTrace();
  46. }
  47. try {
  48. Generator.generate(options, project);
  49. notificationsService.showInfoNotification("Generation successful", "Files have been successfully generated");
  50. } catch (Exception e1) {
  51. notificationsService.showErrorNotification("Error while generating files", e1.toString());
  52. e1.printStackTrace();
  53. }
  54. }
  55. }
  56. @Override
  57. public void update(AnActionEvent e) {
  58. if (e.getPlace().equals("MainMenu")) {
  59. super.update(e);
  60. }
  61. else {
  62. final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
  63. e.getPresentation().setVisible(file != null && file.getName().endsWith(".json"));
  64. }
  65. }
  66. }