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

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