summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/ProjectsTable.java
blob: 2137e7d019a509549fa6359ca1b4fedcd2063547 (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
// Copyright (C) 2010 The Android Open Source Project
// Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
//
// 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.ui;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.ui.NavigationTable;
import com.google.gerrit.client.ui.AbstractKeyNavigation.Action;
import com.google.gerrit.reviewdb.Project;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

import java.util.List;

public class ProjectsTable extends NavigationTable<Project> {
  public ProjectsTable() {
    keyNavigation = new DefaultKeyNavigation(this);
    keyNavigation.setKeyHelp(Action.NEXT, Util.C.projectListNext());
    keyNavigation.setKeyHelp(Action.PREV, Util.C.projectListPrev());
    keyNavigation.setKeyHelp(Action.OPEN, Util.C.projectListOpen());
    keyNavigation.initializeKeys();

    table.setText(0, 1, Util.C.projectName());
    table.setText(0, 2, Util.C.projectDescription());

    final FlexCellFormatter fmt = table.getFlexCellFormatter();
    fmt.addStyleName(0, 1, Gerrit.RESOURCES.css().dataHeader());
    fmt.addStyleName(0, 2, Gerrit.RESOURCES.css().dataHeader());
  }

  protected MyFlexTable createFlexTable() {
    MyFlexTable table = new MyFlexTable() {
      @Override
      public void onBrowserEvent(final Event event) {
        switch (DOM.eventGetType(event)) {
          case Event.ONCLICK: {
            // Find out which cell was actually clicked.
            final Element td = getEventTargetCell(event);
            if (td == null) {
              break;
            }
            final int row = rowOf(td);
            if (getRowItem(row) != null) {
              ProjectsTable.this.movePointerTo(row);
              return;
            }
            break;
          }
          case Event.ONDBLCLICK: {
            // Find out which cell was actually clicked.
            Element td = getEventTargetCell(event);
            if (td == null) {
              return;
            }
            onOpenRow(rowOf(td));
            return;
          }
        }
        super.onBrowserEvent(event);
      }
    };

    table.sinkEvents(Event.ONDBLCLICK | Event.ONCLICK);
    return table;
  }

  @Override
  protected Object getRowItemKey(final Project item) {
    return item.getNameKey();
  }

  @Override
  protected void onOpenRow(final int row) {
    if (row > 0) {
      movePointerTo(row);
    }
  }

  public void display(final List<Project> projects) {
    while (1 < table.getRowCount())
      table.removeRow(table.getRowCount() - 1);

    for (final Project k : projects)
      insert(table.getRowCount(), k);

    finishDisplay();
  }

  protected void insert(final int row, final Project k) {
    table.insertRow(row);

    applyDataRowStyle(row);

    final FlexCellFormatter fmt = table.getFlexCellFormatter();
    fmt.addStyleName(row, 1, Gerrit.RESOURCES.css().dataCell());
    fmt.addStyleName(row, 1, Gerrit.RESOURCES.css().cPROJECT());
    fmt.addStyleName(row, 2, Gerrit.RESOURCES.css().dataCell());

    populate(row, k);
  }

  protected void populate(final int row, final Project k) {
    table.setText(row, 1, k.getName());
    table.setText(row, 2, k.getDescription());

    setRowItem(row, k);
  }
}