diff: disable action if changes can't be shown

* allow to create changes without Project
This commit is contained in:
Aleksey Pivovarov
2015-02-19 14:59:18 +03:00
parent c83edac8f2
commit d2e64e7dc3
9 changed files with 50 additions and 40 deletions
@@ -66,11 +66,11 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
private static Key<List<String>> CONTEXT_KEY = Key.create("Diff.ChangeDiffRequestPresentableContextKey");
public static Key<Change> CHANGE_KEY = Key.create("DiffRequestPresentable.Change");
@NotNull private final Project myProject;
@Nullable private final Project myProject;
@NotNull private final Change myChange;
@NotNull private final Map<Key, Object> myChangeContext;
private ChangeDiffRequestProducer(@NotNull Project project, @NotNull Change change, @NotNull Map<Key, Object> changeContext) {
private ChangeDiffRequestProducer(@Nullable Project project, @NotNull Change change, @NotNull Map<Key, Object> changeContext) {
myChange = change;
myProject = project;
myChangeContext = changeContext;
@@ -81,7 +81,7 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
return myChange;
}
@NotNull
@Nullable
public Project getProject() {
return myProject;
}
@@ -111,19 +111,19 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
}
@Nullable
public static ChangeDiffRequestProducer create(@NotNull Project project, @NotNull Change change) {
public static ChangeDiffRequestProducer create(@Nullable Project project, @NotNull Change change) {
return create(project, change, Collections.<Key, Object>emptyMap());
}
@Nullable
public static ChangeDiffRequestProducer create(@NotNull Project project,
@NotNull Change change,
@NotNull Map<Key, Object> changeContext) {
public static ChangeDiffRequestProducer create(@Nullable Project project,
@NotNull Change change,
@NotNull Map<Key, Object> changeContext) {
if (!canCreate(project, change)) return null;
return new ChangeDiffRequestProducer(project, change, changeContext);
}
private static boolean canCreate(@NotNull Project project, @NotNull Change change) {
public static boolean canCreate(@Nullable Project project, @NotNull Change change) {
for (ChangeDiffViewerWrapperProvider provider : ChangeDiffViewerWrapperProvider.EP_NAME.getExtensions()) {
if (provider.canCreate(project, change)) return true;
}
@@ -223,7 +223,7 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
}
@NotNull
private static DiffRequest createRequest(@NotNull Project project,
private static DiffRequest createRequest(@Nullable Project project,
@NotNull Change change,
@NotNull UserDataHolder context,
@NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
@@ -238,6 +238,9 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
}
if (file == null) throw new DiffRequestProducerException("Can't show merge conflict - file not found");
if (project == null) {
throw new DiffRequestProducerException("Can't show merge conflict - project is unknown");
}
final AbstractVcs vcs = ChangesUtil.getVcsForChange(change, project);
if (vcs == null || vcs.getMergeProvider() == null) {
throw new DiffRequestProducerException("Can't show merge conflict - operation nos supported");
@@ -353,7 +356,7 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
}
@NotNull
public static DiffContent createContent(@NotNull Project project,
public static DiffContent createContent(@Nullable Project project,
@Nullable ContentRevision revision,
@NotNull UserDataHolder context,
@NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
@@ -400,7 +403,7 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
return DiffContentFactory.getInstance().create(CharsetToolkit.bytesToString(bytes, file.getCharset()), file.getFileType());
}
public static void checkContentRevision(@NotNull Project project,
public static void checkContentRevision(@Nullable Project project,
@NotNull ContentRevision rev,
@NotNull UserDataHolder context,
@NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
@@ -409,7 +412,7 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
}
}
private static void checkAssociate(@NotNull final Project project,
private static void checkAssociate(@Nullable final Project project,
@NotNull final FilePath file,
@NotNull final UserDataHolder context,
@NotNull ProgressIndicator indicator) {
@@ -25,6 +25,7 @@ import com.intellij.diff.requests.DiffRequest;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.util.ThreeState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface ChangeDiffRequestProvider {
ExtensionPointName<ChangeDiffRequestProvider> EP_NAME =
@@ -33,7 +34,7 @@ public interface ChangeDiffRequestProvider {
@NotNull
ThreeState isEquals(@NotNull Change change1, @NotNull Change change2);
boolean canCreate(@NotNull Project project, @NotNull Change change);
boolean canCreate(@Nullable Project project, @NotNull Change change);
@NotNull
DiffRequest process(@NotNull ChangeDiffRequestProducer presentable,
@@ -25,6 +25,7 @@ import com.intellij.diff.impl.DiffViewerWrapper;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.util.ThreeState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface ChangeDiffViewerWrapperProvider {
ExtensionPointName<ChangeDiffViewerWrapperProvider> EP_NAME =
@@ -33,7 +34,7 @@ public interface ChangeDiffViewerWrapperProvider {
@NotNull
ThreeState isEquals(@NotNull Change change1, @NotNull Change change2);
boolean canCreate(@NotNull Project project, @NotNull Change change);
boolean canCreate(@Nullable Project project, @NotNull Change change);
@NotNull
DiffViewerWrapper process(@NotNull ChangeDiffRequestProducer presentable,
@@ -20,10 +20,10 @@ import org.jetbrains.annotations.Nullable;
import java.nio.charset.Charset;
public class FileAwareDocumentContent extends DocumentContentImpl {
@NotNull private final Project myProject;
@Nullable private final Project myProject;
@Nullable private final VirtualFile myLocalFile;
public FileAwareDocumentContent(@NotNull Project project,
public FileAwareDocumentContent(@Nullable Project project,
@NotNull Document document,
@Nullable FileType fileType,
@Nullable VirtualFile localFile,
@@ -35,11 +35,12 @@ public class FileAwareDocumentContent extends DocumentContentImpl {
}
public OpenFileDescriptor getOpenFileDescriptor(int offset) {
return myLocalFile == null ? null : new OpenFileDescriptor(myProject, myLocalFile, offset);
if (myProject == null || myLocalFile == null) return null;
return new OpenFileDescriptor(myProject, myLocalFile, offset);
}
@NotNull
public static DiffContent create(@NotNull Project project,
public static DiffContent create(@Nullable Project project,
@NotNull String content,
@NotNull FilePath path) {
VirtualFile localFile = LocalFileSystem.getInstance().findFileByPath(path.getPath());
@@ -58,7 +59,7 @@ public class FileAwareDocumentContent extends DocumentContentImpl {
}
@NotNull
private static DiffContent create(@NotNull Project project,
private static DiffContent create(@Nullable Project project,
@NotNull String content,
@Nullable FileType fileType,
@Nullable VirtualFile file,
@@ -15,6 +15,10 @@
*/
package com.intellij.openapi.vcs.changes.actions.diff;
import com.intellij.diff.DiffDialogHints;
import com.intellij.diff.DiffManager;
import com.intellij.diff.chains.DiffRequestChain;
import com.intellij.diff.util.DiffUserDataKeys;
import com.intellij.icons.AllIcons;
import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.actionSystem.AnAction;
@@ -26,10 +30,6 @@ import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.diff.DiffDialogHints;
import com.intellij.diff.DiffManager;
import com.intellij.diff.chains.DiffRequestChain;
import com.intellij.diff.util.DiffUserDataKeys;
import com.intellij.openapi.vcs.VcsDataKeys;
import com.intellij.openapi.vcs.changes.*;
import com.intellij.openapi.vcs.changes.actions.ShowDiffUIContext;
@@ -55,11 +55,15 @@ public class ShowDiffAction extends AnAction implements DumbAware {
public void update(@NotNull AnActionEvent e) {
Change[] changes = e.getData(VcsDataKeys.CHANGES);
Project project = e.getData(CommonDataKeys.PROJECT);
e.getPresentation().setEnabled(project != null && canShowDiff(changes));
e.getPresentation().setEnabled(project != null && canShowDiff(project, changes));
}
protected static boolean canShowDiff(@Nullable Change[] changes) {
return changes != null && changes.length != 0;
protected static boolean canShowDiff(@Nullable Project project, @Nullable Change[] changes) {
if (changes == null || changes.length == 0) return false;
for (Change change : changes) {
if (ChangeDiffRequestProducer.canCreate(project, change)) return true;
}
return false;
}
public void actionPerformed(@NotNull final AnActionEvent e) {
@@ -116,7 +120,7 @@ public class ShowDiffAction extends AnAction implements DumbAware {
}
}
private static boolean checkIfThereAreFakeRevisions(final Project project, final Change[] changes) {
private static boolean checkIfThereAreFakeRevisions(@NotNull Project project, @NotNull Change[] changes) {
boolean needsConversion = false;
for (Change change : changes) {
final ContentRevision beforeRevision = change.getBeforeRevision();
@@ -134,7 +138,7 @@ public class ShowDiffAction extends AnAction implements DumbAware {
}
@Nullable
private static Change[] loadFakeRevisions(final Project project, final Change[] changes) {
private static Change[] loadFakeRevisions(@NotNull Project project, @NotNull Change[] changes) {
List<Change> matchingChanges = new ArrayList<Change>();
for (Change change : changes) {
matchingChanges.addAll(ChangeListManager.getInstance(project).getChangesIn(ChangesUtil.getFilePath(change)));
@@ -146,15 +150,15 @@ public class ShowDiffAction extends AnAction implements DumbAware {
// Impl
//
public static void showDiffForChange(@NotNull Project project, @NotNull Iterable<Change> changes) {
public static void showDiffForChange(@Nullable Project project, @NotNull Iterable<Change> changes) {
showDiffForChange(project, changes, 0);
}
public static void showDiffForChange(@NotNull Project project, @NotNull Iterable<Change> changes, int index) {
public static void showDiffForChange(@Nullable Project project, @NotNull Iterable<Change> changes, int index) {
showDiffForChange(project, changes, index, new ShowDiffContext());
}
public static void showDiffForChange(@NotNull Project project,
public static void showDiffForChange(@Nullable Project project,
@NotNull Iterable<Change> changes,
@NotNull Condition<Change> condition,
@NotNull ShowDiffContext context) {
@@ -169,7 +173,7 @@ public class ShowDiffAction extends AnAction implements DumbAware {
showDiffForChange(project, presentables, index, context);
}
public static void showDiffForChange(@NotNull Project project,
public static void showDiffForChange(@Nullable Project project,
@NotNull Iterable<Change> changes,
int index,
@NotNull ShowDiffContext context) {
@@ -188,7 +192,7 @@ public class ShowDiffAction extends AnAction implements DumbAware {
showDiffForChange(project, presentables, newIndex, context);
}
private static void showDiffForChange(@NotNull Project project,
private static void showDiffForChange(@Nullable Project project,
@NotNull List<ChangeDiffRequestProducer> presentables,
int index,
@NotNull ShowDiffContext context) {
@@ -329,7 +329,7 @@ public class ChangesBrowser extends JPanel implements TypeSafeDataProvider {
myDiffAction = new ShowDiffAction() {
public void update(AnActionEvent e) {
Change[] changes = e.getData(VcsDataKeys.CHANGES);
e.getPresentation().setEnabled(canShowDiff(changes));
e.getPresentation().setEnabled(canShowDiff(myProject, changes));
}
public void actionPerformed(AnActionEvent e) {
@@ -1,13 +1,13 @@
package org.jetbrains.idea.svn.difftool;
import com.intellij.diff.chains.DiffRequestProducerException;
import com.intellij.diff.impl.DiffViewerWrapper;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.UserDataHolder;
import com.intellij.diff.chains.DiffRequestProducerException;
import com.intellij.diff.impl.DiffViewerWrapper;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
@@ -41,7 +41,7 @@ public class SvnChangeDiffViewerProvider implements ChangeDiffViewerWrapperProvi
}
@Override
public boolean canCreate(@NotNull Project project, @NotNull Change change) {
public boolean canCreate(@Nullable Project project, @NotNull Change change) {
return getSvnChangeLayer(change) != null; // TODO: do not show, if no properties are set in both revisions ?
}
@@ -26,7 +26,7 @@ public class SvnPhantomChangeDiffRequestProvider implements ChangeDiffRequestPro
}
@Override
public boolean canCreate(@NotNull Project project, @NotNull Change change) {
public boolean canCreate(@Nullable Project project, @NotNull Change change) {
return change.isPhantom();
}
@@ -38,7 +38,7 @@ public class SvnTreeConflictDiffRequestProvider implements ChangeDiffRequestProv
}
@Override
public boolean canCreate(@NotNull Project project, @NotNull Change change) {
public boolean canCreate(@Nullable Project project, @NotNull Change change) {
return change instanceof ConflictedSvnChange && ((ConflictedSvnChange)change).getConflictState().isTree();
}