]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java
Merge branch 'master' of
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / pizzeria / gui / tablemodels / TablesOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import at.ac.tuwien.sbc.valesriegler.DEP_GroupDataMin;
7 import at.ac.tuwien.sbc.valesriegler.DEP_Table;
8 import at.ac.tuwien.sbc.valesriegler.DEP_Table.TableStatus;
9 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
10
11 public class TablesOverviewModel extends TableModel<DEP_Table> {
12         private static final String TABLE_ID = "ID";
13         private static final String STATUS = "Status";
14         private static final String GROUP_ID = "Group ID";
15         private static final String[] COLUMNS = new String[] {
16                 TABLE_ID, STATUS, GROUP_ID
17         };
18         private int idCounter = 1;
19
20         @Override
21         protected String[] getColumns() {
22                 return COLUMNS;
23         }
24
25         @Override
26         public Object getValueAt(int rowIndex, int columnIndex) {
27                 List<DEP_Table> values = new ArrayList<>(items.values());
28                 DEP_Table table = values.get(rowIndex);
29                 String wantedColumn = COLUMNS[columnIndex];
30                 switch(wantedColumn) {
31                 case TABLE_ID : return table.getId();
32                 case STATUS : return table.getStatus();
33                 case GROUP_ID : return table.getGroupId();
34                 default : throw new RuntimeException(UNHANDLEDCOLUMN); 
35                 }
36         }
37
38         public int getNumberOfFreeTables() {
39                 return getIdsOfFreeTables().size();
40         }
41         
42         public List<Integer> getIdsOfFreeTables() {
43                 List<Integer> ids = new ArrayList<>();
44                 
45                 synchronized(items) {
46                         for(DEP_Table table : items.values()) {
47                                 if(table.getStatus() == TableStatus.FREE) {
48                                         ids.add(table.getId());
49                                 }
50                         }
51                 }
52                 
53                 return ids;
54         }
55         
56         public List<DEP_Table> createFreeTables(int numberOfTables) {
57                 synchronized(items) {
58                         List<DEP_Table> tables = new ArrayList<DEP_Table>();
59                         for(int i=0; i<numberOfTables; i++) {
60                                 DEP_Table table = createFreeTable();
61                                 tables.add(table);
62                         }
63                         
64                         return tables;
65                 }
66         }
67         
68         private DEP_Table createFreeTable() {
69                 DEP_Table table = new DEP_Table();
70                 table.setId(idCounter);
71                 idCounter = idCounter + 1;
72                 table.setStatus(TableStatus.FREE);
73                 return table;
74         }
75
76         public void mergeTables(List<DEP_Table> tables) {
77                 for (DEP_Table table : tables) {
78                         items.put(table.getId(), table);
79                 }
80                 fireTableDataChanged();
81         }
82
83 }