From 192702e280addb6542191c0b911d55b5d83c1873 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Tue, 23 Apr 2013 19:20:49 +0200 Subject: [PATCH] Basic Group GUI implementation --- .classpath | 6 +- .settings/org.eclipse.jdt.core.prefs | 13 +- README.md | 7 +- .../sbc/valesriegler/common/PizzaType.java | 13 ++ .../tuwien/sbc/valesriegler/group/Group.java | 40 +++++ .../sbc/valesriegler/group/GroupAgent.java | 31 ++++ .../sbc/valesriegler/group/GroupState.java | 5 + .../group/gui/GroupCreationPanel.java | 90 +++++++++++ .../group/gui/GroupCreationRequest.java | 18 +++ .../valesriegler/group/gui/GroupFrame.java | 129 +++++++++++++++ .../group/gui/GroupOverviewModel.java | 148 ++++++++++++++++++ .../sbc/valesriegler/{ => jms}/cook/Cook.java | 2 +- .../valesriegler/{ => jms}/waiter/Waiter.java | 2 +- 13 files changed, 492 insertions(+), 12 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/{ => jms}/cook/Cook.java (89%) rename src/main/java/at/ac/tuwien/sbc/valesriegler/{ => jms}/waiter/Waiter.java (88%) diff --git a/.classpath b/.classpath index 534b5e5..96fa85c 100644 --- a/.classpath +++ b/.classpath @@ -22,15 +22,11 @@ - - - - - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 60105c1..6249222 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,12 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 -org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.6 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/README.md b/README.md index b893ac9..6b1ec12 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ Space Based Computing SS 2013 ================== +### How to start the Group GUI + mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" ### How to start a Waiter with Id 1 - mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.waiter.Waiter" -Dexec.args="1" + mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.jms.waiter.Waiter" -Dexec.args="1" ### How to start a Cook with Id 1 - mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.cook.Cook" -Dexec.args="1" \ No newline at end of file + mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.jms.cook.Cook" -Dexec.args="1" + diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java new file mode 100644 index 0000000..ee61c7b --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java @@ -0,0 +1,13 @@ +package at.ac.tuwien.sbc.valesriegler.common; + +public enum PizzaType { + MARGHERITA(5, 3), SALAMI(5.5, 7), CARDINALE(6,5); + + public final double price; + public final double duration; + + private PizzaType(double price, double duration) { + this.price = price; + this.duration = duration; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java new file mode 100644 index 0000000..d407046 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -0,0 +1,40 @@ +package at.ac.tuwien.sbc.valesriegler.group; + +import java.util.ArrayList; +import java.util.List; + +import at.ac.tuwien.sbc.valesriegler.common.PizzaType; + +public class Group { + private int id; + private int size; + + private List pizzas = new ArrayList(); + private GroupState state = GroupState.WAITING; + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public int getSize() { + return size; + } + public void setSize(int size) { + this.size = size; + } + public List getPizzas() { + return pizzas; + } + public void setPizzas(List pizzas) { + this.pizzas = pizzas; + } + public GroupState getState() { + return state; + } + public void setState(GroupState state) { + this.state = state; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java new file mode 100644 index 0000000..c7fdcf5 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java @@ -0,0 +1,31 @@ +package at.ac.tuwien.sbc.valesriegler.group; + +import javax.swing.SwingUtilities; + +import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame; + +/** + * + * + * @author Gregor Riegler + * + */ +public class GroupAgent { + + public static void main(String[] args) { + // TODO initiate communication + + SwingUtilities.invokeLater(new GroupGUI()); + + } + + static class GroupGUI implements Runnable { + @Override + public void run() { + GroupFrame frame = new GroupFrame(); + frame.pack(); + frame.setVisible(true); + } + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java new file mode 100644 index 0000000..c2f499b --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java @@ -0,0 +1,5 @@ +package at.ac.tuwien.sbc.valesriegler.group; + +public enum GroupState { + WAITING, SITTING, ORDERED, EATING, PAY, GONE +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java new file mode 100644 index 0000000..c8cadc5 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java @@ -0,0 +1,90 @@ +package at.ac.tuwien.sbc.valesriegler.group.gui; + +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.SpinnerNumberModel; + +import at.ac.tuwien.sbc.valesriegler.common.PizzaType; +import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame.GroupCreationHandler; + +public class GroupCreationPanel extends JPanel { + + private GroupCreationHandler handler; + private int numberMembers; + private JSpinner numberOfGroupsSpinner; + private List combos = new ArrayList(); + + public GroupCreationPanel(int numberMembers) { + this.numberMembers = numberMembers; + + GridLayout layout = new GridLayout(0, 2); + setLayout(layout); + + for(int i=1; i<=numberMembers; i++) { + JLabel memberLabel = new JLabel(i + ". Person"); + JComboBox pizzaDropdown = new JComboBox(); + combos.add(pizzaDropdown); + for(PizzaType type : PizzaType.values()) { + pizzaDropdown.addItem(type); + } + this.add(memberLabel); + this.add(pizzaDropdown); + } + JLabel numberOfGroupsLabel = new JLabel("Number of groups to create: "); + SpinnerNumberModel model = new SpinnerNumberModel(1,1,100,1); + numberOfGroupsSpinner = new JSpinner(model); + + this.add(numberOfGroupsLabel); + this.add(numberOfGroupsSpinner); + + JButton create = new JButton("Create"); + JButton cancel = new JButton("Cancel"); + + this.add(create); + this.add(cancel); + + addCreateHandler(create); + addCancelHandler(cancel); + } + + + private void addCancelHandler(JButton cancel) { + cancel.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + handler.handleCancel(); + } + }); + } + + private void addCreateHandler(JButton create) { + create.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + List types = new ArrayList<>(); + for(JComboBox combo : combos) { + types.add((PizzaType) combo.getSelectedItem()); + } + GroupCreationRequest gcr = new GroupCreationRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types); + handler.handleGroupCreation(gcr); + } + }); + } + + public void setCreateAndCancelHandler( + GroupCreationHandler groupCreationCallback) { + this.handler = groupCreationCallback; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java new file mode 100644 index 0000000..1c24d8c --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java @@ -0,0 +1,18 @@ +package at.ac.tuwien.sbc.valesriegler.group.gui; + +import java.util.List; + +import at.ac.tuwien.sbc.valesriegler.common.PizzaType; + +public class GroupCreationRequest { + public final int size; + public final int numberOfGroups; + public final List pizzaTypes; + + public GroupCreationRequest(int size, int numberOfGroups, + List pizzaTypes) { + this.size = size; + this.numberOfGroups = numberOfGroups; + this.pizzaTypes = pizzaTypes; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java new file mode 100644 index 0000000..9f73cc3 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java @@ -0,0 +1,129 @@ +package at.ac.tuwien.sbc.valesriegler.group.gui; + +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSpinner; +import javax.swing.JTable; +import javax.swing.SpinnerNumberModel; +import javax.swing.border.TitledBorder; + +import at.ac.tuwien.sbc.valesriegler.group.Group; + +/** + * Base Frame of the Group UI + * + * @author Gregor Riegler + * + */ +public class GroupFrame extends JFrame { + private AtomicInteger idSource = new AtomicInteger(1); + + private GroupOverviewModel groupModel; + protected GroupCreationPanel groupCreationPanel; + private static JPanel creationPanel; + private static JPanel chooseGroupSizePanel; + + public GroupFrame() { + super("Group Creator"); + JPanel wrapper = new JPanel(); + GridLayout wrapperLayout = new GridLayout(2, 1); + wrapper.setLayout(wrapperLayout); + + initGroupCreation(wrapper); + initGroupOverview(wrapper); + + setContentPane(wrapper); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + private void initGroupCreation(JPanel wrapper) { + creationPanel = new JPanel(); + chooseGroupSizePanel = new JPanel(); + JLabel creationLabel = new JLabel("How many members should the group have?"); + JButton next = new JButton("Next"); + SpinnerNumberModel model = new SpinnerNumberModel(1,1,4,1); + final JSpinner spinner = new JSpinner(model); + + next.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + int numberMembers = (int) spinner.getValue(); + + groupCreationPanel = new GroupCreationPanel(numberMembers); + groupCreationPanel.setCreateAndCancelHandler(new GroupCreationHandler()); + + chooseGroupSizePanel.setVisible(false); + creationPanel.add(groupCreationPanel); + } + }); + + GridLayout creationPanelLayout = new GridLayout(3,1); + chooseGroupSizePanel.setLayout(creationPanelLayout); + creationPanel.setBorder(new TitledBorder("Create Groups")); + + creationPanel.add(chooseGroupSizePanel); + chooseGroupSizePanel.add(creationLabel); + chooseGroupSizePanel.add(spinner); + chooseGroupSizePanel.add(next); + + wrapper.add(creationPanel); + } + + private void initGroupOverview(JPanel wrapper) { + JPanel overviewPanel = new JPanel(); + groupModel = new GroupOverviewModel(); + JTable overviewTable = new JTable(groupModel); + JScrollPane scrollPane = new JScrollPane(overviewTable); + + overviewPanel.setBorder(new TitledBorder("Group Overview")); + scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); + + overviewPanel.add(scrollPane); + wrapper.add(overviewPanel); + } + + + class GroupCreationHandler { + public void handleGroupCreation(GroupCreationRequest gc) { + chooseGroupSizePanel.setVisible(true); + creationPanel.remove(groupCreationPanel); + creationPanel.repaint(); + + createGroups(gc); + + JOptionPane.showMessageDialog(null,"Groups were created!"); + } + + private void createGroups(GroupCreationRequest gc) { + List newGroups = new ArrayList<>(); + for(int i=0; i groups = new ArrayList(); + + public GroupOverviewModel() { + insertTestData(); + } + + private void insertTestData() { + Group group1 = new Group(); + group1.setId(1); + group1.setSize(3); + group1.setState(GroupState.EATING); + + Group group2 = new Group(); + group2.setId(2); + group2.setSize(1); + group2.setState(GroupState.GONE); + + Group group3 = new Group(); + group3.setId(3); + group3.setSize(2); + group3.setState(GroupState.EATING); + + ArrayList pizzas1 = new ArrayList(); + pizzas1.add(PizzaType.MARGHERITA); + pizzas1.add(PizzaType.CARDINALE); + pizzas1.add(PizzaType.CARDINALE); + + ArrayList pizzas2 = new ArrayList(); + pizzas2.add(PizzaType.MARGHERITA); + pizzas2.add(PizzaType.MARGHERITA); + pizzas2.add(PizzaType.CARDINALE); + + ArrayList pizzas3 = new ArrayList(); + pizzas3.add(PizzaType.MARGHERITA); + pizzas3.add(PizzaType.SALAMI); + pizzas3.add(PizzaType.CARDINALE); + + group1.setPizzas(pizzas1); + group2.setPizzas(pizzas2); + group3.setPizzas(pizzas3); + + groups.add(group1); + groups.add(group2); + groups.add(group3); + + fireTableDataChanged(); + } + + + @Override + public int getRowCount() { + return groups.size(); + } + + @Override + public String getColumnName(int column) { + return COLUMNS[column]; + } + + @Override + public int getColumnCount() { + return COLUMNS.length; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + Group group = groups.get(rowIndex); + String wantedColumn = COLUMNS[columnIndex]; + switch(wantedColumn) { + case ID : + int groupId = group.getId(); + log.info("This is the group id : {}", groupId); + return groupId; + case SIZE : + int groupSize = group.getSize(); + log.info("This is the size : {}", groupSize); + return groupSize; + case PIZZAS : return pizzaDisplay(group.getPizzas()); + case STATE : return group.getState(); + default : throw new RuntimeException(UNHANDLEDCOLUMN); + } + } + + /** + * + * @return A display string for PizzaTypes. + *

+ * e.g. Cardinale, Cardinale, Margherita is displayed as "2xCARDINALE,1xMARGHERITA" + */ + private String pizzaDisplay(List pizzas) { + HashMap pizzaCount = new HashMap(); + for (PizzaType pizzaType : pizzas) { + if(pizzaCount.containsKey(pizzaType)) { + pizzaCount.put(pizzaType, pizzaCount.get(pizzaType)+1); + } + else pizzaCount.put(pizzaType, 1); + } + Set pizzaTypes = pizzaCount.keySet(); + StringBuilder sb = new StringBuilder(); + + boolean multiplePizzas = false; + for (PizzaType pizzaType : pizzaTypes) { + if(multiplePizzas) sb.append(","); + else multiplePizzas = true; + + sb.append(pizzaCount.get(pizzaType) + "x"); + sb.append(pizzaType.toString()); + } + + return sb.toString(); + } + + public void addGroup(Group group) { + groups.add(group); + + fireTableDataChanged(); + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java similarity index 89% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java index 56c7a90..df34dd4 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.sbc.valesriegler.cook; +package at.ac.tuwien.sbc.valesriegler.jms.cook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java similarity index 88% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java index 4562a27..6ad521a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.sbc.valesriegler.waiter; +package at.ac.tuwien.sbc.valesriegler.jms.waiter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -- 2.43.0