]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java
[XVSM]Some NPE fixes, GUI fixes
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / pizzeria / gui / tablemodels / GroupsOverviewModel.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.types.GroupData;
5 import at.ac.tuwien.sbc.valesriegler.types.Table;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import java.util.List;
13
14 public class GroupsOverviewModel extends TableModel<GroupData> {
15     private static final Logger log = LoggerFactory.getLogger(GroupsOverviewModel.class);
16         private static final String ID = "ID";
17         private static final String SIZE = "Size";
18
19         private static final String[] COLUMNS = new String[] { ID, SIZE };
20
21         @Override
22         protected String[] getColumns() {
23                 return COLUMNS;
24         }
25         
26
27         @Override
28         public Object getValueAt(int rowIndex, int columnIndex) {
29                 List<GroupData> values = new ArrayList<>(items.values());
30
31         // TODO: make sure that is necessary
32         Collections.sort(values, new Comparator<GroupData>() {
33             @Override
34             public int compare(GroupData o1, GroupData o2) {
35                 final int o1Id = o1.getOrder().getId();
36                 final int o2Id = o2.getOrder().getId();
37                 if (o1Id == 0) {
38                     if (o2Id > 0) return 1;
39                 }
40                 if (o2Id == 0) {
41                     if (o1Id > 0) return 1;
42                 }
43                 if (o1Id < o2Id) return -1;
44                 else if (o1Id > o2Id) return 1;
45                 else return 0;
46             }
47         });
48                 GroupData group = values.get(rowIndex);
49                 String wantedColumn = COLUMNS[columnIndex];
50                 switch (wantedColumn) {
51                 case ID:
52                         return group.getId();
53                 case SIZE:
54                         return group.getSize();
55                 default:
56                         throw new RuntimeException(UNHANDLEDCOLUMN);
57                 }
58         }
59
60         public void removeGroup(int id) {
61                 items.remove(id);
62                 
63                 fireTableDataChanged();
64         }
65
66     public void removeGroupsFromTables(List<Table> tables) {
67         synchronized (items) {
68             for (Table table : tables) {
69                 final int groupId = table.getGroupId();
70                 items.remove(groupId);
71             }
72         }
73         fireTableDataChanged();
74     }
75 }