]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
großer Brocken an GruppenGui-Änderungen kombiniert mit ersten Teil des Bestellworkflows.
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
11 import at.ac.tuwien.sbc.valesriegler.group.Group;
12 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
13
14 public class GroupOverviewModel extends TableModel<Group> {
15         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
16
17         private static final String STATE = "State";
18         private static final String PIZZAS = "Pizzas";
19         private static final String SIZE = "Size";
20         private static final String ID = "ID";
21         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
22
23         public GroupOverviewModel() {
24                 super();
25                 super.items.add(new Group()); // TODO: remove, debuggroup!
26         }
27
28         @Override
29         protected String[] getColumns() {
30                 return COLUMNS;
31         }
32
33         public List<Group> getItems() {
34                 return items;
35         }
36
37         public Group getGroupByID(int id) {
38                 for (Group group : items) {
39                         if (group.getGroupData().getId() == id)
40                                 return group;
41                 }
42                 return null;
43         }
44
45         @Override
46         public Object getValueAt(int rowIndex, int columnIndex) {
47                 Group group = items.get(rowIndex);
48                 String wantedColumn = COLUMNS[columnIndex];
49                 switch (wantedColumn) {
50                 case ID:
51                         int groupId = group.getGroupData().getId();
52                         log.info("This is the group id : {}", groupId);
53                         return groupId;
54                 case SIZE:
55                         int groupSize = group.getGroupData().getSize();
56                         log.info("This is the size : {}", groupSize);
57                         return groupSize;
58                 case PIZZAS:
59                         return pizzaDisplay(group.getGroupData().getPizzas());
60                 case STATE:
61                         return group.getGroupData().getState();
62                 default:
63                         throw new RuntimeException(UNHANDLEDCOLUMN);
64                 }
65         }
66
67         /**
68          * 
69          * @return A display string for PizzaTypes.
70          *         <p />
71          *         e.g. Cardinale, Cardinale, Margherita is displayed as
72          *         "2xCARDINALE,1xMARGHERITA"
73          */
74         private String pizzaDisplay(List<PizzaType> pizzas) {
75                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
76                 for (PizzaType pizzaType : pizzas) {
77                         if (pizzaCount.containsKey(pizzaType)) {
78                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
79                         } else
80                                 pizzaCount.put(pizzaType, 1);
81                 }
82                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
83                 StringBuilder sb = new StringBuilder();
84
85                 boolean multiplePizzas = false;
86                 for (PizzaType pizzaType : pizzaTypes) {
87                         if (multiplePizzas)
88                                 sb.append(",");
89                         else
90                                 multiplePizzas = true;
91
92                         sb.append(pizzaCount.get(pizzaType) + "x");
93                         sb.append(pizzaType.toString());
94                 }
95
96                 return sb.toString();
97         }
98
99         @Override
100         public void addItems(List<Group> newItems) {
101                 log.info("addItems()");
102                 super.addItems(newItems);
103                 for (Group g : newItems) {
104                         g.goGrabSomeFood();
105                 }
106         }
107 }