]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
added some not really helpful class descriptions + removed types that got duplicated...
[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.PizzaOrder;
13 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
14
15 /**
16  * Might not the nicest way to store global data, but this is an elemental
17  * Object, storing ALL the Group's data. Might consider splitting those two
18  * usecases apart.
19  * 
20  * @author jan
21  * 
22  */
23 public class GroupOverviewModel extends TableModel<Group> {
24         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
25
26         private static final String STATE = "State";
27         private static final String PIZZAS = "Pizzas";
28         private static final String SIZE = "Size";
29         private static final String ID = "ID";
30         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
31
32         public GroupOverviewModel() {
33                 super();
34                 super.items.add(new Group()); // TODO: remove, debuggroup!
35         }
36
37         @Override
38         protected String[] getColumns() {
39                 return COLUMNS;
40         }
41
42         public List<Group> getItems() {
43                 return items;
44         }
45
46         public Group getGroupByID(int id) {
47                 for (Group group : items) {
48                         if (group.getGroupData().getId() == id)
49                                 return group;
50                 }
51                 return null;
52         }
53
54         @Override
55         public Object getValueAt(int rowIndex, int columnIndex) {
56                 Group group = items.get(rowIndex);
57                 String wantedColumn = COLUMNS[columnIndex];
58                 switch (wantedColumn) {
59                 case ID:
60                         int groupId = group.getGroupData().getId();
61                         log.info("This is the group id : {}", groupId);
62                         return groupId;
63                 case SIZE:
64                         int groupSize = group.getGroupData().getSize();
65                         log.info("This is the size : {}", groupSize);
66                         return groupSize;
67                 case PIZZAS:
68                         if (group.getGroupData().getOrder() == null)
69                                 return "none";
70                         return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
71                 case STATE:
72                         return group.getGroupData().getState();
73                 default:
74                         throw new RuntimeException(UNHANDLEDCOLUMN);
75                 }
76         }
77
78         /**
79          * 
80          * @return A display string for PizzaTypes.
81          *         <p />
82          *         e.g. Cardinale, Cardinale, Margherita is displayed as
83          *         "2xCARDINALE,1xMARGHERITA"
84          */
85         private String pizzaDisplay(List<PizzaOrder> list) {
86                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
87                 for (PizzaOrder pizzaOrder : list) {
88                         PizzaType pizzaType = pizzaOrder.getPizzaType();
89                         if (pizzaCount.containsKey(pizzaType)) {
90                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
91                         } else
92                                 pizzaCount.put(pizzaType, 1);
93                 }
94                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
95                 StringBuilder sb = new StringBuilder();
96
97                 boolean multiplePizzas = false;
98                 for (PizzaType pizzaType : pizzaTypes) {
99                         if (multiplePizzas)
100                                 sb.append(", ");
101                         else
102                                 multiplePizzas = true;
103
104                         sb.append(pizzaCount.get(pizzaType) + "x");
105                         sb.append(pizzaType.toString());
106                 }
107
108                 return sb.toString();
109         }
110
111         @Override
112         public void addItems(List<Group> newItems) {
113                 log.info("addItems()");
114                 super.addItems(newItems);
115                 for (Group g : newItems) {
116                         g.goGrabSomeFood();
117                 }
118         }
119 }