View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * AgentTableUI.java
26  *
27  * The Viewer/Controller for displaying and editing the list of Known Agents
28  *****************************************************************************/
29  
30  package zeus.generator;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import java.util.*;
35  import javax.swing.*;
36  import javax.swing.table.*;
37  import javax.swing.border.*;
38  import javax.swing.event.*;
39  
40  import zeus.gui.*;
41  import zeus.gui.fields.*;
42  import zeus.util.*;
43  
44  public class AgentTableUI extends JPanel {
45    protected JTable          table;
46    protected AgentTableModel model;
47    protected AgentGenerator  generator;
48  
49    static final String[] ERROR_MESSAGE = {
50       /* 0 */ "Please select a row before\ncalling this operation"
51    };
52  
53    public AgentTableUI(AgentGenerator generator, GeneratorModel genmodel) {
54      this.generator = generator;
55  
56      model = new AgentTableModel(genmodel);
57  
58      TableColumnModel tm = new DefaultTableColumnModel();
59      TableColumn column;
60      column = new TableColumn(AgentTableModel.AGENT,12,
61         new DefaultTableCellRenderer(),new DefaultCellEditor(new NameField()));
62      column.setHeaderValue(model.getColumnName(AgentTableModel.AGENT));
63      tm.addColumn(column);
64      column = new TableColumn(AgentTableModel.TASK,24,
65         new AgentTableCellRenderer(),
66         new AgentTableCellEditor(genmodel));
67      column.setHeaderValue(model.getColumnName(AgentTableModel.TASK));
68      tm.addColumn(column);
69  
70      table = new JTable(model,tm);
71      setPreferredSize(new Dimension(240,120));
72  
73      table.getTableHeader().setReorderingAllowed(false);
74      table.setColumnSelectionAllowed(false);
75      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
76  
77      JScrollPane scrollPane = new JScrollPane(table);
78      scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
79      table.setBackground(Color.white);
80  
81      // Add the scroll pane to this panel.
82      setLayout(new BorderLayout());
83      add(scrollPane,BorderLayout.CENTER);
84  
85      MouseListener ml = new MouseAdapter() {
86         public void mouseClicked(MouseEvent e) {
87            int row = table.rowAtPoint(e.getPoint());
88            if ( row != -1 && table.isRowSelected(row) ) {
89               if ( e.getClickCount() == 2 )
90                  editAgent();
91            }
92         }
93      };
94      table.addMouseListener(ml);
95    }
96  
97    void errorMsg(int tag) {
98       JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
99                                     "Error", JOptionPane.ERROR_MESSAGE);
100   }
101 
102   protected String getSelectedAgentName() {
103     int row = table.getSelectedRow();
104     if ( row == -1 ) {
105        errorMsg(0);
106        return null;
107     }
108     return (String)model.getValueAt(row,AgentTableModel.AGENT);
109   }
110 
111   protected String getSelectedAgentId() {
112     int row = table.getSelectedRow();
113     if ( row == -1 ) {
114        errorMsg(0);
115        return null;
116     }
117     return (String)model.getValueAt(row,AgentTableModel.ID);
118   }
119 
120   public void addNewAgent() {
121     model.addNewRow();
122   }
123 
124   public void editAgent() {
125     String id = getSelectedAgentId();
126     if ( id == null ) return;
127     generator.editAgent(id);
128   }
129 
130   public void removeAgent() {
131     String id = getSelectedAgentId();
132     if ( id == null ) return;
133     generator.removeAgent(id);
134   }
135 
136   public void cloneAgent() {
137     String id = getSelectedAgentId();
138     if ( id == null ) return;
139     generator.cloneAgent(id);
140   }
141 
142   public void renameAgent() {
143     int row = table.getSelectedRow();
144     if ( row == -1 ) {
145        errorMsg(0);
146        return;
147     }
148     model.setEditable(true);
149     table.editCellAt(row,AgentTableModel.AGENT);
150     model.setEditable(false);
151   }
152 
153   public void modifyTaskList() {
154     int row = table.getSelectedRow();
155     if ( row == -1 ) {
156        errorMsg(0);
157        return;
158     }
159     model.setEditable(true);
160     table.editCellAt(row,AgentTableModel.TASK);
161     model.setEditable(false);
162   }
163 
164   class AgentTableCellEditor extends DefaultCellEditor
165                              implements ActionListener {
166 
167     protected JButton button = new JButton("");
168     protected GeneratorModel genmodel;
169     protected int row, column;
170     protected MultipleSelectionDialog dialog;
171 
172     public AgentTableCellEditor(GeneratorModel genmodel) {
173       super(new JTextField());
174       this.genmodel = genmodel;
175 
176       setClickCountToStart(1);
177 
178       dialog = new MultipleSelectionDialog(
179          (Frame)SwingUtilities.getRoot(table),"Select Tasks");
180 
181       button.setBackground(Color.white);
182       button.setBorderPainted(false);
183       button.addActionListener(this);
184     }
185 
186     public void actionPerformed(ActionEvent e) {
187       Object src = e.getSource();
188       if ( src == button ) {
189          dialog.setLocationRelativeTo(button);
190          fireEditingCanceled();
191          Object[] items = dialog.getSelection();
192          model.setValueAt(Misc.stringArray(items),row,column);
193       }
194     }
195     public Component getTableCellEditorComponent(JTable table, Object value,
196                                                  boolean isSelected,
197                                                  int row, int column) {
198 
199       this.row = row;
200       this.column = column;
201       String[] data = genmodel.getTaskNames();
202       dialog.setListData(data);
203       dialog.setSelection((String[])value);
204       button.setText("Click to edit");
205       return button;
206     }
207   }
208 
209   class AgentTableCellRenderer extends DefaultTableCellRenderer {
210      public void setValue(Object value) {
211         String[] items = (String[])value;
212         String text = Misc.concat(items);
213         super.setValue(text);
214      }
215   }
216 }