📄 newoperatordialog.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.gui;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.tools.GroupTree;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.OperatorService;
import edu.udo.cs.yale.tools.OperatorDescription;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
/** Dialog for creating a new operator. */
public class NewOperatorDialog extends JDialog implements ItemListener {
private JTextField nameField = new JTextField("NewOperator");
private JComboBox classCombo;
private JComboBox groupCombo;
private boolean ok = false;
private JTextArea descriptionText = new JTextArea("",3,20);
public NewOperatorDialog(Frame owner) {
super(owner, "New operator", true);
getContentPane().setLayout(new BorderLayout());
JPanel rootPanel = new JPanel(new BorderLayout());
rootPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
getContentPane().add(rootPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { ok(); }
});
buttonPanel.add(okButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { cancel(); }
});
buttonPanel.add(cancelButton);
rootPanel.add(buttonPanel, BorderLayout.SOUTH);
groupCombo = new JComboBox();
groupCombo.addItemListener(this);
groupCombo.setEditable(false);
groupCombo.addItem("All");
Iterator i = OperatorService.getGroups().getSubGroups().iterator();
while (i.hasNext()) {
groupCombo.addItem(((GroupTree)i.next()).getName());
}
groupCombo.setSelectedIndex(0);
classCombo = new JComboBox();
classCombo.addItemListener(this);
classCombo.setEditable(true);
setClasses();
GridBagLayout mainLayout = new GridBagLayout();
JPanel mainPanel = new JPanel(mainLayout);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
JLabel label = new JLabel("Name");
c.gridwidth = GridBagConstraints.RELATIVE;
c.ipadx = 10;
mainLayout.setConstraints(label, c);
mainPanel.add(label);
c.gridwidth = GridBagConstraints.REMAINDER;
mainLayout.setConstraints(nameField, c);
mainPanel.add(nameField);
label = new JLabel("Group");
c.ipadx = 10;
c.gridwidth = GridBagConstraints.RELATIVE;
mainLayout.setConstraints(label, c);
mainPanel.add(label);
c.gridwidth = GridBagConstraints.REMAINDER;
mainLayout.setConstraints(groupCombo, c);
mainPanel.add(groupCombo);
label = new JLabel("Class");
c.ipadx = 10;
c.gridwidth = GridBagConstraints.RELATIVE;
mainLayout.setConstraints(label, c);
mainPanel.add(label);
c.gridwidth = GridBagConstraints.REMAINDER;
mainLayout.setConstraints(classCombo, c);
mainPanel.add(classCombo);
descriptionText.setEditable(false);
descriptionText.setLineWrap(true);
descriptionText.setWrapStyleWord(true);
descriptionText.setBackground(this.getBackground());
c.gridwidth = GridBagConstraints.REMAINDER;
JScrollPane textScrollPane = new JScrollPane(descriptionText);
textScrollPane.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));
mainLayout.setConstraints(textScrollPane, c);
mainPanel.add(textScrollPane);
rootPanel.add(mainPanel, BorderLayout.NORTH);
pack();
setLocationRelativeTo(owner);
}
private void ok() {
ok = true;
dispose();
}
private void cancel() {
ok = false;
dispose();
}
private Operator getOperatorInstance() {
return getOperatorInstance((String)classCombo.getSelectedItem(), nameField.getText());
}
public static Operator getOperatorInstance(String classname, String name) {
try {
Class operatorClass = OperatorService.getOperatorClass(classname);
Operator operator = (Operator)operatorClass.newInstance();
operator.register(Yale.getExperiment(), name);
return operator;
} catch (Exception e) {
SwingTools.showErrorMessage("Cannot instantiate '"+name+"'.", e);
return null;
}
}
public static Operator newOperator(MainFrame mainFrame) {
NewOperatorDialog dialog = new NewOperatorDialog(mainFrame);
dialog.show();
if (dialog.ok) return dialog.getOperatorInstance();
else return null;
}
private void setClasses() {
if (classCombo == null) return;
String group = (String)groupCombo.getSelectedItem();
Set sortedNames = null;
if (group.equals("All")) {
sortedNames = new TreeSet(OperatorService.getOperatorNames());
} else {
GroupTree parentGroup = OperatorService.getGroups();
sortedNames = parentGroup.getSubGroup(group).getAllOperators();
}
classCombo.removeAllItems();
Iterator i = sortedNames.iterator();
while (i.hasNext()) {
String current = (String)i.next();
classCombo.addItem(current);
}
}
private void selectItem() {
String typed = ((JTextField)classCombo.getEditor().getEditorComponent()).getText();
for (int i = 0; i < classCombo.getItemCount(); i++) {
if (((String)classCombo.getItemAt(i)).startsWith(typed)) {
classCombo.setSelectedIndex(i);
break;
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource().equals(classCombo)) {
String name = (String)classCombo.getSelectedItem();
OperatorDescription descr = OperatorService.getOperatorDescription(name);
if (descr != null)
descriptionText.setText(descr.getDescription());
} else if (e.getSource().equals(groupCombo))
setClasses();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -