📄 coordinationpanel.java
字号:
/*
* The contents of this file are subject to the BT "ZEUS" Open Source
* Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
* except in compliance with the Licence. You may obtain a copy of the Licence
* from $ZEUS_INSTALL/licence.html or alternatively from
* http://www.labs.bt.com/projects/agents/zeus/licence.htm
*
* Except as stated in Clause 7 of the Licence, software distributed under the
* Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the Licence for the specific language governing rights and
* limitations under the Licence.
*
* The Original Code is within the package zeus.*.
* The Initial Developer of the Original Code is British Telecommunications
* public limited company, whose registered office is at 81 Newgate Street,
* London, EC1A 7AJ, England. Portions created by British Telecommunications
* public limited company are Copyright 1996-9. All Rights Reserved.
*
* THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
*/
/****************************************************************************
* CoordinationPanel.java
*
* Panel through which coordination protocol are entered
* (In future it will also allow them to be defined)
***************************************************************************/
package zeus.generator.agent;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import zeus.generator.*;
import zeus.generator.util.*;
import zeus.ontology.*;
import zeus.util.*;
import zeus.concepts.*;
import zeus.gui.help.*;
import zeus.gui.*;
/**
CoordinationPanel is the thing that you interact with in the
zeus.generator.AgentBuilder to arrange co-ordination methods for agents
*/
public class CoordinationPanel extends JPanel {
static final String[] ERROR_MESSAGE = {
/* 0 */ "Please select a row before\ncalling this operation"
};
protected ControlPanel controlPane;
protected AgentDescription agent;
protected ProtocolModel protocolModel;
protected JTable protocolTable;
protected StrategyToolBar strategyToolBar;
protected AttributeModel attributeModel;
protected JTable strategyTable;
protected StrategyModel strategyModel;
protected Fact[] clipboard = null;
protected OntologyDb ontologyDb;
protected GeneratorModel genmodel;
protected AgentEditor editor;
public CoordinationPanel(AgentGenerator generator,
GeneratorModel genmodel,
OntologyDb ontologyDb,
AgentEditor editor,
AgentDescription agent) {
this.agent = agent;
this.genmodel = genmodel;
this.ontologyDb = ontologyDb;
this.editor = editor;
GridBagLayout gridBagLayout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gridBagLayout);
setBackground(Color.lightGray);
setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
controlPane = new ControlPanel(editor,"Agent Coordination Panel",false,false);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(8,8,8,8);
gridBagLayout.setConstraints(controlPane,gbc);
add(controlPane);
// Add the panel containing the agent's protocol to the panel.
JPanel protocolPanel = new JPanel();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(8,8,8,8);
gbc.weightx = gbc.weighty = 1;
gridBagLayout.setConstraints(protocolPanel,gbc);
add(protocolPanel);
// create Strategies info
JPanel strategyPanel = new JPanel();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = gbc.weighty = 1;
gbc.insets = new Insets(8,8,8,8);
gridBagLayout.setConstraints(strategyPanel,gbc);
add(strategyPanel);
// Create Protocol info
AttributeModel attributeModel = new AttributeModel();
AttributeTable attributePanel = new AttributeTable(attributeModel);
strategyModel = new StrategyModel(genmodel,ontologyDb,attributeModel);
protocolModel = new ProtocolModel(ontologyDb,strategyModel,agent.getProtocols());
protocolModel.addChangeListener(editor);
attributeModel.addChangeListener(editor);
strategyModel.addChangeListener(editor);
TableColumnModel tm = new DefaultTableColumnModel();
TableColumn column;
column = new TableColumn(ProtocolModel.TYPE,24);
column.setHeaderValue(protocolModel.getColumnName(ProtocolModel.TYPE));
tm.addColumn(column);
column = new TableColumn(ProtocolModel.PROTOCOL,24,
new FriendlyRenderer(), new DefaultCellEditor(new JTextField()));
column.setHeaderValue(protocolModel.getColumnName(ProtocolModel.PROTOCOL));
tm.addColumn(column);
JCheckBox checkbox = new JCheckBox();
checkbox.setHorizontalAlignment(JCheckBox.CENTER);
DefaultCellEditor cellEditor = new DefaultCellEditor(checkbox);
checkbox.addItemListener(new SymItemAction());
column = new TableColumn(ProtocolModel.STATE,8,
new CheckBoxCellRenderer(), cellEditor);
column.setHeaderValue(protocolModel.getColumnName(ProtocolModel.STATE));
tm.addColumn(column);
protocolTable = new JTable(protocolModel,tm);
protocolTable.getTableHeader().setReorderingAllowed(false);
protocolTable.setColumnSelectionAllowed(false);
ListSelectionModel selectionModel = protocolTable.getSelectionModel();
selectionModel.addListSelectionListener(new SymListAction1());
TitledBorder border = BorderFactory.createTitledBorder("Coordination Protocols");
border.setTitlePosition(TitledBorder.TOP);
border.setTitleJustification(TitledBorder.RIGHT);
border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
border.setTitleColor(Color.blue);
protocolPanel.setBorder(border);
gridBagLayout = new GridBagLayout();
protocolPanel.setLayout(gridBagLayout);
protocolPanel.setBackground(Color.lightGray);
JToolBar toolbar = new ProtocolToolBar();
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(0,8,0,0);
gridBagLayout.setConstraints(toolbar,gbc);
protocolPanel.add(toolbar);
JScrollPane scrollPane = new JScrollPane(protocolTable);
scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scrollPane.setMinimumSize(new Dimension(160,80));
scrollPane.setPreferredSize(new Dimension(200,80));
protocolTable.setBackground(Color.white);
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = gbc.weighty = 1;
gbc.insets = new Insets(8,8,8,8);
gridBagLayout.setConstraints(scrollPane,gbc);
protocolPanel.add(scrollPane);
// Create strategy panel info
gridBagLayout = new GridBagLayout();
strategyPanel.setLayout(gridBagLayout);
strategyPanel.setBackground(Color.lightGray);
tm = new DefaultTableColumnModel();
JCheckBox checkbox1 = new JCheckBox();
checkbox1.setHorizontalAlignment(JCheckBox.CENTER);
column = new TableColumn(StrategyModel.MODE,4,
new CheckBoxCellRenderer(), new DefaultCellEditor(checkbox1));
column.setHeaderValue(strategyModel.getColumnName(StrategyModel.MODE));
tm.addColumn(column);
column = new TableColumn(StrategyModel.TYPE,12);
column.setHeaderValue(strategyModel.getColumnName(StrategyModel.TYPE));
tm.addColumn(column);
column = new TableColumn(StrategyModel.AGENTS,24,
new StringArrayCellRenderer(), new AgentCellEditor());
column.setHeaderValue(strategyModel.getColumnName(StrategyModel.AGENTS));
tm.addColumn(column);
column = new TableColumn(StrategyModel.RELATIONS,24,
new StringArrayCellRenderer(), new RelationsCellEditor());
column.setHeaderValue(strategyModel.getColumnName(StrategyModel.RELATIONS));
tm.addColumn(column);
column = new TableColumn(StrategyModel.STRATEGY,24,
new FriendlyRenderer(), new StrategyCellEditor());
column.setHeaderValue(strategyModel.getColumnName(StrategyModel.STRATEGY));
tm.addColumn(column);
column = new TableColumn(StrategyModel.PARAMETERS,24,
new HashtableCellRenderer(), new HashtableCellEditor());
column.setHeaderValue(strategyModel.getColumnName(StrategyModel.PARAMETERS));
tm.addColumn(column);
strategyTable = new JTable(strategyModel,tm);
strategyTable.getTableHeader().setReorderingAllowed(false);
strategyTable.setColumnSelectionAllowed(false);
selectionModel = strategyTable.getSelectionModel();
selectionModel.addListSelectionListener(new SymListAction2());
scrollPane = new JScrollPane(strategyTable);
scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scrollPane.setPreferredSize(new Dimension(400,100));
strategyTable.setBackground(Color.white);
border = (BorderFactory.createTitledBorder("Coordination Strategies"));
border.setTitlePosition(TitledBorder.TOP);
border.setTitleJustification(TitledBorder.RIGHT);
border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
border.setTitleColor(Color.blue);
strategyPanel.setBorder(border);
gbc = new GridBagConstraints();
strategyToolBar = new StrategyToolBar();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = gbc.weighty = 0;
gbc.insets = new Insets(0,8,0,0);
gridBagLayout.setConstraints(strategyToolBar,gbc);
strategyPanel.add(strategyToolBar);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = gbc.weighty = 1;
gbc.insets = new Insets(8,8,8,8);
gridBagLayout.setConstraints(scrollPane,gbc);
strategyPanel.add(scrollPane);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = gbc.weighty = 1;
gbc.insets = new Insets(8,8,8,8);
gridBagLayout.setConstraints(attributePanel,gbc);
strategyPanel.add(attributePanel);
strategyToolBar.setEnabled(false);
}
class SymItemAction implements ItemListener {
public void itemStateChanged(ItemEvent e) {
strategyToolBar.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
repaint();
}
}
class StrategyCellEditor extends DefaultCellEditor {
public StrategyCellEditor() {
super( new JComboBox() {
public void contentsChanged(ListDataEvent e) {
selectedItemReminder = null;
super.contentsChanged(e);
}
}
);
JComboBox combo = (JComboBox)editorComponent;
DefaultListCellRenderer renderer = new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean hasFocus) {
if ( value != null )
value = SystemProps.getProperty("friendly.name." + value,
(String)value);
return super.getListCellRendererComponent(
list, value, index, isSelected, hasFocus);
}
};
combo.setRenderer(renderer);
setClickCountToStart(2);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row, int column) {
Vector List = null;
JComboBox combo = (JComboBox)editorComponent;
if ( combo.getModel().getSize() != 0 )
combo.removeAllItems();
String type = (String)protocolModel.getValueAt(
protocolTable.getSelectedRow(),ProtocolModel.TYPE);
if ( type.equals(ProtocolInfo.INITIATOR) )
List = StrategyModel.INITIATOR_STRATEGY_LIST;
else
List = StrategyModel.RESPONDENT_STRATEGY_LIST;
for(int i = 0; i < List.size(); i++ )
combo.addItem(List.elementAt(i));
return super.getTableCellEditorComponent(table, value, isSelected,
row, column);
}
}
class AgentCellEditor extends DefaultCellEditor
implements ActionListener {
protected JButton button = new JButton("");
protected int row, column;
protected EditableMultipleSelectionDialog dialog;
protected Object value;
public AgentCellEditor() {
super(new JTextField());
setClickCountToStart(2);
dialog = new EditableMultipleSelectionDialog(
(Frame)SwingUtilities.getRoot(editor),"Select Agents");
button.setBackground(Color.white);
button.setHorizontalAlignment(JButton.LEFT);
button.setBorderPainted(false);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if ( src == button ) {
String[] data = genmodel.getAgentNames();
Vector items = Misc.stringVector(data);
items.removeElement(editor.getObjectName());
data = Misc.stringArray(items);
dialog.setListData(data);
dialog.setSelection((String[])value);
dialog.setLocationRelativeTo(button);
fireEditingCanceled();
Object[] output = dialog.getSelection();
strategyModel.setValueAt(Misc.stringArray(output),row,column);
}
repaint();
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row, int column) {
this.row = row;
this.column = column;
this.value = value;
return button;
}
}
class RelationsCellEditor extends DefaultCellEditor
implements ActionListener {
protected JButton button = new JButton("");
protected int row, column;
protected MultipleSelectionDialog dialog;
protected Object value;
public RelationsCellEditor() {
super(new JTextField());
setClickCountToStart(2);
dialog = new MultipleSelectionDialog(
(Frame)SwingUtilities.getRoot(editor), "Select Relations");
dialog.setListData(Misc.stringArray(AcquaintanceModel.RELATIONS_LIST));
button.setBackground(Color.white);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -