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