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