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