/******************************************************************************* * * TableauControle: GUI pour le projet Livraison (ift6802-H04) * ---------------- * Gui for the BankAgentGui. It shows how to build an * Adapte de BankAgentGui de A Ncho. * * Version 0.1 - mars 2004. * Jean Vaucher. * Departement d'Informatique et de Recherche Operationnelle (DIRO). * Universite de Montreal. * ********************************************************************************/ import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import javax.swing.border.*; import jade.core.*; import jade.gui.*; class TableauControle extends JFrame implements ActionListener, Constants6802 { // ---------------------------------------------------------------------------- Object[] vitesses = {"0","1","2","3","4"}; String columns[] = {"#","ID","Equipe","$","CFP","Cumul", "Points"}; final static int IN_PROCESS = 0; final static int WAIT_CONFIRM = 1; final static int IN_LINE = 2; final static int DEPOSIT = 2; final static int WITHDRAWAL = 2; private int status = IN_PROCESS; private JTextField msg, input, acInfo; private JComboBox vitesse; JCheckBox test; private JList acList; private JTable playerTable; private JButton start, quit; private JTextField jourFld, heureFld; MyModel dataModel ; private ArrayList data ; private Controleur myAgent; public TableauControle( Controleur a, ArrayList data) { // ---------------------------------------------------------- Constructor myAgent = a; // Reference to class BankClientAgent this.data = data; setTitle("Projet 6802 - " + myAgent.VERSION); JPanel base = new JPanel(); base.setBorder(new EmptyBorder(15,15,15,15)); base.setLayout(new BorderLayout(10,10)); getContentPane().add(base); JPanel panel = new JPanel(); base.add(panel, BorderLayout.WEST); panel.setLayout(new BorderLayout(0,16)); JPanel pane = new JPanel(); pane.setLayout(new BorderLayout(0,0)); pane.add(new JLabel("Message"), BorderLayout.NORTH); pane.add(msg = new JTextField("Attendre que les agents demarrent", 15)); msg.setEditable(false); msg.setBackground(Color.black); msg.setForeground(Color.white); msg.setFont(new Font("Arial", Font.BOLD, 12)); msg.setHorizontalAlignment(JTextField.CENTER); panel.add(pane, BorderLayout.NORTH); pane = new JPanel(); pane.setLayout(new BorderLayout(5,0)); JPanel p ; p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("Vitesse")); p.add(vitesse = new JComboBox(vitesses)); vitesse.setSelectedIndex(1); vitesse.addActionListener(this); pane.add(p); // pane.add(p, BorderLayout.EAST); panel.add(pane, BorderLayout.CENTER); pane = new JPanel(); pane.setLayout(new BorderLayout(0,0)); JPanel p0 = new JPanel(); p0.setLayout(new BorderLayout(0,3)); p = new JPanel(); p.setLayout(new BorderLayout(0,0)); p.add(new JLabel("Agents"), BorderLayout.WEST); p0.add(p, BorderLayout.NORTH); dataModel = new MyModel( data, columns ); playerTable = new JTable( dataModel ); playerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); playerTable.setPreferredScrollableViewportSize(new Dimension(300,150)); playerTable.setFont(new Font("Arial", Font.PLAIN, 11)); TableColumn col0 = playerTable.getColumnModel().getColumn(0); TableColumn col1 = playerTable.getColumnModel().getColumn(1); TableColumn col2 = playerTable.getColumnModel().getColumn(2); col0.setPreferredWidth(20); col2.setPreferredWidth(150); col0.setCellRenderer( new RenderLabel()); // p0.add(new JScrollPane(playerTable), BorderLayout.SOUTH); p0.add(new JScrollPane(playerTable), BorderLayout.CENTER); pane.add(p0, BorderLayout.SOUTH); panel.add(pane, BorderLayout.SOUTH); panel = new JPanel(); base.add(panel, BorderLayout.EAST); panel.setLayout(new BorderLayout(0,10)); pane = new JPanel(new GridLayout(5, 1)); pane.add(new JLabel("Jour")); pane.add(jourFld = new JTextField("1",3)); jourFld.setHorizontalAlignment(JTextField.CENTER); jourFld.setEditable(false); pane.add(new JLabel("Heure")); pane.add(heureFld = new JTextField("0",3)); heureFld.setHorizontalAlignment(JTextField.CENTER); heureFld.setEditable(false); test = new JCheckBox("Test") ; pane .add ( test ); panel.add(pane, BorderLayout.NORTH); // Boutons pane = new JPanel(); panel.add(pane, BorderLayout.SOUTH); pane.setBorder(new EmptyBorder(0,0,50,0)); pane.setLayout(new GridLayout(3,1,0,5)); pane.add(start = new JButton("START")); start.setToolTipText("Start Competition"); start.addActionListener(this); pane.add(quit = new JButton("QUIT")); quit.setToolTipText("Stop agent and exit"); quit.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { shutDown(); } }); setSize(440, 340); setResizable(false); Rectangle r = getGraphicsConfiguration().getBounds(); setLocation(r.x + 2*(r.width - getWidth())/3, r.y + (r.height - getHeight())/2); } public void actionPerformed(ActionEvent ae) { // --------------------------------------------- if (ae.getSource() == quit) { shutDown(); } else if (ae.getSource() == start) { GuiEvent ge = new GuiEvent(this, START); myAgent.postGuiEvent(ge); } else if (ae.getSource() == vitesse ) { GuiEvent ge = new GuiEvent(this, VITESSE); ge.addParameter(vitesse.getSelectedItem()); myAgent.postGuiEvent(ge); } } void shutDown() { // ----------------- Control the closing of this gui // int rep = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", // myAgent.getLocalName(), // JOptionPane.YES_NO_CANCEL_OPTION); // if (rep == JOptionPane.YES_OPTION) { // GuiEvent ge = new GuiEvent(this, QUIT); // myAgent.postGuiEvent(ge); // } myAgent.postGuiEvent(new GuiEvent(this, QUIT)); } void alertInfo(String s) { // -------------------------- Toolkit.getDefaultToolkit().beep(); msg.setText(s); } public void alertResponse( String s) { // ------------------------------------- msg.setText(s); } void upDate( int j, int h) { jourFld. setText(""+j); heureFld.setText(""+h); } public void resetStatus() { // --------------------------- status = IN_PROCESS; } public String getVitesse() { // --------------------------- return( (String) vitesse.getSelectedItem()); } } // ================== Modele pour la table des agents =======================// class MyModel extends AbstractTableModel { // ------------------------------------------------- private String[] columns; private ArrayList data ; public MyModel(ArrayList data, String[] columns) { // ---------------------------------------------------------- Constructor this.columns = columns; this.data = data; } public int getColumnCount() { // ----------------------------- Return the number of columns in the table return columns.length; } public int getRowCount() { // -------------------------- Return the number of rows in the table return data.size(); } public String getColumnName(int col) { // -------------------------------------- Return the name of a column return columns[col]; } public Object getValueAt( int row, int col) { // -------------------------------------------- Return the value at a specific // row and column if ( data.isEmpty() ) return null; ModelData j = (ModelData) data.get( row ); return j.get( col ); } public Class getColumnClass(int col) { // -------------------------------------- Return the class of the values held // by a column // Object o = getValueAt(0, col); // if (o == null) return columns[col].getClass(); return getValueAt(0, col).getClass(); } public void setValueAt(Object value, int row, int col){ // ------------------------------------------------------ Set the value at a specific // row and column ModelData j = (ModelData) data.get( row ); j.put( col, value); fireTableCellUpdated(row,col); } public void setData(ArrayList data){ // ------------------------------------ Update the entire data in the table this.data = data; fireTableDataChanged(); } ArrayList getData(){ // --------------------- Return the entire data of the table return data; } void changed( int row) { fireTableRowsUpdated(row, row); } }// end TableDataModel // --------------------------------------------------------------------------- class RenderLabel extends JLabel implements TableCellRenderer, Constants6802 // --------------------------------------------------------------------------- { public RenderLabel(){ setOpaque(true); } public Component getTableCellRendererComponent (JTable table, Object o, boolean isSelected, boolean hasFocus, int row, int col) { int ci = ((Integer) o).intValue(); Color c = (ci==0 ? Color.gray : color [ (ci-1)%color.length ] ) ; setForeground( Color.black ); setBackground( c ); setHorizontalAlignment( SwingConstants.CENTER ); setText( o.toString() ); return this; } }