📄 attributeweightsdialog.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.example.AttributeWeights;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
public class AttributeWeightsDialog extends JDialog {
private boolean ok = false;
private AttributeTableModel attributeTableModel;
private JTextField minWeightField = new JTextField("0.0");
private JCheckBox minWeightCheckBox = new JCheckBox("Show smaller weights");
private JLabel selectionCount = new JLabel();
/** Creates a new dialog for the given feature weights. */
public AttributeWeightsDialog(AttributeWeights weights) {
super((java.awt.Frame)null, "Attribute Weights", true);
getContentPane().setLayout(new BorderLayout());
// buttons
JPanel buttonPanel = new JPanel();
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ok = true;
AttributeWeightsDialog.this.dispose();
}
});
buttonPanel.add(okButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ok = false;
AttributeWeightsDialog.this.dispose();
}
});
buttonPanel.add(cancelButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// ================================================================================
// table with feature weights
attributeTableModel = new AttributeTableModel(weights);
JTable weightTable = new JTable() {
public TableCellEditor getCellEditor(int row, int column) {
if (column == 1) {
return attributeTableModel.getWeightEditor(row);
} else {
return super.getCellEditor(row, column);
}
}
public TableCellRenderer getCellRenderer(int row, int column) {
if (column == 1) {
return attributeTableModel.getWeightEditor(row);
} else {
return super.getCellRenderer(row, column);
}
}
};
weightTable.setColumnSelectionAllowed(false);
weightTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
weightTable.setRowHeight(weightTable.getRowHeight()+8);
weightTable.getTableHeader().setReorderingAllowed(false);
weightTable.setModel(attributeTableModel);
attributeTableModel.addMouseListenerToHeaderInTable(weightTable);
JScrollPane scrollPane = new JScrollPane(weightTable);
getContentPane().add(scrollPane, BorderLayout.CENTER);
// ================================================================================
// control button panel
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JPanel controlPanel = new JPanel();
controlPanel.setLayout(layout);
c.fill = c.BOTH;
c.weightx = 1;
c.gridwidth = c.REMAINDER;
c.insets = new Insets(11,11,11,11);
final JComboBox viewModes = new JComboBox(AttributeTableModel.VIEW_MODES);
viewModes.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
attributeTableModel.setViewMode(viewModes.getSelectedIndex());
update();
}
});
layout.setConstraints(viewModes, c);
controlPanel.add(viewModes);
JButton updateButton = new JButton("Update");
updateButton.setToolTipText("Click to update the view.");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
}
});
layout.setConstraints(updateButton, c);
controlPanel.add(updateButton);
JLabel minWeightLabel = new JLabel("Min weight:");
c.weightx = 0;
c.gridwidth = c.RELATIVE;
layout.setConstraints(minWeightLabel, c);
controlPanel.add(minWeightLabel);
c.weightx = 1;
c.gridwidth = c.REMAINDER;
minWeightField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
}
});
layout.setConstraints(minWeightField, c);
controlPanel.add(minWeightField);
minWeightCheckBox.setToolTipText("If not marked only weights greater than min weight will be shown.");
minWeightCheckBox.setSelected(true);
minWeightCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
}
});
layout.setConstraints(minWeightCheckBox, c);
controlPanel.add(minWeightCheckBox);
layout.setConstraints(selectionCount, c);
controlPanel.add(selectionCount);
updateSelectionCounter();
JPanel dummy = new JPanel();
c.weighty = 1;
layout.setConstraints(dummy, c);
controlPanel.add(dummy);
c.weighty = 0;
final JCheckBox overwriteCheckBox = new JCheckBox("overwrite");
overwriteCheckBox.setToolTipText("If marked loaded weights will overwrite the current ones (weight of 0 does always overwrite).");
overwriteCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
attributeTableModel.setOverwriteMode(overwriteCheckBox.isSelected());
}
});
layout.setConstraints(overwriteCheckBox, c);
controlPanel.add(overwriteCheckBox);
JButton loadButton = new JButton("Load");
loadButton.setToolTipText("Load weights from file.");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
load();
}
});
layout.setConstraints(loadButton, c);
controlPanel.add(loadButton);
JButton saveButton = new JButton("Save");
saveButton.setToolTipText("Save current weights to file.");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
save();
}
});
layout.setConstraints(saveButton, c);
controlPanel.add(saveButton);
controlPanel.setBorder(BorderFactory.createEmptyBorder(11,11,11,11));
getContentPane().add(controlPanel, BorderLayout.WEST);
pack();
setLocationRelativeTo(null);
}
private void updateSelectionCounter() {
selectionCount.setText(attributeTableModel.getNumberOfSelected() + " selected / " + attributeTableModel.getTotalNumber() +
" total");
}
private void update() {
double minWeight = Double.NEGATIVE_INFINITY;
try {
minWeight = Double.parseDouble(minWeightField.getText().trim());
} catch (NumberFormatException e) {
minWeightField.setText(attributeTableModel.getMinWeight() + "");
}
if (!minWeightCheckBox.isSelected()) {
attributeTableModel.setMinWeight(minWeight);
} else {
attributeTableModel.setMinWeight(Double.NEGATIVE_INFINITY);
}
attributeTableModel.updateTable();
updateSelectionCounter();
}
private void load() {
File file = SwingTools.chooseFile(null, null, true);
try {
AttributeWeights fileWeights = AttributeWeights.load(file);
attributeTableModel.mergeWeights(fileWeights);
} catch (IOException e) {
SwingTools.showSimpleErrorMessage("Cannot load attribute weights from file '"+file+".", e);
}
update();
}
private void save() {
File file = SwingTools.chooseFile(null, null, true);
try {
attributeTableModel.getAttributeWeights().save(file);
} catch (IOException e) {
SwingTools.showSimpleErrorMessage("Cannot write attribute weights to file '"+file+".", e);
}
}
public boolean isOk() { return ok; }
public AttributeWeights getAttributeWeights() {
return attributeTableModel.getAttributeWeights();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -