summaryrefslogtreecommitdiffstats
path: root/app/table_model.cc
blob: 821e14c63fa057c5898b6f627b10082520e8ec62 (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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "app/table_model.h"

#include "app/l10n_util.h"
#include "app/l10n_util_collator.h"
#include "base/logging.h"
#include "third_party/skia/include/core/SkBitmap.h"

// TableColumn -----------------------------------------------------------------

TableColumn::TableColumn()
    : id(0),
      title(),
      alignment(LEFT),
      width(-1),
      percent(),
      min_visible_width(0),
      sortable(false) {
}

TableColumn::TableColumn(int id, const std::wstring& title,
                         Alignment alignment,
                         int width)
    : id(id),
      title(title),
      alignment(alignment),
      width(width),
      percent(0),
      min_visible_width(0),
      sortable(false) {
}

TableColumn::TableColumn(int id, const std::wstring& title,
                         Alignment alignment, int width, float percent)
    : id(id),
      title(title),
      alignment(alignment),
      width(width),
      percent(percent),
      min_visible_width(0),
      sortable(false) {
}

// It's common (but not required) to use the title's IDS_* tag as the column
// id. In this case, the provided conveniences look up the title string on
// bahalf of the caller.
TableColumn::TableColumn(int id, Alignment alignment, int width)
    : id(id),
      alignment(alignment),
      width(width),
      percent(0),
      min_visible_width(0),
      sortable(false) {
  title = l10n_util::GetString(id);
}

TableColumn::TableColumn(int id, Alignment alignment, int width, float percent)
    : id(id),
      alignment(alignment),
      width(width),
      percent(percent),
      min_visible_width(0),
      sortable(false) {
  title = l10n_util::GetString(id);
}

// TableModel -----------------------------------------------------------------

// Used for sorting.
static icu::Collator* collator = NULL;

SkBitmap TableModel::GetIcon(int row) {
  return SkBitmap();
}

std::wstring TableModel::GetTooltip(int row) {
  return std::wstring();
}

bool TableModel::HasGroups() {
  return false;
}

TableModel::Groups TableModel::GetGroups() {
  // If you override HasGroups to return true, you must override this as
  // well.
  NOTREACHED();
  return std::vector<Group>();
}

int TableModel::GetGroupID(int row) {
  // If you override HasGroups to return true, you must override this as
  // well.
  NOTREACHED();
  return 0;
}

int TableModel::CompareValues(int row1, int row2, int column_id) {
  DCHECK(row1 >= 0 && row1 < RowCount() &&
         row2 >= 0 && row2 < RowCount());
  std::wstring value1 = GetText(row1, column_id);
  std::wstring value2 = GetText(row2, column_id);
  icu::Collator* collator = GetCollator();

  if (collator)
    return l10n_util::CompareStringWithCollator(collator, value1, value2);

  NOTREACHED();
  return 0;
}

icu::Collator* TableModel::GetCollator() {
  if (!collator) {
    UErrorCode create_status = U_ZERO_ERROR;
    collator = icu::Collator::createInstance(create_status);
    if (!U_SUCCESS(create_status)) {
      collator = NULL;
      NOTREACHED();
    }
  }
  return collator;
}

void TableModel::ClearCollator() {
  delete collator;
  collator = NULL;
}