1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.vcs.log.ui;
4 import com.intellij.openapi.actionSystem.DataProvider;
5 import com.intellij.openapi.actionSystem.PlatformCoreDataKeys;
6 import com.intellij.openapi.vcs.VcsDataKeys;
7 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
8 import com.intellij.ui.components.JBPanel;
9 import com.intellij.ui.navigation.History;
10 import com.intellij.util.containers.ContainerUtil;
11 import com.intellij.vcs.log.CommitId;
12 import com.intellij.vcs.log.impl.VcsLogManager;
13 import com.intellij.vcs.log.util.VcsLogUtil;
14 import org.jetbrains.annotations.NonNls;
15 import org.jetbrains.annotations.NotNull;
16 import org.jetbrains.annotations.Nullable;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Objects;
25 import static com.intellij.vcs.log.VcsLogDataKeys.*;
27 public class VcsLogPanel extends JBPanel implements DataProvider {
28 @NotNull private final VcsLogManager myManager;
29 @NotNull private final VcsLogUiEx myUi;
31 public VcsLogPanel(@NotNull VcsLogManager manager, @NotNull VcsLogUiEx logUi) {
32 super(new BorderLayout());
35 add(myUi.getMainComponent(), BorderLayout.CENTER);
39 public VcsLogUiEx getUi() {
45 public Object getData(@NotNull @NonNls String dataId) {
46 if (VcsLogInternalDataKeys.LOG_MANAGER.is(dataId)) {
49 else if (VCS_LOG.is(dataId)) {
50 return myUi.getVcsLog();
52 else if (VCS_LOG_UI.is(dataId)) {
55 else if (VCS_LOG_DATA_PROVIDER.is(dataId) || VcsLogInternalDataKeys.LOG_DATA.is(dataId)) {
56 return myManager.getDataManager();
58 else if (VcsDataKeys.VCS_REVISION_NUMBER.is(dataId)) {
59 List<CommitId> hashes = myUi.getVcsLog().getSelectedCommits();
60 if (hashes.isEmpty()) return null;
61 return VcsLogUtil.convertToRevisionNumber(Objects.requireNonNull(ContainerUtil.getFirstItem(hashes)).getHash());
63 else if (VcsDataKeys.VCS_REVISION_NUMBERS.is(dataId)) {
64 List<CommitId> hashes = myUi.getVcsLog().getSelectedCommits();
65 if (hashes.size() > VcsLogUtil.MAX_SELECTED_COMMITS) return null;
66 return ContainerUtil.map(hashes,
67 commitId -> VcsLogUtil.convertToRevisionNumber(commitId.getHash())).toArray(new VcsRevisionNumber[0]);
69 else if (PlatformCoreDataKeys.HELP_ID.is(dataId)) {
70 return myUi.getHelpId();
72 else if (History.KEY.is(dataId)) {
73 return myUi.getNavigationHistory();
78 public static @NotNull List<VcsLogUiEx> getLogUis(@NotNull JComponent c) {
79 Set<VcsLogPanel> panels = new HashSet<>();
80 collectLogPanelInstances(c, panels);
82 return ContainerUtil.map(panels, VcsLogPanel::getUi);
85 private static void collectLogPanelInstances(@NotNull JComponent component, @NotNull Set<VcsLogPanel> result) {
86 if (component instanceof VcsLogPanel) {
87 result.add((VcsLogPanel)component);
90 for (Component childComponent : component.getComponents()) {
91 if (childComponent instanceof JComponent) {
92 collectLogPanelInstances((JComponent)childComponent, result);