Search in sources :

Example 1 with CheckinHandler

use of com.intellij.openapi.vcs.checkin.CheckinHandler in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoFmtCheckinFactory method createHandler.

@Override
@NotNull
public CheckinHandler createHandler(@NotNull CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
    return new CheckinHandler() {

        @Override
        public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
            JCheckBox checkBox = new JCheckBox("Go fmt");
            return new RefreshableOnComponent() {

                @Override
                @NotNull
                public JComponent getComponent() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(checkBox, BorderLayout.WEST);
                    return panel;
                }

                @Override
                public void refresh() {
                }

                @Override
                public void saveState() {
                    PropertiesComponent.getInstance(panel.getProject()).setValue(GO_FMT, Boolean.toString(checkBox.isSelected()));
                }

                @Override
                public void restoreState() {
                    checkBox.setSelected(enabled(panel));
                }
            };
        }

        @Override
        public ReturnResult beforeCheckin(@Nullable CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) {
            if (enabled(panel)) {
                Ref<Boolean> success = Ref.create(true);
                FileDocumentManager.getInstance().saveAllDocuments();
                for (PsiFile file : getPsiFiles()) {
                    VirtualFile virtualFile = file.getVirtualFile();
                    new GoFmtFileAction().doSomething(virtualFile, ModuleUtilCore.findModuleForPsiElement(file), file.getProject(), "Go fmt", true, result -> {
                        if (!result)
                            success.set(false);
                    });
                }
                if (!success.get()) {
                    return showErrorMessage(executor);
                }
            }
            return super.beforeCheckin();
        }

        @NotNull
        private ReturnResult showErrorMessage(@Nullable CommitExecutor executor) {
            String[] buttons = new String[] { "&Details...", commitButtonMessage(executor, panel), CommonBundle.getCancelButtonText() };
            int answer = Messages.showDialog(panel.getProject(), "<html><body>GoFmt returned non-zero code on some of the files.<br/>" + "Would you like to commit anyway?</body></html>\n", "Go Fmt", null, buttons, 0, 1, UIUtil.getWarningIcon());
            if (answer == Messages.OK) {
                return ReturnResult.CLOSE_WINDOW;
            }
            if (answer == Messages.NO) {
                return ReturnResult.COMMIT;
            }
            return ReturnResult.CANCEL;
        }

        @NotNull
        private List<PsiFile> getPsiFiles() {
            Collection<VirtualFile> files = panel.getVirtualFiles();
            List<PsiFile> psiFiles = ContainerUtil.newArrayList();
            PsiManager manager = PsiManager.getInstance(panel.getProject());
            for (VirtualFile file : files) {
                PsiFile psiFile = manager.findFile(file);
                if (psiFile instanceof GoFile) {
                    psiFiles.add(psiFile);
                }
            }
            return psiFiles;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoFile(com.goide.psi.GoFile) PsiManager(com.intellij.psi.PsiManager) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) CommitExecutor(com.intellij.openapi.vcs.changes.CommitExecutor) PairConsumer(com.intellij.util.PairConsumer) RefreshableOnComponent(com.intellij.openapi.vcs.ui.RefreshableOnComponent) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CheckinHandler

use of com.intellij.openapi.vcs.checkin.CheckinHandler in project intellij-community by JetBrains.

the class SvnCheckinHandlerFactory method createVcsHandler.

@NotNull
@Override
protected CheckinHandler createVcsHandler(final CheckinProjectPanel panel) {
    final Project project = panel.getProject();
    final Collection<VirtualFile> commitRoots = panel.getRoots();
    return new CheckinHandler() {

        private Collection<Change> myChanges = panel.getSelectedChanges();

        @Override
        public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
            return null;
        }

        @Override
        public ReturnResult beforeCheckin(@Nullable CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) {
            if (executor instanceof LocalCommitExecutor)
                return ReturnResult.COMMIT;
            final SvnVcs vcs = SvnVcs.getInstance(project);
            final MultiMap<String, WorkingCopyFormat> copiesInfo = splitIntoCopies(vcs, myChanges);
            final List<String> repoUrls = new ArrayList<>();
            for (Map.Entry<String, Collection<WorkingCopyFormat>> entry : copiesInfo.entrySet()) {
                if (entry.getValue().size() > 1) {
                    repoUrls.add(entry.getKey());
                }
            }
            if (!repoUrls.isEmpty()) {
                final String join = StringUtil.join(repoUrls, ",\n");
                final int isOk = Messages.showOkCancelDialog(project, SvnBundle.message("checkin.different.formats.involved", repoUrls.size() > 1 ? 1 : 0, join), "Subversion: Commit Will Split", Messages.getWarningIcon());
                return Messages.OK == isOk ? ReturnResult.COMMIT : ReturnResult.CANCEL;
            }
            return ReturnResult.COMMIT;
        }

        @Override
        public void includedChangesChanged() {
            myChanges = panel.getSelectedChanges();
        }

        @Override
        public void checkinSuccessful() {
            if (SvnConfiguration.getInstance(project).isAutoUpdateAfterCommit()) {
                final VirtualFile[] roots = ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs(SvnVcs.getInstance(project));
                final List<FilePath> paths = new ArrayList<>();
                for (VirtualFile root : roots) {
                    boolean take = false;
                    for (VirtualFile commitRoot : commitRoots) {
                        if (VfsUtilCore.isAncestor(root, commitRoot, false)) {
                            take = true;
                            break;
                        }
                    }
                    if (take) {
                        paths.add(VcsUtil.getFilePath(root));
                    }
                }
                if (paths.isEmpty())
                    return;
                ApplicationManager.getApplication().invokeLater(() -> AutoSvnUpdater.run(new AutoSvnUpdater(project, paths.toArray(new FilePath[paths.size()])), ActionInfo.UPDATE.getActionName()), ModalityState.NON_MODAL);
            }
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) LocalCommitExecutor(com.intellij.openapi.vcs.changes.LocalCommitExecutor) ArrayList(java.util.ArrayList) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) LocalCommitExecutor(com.intellij.openapi.vcs.changes.LocalCommitExecutor) CommitExecutor(com.intellij.openapi.vcs.changes.CommitExecutor) Project(com.intellij.openapi.project.Project) PairConsumer(com.intellij.util.PairConsumer) Collection(java.util.Collection) AutoSvnUpdater(org.jetbrains.idea.svn.update.AutoSvnUpdater) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with CheckinHandler

use of com.intellij.openapi.vcs.checkin.CheckinHandler in project intellij-community by JetBrains.

the class TaskCheckinHandlerFactory method createHandler.

@NotNull
@Override
public CheckinHandler createHandler(@NotNull final CheckinProjectPanel panel, @NotNull final CommitContext commitContext) {
    return new CheckinHandler() {

        @Override
        public void checkinSuccessful() {
            final String message = panel.getCommitMessage();
            final Project project = panel.getProject();
            final TaskManagerImpl manager = (TaskManagerImpl) TaskManager.getManager(project);
            if (manager.getState().saveContextOnCommit) {
                Task task = findTaskInRepositories(message, manager);
                if (task == null) {
                    task = manager.createLocalTask(message);
                }
                final LocalTask localTask = manager.addTask(task);
                localTask.setUpdated(new Date());
                ApplicationManager.getApplication().invokeLater(() -> WorkingContextManager.getInstance(project).saveContext(localTask), project.getDisposed());
            }
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) Task(com.intellij.tasks.Task) LocalTask(com.intellij.tasks.LocalTask) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) LocalTask(com.intellij.tasks.LocalTask) Date(java.util.Date) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with CheckinHandler

use of com.intellij.openapi.vcs.checkin.CheckinHandler in project intellij-community by JetBrains.

the class CvsCheckinHandlerFactory method createVcsHandler.

@NotNull
@Override
protected CheckinHandler createVcsHandler(final CheckinProjectPanel panel) {
    return new CheckinHandler() {

        @Nullable
        public RefreshableOnComponent getAfterCheckinConfigurationPanel(Disposable parentDisposable) {
            final Project project = panel.getProject();
            final CvsVcs2 cvs = CvsVcs2.getInstance(project);
            final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
            final Collection<VirtualFile> roots = panel.getRoots();
            final Collection<FilePath> files = new HashSet<>();
            for (VirtualFile root : roots) {
                final VcsRoot vcsRoot = vcsManager.getVcsRootObjectFor(root);
                if (vcsRoot == null || vcsRoot.getVcs() != cvs) {
                    continue;
                }
                files.add(VcsContextFactory.SERVICE.getInstance().createFilePathOn(root));
            }
            return new AdditionalOptionsPanel(CvsConfiguration.getInstance(project), files, project);
        }
    };
}
Also used : Disposable(com.intellij.openapi.Disposable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) Project(com.intellij.openapi.project.Project) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) VcsRoot(com.intellij.openapi.vcs.VcsRoot) AdditionalOptionsPanel(com.intellij.cvsSupport2.checkinProject.AdditionalOptionsPanel) ProjectLevelVcsManager(com.intellij.openapi.vcs.ProjectLevelVcsManager) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with CheckinHandler

use of com.intellij.openapi.vcs.checkin.CheckinHandler in project intellij-community by JetBrains.

the class ExternalToolsCheckinHandlerFactory method createHandler.

@NotNull
@Override
public CheckinHandler createHandler(@NotNull final CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
    final ToolsProjectConfig config = ToolsProjectConfig.getInstance(panel.getProject());
    return new CheckinHandler() {

        @Override
        public RefreshableOnComponent getAfterCheckinConfigurationPanel(Disposable parentDisposable) {
            final JLabel label = new JLabel(ToolsBundle.message("tools.after.commit.description"));
            ComboboxWithBrowseButton listComponent = new ComboboxWithBrowseButton();
            final JComboBox comboBox = listComponent.getComboBox();
            comboBox.setModel(new CollectionComboBoxModel(getComboBoxElements(), null));
            comboBox.setRenderer(new ListCellRendererWrapper<Object>() {

                @Override
                public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
                    if (value instanceof ToolsGroup) {
                        setText(StringUtil.notNullize(((ToolsGroup) value).getName(), ToolsBundle.message("tools.unnamed.group")));
                    } else if (value instanceof Tool) {
                        setText("  " + StringUtil.notNullize(((Tool) value).getName()));
                    } else {
                        setText(ToolsBundle.message("tools.list.item.none"));
                    }
                }
            });
            listComponent.getButton().addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    final Object item = comboBox.getSelectedItem();
                    String id = null;
                    if (item instanceof Tool) {
                        id = ((Tool) item).getActionId();
                    }
                    final ToolSelectDialog dialog = new ToolSelectDialog(panel.getProject(), id, new ToolsPanel());
                    if (!dialog.showAndGet()) {
                        return;
                    }
                    comboBox.setModel(new CollectionComboBoxModel(getComboBoxElements(), dialog.getSelectedTool()));
                }
            });
            BorderLayout layout = new BorderLayout();
            layout.setVgap(3);
            final JPanel panel = new JPanel(layout);
            panel.add(label, BorderLayout.NORTH);
            panel.add(listComponent, BorderLayout.CENTER);
            listComponent.setBorder(BorderFactory.createEmptyBorder(0, 0, 3, 0));
            if (comboBox.getItemCount() == 0 || (comboBox.getItemCount() == 1 && comboBox.getItemAt(0) == NONE_TOOL)) {
                return null;
            }
            return new RefreshableOnComponent() {

                @Override
                public JComponent getComponent() {
                    return panel;
                }

                @Override
                public void refresh() {
                    String id = config.getAfterCommitToolsId();
                    if (id == null) {
                        comboBox.setSelectedIndex(-1);
                    } else {
                        for (int i = 0; i < comboBox.getItemCount(); i++) {
                            final Object itemAt = comboBox.getItemAt(i);
                            if (itemAt instanceof Tool && id.equals(((Tool) itemAt).getActionId())) {
                                comboBox.setSelectedIndex(i);
                                return;
                            }
                        }
                    }
                }

                @Override
                public void saveState() {
                    Object item = comboBox.getSelectedItem();
                    config.setAfterCommitToolId(item instanceof Tool ? ((Tool) item).getActionId() : null);
                }

                @Override
                public void restoreState() {
                    refresh();
                }
            };
        }

        @Override
        public void checkinSuccessful() {
            final String id = config.getAfterCommitToolsId();
            if (id == null) {
                return;
            }
            DataManager.getInstance().getDataContextFromFocus().doWhenDone(new Consumer<DataContext>() {

                @Override
                public void consume(final DataContext context) {
                    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

                        @Override
                        public void run() {
                            ToolAction.runTool(id, context);
                        }
                    });
                }
            });
        }
    };
}
Also used : ActionEvent(java.awt.event.ActionEvent) DataContext(com.intellij.openapi.actionSystem.DataContext) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) Disposable(com.intellij.openapi.Disposable) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) ActionListener(java.awt.event.ActionListener) ComboboxWithBrowseButton(com.intellij.ui.ComboboxWithBrowseButton) RefreshableOnComponent(com.intellij.openapi.vcs.ui.RefreshableOnComponent) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CheckinHandler (com.intellij.openapi.vcs.checkin.CheckinHandler)7 NotNull (org.jetbrains.annotations.NotNull)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Project (com.intellij.openapi.project.Project)3 CommitExecutor (com.intellij.openapi.vcs.changes.CommitExecutor)3 RefreshableOnComponent (com.intellij.openapi.vcs.ui.RefreshableOnComponent)3 PairConsumer (com.intellij.util.PairConsumer)3 Nullable (org.jetbrains.annotations.Nullable)3 Disposable (com.intellij.openapi.Disposable)2 FilePath (com.intellij.openapi.vcs.FilePath)2 PsiFile (com.intellij.psi.PsiFile)2 PsiManager (com.intellij.psi.PsiManager)2 ArrayList (java.util.ArrayList)2 GoFile (com.goide.psi.GoFile)1 AdditionalOptionsPanel (com.intellij.cvsSupport2.checkinProject.AdditionalOptionsPanel)1 DelegatingProgressIndicator (com.intellij.ide.util.DelegatingProgressIndicator)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProjectLevelVcsManager (com.intellij.openapi.vcs.ProjectLevelVcsManager)1 VcsRoot (com.intellij.openapi.vcs.VcsRoot)1