summaryrefslogtreecommitdiff
path: root/platform/vcs-log/graph/src/com/intellij/vcs/log/graph/impl/facade/AbstractVisibleGraph.java
blob: 18d7222d92572aaa9b9ffd95e2fc3f2df8c5ced2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * 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 com.intellij.vcs.log.graph.impl.facade;

import com.intellij.vcs.log.graph.*;
import com.intellij.vcs.log.graph.actions.ActionController;
import com.intellij.vcs.log.graph.actions.GraphAnswer;
import com.intellij.vcs.log.graph.actions.GraphMouseAction;
import com.intellij.vcs.log.graph.api.LinearGraph;
import com.intellij.vcs.log.graph.api.LinearGraphWithCommitInfo;
import com.intellij.vcs.log.graph.api.permanent.PermanentGraphInfo;
import com.intellij.vcs.log.graph.api.elements.GraphEdge;
import com.intellij.vcs.log.graph.api.elements.GraphElement;
import com.intellij.vcs.log.graph.api.elements.GraphNode;
import com.intellij.vcs.log.graph.api.printer.PrintElementGenerator;
import com.intellij.vcs.log.graph.api.printer.PrintElementWithGraphElement;
import com.intellij.vcs.log.graph.api.printer.PrintElementsManager;
import com.intellij.vcs.log.graph.impl.print.PrintElementGeneratorImpl;
import com.intellij.vcs.log.graph.impl.visible.CurrentBranches;
import com.intellij.vcs.log.graph.impl.visible.adapters.LinearGraphAsGraphWithHiddenNodes;
import com.intellij.vcs.log.graph.utils.Flags;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

public abstract class AbstractVisibleGraph<CommitId> implements VisibleGraph<CommitId> {
  @NotNull
  protected static <CommitId> LinearGraphAsGraphWithHiddenNodes createBranchesGraph(@NotNull final PermanentGraphInfo<CommitId> permanentGraph,
                                                                  @Nullable Set<CommitId> heads) {
    if (heads == null) {
      return new LinearGraphAsGraphWithHiddenNodes(permanentGraph.getPermanentLinearGraph());
    } else {
      Set<Integer> headIndexes = permanentGraph.getPermanentCommitsInfo().convertToCommitIndexes(heads);
      if (headIndexes.contains(-1))
        throw new IllegalStateException();
      Flags visibleNodes = CurrentBranches.getVisibleNodes(permanentGraph.getPermanentLinearGraph(), headIndexes);
      return new LinearGraphAsGraphWithHiddenNodes(permanentGraph.getPermanentLinearGraph(), visibleNodes);
    }
  }

  @NotNull
  protected final GraphAnswerImpl<CommitId> COMMIT_ID_GRAPH_ANSWER = new GraphAnswerImpl<CommitId>(null, Cursor.getDefaultCursor());

  @NotNull
  protected final LinearGraphWithCommitInfo<CommitId> myLinearGraphWithCommitInfo;
  @NotNull
  protected final PrintElementGenerator myPrintElementGenerator;
  @NotNull
  protected final PrintElementsManager myPrintElementsManager;
  @NotNull
  protected final Map<CommitId, GraphCommit<CommitId>> myCommitsWithNotLoadParent;

  protected AbstractVisibleGraph(@NotNull LinearGraphWithCommitInfo<CommitId> linearGraphWithCommitInfo,
                                 @NotNull Map<CommitId, GraphCommit<CommitId>> commitsWithNotLoadParent,
                                 @NotNull PrintElementsManager printElementsManager) {
    myLinearGraphWithCommitInfo = linearGraphWithCommitInfo;
    myCommitsWithNotLoadParent = commitsWithNotLoadParent;
    myPrintElementsManager = printElementsManager;
    myPrintElementGenerator = new PrintElementGeneratorImpl(linearGraphWithCommitInfo, printElementsManager);
  }

  @Override
  public int getVisibleCommitCount() {
    return myLinearGraphWithCommitInfo.nodesCount();
  }

  @NotNull
  @Override
  public RowInfo<CommitId> getRowInfo(final int visibleRow) {
    return new RowInfo<CommitId>() {
      @NotNull
      @Override
      public CommitId getCommit() {
        return myLinearGraphWithCommitInfo.getHashIndex(visibleRow);
      }

      @NotNull
      @Override
      public CommitId getOneOfHeads() {
        return myLinearGraphWithCommitInfo.getOneOfHeads(visibleRow);
      }

      @NotNull
      @Override
      public Collection<PrintElement> getPrintElements() {
        return myPrintElementGenerator.getPrintElements(visibleRow);
      }
    };
  }

  @Override
  public int getVisibleRowIndex(@NotNull CommitId commitId) {
    return myLinearGraphWithCommitInfo.getNodeIndex(commitId);
  }

  @NotNull
  @Override
  public ActionController<CommitId> getActionController() {
    return new ActionControllerImpl();
  }

  abstract protected void setLinearBranchesExpansion(boolean collapse);

