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