table_model.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/base/models/table_model.h"
6
7#include "base/logging.h"
8#include "ui/base/l10n/l10n_util.h"
9#include "ui/base/l10n/l10n_util_collator.h"
10#include "ui/gfx/image/image_skia.h"
11
12namespace ui {
13
14// TableColumn -----------------------------------------------------------------
15
16TableColumn::TableColumn()
17    : id(0),
18      title(),
19      alignment(LEFT),
20      width(-1),
21      percent(),
22      min_visible_width(0),
23      sortable(false) {
24}
25
26TableColumn::TableColumn(int id, const string16& title,
27                         Alignment alignment,
28                         int width)
29    : id(id),
30      title(title),
31      alignment(alignment),
32      width(width),
33      percent(0),
34      min_visible_width(0),
35      sortable(false) {
36}
37
38TableColumn::TableColumn(int id, const string16& title,
39                         Alignment alignment, int width, float percent)
40    : id(id),
41      title(title),
42      alignment(alignment),
43      width(width),
44      percent(percent),
45      min_visible_width(0),
46      sortable(false) {
47}
48
49// It's common (but not required) to use the title's IDS_* tag as the column
50// id. In this case, the provided conveniences look up the title string on
51// bahalf of the caller.
52TableColumn::TableColumn(int id, Alignment alignment, int width)
53    : id(id),
54      alignment(alignment),
55      width(width),
56      percent(0),
57      min_visible_width(0),
58      sortable(false) {
59  title = l10n_util::GetStringUTF16(id);
60}
61
62TableColumn::TableColumn(int id, Alignment alignment, int width, float percent)
63    : id(id),
64      alignment(alignment),
65      width(width),
66      percent(percent),
67      min_visible_width(0),
68      sortable(false) {
69  title = l10n_util::GetStringUTF16(id);
70}
71
72// TableModel -----------------------------------------------------------------
73
74// Used for sorting.
75static icu::Collator* collator = NULL;
76
77gfx::ImageSkia TableModel::GetIcon(int row) {
78  return gfx::ImageSkia();
79}
80
81string16 TableModel::GetTooltip(int row) {
82  return string16();
83}
84
85bool TableModel::ShouldIndent(int row) {
86  return false;
87}
88
89bool TableModel::HasGroups() {
90  return false;
91}
92
93TableModel::Groups TableModel::GetGroups() {
94  // If you override HasGroups to return true, you must override this as
95  // well.
96  NOTREACHED();
97  return std::vector<Group>();
98}
99
100int TableModel::GetGroupID(int row) {
101  // If you override HasGroups to return true, you must override this as
102  // well.
103  NOTREACHED();
104  return 0;
105}
106
107int TableModel::CompareValues(int row1, int row2, int column_id) {
108  DCHECK(row1 >= 0 && row1 < RowCount() &&
109         row2 >= 0 && row2 < RowCount());
110  string16 value1 = GetText(row1, column_id);
111  string16 value2 = GetText(row2, column_id);
112  icu::Collator* collator = GetCollator();
113
114  if (collator)
115    return l10n_util::CompareString16WithCollator(collator, value1, value2);
116
117  NOTREACHED();
118  return 0;
119}
120
121void TableModel::ClearCollator() {
122  delete collator;
123  collator = NULL;
124}
125
126icu::Collator* TableModel::GetCollator() {
127  if (!collator) {
128    UErrorCode create_status = U_ZERO_ERROR;
129    collator = icu::Collator::createInstance(create_status);
130    if (!U_SUCCESS(create_status)) {
131      collator = NULL;
132      NOTREACHED();
133    }
134  }
135  return collator;
136}
137
138}  // namespace ui
139