📄 attributetablemodel.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.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
public class AttributeTableModel extends AbstractTableModel {
public static final int VIEW_ALL = 0;
public static final int VIEW_FILE = 1;
public static final int VIEW_EXPERIMENT = 2;
public static final int VIEW_UPDATED = 3;
public static final int VIEW_SELECTED = 4;
public static final String[] VIEW_MODES = { "Show all", "Show from file", "Show from experiment", "Show updated", "Show selected" };
private class State {
public static final int SOURCE_EXPERIMENT = 1;
public static final int SOURCE_FILE = 2;
public static final int SOURCE_BOTH = 3;
private int source = SOURCE_EXPERIMENT;
private double oldWeight = 0.0d;
private boolean updated = false;
public State(int source, double oldWeight) {
this.source = source;
this.oldWeight = oldWeight;
}
public int getSource() { return source; }
public void setSource(int source) { this.source = source; }
public boolean isUpdated() { return updated; }
public void setUpdated(boolean updated) { this.updated = updated; }
public double getOldWeight() { return oldWeight; }
}
private class NameComparator implements Comparator {
private int direction;
public NameComparator(int direction) {
this.direction = direction;
}
public int compare(Object o1, Object o2) {
if ((o1 instanceof String) && (o2 instanceof String))
return ((String)o1).compareTo((String)o2) * direction;
else return 0;
}
}
private AttributeWeights weights;
private String[] attributeNames;
private PropertyCellEditor[] editors;
private Map updateMap = new HashMap();
private int viewMode = VIEW_ALL;
private boolean overwrite = false;
private double minWeight = Double.NEGATIVE_INFINITY;
private int selectionCount = 0;
public AttributeTableModel(AttributeWeights weights) {
if (weights != null) {
this.weights = (AttributeWeights)weights.clone();
} else {
this.weights = new AttributeWeights();
}
Iterator i = this.weights.getAttributeNames().iterator();
while (i.hasNext()) {
String attributeName = (String)i.next();
double oldWeight = this.weights.getWeight(attributeName);
updateMap.put(attributeName, new State(State.SOURCE_EXPERIMENT, oldWeight));
}
updateTable();
}
public Object getValueAt(int row, int column) {
if (column == 0) return attributeNames[row];
else if (column == 1) return new Double(weights.getWeight(attributeNames[row]));
else return null;
}
public void setValueAt(Object value, int row, int column) {
double weight = weights.getWeight(attributeNames[row]);
try {
weight = Double.parseDouble((String)value);
} catch (NumberFormatException e) {}
weights.setWeight(attributeNames[row], weight);
}
public boolean isCellEditable(int row, int column) {
if (column == 0) return false;
else return true;
}
public int getRowCount() { return attributeNames.length; }
public int getColumnCount() { return 2; }
public String getColumnName(int column) {
switch (column) {
case 0: return "Attribute";
case 1: return "Weight";
default: return "?";
}
}
// ================================================================================
public AttributeWeights getAttributeWeights() {
return weights;
}
public PropertyCellEditor getWeightEditor(int row) {
return editors[row];
}
public void setViewMode(int mode) {
this.viewMode = mode;
}
/** Indicates if values which are merge with the current weights should overwrite them. Please note
* that a value of zero always overwrites the current weight! */
public void setOverwriteMode(boolean overwrite) {
this.overwrite = overwrite;
}
public void setMinWeight(double minWeight) {
this.minWeight = minWeight;
}
public double getMinWeight() {
return minWeight;
}
public void mergeWeights(AttributeWeights fileWeights) {
Iterator i = fileWeights.getAttributeNames().iterator();
while (i.hasNext()) {
String attributeName = (String)i.next();
double fileWeight = fileWeights.getWeight(attributeName);
double experimentWeight = weights.getWeight(attributeName);
if (fileWeight == 0.0d) {
weights.setWeight(attributeName, 0.0d);
} else if (overwrite) {
weights.setWeight(attributeName, fileWeight);
} else {
if (Double.isNaN(weights.getWeight(attributeName))) { // overwrite only if not set by experiment
weights.setWeight(attributeName, fileWeight);
}
}
// update existing state
State state = (State)updateMap.get(attributeName);
if (state != null) {
if (state.getSource() == State.SOURCE_EXPERIMENT)
state.setSource(State.SOURCE_BOTH);
if (fileWeight != experimentWeight)
state.setUpdated(true);
} else { // add new state
updateMap.put(attributeName, new State(State.SOURCE_FILE, weights.getWeight(attributeName)));
}
}
}
public void sortByColumn(int column, int direction) {
if (column == 0) {
Arrays.sort(attributeNames, new NameComparator(direction));
} else {
weights.sortByWeight(attributeNames, direction);
}
super.fireTableChanged(new TableModelEvent(this));
}
public int getNumberOfSelected() {
return selectionCount;
}
public int getTotalNumber() {
return this.weights.size();
}
// ================================================================================
public void updateTable() {
// attribute names
Iterator i = this.weights.getAttributeNames().iterator();
List names = new LinkedList();
this.selectionCount = 0;
while (i.hasNext()) {
String attributeName = (String)i.next();
double weight = weights.getWeight(attributeName);
if (weight != 0.0d) selectionCount++;
if (weight >= minWeight) {
State state = (State)updateMap.get(attributeName);
switch (viewMode) {
case VIEW_FILE:
if ((state.getSource() == state.SOURCE_FILE) || (state.getSource() == state.SOURCE_BOTH))
names.add(attributeName);
break;
case VIEW_EXPERIMENT:
if ((state.getSource() == state.SOURCE_EXPERIMENT) || (state.getSource() == state.SOURCE_BOTH))
names.add(attributeName);
break;
case VIEW_UPDATED:
if (state.isUpdated()) names.add(attributeName);
break;
case VIEW_SELECTED:
if (weights.getWeight(attributeName) != 0.0d) names.add(attributeName);
break;
default:
names.add(attributeName);
break;
}
}
}
attributeNames = new String[names.size()];
names.toArray(attributeNames);
// weight editors
editors = new PropertyCellEditor[attributeNames.length];
for (int k = 0; k < editors.length; k++) {
//editors[k] = new AttributeWeightCellEditor(weights.getWeight(attributeNames[k]));
editors[k] = new AttributeWeightCellEditor(((State)updateMap.get(attributeNames[k])).getOldWeight());
}
// repaint
super.fireTableChanged(new TableModelEvent(this));
}
// ================================================================================
public void addMouseListenerToHeaderInTable(final JTable table) {
MouseAdapter listMouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
TableColumnModel columnModel = table.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = table.convertColumnIndexToModel(viewColumn);
if (e.getClickCount() == 1 && column != -1) {
int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK;
int direction = (shiftPressed == 0) ? AttributeWeights.ASCENDING : AttributeWeights.DESCENDING;
sortByColumn(column, direction);
}
}
};
table.getTableHeader().addMouseListener(listMouseListener);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -