mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
refactoring branch actions
*common branch actions moved to a separate class; *bookmark actions extends common branch actions to avoid code duplication; *common child branch abstract action created as a base class for every other branch and bookmarks actions
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.zmlx.hg4idea.action;
|
||||
|
||||
import com.intellij.openapi.project.DumbAwareAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.zmlx.hg4idea.repo.HgRepository;
|
||||
|
||||
public abstract class HgBranchAbstractAction extends DumbAwareAction {
|
||||
@NotNull protected final Project myProject;
|
||||
@NotNull protected final HgRepository mySelectedRepository;
|
||||
@NotNull protected final String myBranchName;
|
||||
|
||||
public HgBranchAbstractAction(@NotNull Project project, @NotNull String title,
|
||||
@NotNull HgRepository selectedRepository,
|
||||
@NotNull String branchName) {
|
||||
super(title);
|
||||
myProject = project;
|
||||
mySelectedRepository = selectedRepository;
|
||||
myBranchName = branchName;
|
||||
}
|
||||
}
|
||||
@@ -23,13 +23,10 @@ import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.DefaultActionGroup;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.project.DumbAwareAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vcs.VcsException;
|
||||
import com.intellij.openapi.vcs.update.UpdatedFiles;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
@@ -39,14 +36,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.zmlx.hg4idea.HgNameWithHashInfo;
|
||||
import org.zmlx.hg4idea.HgRevisionNumber;
|
||||
import org.zmlx.hg4idea.HgVcs;
|
||||
import org.zmlx.hg4idea.HgVcsMessages;
|
||||
import org.zmlx.hg4idea.command.*;
|
||||
import org.zmlx.hg4idea.command.HgBookmarkCommand;
|
||||
import org.zmlx.hg4idea.command.HgBranchCreateCommand;
|
||||
import org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand;
|
||||
import org.zmlx.hg4idea.execution.HgCommandException;
|
||||
import org.zmlx.hg4idea.execution.HgCommandResult;
|
||||
import org.zmlx.hg4idea.execution.HgCommandResultHandler;
|
||||
import org.zmlx.hg4idea.provider.update.HgConflictResolver;
|
||||
import org.zmlx.hg4idea.provider.update.HgHeadMerger;
|
||||
import org.zmlx.hg4idea.repo.HgRepository;
|
||||
import org.zmlx.hg4idea.ui.HgBookmarkDialog;
|
||||
import org.zmlx.hg4idea.util.HgErrorUtil;
|
||||
@@ -55,9 +50,6 @@ import java.util.*;
|
||||
|
||||
import static org.zmlx.hg4idea.util.HgUtil.*;
|
||||
|
||||
/**
|
||||
* @author Nadya Zabrodina
|
||||
*/
|
||||
public class HgBranchPopupActions {
|
||||
|
||||
private final Project myProject;
|
||||
@@ -80,7 +72,6 @@ public class HgBranchPopupActions {
|
||||
popupGroup.addSeparator("Bookmarks");
|
||||
List<String> bookmarkNames = getNamesWithoutHashes(myRepository.getBookmarks());
|
||||
String currentBookmark = myRepository.getCurrentBookmark();
|
||||
Collections.sort(bookmarkNames);
|
||||
for (String bookmark : bookmarkNames) {
|
||||
AnAction bookmarkAction = new BookmarkActions(myProject, myRepository, bookmark);
|
||||
if (bookmark.equals(currentBookmark)) {
|
||||
@@ -94,7 +85,7 @@ public class HgBranchPopupActions {
|
||||
Collections.sort(branchNamesList);
|
||||
for (String branch : branchNamesList) {
|
||||
if (!branch.equals(myRepository.getCurrentBranch())) { // don't show current branch in the list
|
||||
popupGroup.add(new BranchActions(myProject, myRepository, branch));
|
||||
popupGroup.add(new HgCommonBranchActions(myProject, myRepository, branch));
|
||||
}
|
||||
}
|
||||
return popupGroup;
|
||||
@@ -219,7 +210,7 @@ public class HgBranchPopupActions {
|
||||
public AnAction[] getChildren(@Nullable AnActionEvent e) {
|
||||
List<AnAction> branchHeadActions = new ArrayList<AnAction>();
|
||||
for (Hash hash : myHeads) {
|
||||
branchHeadActions.add(new BranchActions(myProject, myRepository, hash.toShortString()));
|
||||
branchHeadActions.add(new HgCommonBranchActions(myProject, myRepository, hash.toShortString()));
|
||||
}
|
||||
return ContainerUtil.toArray(branchHeadActions, new AnAction[branchHeadActions.size()]);
|
||||
}
|
||||
@@ -236,78 +227,31 @@ public class HgBranchPopupActions {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Actions available for branches.
|
||||
*/
|
||||
static class BranchActions extends ActionGroup {
|
||||
|
||||
private final Project myProject;
|
||||
private String myBranchName;
|
||||
@NotNull private final HgRepository mySelectedRepository;
|
||||
|
||||
BranchActions(@NotNull Project project, @NotNull HgRepository selectedRepository, @NotNull String branchName) {
|
||||
super("", true);
|
||||
myProject = project;
|
||||
myBranchName = branchName;
|
||||
mySelectedRepository = selectedRepository;
|
||||
getTemplatePresentation().setText(myBranchName, false); // no mnemonics
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public AnAction[] getChildren(@Nullable AnActionEvent e) {
|
||||
return new AnAction[]{
|
||||
new UpdateToAction(myProject, mySelectedRepository, myBranchName),
|
||||
new MergeAction(myProject, mySelectedRepository, myBranchName)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actions available for bookmarks.
|
||||
*/
|
||||
static class BookmarkActions extends ActionGroup {
|
||||
static class BookmarkActions extends HgCommonBranchActions {
|
||||
|
||||
@NotNull private final Project myProject;
|
||||
@NotNull private String myBookmarkName;
|
||||
@NotNull private final HgRepository mySelectedRepository;
|
||||
|
||||
BookmarkActions(@NotNull Project project,
|
||||
@NotNull HgRepository selectedRepository, @NotNull String bookmarkName) {
|
||||
super("", true);
|
||||
myProject = project;
|
||||
myBookmarkName = bookmarkName;
|
||||
mySelectedRepository = selectedRepository;
|
||||
getTemplatePresentation().setText(myBookmarkName, false); // no mnemonics
|
||||
BookmarkActions(@NotNull Project project, @NotNull HgRepository selectedRepository, @NotNull String branchName) {
|
||||
super(project, selectedRepository, branchName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public AnAction[] getChildren(@Nullable AnActionEvent e) {
|
||||
return new AnAction[]{
|
||||
new UpdateToAction(myProject, mySelectedRepository, myBookmarkName),
|
||||
new MergeAction(myProject, mySelectedRepository, myBookmarkName),
|
||||
new DeleteBookmarkAction(myProject, mySelectedRepository, myBookmarkName)
|
||||
};
|
||||
return ArrayUtil.append(super.getChildren(e), new DeleteBookmarkAction(myProject, mySelectedRepository, myBranchName));
|
||||
}
|
||||
|
||||
private static class DeleteBookmarkAction extends DumbAwareAction {
|
||||
@NotNull private final Project myProject;
|
||||
@NotNull private final HgRepository mySelectedRepository;
|
||||
@NotNull private final String myBookmarkName;
|
||||
private static class DeleteBookmarkAction extends HgBranchAbstractAction {
|
||||
|
||||
DeleteBookmarkAction(@NotNull Project project, @NotNull HgRepository selectedRepository, @NotNull String bookmarkName) {
|
||||
super("Delete");
|
||||
myProject = project;
|
||||
mySelectedRepository = selectedRepository;
|
||||
myBookmarkName = bookmarkName;
|
||||
DeleteBookmarkAction(@NotNull Project project, @NotNull HgRepository selectedRepository, @NotNull String branchName) {
|
||||
super(project, "Delete", selectedRepository, branchName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
try {
|
||||
new HgBookmarkCommand(myProject, mySelectedRepository.getRoot(), myBookmarkName).deleteBookmark();
|
||||
new HgBookmarkCommand(myProject, mySelectedRepository.getRoot(), myBranchName).deleteBookmark();
|
||||
}
|
||||
catch (HgCommandException exception) {
|
||||
HgAbstractGlobalAction.handleException(myProject, exception);
|
||||
@@ -315,86 +259,4 @@ public class HgBranchPopupActions {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class MergeAction extends DumbAwareAction {
|
||||
|
||||
@NotNull private final Project myProject;
|
||||
@NotNull private final HgRepository mySelectedRepository;
|
||||
@NotNull private final String myBranchName;
|
||||
|
||||
public MergeAction(@NotNull Project project,
|
||||
@NotNull HgRepository selectedRepository,
|
||||
@NotNull String branchName) {
|
||||
super("Merge");
|
||||
myProject = project;
|
||||
mySelectedRepository = selectedRepository;
|
||||
myBranchName = branchName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final UpdatedFiles updatedFiles = UpdatedFiles.create();
|
||||
final HgMergeCommand hgMergeCommand = new HgMergeCommand(myProject, mySelectedRepository.getRoot());
|
||||
hgMergeCommand.setRevision(myBranchName);//there is no difference between branch or revision or bookmark as parameter to merge,
|
||||
// we need just a string
|
||||
final HgCommandResultNotifier notifier = new HgCommandResultNotifier(myProject);
|
||||
new Task.Backgroundable(myProject, "Merging changes...") {
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
try {
|
||||
new HgHeadMerger(myProject, hgMergeCommand)
|
||||
.merge(mySelectedRepository.getRoot());
|
||||
new HgConflictResolver(myProject, updatedFiles).resolve(mySelectedRepository.getRoot());
|
||||
}
|
||||
|
||||
catch (VcsException exception) {
|
||||
if (exception.isWarning()) {
|
||||
notifier.notifyWarning("Warning during merge", exception.getMessage());
|
||||
}
|
||||
else {
|
||||
notifier.notifyError(null, "Exception during merge", exception.getMessage());
|
||||
}
|
||||
}
|
||||
catch (Exception e1) {
|
||||
HgAbstractGlobalAction.handleException(myProject, e1);
|
||||
}
|
||||
}
|
||||
}.queue();
|
||||
}
|
||||
}
|
||||
|
||||
private static class UpdateToAction extends DumbAwareAction {
|
||||
|
||||
@NotNull private final Project myProject;
|
||||
@NotNull private final HgRepository mySelectedRepository;
|
||||
@NotNull private final String myBranch;
|
||||
|
||||
public UpdateToAction(@NotNull Project project,
|
||||
@NotNull HgRepository selectedRepository,
|
||||
@NotNull String branch) {
|
||||
super("Update To");
|
||||
myProject = project;
|
||||
mySelectedRepository = selectedRepository;
|
||||
myBranch = branch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final VirtualFile repository = mySelectedRepository.getRoot();
|
||||
final HgUpdateCommand hgUpdateCommand = new HgUpdateCommand(myProject, repository);
|
||||
hgUpdateCommand.setBranch(myBranch);
|
||||
new Task.Backgroundable(myProject, HgVcsMessages.message("action.hg4idea.updateTo.description", myBranch)) {
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
HgCommandResult result = hgUpdateCommand.execute();
|
||||
assert myProject != null; // myProject couldn't be null, see annotation for updateTo action
|
||||
if (HgErrorUtil.hasErrorsInCommandExecution(result)) {
|
||||
new HgCommandResultNotifier(myProject).notifyError(result, "", "Update failed");
|
||||
new HgConflictResolver(myProject).resolve(repository);
|
||||
}
|
||||
myProject.getMessageBus().syncPublisher(HgVcs.BRANCH_TOPIC).update(myProject, null);
|
||||
}
|
||||
}.queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.zmlx.hg4idea.action;
|
||||
|
||||
import com.intellij.openapi.actionSystem.ActionGroup;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vcs.VcsException;
|
||||
import com.intellij.openapi.vcs.update.UpdatedFiles;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.zmlx.hg4idea.HgVcs;
|
||||
import org.zmlx.hg4idea.HgVcsMessages;
|
||||
import org.zmlx.hg4idea.command.HgMergeCommand;
|
||||
import org.zmlx.hg4idea.command.HgUpdateCommand;
|
||||
import org.zmlx.hg4idea.execution.HgCommandResult;
|
||||
import org.zmlx.hg4idea.provider.update.HgConflictResolver;
|
||||
import org.zmlx.hg4idea.provider.update.HgHeadMerger;
|
||||
import org.zmlx.hg4idea.repo.HgRepository;
|
||||
import org.zmlx.hg4idea.util.HgErrorUtil;
|
||||
|
||||
public class HgCommonBranchActions extends ActionGroup {
|
||||
|
||||
@NotNull protected final Project myProject;
|
||||
@NotNull protected String myBranchName;
|
||||
@NotNull protected final HgRepository mySelectedRepository;
|
||||
|
||||
HgCommonBranchActions(@NotNull Project project, @NotNull HgRepository selectedRepository, @NotNull String branchName) {
|
||||
super("", true);
|
||||
myProject = project;
|
||||
myBranchName = branchName;
|
||||
mySelectedRepository = selectedRepository;
|
||||
getTemplatePresentation().setText(myBranchName, false); // no mnemonics
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public AnAction[] getChildren(@Nullable AnActionEvent e) {
|
||||
return new AnAction[]{
|
||||
new UpdateAction(myProject, mySelectedRepository, myBranchName),
|
||||
new MergeAction(myProject, mySelectedRepository, myBranchName)
|
||||
};
|
||||
}
|
||||
|
||||
private static class MergeAction extends HgBranchAbstractAction {
|
||||
|
||||
public MergeAction(@NotNull Project project,
|
||||
@NotNull HgRepository selectedRepository,
|
||||
@NotNull String branchName) {
|
||||
super(project, "Merge", selectedRepository, branchName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final UpdatedFiles updatedFiles = UpdatedFiles.create();
|
||||
final HgMergeCommand hgMergeCommand = new HgMergeCommand(myProject, mySelectedRepository.getRoot());
|
||||
hgMergeCommand.setRevision(myBranchName);//there is no difference between branch or revision or bookmark as parameter to merge,
|
||||
// we need just a string
|
||||
final HgCommandResultNotifier notifier = new HgCommandResultNotifier(myProject);
|
||||
new Task.Backgroundable(myProject, "Merging changes...") {
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
try {
|
||||
new HgHeadMerger(myProject, hgMergeCommand)
|
||||
.merge(mySelectedRepository.getRoot());
|
||||
new HgConflictResolver(myProject, updatedFiles).resolve(mySelectedRepository.getRoot());
|
||||
}
|
||||
|
||||
catch (VcsException exception) {
|
||||
if (exception.isWarning()) {
|
||||
notifier.notifyWarning("Warning during merge", exception.getMessage());
|
||||
}
|
||||
else {
|
||||
notifier.notifyError(null, "Exception during merge", exception.getMessage());
|
||||
}
|
||||
}
|
||||
catch (Exception e1) {
|
||||
HgAbstractGlobalAction.handleException(myProject, e1);
|
||||
}
|
||||
}
|
||||
}.queue();
|
||||
}
|
||||
}
|
||||
|
||||
private static class UpdateAction extends HgBranchAbstractAction {
|
||||
|
||||
public UpdateAction(@NotNull Project project,
|
||||
@NotNull HgRepository selectedRepository,
|
||||
@NotNull String branchName) {
|
||||
super(project, "Update", selectedRepository, branchName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final VirtualFile repository = mySelectedRepository.getRoot();
|
||||
final HgUpdateCommand hgUpdateCommand = new HgUpdateCommand(myProject, repository);
|
||||
hgUpdateCommand.setBranch(myBranchName);
|
||||
new Task.Backgroundable(myProject, HgVcsMessages.message("action.hg4idea.updateTo.description", myBranchName)) {
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
HgCommandResult result = hgUpdateCommand.execute();
|
||||
assert myProject != null; // myProject couldn't be null, see annotation for updateTo action
|
||||
if (HgErrorUtil.hasErrorsInCommandExecution(result)) {
|
||||
new HgCommandResultNotifier(myProject).notifyError(result, "", "Update failed");
|
||||
new HgConflictResolver(myProject).resolve(repository);
|
||||
}
|
||||
myProject.getMessageBus().syncPublisher(HgVcs.BRANCH_TOPIC).update(myProject, null);
|
||||
}
|
||||
}.queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user