From 08a8dd74b0bf572ac2517e024aacc2d423a66eaf Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 29 Apr 2013 14:04:26 +0200 Subject: [PATCH] UI Fix: Handle the vertical size of tables correctly --- .../valesriegler/group/gui/GroupFrame.java | 14 +- .../pizzeria/gui/PizzeriaFrame.java | 144 ++++++++++-------- 2 files changed, 93 insertions(+), 65 deletions(-) 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 index 84156a2..200cccf 100644 --- 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 @@ -1,6 +1,9 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; import java.awt.GridLayout; +import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; @@ -83,7 +86,8 @@ public class GroupFrame extends JFrame { } private void initGroupOverview(JPanel wrapper) { - JPanel overviewPanel = new JPanel(); + JPanel overviewPanel = new JPanel(new GridBagLayout()); + groupModel = new GroupOverviewModel(); JTable overviewTable = new JTable(groupModel); JScrollPane scrollPane = new JScrollPane(overviewTable); @@ -91,7 +95,13 @@ public class GroupFrame extends JFrame { overviewPanel.setBorder(new TitledBorder("Group Overview")); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); - overviewPanel.add(scrollPane); + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 0; + c.weightx = 1; + c.weighty = 1; + c.insets = new Insets(10, 10, 10, 10); + c.fill = GridBagConstraints.BOTH; + overviewPanel.add(scrollPane, c); wrapper.add(overviewPanel); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java index 1656599..75f46aa 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java @@ -1,6 +1,9 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; import java.awt.GridLayout; +import java.awt.Insets; import java.util.ArrayList; import java.util.List; @@ -20,7 +23,6 @@ import javax.swing.event.TableModelListener; import at.ac.tuwien.sbc.valesriegler.common.PizzaType; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzaOrder.PizzaOrderStatus; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Table.TableStatus; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; @@ -40,60 +42,64 @@ public class PizzeriaFrame extends JFrame { private static String WAITER_IDS_CAPTION = "Waiter-IDs of order %d"; private static String PIZZAS_CAPTION = "Pizzas of order %d"; private static String FREE_TABLES = "There are currently %d free table(s)"; - + private OrdersOverviewModel ordersModel; private GroupsOverviewModel groupModel; private TablesOverviewModel tablesModel; private WaitersOfOrderModel waitersModel; private PizzasOfOrderModel pizzasModel; - public PizzeriaFrame() { super("Pizzeria"); - + initModels(); - + JPanel wrapper = new JPanel(); GridLayout wrapperLayout = new GridLayout(2, 2); wrapper.setLayout(wrapperLayout); - + // Create the tables overview initTablesOverview(wrapper); - + // Create the waiting groups table overview initWaitingGroupsOverview(wrapper); - - //Create the overview table of the orders + + // Create the overview table of the orders JTable ordersTable = initOrdersOverview(wrapper); - + // Create the order detail tables initOrdersDetails(wrapper, ordersTable); - + setContentPane(wrapper); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private int getNumberOfTables() { int numberOfTables; - String numberOfTablesInput = JOptionPane.showInputDialog(PLEASE_INPUT_THE_NUMBER_OF_TABLES_TO_CREATE); + String numberOfTablesInput = JOptionPane + .showInputDialog(PLEASE_INPUT_THE_NUMBER_OF_TABLES_TO_CREATE); try { numberOfTables = Integer.parseInt(numberOfTablesInput); } catch (NumberFormatException e) { - JOptionPane.showMessageDialog(this, YOUR_INPUT_CANNOT_BE_PARSED_AS_A_NUMBER, WRONG_INPUT, JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(this, + YOUR_INPUT_CANNOT_BE_PARSED_AS_A_NUMBER, WRONG_INPUT, + JOptionPane.ERROR_MESSAGE); return getNumberOfTables(); } - - if(numberOfTables < 1) { - JOptionPane.showMessageDialog(this, THE_NUMBER_MUST_BE_GREATER_OR_EQUAL_TO_1, WRONG_INPUT, JOptionPane.ERROR_MESSAGE); + + if (numberOfTables < 1) { + JOptionPane.showMessageDialog(this, + THE_NUMBER_MUST_BE_GREATER_OR_EQUAL_TO_1, WRONG_INPUT, + JOptionPane.ERROR_MESSAGE); return getNumberOfTables(); } - + return numberOfTables; } private void initModels() { List orders = getOrdersTestData(); - + ordersModel = new OrdersOverviewModel(orders); groupModel = new GroupsOverviewModel(); tablesModel = new TablesOverviewModel(getNumberOfTables()); @@ -105,102 +111,114 @@ public class PizzeriaFrame extends JFrame { JPanel detailWrapper = new JPanel(); GridLayout wrapperLayout = new GridLayout(2, 1); detailWrapper.setLayout(wrapperLayout); - + final JPanel waitersPanel = new JPanel(); final JPanel pizzasPanel = new JPanel(); createTableInTitledPanel(waitersPanel, waitersModel, ""); createTableInTitledPanel(pizzasPanel, pizzasModel, ""); - + /** - * Update the displayed order in the waiter and pizza detail tables when an order - * gets selected in the orders table + * Update the displayed order in the waiter and pizza detail tables when + * an order gets selected in the orders table */ - ordersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { - @Override - public void valueChanged(ListSelectionEvent e) { - ListSelectionModel lsm = (ListSelectionModel)e.getSource(); - int minIndex = lsm.getMinSelectionIndex(); - - Order currentOrder = ordersModel.getOrderOfRow(minIndex); - waitersPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION,currentOrder.getId()))); - pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION,currentOrder.getId()))); - waitersModel.setCurrentOrder(currentOrder); - pizzasModel.setCurrentOrder(currentOrder); - - waitersPanel.setVisible(true); - pizzasPanel.setVisible(true); - } - }); - + ordersTable.getSelectionModel().addListSelectionListener( + new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + ListSelectionModel lsm = (ListSelectionModel) e + .getSource(); + int minIndex = lsm.getMinSelectionIndex(); + + Order currentOrder = ordersModel + .getOrderOfRow(minIndex); + waitersPanel.setBorder(new TitledBorder(String.format( + WAITER_IDS_CAPTION, currentOrder.getId()))); + pizzasPanel.setBorder(new TitledBorder(String.format( + PIZZAS_CAPTION, currentOrder.getId()))); + waitersModel.setCurrentOrder(currentOrder); + pizzasModel.setCurrentOrder(currentOrder); + + waitersPanel.setVisible(true); + pizzasPanel.setVisible(true); + } + }); + detailWrapper.add(waitersPanel); detailWrapper.add(pizzasPanel); waitersPanel.setVisible(false); pizzasPanel.setVisible(false); - + wrapper.add(detailWrapper); - + } private JTable initOrdersOverview(JPanel wrapper) { JPanel tablePanel = new JPanel(); - JTable orderTable = createTableInTitledPanel(tablePanel, ordersModel, "Orders"); - + JTable orderTable = createTableInTitledPanel(tablePanel, ordersModel, + "Orders"); + wrapper.add(tablePanel); - + return orderTable; } private void initWaitingGroupsOverview(JPanel wrapper) { JPanel tablePanel = new JPanel(); createTableInTitledPanel(tablePanel, groupModel, "Groups"); - + wrapper.add(tablePanel); } private void initTablesOverview(JPanel wrapper) { - JPanel tablePanel = new JPanel(); + JPanel scrollPanePanel = new JPanel(new GridBagLayout()); final JLabel freeTablesLabel = new JLabel(getNumberOfFreeTables()); - tablePanel.add(freeTablesLabel); - createTableInTitledPanel(tablePanel, tablesModel, "Tables"); - + scrollPanePanel.add(freeTablesLabel, new GridBagConstraints()); + createTableInTitledPanel(scrollPanePanel, tablesModel, "Tables"); + tablesModel.addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { freeTablesLabel.setText(getNumberOfFreeTables()); } }); - - - wrapper.add(tablePanel); + + wrapper.add(scrollPanePanel); } - - - private JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel model, String title) { + + private JTable createTableInTitledPanel(JPanel wrapperPanel, + TableModel model, String title) { JTable table = new JTable(model); + wrapperPanel.setLayout(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 0; + c.weightx = 1; + c.weighty = 1; + c.insets = new Insets(10, 10, 10, 10); + c.fill = GridBagConstraints.BOTH; JScrollPane scrollPane = new JScrollPane(table); - + wrapperPanel.setBorder(new TitledBorder(title)); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); - wrapperPanel.add(scrollPane); - + wrapperPanel.add(scrollPane, c); + return table; } - + private List getOrdersTestData() { List orders = new ArrayList<>(); Order order1 = new Order(); Order order2 = new Order(); Order order3 = new Order(); Order order4 = new Order(); - + order1.setId(1); order2.setId(2); order3.setId(3); order4.setId(4); - + order1.setWaiterIdOfOrder(4); order2.setWaiterIdOfServing(2); - + List pizzas = new ArrayList<>(); PizzaOrder pizzaOrder1 = new PizzaOrder(); pizzaOrder1.setPizzaType(PizzaType.CARDINALE); @@ -208,7 +226,7 @@ public class PizzeriaFrame extends JFrame { pizzaOrder1.setCookId(3); pizzas.add(pizzaOrder1); order1.setPizzaOrders(pizzas); - + orders.add(order1); orders.add(order2); orders.add(order3); -- 2.43.0