/** Refresh the details panel. */ public void refreshDetails() { int sel = table.getSelectedRow(); if (sel != -1) { final Order order = (Order) orders.get(sel); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; IExpression exp = bia.getExpressionbase().getExpression("search_reports"); final List res = (List) exp.execute("$order", order); SwingUtilities.invokeLater( new Runnable() { public void run() { while (detailsdm.getRowCount() > 0) detailsdm.removeRow(0); for (int i = 0; i < res.size(); i++) { detailsdm.addRow(new Object[] {res.get(i)}); // System.out.println(""+i+res.get(i)); } } }); return null; } }); // agent.getExpressionbase().getExpression("search_reports").addResultListener(new // DefaultResultListener() // { // public void resultAvailable(Object source, final Object result) // { // IEAExpression exp = (IEAExpression)result; // // exp.execute("$order", order).addResultListener(new // SwingDefaultResultListener(GuiPanel.this) // { // public void customResultAvailable(Object source, Object result) // { // List res = (List)result; //// for(int i=0; i<res.size(); i++) //// System.out.println(""+i+res.get(i)); // // while(detailsdm.getRowCount()>0) // detailsdm.removeRow(0); // for(int i=0; i<res.size(); i++) // { // detailsdm.addRow(new Object[]{res.get(i)}); // //System.out.println(""+i+res.get(i)); // } // } // }); // } // }); } }
/** Method to be called when goals may have changed. */ public void refresh() { agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; final Object[] aorders = bia.getBeliefbase().getBeliefSet("orders").getFacts(); SwingUtilities.invokeLater( new Runnable() { public void run() { for (int i = 0; i < aorders.length; i++) { if (!orders.contains(aorders[i])) { orders.add(aorders[i]); } } items.fireTableDataChanged(); } }); return null; } }); // agent.getBeliefbase().getBeliefSetFacts("orders") // .addResultListener(new SwingDefaultResultListener(GuiPanel.this) // { // public void customResultAvailable(Object source, final Object result) // { // Order[] aorders = (Order[])result; //// System.out.println("refresh: "+SUtil.arrayToString(aorders)+" "+GuiPanel.this); // for(int i = 0; i < aorders.length; i++) // { // if(!orders.contains(aorders[i])) // { // orders.add(aorders[i]); // } // } // items.fireTableDataChanged(); // } // }); }
/** Create a new gui. */ public CustomerPanel(final IBDIExternalAccess agent) { this.agent = agent; this.shops = new HashMap(); final JComboBox shopscombo = new JComboBox(); shopscombo.addItem("none"); shopscombo.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (shops.get(shopscombo.getSelectedItem()) instanceof IShop) { refresh((IShop) shops.get(shopscombo.getSelectedItem())); } } }); remote = new JCheckBox("Remote"); remote.setToolTipText("Also search remote platforms for shops."); final JButton searchbut = new JButton("Search"); searchbut.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { searchbut.setEnabled(false); SServiceProvider.getServices( agent.getServiceProvider(), IShop.class, remote.isSelected(), true) .addResultListener( new SwingDefaultResultListener(CustomerPanel.this) { public void customResultAvailable(Object source, Object result) { searchbut.setEnabled(true); // System.out.println("Customer search result: "+result); Collection coll = (Collection) result; ((DefaultComboBoxModel) shopscombo.getModel()).removeAllElements(); shops.clear(); if (coll != null && coll.size() > 0) { for (Iterator it = coll.iterator(); it.hasNext(); ) { IShop shop = (IShop) it.next(); shops.put(shop.getName(), shop); ((DefaultComboBoxModel) shopscombo.getModel()) .addElement(shop.getName()); } } else { ((DefaultComboBoxModel) shopscombo.getModel()).addElement("none"); } } public void customExceptionOccurred(Object source, Exception exception) { searchbut.setEnabled(true); super.customExceptionOccurred(source, exception); } }); } }); final NumberFormat df = NumberFormat.getInstance(); df.setMaximumFractionDigits(2); df.setMinimumFractionDigits(2); final JTextField money = new JTextField(5); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; final Object mon = bia.getBeliefbase().getBelief("money").getFact(); SwingUtilities.invokeLater( new Runnable() { public void run() { money.setText(df.format(mon)); } }); return null; } }); money.setEditable(false); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; bia.getBeliefbase() .getBelief("money") .addBeliefListener( new IBeliefListener() { public void beliefChanged(final AgentEvent ae) { SwingUtilities.invokeLater( new Runnable() { public void run() { money.setText(df.format(ae.getValue())); } }); } }); return null; } }); JPanel selpanel = new JPanel(new GridBagLayout()); selpanel.setBorder(new TitledBorder(new EtchedBorder(), "Properties")); int x = 0; int y = 0; selpanel.add( new JLabel("Money: "), new GridBagConstraints( x, y, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); x++; selpanel.add( money, new GridBagConstraints( x, y, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); x++; selpanel.add( new JLabel("Available shops: "), new GridBagConstraints( x, y, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); x++; selpanel.add( shopscombo, new GridBagConstraints( x, y, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0)); x++; selpanel.add( searchbut, new GridBagConstraints( x, y, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); x++; selpanel.add( remote, new GridBagConstraints( x, y, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); JPanel shoppanel = new JPanel(new BorderLayout()); shoppanel.setBorder(new TitledBorder(new EtchedBorder(), "Shop Catalog")); shoptable = new JTable(shopmodel); shoptable.setPreferredScrollableViewportSize(new Dimension(600, 120)); shoptable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); shoppanel.add(BorderLayout.CENTER, new JScrollPane(shoptable)); JPanel invpanel = new JPanel(new BorderLayout()); invpanel.setBorder(new TitledBorder(new EtchedBorder(), "Customer Inventory")); invtable = new JTable(invmodel); invtable.setPreferredScrollableViewportSize(new Dimension(600, 120)); invpanel.add(BorderLayout.CENTER, new JScrollPane(invtable)); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; bia.getBeliefbase() .getBeliefSet("inventory") .addBeliefSetListener( new IBeliefSetListener() { public void factRemoved(final AgentEvent ae) { SwingUtilities.invokeLater( new Runnable() { public void run() { invlist.remove(ae.getValue()); invmodel.fireTableDataChanged(); } }); } public void factChanged(final AgentEvent ae) { SwingUtilities.invokeLater( new Runnable() { public void run() { invlist.remove(ae.getValue()); invlist.add(ae.getValue()); invmodel.fireTableDataChanged(); } }); } public void factAdded(final AgentEvent ae) { SwingUtilities.invokeLater( new Runnable() { public void run() { invlist.add(ae.getValue()); invmodel.fireTableDataChanged(); } }); } }); return null; } }); JPanel butpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // butpanel.setBorder(new TitledBorder(new EtchedBorder(), "Actions")); JButton buy = new JButton("Buy"); final JTextField item = new JTextField(8); item.setEditable(false); butpanel.add(new JLabel("Selected item:")); butpanel.add(item); butpanel.add(buy); buy.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int sel = shoptable.getSelectedRow(); if (sel != -1) { final String name = (String) shopmodel.getValueAt(sel, 0); final Double price = (Double) shopmodel.getValueAt(sel, 1); final IShop shop = (IShop) shops.get(shopscombo.getSelectedItem()); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; final IGoal buy = bia.getGoalbase().createGoal("buy"); buy.getParameter("name").setValue(name); buy.getParameter("shop").setValue(shop); buy.getParameter("price").setValue(price); buy.addGoalListener( new IGoalListener() { public void goalFinished(AgentEvent ae) { // Update number of available items refresh(shop); if (!buy.isSucceeded()) { final String text = SUtil.wrapText( "Item could not be bought. " + buy.getException().getMessage()); SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog( SGUI.getWindowParent(CustomerPanel.this), text, "Buy problem", JOptionPane.INFORMATION_MESSAGE); } }); } } public void goalAdded(AgentEvent ae) {} }); bia.getGoalbase().dispatchTopLevelGoal(buy); return null; } }); } } }); shoptable .getSelectionModel() .addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int sel = shoptable.getSelectedRow(); if (sel != -1) { item.setText("" + shopmodel.getValueAt(sel, 0)); } } }); setLayout(new GridBagLayout()); x = 0; y = 0; add( selpanel, new GridBagConstraints( x, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); add( shoppanel, new GridBagConstraints( x, y++, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); add( invpanel, new GridBagConstraints( x, y++, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); add( butpanel, new GridBagConstraints( x, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); // refresh(); }
/** Shows the gui, and updates it when beliefs change. */ public GuiPanel(final IBDIExternalAccess agent) // , final boolean buy) { setLayout(new BorderLayout()); this.agent = agent; final boolean buy = isBuyer(agent); if (buy) { itemlabel = " Books to buy "; goalname = "purchase_book"; addorderlabel = "Add new purchase order"; } else { itemlabel = " Books to sell "; goalname = "sell_book"; addorderlabel = "Add new sell order"; } JPanel itempanel = new JPanel(new BorderLayout()); itempanel.setBorder(new TitledBorder(new EtchedBorder(), itemlabel)); table = new JTable(items); table.setDefaultRenderer( Object.class, new DefaultTableCellRenderer() { public Component getTableCellRendererComponent( JTable table, Object value, boolean selected, boolean focus, int row, int column) { Component comp = super.getTableCellRendererComponent(table, value, selected, focus, row, column); setOpaque(true); if (column == 0) { setHorizontalAlignment(LEFT); } else { setHorizontalAlignment(RIGHT); } if (!selected) { Object state = items.getValueAt(row, 6); if (Order.DONE.equals(state)) { comp.setBackground(new Color(211, 255, 156)); } else if (Order.FAILED.equals(state)) { comp.setBackground(new Color(255, 211, 156)); } else { comp.setBackground(table.getBackground()); } } if (value instanceof Date) { setValue(dformat.format(value)); } return comp; } }); table.setPreferredScrollableViewportSize(new Dimension(600, 120)); JScrollPane scroll = new JScrollPane(table); itempanel.add(BorderLayout.CENTER, scroll); detailsdm = new DefaultTableModel(new String[] {"Negotiation Details"}, 0); JTable details = new JTable(detailsdm); details.setPreferredScrollableViewportSize(new Dimension(600, 120)); JPanel dep = new JPanel(new BorderLayout()); dep.add(BorderLayout.CENTER, new JScrollPane(details)); JPanel south = new JPanel(); // south.setBorder(new TitledBorder(new EtchedBorder(), " Control ")); JButton add = new JButton("Add"); final JButton remove = new JButton("Remove"); final JButton edit = new JButton("Edit"); add.setMinimumSize(remove.getMinimumSize()); add.setPreferredSize(remove.getPreferredSize()); edit.setMinimumSize(remove.getMinimumSize()); edit.setPreferredSize(remove.getPreferredSize()); south.add(add); south.add(remove); south.add(edit); remove.setEnabled(false); edit.setEnabled(false); JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitter.add(itempanel); splitter.add(dep); splitter.setOneTouchExpandable(true); // splitter.setDividerLocation(); add(BorderLayout.CENTER, splitter); add(BorderLayout.SOUTH, south); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; bia.getBeliefbase() .getBeliefSet("orders") .addBeliefSetListener( new IBeliefSetListener() { public void factChanged(AgentEvent ae) { // System.out.println("Changed: "+ae.getValue()); refresh(); } public void factAdded(AgentEvent ae) { // System.out.println("Added: "+ae.getValue()); refresh(); } public void factRemoved(AgentEvent ae) { // System.out.println("Removed: "+ae.getValue()); refresh(); } }); return null; } }); // agent.getBeliefbase().addBeliefSetListener("orders", new IBeliefSetListener() // { // public void factChanged(AgentEvent ae) // { //// System.out.println("Changed: "+ae.getValue()); // refresh(); // } // // public void factAdded(AgentEvent ae) // { //// System.out.println("Added: "+ae.getValue()); // refresh(); // } // // public void factRemoved(AgentEvent ae) // { //// System.out.println("Removed: "+ae.getValue()); // refresh(); // } // }); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; bia.getBeliefbase() .getBeliefSet("negotiation_reports") .addBeliefSetListener( new IBeliefSetListener() { public void factAdded(AgentEvent ae) { // System.out.println("a fact was added"); refreshDetails(); } public void factRemoved(AgentEvent ae) { // System.out.println("a fact was removed"); refreshDetails(); } public void factChanged(AgentEvent ae) { // System.out.println("belset changed"); } }); return null; } }); // agent.getBeliefbase().addBeliefSetListener("negotiation_reports", new IBeliefSetListener() // { // public void factAdded(AgentEvent ae) // { //// System.out.println("a fact was added"); // refreshDetails(); // } // // public void factRemoved(AgentEvent ae) // { //// System.out.println("a fact was removed"); // refreshDetails(); // } // // public void factChanged(AgentEvent ae) // { // //System.out.println("belset changed"); // } // }); table.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { refreshDetails(); } }); final InputDialog dia = new InputDialog(buy); add.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { SServiceProvider.getService(agent.getServiceProvider(), IClockService.class) .addResultListener( new SwingDefaultResultListener(GuiPanel.this) { public void customResultAvailable(Object source, Object result) { IClockService cs = (IClockService) result; while (dia.requestInput(cs.getTime())) { try { String title = dia.title.getText(); int limit = Integer.parseInt(dia.limit.getText()); int start = Integer.parseInt(dia.start.getText()); Date deadline = dformat.parse(dia.deadline.getText()); final Order order = new Order(title, deadline, start, limit, buy, cs); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; IGoal purchase = bia.getGoalbase().createGoal(goalname); purchase.getParameter("order").setValue(order); bia.getGoalbase().dispatchTopLevelGoal(purchase); return null; } }); // agent.createGoal(goalname).addResultListener(new // DefaultResultListener() // { // public void resultAvailable(Object source, Object result) // { // IEAGoal purchase = (IEAGoal)result; // purchase.setParameterValue("order", order); // agent.dispatchTopLevelGoal(purchase); // } // }); orders.add(order); items.fireTableDataChanged(); break; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog( GuiPanel.this, "Price limit must be integer.", "Input error", JOptionPane.ERROR_MESSAGE); } catch (ParseException e1) { JOptionPane.showMessageDialog( GuiPanel.this, "Wrong date format, use YYYY/MM/DD hh:mm.", "Input error", JOptionPane.ERROR_MESSAGE); } } } }); } }); table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table .getSelectionModel() .addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { boolean selected = table.getSelectedRow() >= 0; remove.setEnabled(selected); edit.setEnabled(selected); } }); remove.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); if (row >= 0 && row < orders.size()) { final Order order = (Order) orders.remove(row); items.fireTableRowsDeleted(row, row); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; IGoal[] goals = bia.getGoalbase().getGoals(goalname); for (int i = 0; i < goals.length; i++) { Order or = (Order) goals[i].getParameter("order").getValue(); if (order.equals(or)) { or.setState(Order.FAILED); goals[i].drop(); break; } } return null; } }); // agent.getGoalbase().getGoals(goalname).addResultListener(new // DefaultResultListener() // { // public void resultAvailable(Object source, Object result) // { // final IEAGoal[] goals = (IEAGoal[])result; //// System.out.println("removing: "+SUtil.arrayToString(goals)); // for(int i=0; i<goals.length; i++) // { // final int fi = i; // goals[i].getParameterValue("order").addResultListener(new // DefaultResultListener() // { // public void resultAvailable(Object source, Object result) // { // Order or = (Order)result; // if(order.equals(or)) // { // or.setState(Order.FAILED); // dropGoal(new IEAGoal[]{goals[fi]}, 0, order); // } // } // }); // } // } // }); } } }); final InputDialog edit_dialog = new InputDialog(buy); edit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { SServiceProvider.getService(agent.getServiceProvider(), IClockService.class) .addResultListener( new SwingDefaultResultListener(GuiPanel.this) { public void customResultAvailable(Object source, Object result) { IClockService cs = (IClockService) result; int row = table.getSelectedRow(); if (row >= 0 && row < orders.size()) { final Order order = (Order) orders.get(row); edit_dialog.title.setText(order.getTitle()); edit_dialog.limit.setText(Integer.toString(order.getLimit())); edit_dialog.start.setText(Integer.toString(order.getStartPrice())); edit_dialog.deadline.setText(dformat.format(order.getDeadline())); while (edit_dialog.requestInput(cs.getTime())) { try { String title = edit_dialog.title.getText(); int limit = Integer.parseInt(edit_dialog.limit.getText()); int start = Integer.parseInt(edit_dialog.start.getText()); Date deadline = dformat.parse(edit_dialog.deadline.getText()); order.setTitle(title); order.setLimit(limit); order.setStartPrice(start); order.setDeadline(deadline); items.fireTableDataChanged(); agent.scheduleStep( new IComponentStep() { public Object execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess) ia; IGoal[] goals = bia.getGoalbase().getGoals(goalname); for (int i = 0; i < goals.length; i++) { if (goals[i] .getParameter("order") .getValue() .equals(order)) { goals[i].drop(); } } IGoal goal = bia.getGoalbase().createGoal(goalname); goal.getParameter("order").setValue(order); bia.getGoalbase().dispatchTopLevelGoal(goal); return null; } }); // // agent.getGoalbase().getGoals(goalname).addResultListener(new // DefaultResultListener() // { // public void resultAvailable(Object source, Object result) // { // IEAGoal[] goals = (IEAGoal[])result; // dropGoal(goals, 0, order); // } // }); // agent.createGoal(goalname).addResultListener(new // DefaultResultListener() // { // public void resultAvailable(Object source, Object result) // { // IEAGoal goal = (IEAGoal)result; // goal.setParameterValue("order", order); // agent.dispatchTopLevelGoal(goal); // } // }); break; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog( GuiPanel.this, "Price limit must be integer.", "Input error", JOptionPane.ERROR_MESSAGE); } catch (ParseException e1) { JOptionPane.showMessageDialog( GuiPanel.this, "Wrong date format, use YYYY/MM/DD hh:mm.", "Input error", JOptionPane.ERROR_MESSAGE); } } } } }); } }); refresh(); }