<commit_before>// Copyright (C) 2013 The Android Open Source Project
//
// 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.change;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.GitwebLink;
import com.google.gerrit.client.change.RelatedChanges.ChangeAndCommit;
import com.google.gerrit.client.ui.NavigationTable;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.impl.HyperlinkImpl;
import com.google.gwtexpui.progress.client.ProgressBar;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;

class RelatedChangesTab {
  private static final String OPEN;
  private static final HyperlinkImpl link = GWT.create(HyperlinkImpl.class);

  static {
    OPEN = DOM.createUniqueId().replace('-', '_');
    init(OPEN);
  }

  private static final native void init(String o) /*-{
    $wnd[o] = $entry(function(e,i) {
      return @com.google.gerrit.client.change.RelatedChangesTab::onOpen(Lcom/google/gwt/dom/client/NativeEvent;I)(e,i);
    });
  }-*/;

  private static boolean onOpen(NativeEvent e, int idx) {
    if (link.handleAsClick(e.<Event> cast())) {
      MyTable t = getMyTable(e);
      if (t != null) {
        t.onOpenRow(idx);
        e.preventDefault();
        return false;
      }
    }
    return true;
  }

  private static MyTable getMyTable(NativeEvent event) {
    com.google.gwt.user.client.Element e = event.getEventTarget().cast();
    for (e = DOM.getParent(e); e != null; e = DOM.getParent(e)) {
      EventListener l = DOM.getEventListener(e);
      if (l instanceof MyTable) {
        return (MyTable) l;
      }
    }
    return null;
  }

  private final RelatedChanges parent;
  private final int index;
  private final FlowPanel panel;
  private final ScrollPanel scroll;
  private final ProgressBar progress;

  private String project;
  private MyTable table;
  private boolean register;

  RelatedChangesTab(RelatedChanges parent, int index, FlowPanel panel) {
    this.parent = parent;
    this.index = index;
    this.panel = panel;

    scroll = new ScrollPanel();
    scroll.setVisible(false);
    panel.add(scroll);

    progress = new ProgressBar();
    panel.add(progress);
  }

  void setTitle(String title) {
    parent.setTabTitle(index, title);
  }

  void setChanges(String project, String revision, JsArray<ChangeAndCommit> changes) {
    this.project = project;
    render(revision, changes);
  }

  void setError(String message) {
    progress.setVisible(false);
    scroll.setVisible(false);
    panel.add(new InlineLabel(message));
  }

  void setMaxHeight(int height) {
    scroll.setHeight(height + "px");
  }

  void registerKeys(boolean on) {
    register = on;

    if (table != null) {
      table.setRegisterKeys(on);
    }
  }

  private void render(String revision, JsArray<ChangeAndCommit> list) {
    if (0 < list.length()) {
      DisplayCommand cmd = new DisplayCommand(revision, list);
      if (cmd.execute()) {
        Scheduler.get().scheduleIncremental(cmd);
      }
    } else {
      progress.setVisible(false);
      panel.add(new InlineLabel(Resources.C.noChanges()));
    }
  }

  private void setTable(MyTable t) {
    progress.setVisible(false);

    scroll.clear();
    scroll.add(t);
    scroll.setVisible(true);
    table = t;

    table.setRegisterKeys(register);
  }

  private String url(ChangeAndCommit c) {
    if (c.has_change_number() && c.has_revision_number()) {
      PatchSet.Id id = c.patch_set_id();
      return "#" + PageLinks.toChange(
          id.getParentKey(),
          String.valueOf(id.get()));
    }

    GitwebLink gw = Gerrit.getGitwebLink();
    if (gw != null) {
      return gw.toRevision(project, c.commit().commit());
    }
    return null;
  }

  private class MyTable extends NavigationTable<ChangeAndCommit> {
    private final JsArray<ChangeAndCommit> list;

    MyTable(JsArray<ChangeAndCommit> list) {
      this.list = list;
      table.setWidth("");

      keysNavigation.setName(Gerrit.C.sectionNavigation());
      keysNavigation.add(
          new PrevKeyCommand(0, 'K', Resources.C.previousChange()),
          new NextKeyCommand(0, 'J', Resources.C.nextChange()));
      keysNavigation.add(new OpenKeyCommand(0, 'O', Resources.C.openChange()));
    }

    @Override
    protected Object getRowItemKey(ChangeAndCommit item) {
      return item.id();
    }

