1ca987122SIngo Weinhold /*
2ca987122SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3ca987122SIngo Weinhold * Distributed under the terms of the MIT License.
4ca987122SIngo Weinhold */
5ca987122SIngo Weinhold
6*d8d4b902SIngo Weinhold
7ca987122SIngo Weinhold #include "main_window/TeamsPage.h"
8ca987122SIngo Weinhold
9ca987122SIngo Weinhold #include <stdio.h>
10ca987122SIngo Weinhold
11ca987122SIngo Weinhold #include <new>
12ca987122SIngo Weinhold
13ca987122SIngo Weinhold #include "table/TableColumns.h"
14ca987122SIngo Weinhold
15ca987122SIngo Weinhold
16ca987122SIngo Weinhold // #pragma mark - TeamsTableModel
17ca987122SIngo Weinhold
18ca987122SIngo Weinhold
19ca987122SIngo Weinhold class MainWindow::TeamsPage::TeamsTableModel : public TableModel {
20ca987122SIngo Weinhold public:
TeamsTableModel(Model * model)21ca987122SIngo Weinhold TeamsTableModel(Model* model)
22ca987122SIngo Weinhold :
23ca987122SIngo Weinhold fModel(model)
24ca987122SIngo Weinhold {
25ca987122SIngo Weinhold }
26ca987122SIngo Weinhold
CountColumns() const27ca987122SIngo Weinhold virtual int32 CountColumns() const
28ca987122SIngo Weinhold {
29ca987122SIngo Weinhold return 4;
30ca987122SIngo Weinhold }
31ca987122SIngo Weinhold
CountRows() const32ca987122SIngo Weinhold virtual int32 CountRows() const
33ca987122SIngo Weinhold {
34ca987122SIngo Weinhold return fModel->CountTeams();
35ca987122SIngo Weinhold }
36ca987122SIngo Weinhold
GetValueAt(int32 rowIndex,int32 columnIndex,BVariant & value)37b7c5bd63SIngo Weinhold virtual bool GetValueAt(int32 rowIndex, int32 columnIndex, BVariant& value)
38ca987122SIngo Weinhold {
39ca987122SIngo Weinhold Model::Team* team = fModel->TeamAt(rowIndex);
40ca987122SIngo Weinhold if (team == NULL)
41ca987122SIngo Weinhold return false;
42ca987122SIngo Weinhold
43ca987122SIngo Weinhold switch (columnIndex) {
44ca987122SIngo Weinhold case 0:
45ca987122SIngo Weinhold value.SetTo(team->ID());
46ca987122SIngo Weinhold return true;
47ca987122SIngo Weinhold case 1:
48b7c5bd63SIngo Weinhold value.SetTo(team->Name(), B_VARIANT_DONT_COPY_DATA);
49ca987122SIngo Weinhold return true;
50ca987122SIngo Weinhold case 2:
51ca987122SIngo Weinhold value.SetTo(team->CreationTime());
52ca987122SIngo Weinhold return true;
53ca987122SIngo Weinhold case 3:
54ca987122SIngo Weinhold value.SetTo(team->DeletionTime());
55ca987122SIngo Weinhold return true;
56ca987122SIngo Weinhold default:
57ca987122SIngo Weinhold return false;
58ca987122SIngo Weinhold }
59ca987122SIngo Weinhold }
60ca987122SIngo Weinhold
61ca987122SIngo Weinhold private:
62ca987122SIngo Weinhold Model* fModel;
63ca987122SIngo Weinhold };
64ca987122SIngo Weinhold
65ca987122SIngo Weinhold
66ca987122SIngo Weinhold // #pragma mark - TeamsPage
67ca987122SIngo Weinhold
68ca987122SIngo Weinhold
TeamsPage(MainWindow * parent)69ca987122SIngo Weinhold MainWindow::TeamsPage::TeamsPage(MainWindow* parent)
70ca987122SIngo Weinhold :
71ca987122SIngo Weinhold BGroupView(B_VERTICAL),
72ca987122SIngo Weinhold fParent(parent),
73ca987122SIngo Weinhold fTeamsTable(NULL),
74ca987122SIngo Weinhold fTeamsTableModel(NULL),
75ca987122SIngo Weinhold fModel(NULL)
76ca987122SIngo Weinhold {
77ca987122SIngo Weinhold SetName("Teams");
78ca987122SIngo Weinhold
79ca987122SIngo Weinhold fTeamsTable = new Table("teams list", 0);
80ca987122SIngo Weinhold AddChild(fTeamsTable->ToView());
81ca987122SIngo Weinhold
82ca987122SIngo Weinhold fTeamsTable->AddColumn(new Int32TableColumn(0, "ID", 40, 20, 1000,
83ca987122SIngo Weinhold B_TRUNCATE_MIDDLE, B_ALIGN_RIGHT));
84ca987122SIngo Weinhold fTeamsTable->AddColumn(new StringTableColumn(1, "Name", 80, 40, 1000,
85ca987122SIngo Weinhold B_TRUNCATE_END, B_ALIGN_LEFT));
86*d8d4b902SIngo Weinhold fTeamsTable->AddColumn(new NanotimeTableColumn(2, "Creation", 80, 40, 1000,
87ca987122SIngo Weinhold true, B_TRUNCATE_MIDDLE, B_ALIGN_RIGHT));
88*d8d4b902SIngo Weinhold fTeamsTable->AddColumn(new NanotimeTableColumn(3, "Deletion", 80, 40, 1000,
89ca987122SIngo Weinhold false, B_TRUNCATE_MIDDLE, B_ALIGN_RIGHT));
90ca987122SIngo Weinhold
91ca987122SIngo Weinhold fTeamsTable->AddTableListener(this);
92ca987122SIngo Weinhold }
93ca987122SIngo Weinhold
94ca987122SIngo Weinhold
~TeamsPage()95ca987122SIngo Weinhold MainWindow::TeamsPage::~TeamsPage()
96ca987122SIngo Weinhold {
97ca987122SIngo Weinhold fTeamsTable->SetTableModel(NULL);
98ca987122SIngo Weinhold delete fTeamsTableModel;
99ca987122SIngo Weinhold }
100ca987122SIngo Weinhold
101ca987122SIngo Weinhold
102ca987122SIngo Weinhold void
SetModel(Model * model)103ca987122SIngo Weinhold MainWindow::TeamsPage::SetModel(Model* model)
104ca987122SIngo Weinhold {
105ca987122SIngo Weinhold if (model == fModel)
106ca987122SIngo Weinhold return;
107ca987122SIngo Weinhold
108ca987122SIngo Weinhold if (fModel != NULL) {
109ca987122SIngo Weinhold fTeamsTable->SetTableModel(NULL);
110ca987122SIngo Weinhold delete fTeamsTableModel;
111ca987122SIngo Weinhold fTeamsTableModel = NULL;
112ca987122SIngo Weinhold }
113ca987122SIngo Weinhold
114ca987122SIngo Weinhold fModel = model;
115ca987122SIngo Weinhold
116ca987122SIngo Weinhold if (fModel != NULL) {
117ca987122SIngo Weinhold fTeamsTableModel = new(std::nothrow) TeamsTableModel(fModel);
118ca987122SIngo Weinhold fTeamsTable->SetTableModel(fTeamsTableModel);
1192d11730aSIngo Weinhold fTeamsTable->ResizeAllColumnsToPreferred();
120ca987122SIngo Weinhold }
121ca987122SIngo Weinhold }
122ca987122SIngo Weinhold
123ca987122SIngo Weinhold
124ca987122SIngo Weinhold void
TableRowInvoked(Table * table,int32 rowIndex)125ca987122SIngo Weinhold MainWindow::TeamsPage::TableRowInvoked(Table* table, int32 rowIndex)
126ca987122SIngo Weinhold {
127ca987122SIngo Weinhold Model::Team* team = fModel->TeamAt(rowIndex);
128ca987122SIngo Weinhold if (team != NULL)
129ca987122SIngo Weinhold fParent->OpenTeamWindow(team);
130ca987122SIngo Weinhold }
131