<commit_before>// 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);<commit_msg>Fix NPE in unified diff view when creating a comment editor<commit_after>// 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) {
    initVersions(2);

    final StringBuilder nc = new StringBuilder();
    for (final PatchLine pLine : list) {
      appendLine(nc, pLine);
{'fraction_non_alphanumeric': 0.10521449704142012, 'fraction_numerical': 0.009430473372781065, 'mean_word_length': 2.7484407484407485, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6648223', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1551, 'n_words': 494}