  @NotNull
  abstract protected GraphAnswer<CommitId> clickByElement(@NotNull GraphElement graphElement);

  @NotNull
  protected GraphAnswer<CommitId> clickByElement(@Nullable PrintElementWithGraphElement printElement) {
    if (printElement == null) {
      return COMMIT_ID_GRAPH_ANSWER;
    }

    if (printElement instanceof SimplePrintElement) {
      SimplePrintElement simplePrintElement = (SimplePrintElement)printElement;

      int upNodeIndex, downNodeIndex;
      @NotNull GraphElement graphElement = printElement.getGraphElement();
      switch (simplePrintElement.getType()) {
        case NODE:
          assert graphElement instanceof GraphNode;
          return clickByElement(graphElement);
        case UP_ARROW:
          assert graphElement instanceof GraphEdge;
          upNodeIndex = ((GraphEdge)graphElement).getUpNodeIndex();
          return createJumpAnswer(upNodeIndex);
        case DOWN_ARROW:
          assert graphElement instanceof GraphEdge;
          downNodeIndex = ((GraphEdge)graphElement).getDownNodeIndex();
          if (downNodeIndex != LinearGraph.NOT_LOAD_COMMIT)
            return createJumpAnswer(downNodeIndex);
          else {
            upNodeIndex = ((GraphEdge)graphElement).getUpNodeIndex();
            int edgeIndex = myLinearGraphWithCommitInfo.getDownNodes(upNodeIndex).indexOf(LinearGraph.NOT_LOAD_COMMIT);

            GraphCommit<CommitId> commitIdGraphCommit =
              myCommitsWithNotLoadParent.get(myLinearGraphWithCommitInfo.getHashIndex(upNodeIndex));
            CommitId jumpTo = commitIdGraphCommit.getParents().get(edgeIndex);
            return createJumpAnswer(jumpTo);
          }
        default:
          throw new IllegalStateException("Unsupported SimplePrintElement type: " + simplePrintElement.getType());
      }
    }

    if (printElement instanceof EdgePrintElement) {
      GraphElement graphElement = printElement.getGraphElement();
      assert graphElement instanceof GraphEdge;
      return clickByElement(graphElement);
    }

    return COMMIT_ID_GRAPH_ANSWER;
  }

  protected GraphAnswer<CommitId> createJumpAnswer(int nodeIndex) {
    return createJumpAnswer(myLinearGraphWithCommitInfo.getHashIndex(nodeIndex));
  }

  private GraphAnswerImpl<CommitId> createJumpAnswer(CommitId commitId) {
    myPrintElementGenerator.invalidate();
    return new GraphAnswerImpl<CommitId>(commitId, null);
  }

  protected static class GraphAnswerImpl<CommitId> implements GraphAnswer<CommitId> {
    @Nullable
    private final CommitId myCommitId;

    @Nullable
    private final Cursor myCursor;

    public GraphAnswerImpl(@Nullable CommitId commitId, @Nullable Cursor cursor) {
      myCommitId = commitId;
      myCursor = cursor;
    }

    @Nullable
    @Override
    public Cursor getCursorToSet() {
      return myCursor;
    }

    @Nullable
    @Override
    public CommitId getCommitToJump() {
      return myCommitId;
    }
  }

  protected class ActionControllerImpl implements ActionController<CommitId> {
    @NotNull
    @Override
    public GraphAnswer<CommitId> performMouseAction(@NotNull GraphMouseAction graphMouseAction) {
      PrintElementWithGraphElement printElement = getPrintElementWithGraphElement(graphMouseAction);
      switch (graphMouseAction.getType()) {
        case OVER: {
          Cursor cursor = myPrintElementsManager.performOverElement(printElement);
          return new GraphAnswerImpl<CommitId>(null, cursor);
        }
        case CLICK:
          myPrintElementsManager.performOverElement(printElement);
          return AbstractVisibleGraph.this.clickByElement(printElement);

        default: {
          throw new IllegalStateException("Not supported GraphMouseAction type: " + graphMouseAction.getType());
        }
      }
    }

    @Nullable
    private PrintElementWithGraphElement getPrintElementWithGraphElement(@NotNull GraphMouseAction graphMouseAction) {
      PrintElement affectedElement = graphMouseAction.getAffectedElement();
      if (affectedElement == null)
        return null;

      return myPrintElementGenerator.toPrintElementWithGraphElement(affectedElement);
    }

    @Override
    public boolean areLongEdgesHidden() {
      return myPrintElementGenerator.areLongEdgesHidden();
    }

    @Override
    public void setLongEdgesHidden(boolean longEdgesHidden) {
      myPrintElementGenerator.setLongEdgesHidden(longEdgesHidden);
    }

    @Override
    public void setLinearBranchesExpansion(boolean collapse) {
      AbstractVisibleGraph.this.setLinearBranchesExpansion(collapse);
      myPrintElementGenerator.invalidate();
    }
  }
}