ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / actions / AbstractShowDiffAction.java
blob525687df9df0a7ab0ff2b1e84ad70b420ca302b0
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.vcs.actions;
18 import com.intellij.openapi.actionSystem.AnActionEvent;
19 import com.intellij.openapi.actionSystem.Presentation;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vcs.AbstractVcs;
22 import com.intellij.openapi.vcs.FilePathImpl;
23 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
24 import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl;
25 import com.intellij.openapi.vcs.impl.BackgroundableActionEnabledHandler;
26 import com.intellij.openapi.vcs.impl.VcsBackgroundableActions;
27 import com.intellij.openapi.vcs.diff.DiffProvider;
28 import com.intellij.openapi.vfs.VirtualFile;
30 public abstract class AbstractShowDiffAction extends AbstractVcsAction{
32 protected void update(VcsContext vcsContext, Presentation presentation) {
33 updateDiffAction(presentation, vcsContext, getKey());
36 protected static void updateDiffAction(final Presentation presentation, final VcsContext vcsContext,
37 final VcsBackgroundableActions actionKey) {
38 presentation.setEnabled(isEnabled(vcsContext, actionKey));
39 presentation.setVisible(isVisible(vcsContext));
42 protected boolean forceSyncUpdate(final AnActionEvent e) {
43 return true;
46 protected abstract VcsBackgroundableActions getKey();
48 protected static boolean isVisible(final VcsContext vcsContext) {
49 final Project project = vcsContext.getProject();
50 if (project == null) return false;
51 final AbstractVcs[] vcss = ProjectLevelVcsManager.getInstance(project).getAllActiveVcss();
52 for (AbstractVcs vcs : vcss) {
53 if (vcs.getDiffProvider() != null) {
54 return true;
57 return false;
60 protected static boolean isEnabled(final VcsContext vcsContext, final VcsBackgroundableActions actionKey) {
61 if (!(isVisible(vcsContext))) return false;
63 final Project project = vcsContext.getProject();
64 if (project == null) return false;
65 final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
67 final VirtualFile[] selectedFilePaths = vcsContext.getSelectedFiles();
68 if (selectedFilePaths == null || selectedFilePaths.length != 1) return false;
70 final VirtualFile selectedFile = selectedFilePaths[0];
71 if (selectedFile.isDirectory()) return false;
73 final BackgroundableActionEnabledHandler handler = ((ProjectLevelVcsManagerImpl)vcsManager).getBackgroundableActionHandler(actionKey);
74 if (handler.isInProgress(VcsBackgroundableActions.keyFrom(selectedFile))) return false;
76 final AbstractVcs vcs = vcsManager.getVcsFor(selectedFile);
77 if (vcs == null) return false;
79 final DiffProvider diffProvider = vcs.getDiffProvider();
81 if (diffProvider == null) return false;
83 return AbstractVcs.fileInVcsByFileStatus(project, new FilePathImpl(selectedFile)) ;
87 protected void actionPerformed(VcsContext vcsContext) {
88 final Project project = vcsContext.getProject();
89 final VirtualFile selectedFile = vcsContext.getSelectedFiles()[0];
91 final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
92 final AbstractVcs vcs = vcsManager.getVcsFor(selectedFile);
93 final DiffProvider diffProvider = vcs.getDiffProvider();
95 final DiffActionExecutor actionExecutor = getExecutor(diffProvider, selectedFile, project);
96 actionExecutor.showDiff();
99 protected DiffActionExecutor getExecutor(final DiffProvider diffProvider, final VirtualFile selectedFile, final Project project) {
100 return new DiffActionExecutor.CompareToCurrentExecutor(diffProvider, selectedFile, project, getKey());