summaryrefslogtreecommitdiffstats
path: root/appjar/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java
blob: b2d6682c3dbd1c9ec50225bd5bdb037e028c496e (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
// Copyright 2008 Google Inc.
//
// 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.google.gerrit.client.patches;

import com.google.gerrit.client.data.PatchLine;
import com.google.gerrit.client.reviewdb.PatchLineComment;

import java.util.Iterator;
import java.util.List;

public class UnifiedDiffTable extends AbstractPatchContentTable {
  @Override
  protected void onCellDoubleClick(final int row, final int column) {
    if (column == 1 && getRowItem(row) instanceof PatchLine) {
      final PatchLine pl = (PatchLine) getRowItem(row);
      switch (pl.getType()) {
        case PRE_IMAGE:
        case CONTEXT:
          createCommentEditor(row + 1, column, pl.getOldLineNumber(), (short) 0);
          break;
        case POST_IMAGE:
          createCommentEditor(row + 1, column, pl.getOldLineNumber(), (short) 1);
          break;
      }
    }
  }

  @Override
  protected void onOpenItem(final Object item) {
    if (item instanceof PatchLine) {
      final PatchLine pl = (PatchLine) item;
      final int row = getCurrentRow();
      switch (pl.getType()) {
        case PRE_IMAGE:
        case CONTEXT:
          createCommentEditor(row + 1, 1, pl.getOldLineNumber(), (short) 0);
          break;
        case POST_IMAGE:
          createCommentEditor(row + 1, 1, pl.getOldLineNumber(), (short) 1);
          break;
      }
      return;
    }

    super.onOpenItem(item);
  }

  @Override
  protected void bindDrafts(final List<PatchLineComment> drafts) {
    int row = 0;
    for (final PatchLineComment c : drafts) {
      while (row < table.getRowCount()) {
        if (getRowItem(row) instanceof PatchLine) {
          final PatchLine pl = (PatchLine) getRowItem(row);
          if (pl.getOldLineNumber() >= c.getLine()) {
            break;
          }
        }
        row++;
      }
      table.insertRow(row + 1);
      table.getCellFormatter().setStyleName(row + 1, 0, S_ICON_CELL);
      bindComment(row + 1, 1, c, true);
    }
  }

  public void display(final List<PatchLine> list) {
    final StringBuilder nc = new StringBuilder();
    for (final PatchLine pLine : list) {
      appendLine(nc, pLine);
    }
    resetHtml(nc.toString());

    int row = 0;
    for (final PatchLine pLine : list) {
      setRowItem(row, pLine);
      row++;

      final List<PatchLineComment> comments = pLine.getComments();
      if (comments != null) {
        for (final Iterator<PatchLineComment> ci = comments.iterator(); ci
            .hasNext();) {
          final PatchLineComment c = ci.next();
          table.insertRow(row);
          table.getCellFormatter().setStyleName(row, 0, S_ICON_CELL);
          bindComment(row, 1, c, !ci.hasNext());
          row++;
        }
      }
    }
  }

  private void appendLine(final StringBuilder nc, final PatchLine line) {
    nc.append("<tr>");
    nc.append("<td class=\"" + S_ICON_CELL + "\">&nbsp;</td>");

    nc.append("<td class=\"DiffText DiffText-");
    nc.append(line.getType().name());
    nc.append("\">");
    if (!"".equals(line.getText())) {
      nc.append(PatchUtil.lineToHTML(line.getText(), 0));
    } else {
      nc.append("&nbsp;");
    }
    nc.append("</td>");

    nc.append("</tr>");
  }
}