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/check.h"
8 #include "base/i18n/string_compare.h"
9 #include "base/notreached.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/gfx/image/image_skia.h"
12
13 namespace ui {
14
15 // TableColumn -----------------------------------------------------------------
16
TableColumn()17 TableColumn::TableColumn()
18 : id(0),
19 title(),
20 alignment(LEFT),
21 width(-1),
22 percent(),
23 min_visible_width(0),
24 sortable(false),
25 initial_sort_is_ascending(true) {
26 }
27
TableColumn(int id,Alignment alignment,int width,float percent)28 TableColumn::TableColumn(int id, Alignment alignment, int width, float percent)
29 : id(id),
30 title(l10n_util::GetStringUTF16(id)),
31 alignment(alignment),
32 width(width),
33 percent(percent),
34 min_visible_width(0),
35 sortable(false),
36 initial_sort_is_ascending(true) {
37 }
38
39 TableColumn::TableColumn(const TableColumn& other) = default;
40
41 // TableModel -----------------------------------------------------------------
42
43 // Used for sorting.
44 static icu::Collator* collator = NULL;
45
GetIcon(int row)46 gfx::ImageSkia TableModel::GetIcon(int row) {
47 return gfx::ImageSkia();
48 }
49
GetTooltip(int row)50 base::string16 TableModel::GetTooltip(int row) {
51 return base::string16();
52 }
53
CompareValues(int row1,int row2,int column_id)54 int TableModel::CompareValues(int row1, int row2, int column_id) {
55 DCHECK(row1 >= 0 && row1 < RowCount() &&
56 row2 >= 0 && row2 < RowCount());
57 base::string16 value1 = GetText(row1, column_id);
58 base::string16 value2 = GetText(row2, column_id);
59 icu::Collator* collator = GetCollator();
60
61 if (collator)
62 return base::i18n::CompareString16WithCollator(*collator, value1, value2);
63
64 NOTREACHED();
65 return 0;
66 }
67
ClearCollator()68 void TableModel::ClearCollator() {
69 delete collator;
70 collator = NULL;
71 }
72
GetCollator()73 icu::Collator* TableModel::GetCollator() {
74 if (!collator) {
75 UErrorCode create_status = U_ZERO_ERROR;
76 collator = icu::Collator::createInstance(create_status);
77 if (!U_SUCCESS(create_status)) {
78 collator = NULL;
79 NOTREACHED();
80 }
81 }
82 return collator;
83 }
84
85 } // namespace ui
86