]> 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 https://bitbucket.org/rgregor/sbc-ss-2013
[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.types.Table;
8
9 public class TablesOverviewModel extends TableModel<Table> {
10         private static final String TABLE_ID = "ID";
11         private static final String STATUS = "Is free";
12         private static final String GROUP_ID = "Group ID";
13         private static final String[] COLUMNS = new String[] { TABLE_ID, STATUS, GROUP_ID };
14         private int idCounter = 1;
15
16         @Override
17         protected String[] getColumns() {
18                 return COLUMNS;
19         }
20
21         @Override
22         public Object getValueAt(int rowIndex, int columnIndex) {
23                 List<Table> values = new ArrayList<>(items.values());
24                 Table table = values.get(rowIndex);
25                 String wantedColumn = COLUMNS[columnIndex];
26                 switch (wantedColumn) {
27                 case TABLE_ID:
28                         return table.getId();
29                 case STATUS:
30                         return table.isFree();
31                 case GROUP_ID:
32                         return table.getGroupId();
33                 default:
34                         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 (Table table : items.values()) {
47                                 if (table.isFree()) {
48                                         ids.add(table.getId());
49                                 }
50                         }
51                 }
52
53                 return ids;
54         }
55
56         public List<Table> createFreeTables(int numberOfTables) {
57                 synchronized (items) {
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                         setItems(tables);
65
66                         return tables;
67                 }
68         }
69
70         private Table createFreeTable() {
71                 return new Table();
72         }
73
74         public void mergeTables(List<Table> tables) {
75                 for (Table table : tables) {
76                         items.put(table.getId(), table);
77                 }
78                 fireTableDataChanged();
79         }
80
81 }