]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java
replaced all <> with explicit types - needed for jenkins
[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 at.ac.tuwien.sbc.valesriegler.common.TableModel;
4 import at.ac.tuwien.sbc.valesriegler.common.Util;
5 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
6 import at.ac.tuwien.sbc.valesriegler.types.Table;
7
8 import java.util.ArrayList;
9 import java.util.List;
10
11 public class TablesOverviewModel extends TableModel<Table> {
12         private static final String TABLE_ID = "ID";
13         private static final String STATUS = "Is free";
14         private static final String GROUP_ID = "Group ID";
15         private static final String[] COLUMNS = new String[] { TABLE_ID, STATUS, GROUP_ID };
16
17         @Override
18         protected String[] getColumns() {
19                 return COLUMNS;
20         }
21
22         @Override
23         public Object getValueAt(int rowIndex, int columnIndex) {
24                 List<Table> values = new ArrayList<Table>(items.values());
25                 Table table = values.get(rowIndex);
26                 String wantedColumn = COLUMNS[columnIndex];
27                 switch (wantedColumn) {
28                 case TABLE_ID:
29                         return table.getId();
30                 case STATUS:
31                         return table.isFree();
32                 case GROUP_ID:
33                         return Util.getId(table.getGroupId());
34                 default:
35                         throw new RuntimeException(UNHANDLEDCOLUMN);
36                 }
37         }
38
39         public int getNumberOfFreeTables() {
40                 return getIdsOfFreeTables().size();
41         }
42
43         public List<Integer> getIdsOfFreeTables() {
44                 List<Integer> ids = new ArrayList<Integer>();
45
46                 synchronized (items) {
47                         for (Table table : items.values()) {
48                                 if (table.isFree()) {
49                                         ids.add(table.getId());
50                                 }
51                         }
52                 }
53
54                 return ids;
55         }
56
57         public List<Table> createFreeTables(int numberOfTables) {
58                 List<Table> tables = new ArrayList<Table>();
59                 for (int i = 0; i < numberOfTables; i++) {
60                         Table table = createFreeTable();
61                         tables.add(table);
62                 }
63
64                 if (Util.useJMS)
65                         setItems(tables);
66
67                 return tables;
68         }
69
70         private Table createFreeTable() {
71                 return new Table();
72         }
73
74         public int getTableIdOfGroup(GroupData group) {
75                 synchronized (items) {
76                         for (Table table : items.values()) {
77                                 if (table.getGroupId() == group.getId()) {
78                                         return table.getId();
79                                 }
80                         }
81                 }
82                 throw new RuntimeException("Could not find table with the wanted group");
83         }
84
85         public Table getTableById(int tableId) {
86                 synchronized (items) {
87                         for (Table table : items.values()) {
88                                 if (table.getId() == tableId) {
89                                         return table;
90                                 }
91                         }
92                 }
93                 throw new RuntimeException("Could not find table with the wanted group");
94         }
95 }