use of javax.swing.event.RowSorterEvent in project JMRI by JMRI.
the class RowSorterUtil method addSingleSortableColumnListener.
/**
* Add a RowSorterListener to the rowSorter that prevents multiple columns
* from being considered while sorting.
*
* @param rowSorter the sorter to add the listener to
* @return the added listener
* @throws NullPointerException if rowSorter is null
*/
public static RowSorterListener addSingleSortableColumnListener(@Nonnull RowSorter<? extends TableModel> rowSorter) {
Objects.requireNonNull(rowSorter, "rowSorter must be nonnull.");
RowSorterListener listener = new RowSorterListener() {
List<? extends SortKey> priorSortKeys = new ArrayList<>();
@Override
public void sorterChanged(RowSorterEvent e) {
if (e.getType().equals(RowSorterEvent.Type.SORT_ORDER_CHANGED)) {
List<? extends SortKey> newSortKeys = new ArrayList<>(e.getSource().getSortKeys());
newSortKeys.removeAll(priorSortKeys);
if (!newSortKeys.isEmpty()) {
priorSortKeys = newSortKeys;
e.getSource().setSortKeys(priorSortKeys);
e.getSource().allRowsChanged();
}
}
}
};
rowSorter.addRowSorterListener(listener);
return listener;
}
use of javax.swing.event.RowSorterEvent in project wekapar by gems-uff.
the class PostprocessAssociationsPanel method loadRules.
/**
* Loads rules into a JTable
*/
public static void loadRules() {
/* Clears table model by setting a new empty one */
table.setModel(new RulesTableModel());
/* Disable components while running loading process */
setEnabledAll(rulesPanel, false);
/* Reset components values */
comboFilter.removeAllItems();
antecedentMap.removeAll();
consequentMap.removeAll();
spinDecimal.reset();
/* Reset counters */
lblTotalRulesValue.setText("0");
lblFilteredRulesValue.setText("0");
/* The association rules */
List<AssociationRule> rulesList = associationRules.getRules();
/* Create a new table model */
final RulesTableModel tableModel = new RulesTableModel();
/* Metric names */
String[] metrics = rulesList.get(0).getMetricNamesForRule();
/* Columns */
tableModel.addColumn("Antecedent (X)");
tableModel.addColumn("Consequent (Y)");
tableModel.addColumn("Support");
for (String m : metrics) {
tableModel.addColumn(m);
}
/* Adds rules to table */
String antecedent = "";
String consequent = "";
double support;
double metricValue;
for (AssociationRule r : rulesList) {
/* Antecedent */
antecedent = "";
for (Item p : r.getPremise()) {
antecedent += p + " ";
antecedentMap.addAttribute(p.toString());
}
/* Consequent */
consequent = "";
for (Item c : r.getConsequence()) {
consequent += c + " ";
consequentMap.addAttribute(c.toString());
}
/* Support */
support = ((double) r.getTotalSupport()) / r.getTotalTransactions();
/* List containing each column's content to be added to table */
List<Object> row = new ArrayList<Object>();
/* Adds antecedent, consequent and support */
row.add(antecedent.trim());
row.add(consequent.trim());
row.add(support);
/* Adds metric values */
for (String m : metrics) {
try {
metricValue = r.getNamedMetricValue(m);
row.add(metricValue);
} catch (Exception e) {
log.statusMessage("FAILED! See log.");
log.logMessage(e.getMessage());
}
}
/* Adds a row with values to table */
tableModel.addRow(row.toArray());
}
/* Set TableModel */
table.setModel(tableModel);
/* Get column model */
final RulesTableColumnModel columnModel = (RulesTableColumnModel) table.getColumnModel();
/* Set minimum widths for antecedent and consequent columns */
columnModel.getColumn(0).setMinWidth(220);
columnModel.getColumn(1).setMinWidth(120);
/* Set custom renderer for antecedent and consequent cells */
columnModel.getColumn(0).setCellRenderer(new RulesTableCellRenderer());
columnModel.getColumn(1).setCellRenderer(new RulesTableCellRenderer());
for (int i = 2; i < table.getColumnCount(); i++) {
/* Set custom renderer for metric's cells */
columnModel.getColumn(i).setCellRenderer(decimalCellRenderer);
/* Set minimum size values for metric's columns */
columnModel.getColumn(i).setPreferredWidth(105);
columnModel.getColumn(i).setMinWidth(70);
columnModel.getColumn(i).setMaxWidth(120);
}
/* Set table's RowSorter for sorting and filterig */
sorter = new TableRowSorter<TableModel>(tableModel);
table.setRowSorter(sorter);
table.getRowSorter().addRowSorterListener(new RowSorterListener() {
// TODO: Improve sorting performance.
@Override
public void sorterChanged(RowSorterEvent e) {
if (e.getType() == Type.SORTED) {
Enumeration<TableColumn> columns = columnModel.getColumns();
TableColumn column;
while (columns.hasMoreElements()) {
column = columns.nextElement();
if (column.getCellRenderer() instanceof ProgressCellRenderer) {
column.setCellRenderer(decimalCellRenderer);
}
}
List<?> sortKeys = e.getSource().getSortKeys();
if (sortKeys.size() > 0) {
RowSorter.SortKey key = (SortKey) sortKeys.get(0);
int keyColumn = key.getColumn();
String columnName = tableModel.getColumnName(keyColumn);
if (columnModel.hasColumn(columnName)) {
int columnIndex = columnModel.getColumnIndex(columnName);
if (tableModel.getColumnClass(columnIndex) == Double.class) {
columnModel.getColumn(columnIndex).setCellRenderer(new ProgressCellRenderer(table, columnIndex));
}
}
}
}
}
});
/* Get total values for results */
lblTotalRulesValue.setText(String.valueOf(rulesList.size()));
lblFilteredRulesValue.setText(String.valueOf(table.getRowCount()));
/* Initializes attribute's component */
comboRuleSide.removeAllItems();
comboRuleSide.addItem(new ComboBoxItem(0, "Antecedent (X)", "X"));
comboRuleSide.addItem(new ComboBoxItem(1, "Consequent (Y)", "Y"));
/* Initializes filter parameters for metrics */
initMetrics();
setEnabledAll(rulesPanel, true);
comboLogicalOperator.setEnabled(false);
btnApply.setEnabled(false);
btnClear.setEnabled(false);
btnApplyMetric.setEnabled(false);
btnResetMetric.setEnabled(false);
}
use of javax.swing.event.RowSorterEvent in project Spark by igniterealtime.
the class PhonebookUI method init.
private void init() {
this.removeAll();
model = new DefaultTableModel() {
private static final long serialVersionUID = -1231025049889503785L;
public boolean isCellEditable(int i, int j) {
return false;
}
};
sorter = new TableRowSorter<TableModel>(model);
table = new JTable(model);
JPanel pbtn = new JPanel();
JPanel psearch = new JPanel();
tfsearch = new JTextField();
JScrollPane scroll = new JScrollPane(table);
table.setRowSorter(sorter);
table.getTableHeader().setReorderingAllowed(false);
btnAdd = new RolloverButton(PhoneRes.getIString("btn.add"));
btnDel = new RolloverButton(PhoneRes.getIString("btn.del"));
btnEdit = new RolloverButton(PhoneRes.getIString("btn.edit"));
btnDial = new RolloverButton(PhoneRes.getIString("btn.dial"));
// add icons
btnAdd.setIcon(new ImageIcon(PhoneRes.getImage("ADDICON").getScaledInstance(iconwidth, iconheight, Image.SCALE_SMOOTH)));
btnDel.setIcon(new ImageIcon(PhoneRes.getImage("DELICON").getScaledInstance(iconwidth, iconheight, Image.SCALE_SMOOTH)));
btnEdit.setIcon(new ImageIcon(PhoneRes.getImage("EDITICON").getScaledInstance(iconwidth, iconheight, Image.SCALE_SMOOTH)));
btnDial.setIcon(new ImageIcon(PhoneRes.getImage("DIALICON").getScaledInstance(iconwidth, iconheight, Image.SCALE_SMOOTH)));
// add actionlisteners
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnAddPerformed();
}
});
btnDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] selected = table.getSelectedRows();
if (selected.length == 0) {
JOptionPane.showMessageDialog(PhonebookUI.this, PhoneRes.getIString("book.noEntry"), PhoneRes.getIString("book.warning"), JOptionPane.WARNING_MESSAGE);
}
for (int select : selected) {
if (select > -1) {
manager.deleteEntry(table.getValueAt(select, 0).toString(), table.getValueAt(select, 1).toString());
model.removeRow(sorter.convertRowIndexToModel(select));
}
}
loadEntries();
}
});
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selected = table.getSelectedRow();
if (selected > -1) {
btnEditPerformed(table.getValueAt(selected, 0).toString(), table.getValueAt(selected, 1).toString());
} else {
JOptionPane.showMessageDialog(PhonebookUI.this, PhoneRes.getIString("book.noEntry"), PhoneRes.getIString("book.warning"), JOptionPane.WARNING_MESSAGE);
}
}
});
btnDial.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selected = table.getSelectedRow();
if (selected > -1) {
final SoftPhoneManager phoneManager = SoftPhoneManager.getInstance();
if (phoneManager.getInterlocutors().size() > 0) {
phoneManager.getDefaultGuiManager().hangupAll();
} else {
phoneManager.getDefaultGuiManager().dial(table.getValueAt(selected, 1).toString());
}
} else {
JOptionPane.showMessageDialog(PhonebookUI.this, PhoneRes.getIString("book.noEntry"), PhoneRes.getIString("book.warning"), JOptionPane.WARNING_MESSAGE);
}
}
});
sorter.addRowSorterListener(new RowSorterListener() {
public void sorterChanged(RowSorterEvent arg0) {
TableRowSorter<?> rs = (TableRowSorter<?>) arg0.getSource();
String temp = tfsearch.getText();
if (model.getRowCount() > 0 && rs.getViewRowCount() < 1) {
// nur wenn auch was drin steht, erkennen
if (temp.length() > 0) {
tfsearch.setText(temp.substring(0, temp.length() - 1));
}
filterTable(tfsearch.getText());
} else if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);
}
}
});
tfsearch.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
keyReleasedSuchFeld(evt);
if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);
}
}
});
loadEntries();
// add components to layout
pbtn.setLayout(new BoxLayout(pbtn, BoxLayout.X_AXIS));
pbtn.setBorder(new EmptyBorder(5, 5, 5, 5));
pbtn.add(Box.createHorizontalGlue());
pbtn.add(btnDial);
pbtn.add(Box.createHorizontalStrut(5));
pbtn.add(btnAdd);
pbtn.add(Box.createHorizontalStrut(5));
pbtn.add(btnEdit);
pbtn.add(Box.createHorizontalStrut(5));
pbtn.add(btnDel);
pbtn.add(Box.createHorizontalGlue());
psearch.setLayout(new BoxLayout(psearch, BoxLayout.X_AXIS));
psearch.setBorder(new EmptyBorder(5, 5, 5, 5));
psearch.add(new JLabel(PhoneRes.getIString("book.search") + ":"));
psearch.add(tfsearch);
this.setLayout(new BorderLayout());
this.add(psearch, BorderLayout.NORTH);
this.add(pbtn, BorderLayout.SOUTH);
this.add(scroll, BorderLayout.CENTER);
tfsearch.requestFocusInWindow();
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.setTitle(PhoneRes.getIString("frame.title"));
frame.setSize(new Dimension(400, 300));
frame.setIconImage(SparkRes.getImageIcon(SparkRes.ADDRESS_BOOK_16x16).getImage());
}
Aggregations