    @Override
    protected ChangeAndCommit getRowItem(int row) {
      if (0 <= row && row <= list.length()) {
        return list.get(row);<commit_msg>ChangeScreen2: Group Related Changes keys together<commit_after>// Copyright (C) 2013 The Android Open Source Project
//
// 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.change;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.GitwebLink;
import com.google.gerrit.client.change.RelatedChanges.ChangeAndCommit;
import com.google.gerrit.client.ui.NavigationTable;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.impl.HyperlinkImpl;
import com.google.gwtexpui.progress.client.ProgressBar;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;

class RelatedChangesTab {
  private static final String OPEN;
  private static final HyperlinkImpl link = GWT.create(HyperlinkImpl.class);

  static {
    OPEN = DOM.createUniqueId().replace('-', '_');
    init(OPEN);
  }

  private static final native void init(String o) /*-{
    $wnd[o] = $entry(function(e,i) {
      return @com.google.gerrit.client.change.RelatedChangesTab::onOpen(Lcom/google/gwt/dom/client/NativeEvent;I)(e,i);
    });
  }-*/;

  private static boolean onOpen(NativeEvent e, int idx) {
    if (link.handleAsClick(e.<Event> cast())) {
      MyTable t = getMyTable(e);
      if (t != null) {
        t.onOpenRow(idx);
        e.preventDefault();
        return false;
      }
    }
    return true;
  }

  private static MyTable getMyTable(NativeEvent event) {
    com.google.gwt.user.client.Element e = event.getEventTarget().cast();
    for (e = DOM.getParent(e); e != null; e = DOM.getParent(e)) {
      EventListener l = DOM.getEventListener(e);
      if (l instanceof MyTable) {
        return (MyTable) l;
      }
    }
    return null;
  }

  private final RelatedChanges parent;
  private final int index;
  private final FlowPanel panel;
  private final ScrollPanel scroll;
  private final ProgressBar progress;

  private String project;
  private MyTable table;
  private boolean register;

  RelatedChangesTab(RelatedChanges parent, int index, FlowPanel panel) {
    this.parent = parent;
    this.index = index;
    this.panel = panel;

    scroll = new ScrollPanel();
    scroll.setVisible(false);
    panel.add(scroll);

    progress = new ProgressBar();
    panel.add(progress);
  }

  void setTitle(String title) {
    parent.setTabTitle(index, title);
  }

  void setChanges(String project, String revision, JsArray<ChangeAndCommit> changes) {
    this.project = project;
    render(revision, changes);
  }

  void setError(String message) {
    progress.setVisible(false);
    scroll.setVisible(false);
    panel.add(new InlineLabel(message));
  }

  void setMaxHeight(int height) {
    scroll.setHeight(height + "px");
  }

  void registerKeys(boolean on) {
    register = on;

    if (table != null) {
      table.setRegisterKeys(on);
    }
  }

  private void render(String revision, JsArray<ChangeAndCommit> list) {
    if (0 < list.length()) {
      DisplayCommand cmd = new DisplayCommand(revision, list);
      if (cmd.execute()) {
        Scheduler.get().scheduleIncremental(cmd);
      }
    } else {
      progress.setVisible(false);
      panel.add(new InlineLabel(Resources.C.noChanges()));
    }
  }

  private void setTable(MyTable t) {
    progress.setVisible(false);

    scroll.clear();
    scroll.add(t);
    scroll.setVisible(true);
    table = t;

    table.setRegisterKeys(register);
  }

  private String url(ChangeAndCommit c) {
    if (c.has_change_number() && c.has_revision_number()) {
      PatchSet.Id id = c.patch_set_id();
      return "#" + PageLinks.toChange(
          id.getParentKey(),
          String.valueOf(id.get()));
    }

    GitwebLink gw = Gerrit.getGitwebLink();
    if (gw != null) {
      return gw.toRevision(project, c.commit().commit());
    }
    return null;
  }

  private class MyTable extends NavigationTable<ChangeAndCommit> {
    private final JsArray<ChangeAndCommit> list;

    MyTable(JsArray<ChangeAndCommit> list) {
      this.list = list;
      table.setWidth("");

      keysNavigation.setName(Resources.C.relatedChanges());
      keysNavigation.add(
          new PrevKeyCommand(0, 'K', Resources.C.previousChange()),
          new NextKeyCommand(0, 'J', Resources.C.nextChange()));
      keysNavigation.add(new OpenKeyCommand(0, 'O', Resources.C.openChange()));
    }

    @Override
    protected Object getRowItemKey(ChangeAndCommit item) {
      return item.id();
    }

    @Override
    protected ChangeAndCommit getRowItem(int row) {
      if (0 <= row && row <= list.length()) {
        return list.get(row);
{'fraction_non_alphanumeric': 0.12262062859672422, 'fraction_numerical': 0.002390438247011952, 'mean_word_length': 3.422866092404072, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, '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': '2147327', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3473, 'n_words': 802}