Search in sources :

Example 1 with RowSorterEvent

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;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) RowSorterEvent(javax.swing.event.RowSorterEvent) RowSorterListener(javax.swing.event.RowSorterListener)

Example 2 with RowSorterEvent

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);
}
Also used : ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) ProgressCellRenderer(wekapar.table.ProgressCellRenderer) Item(weka.associations.Item) ComboBoxItem(wekapar.ComboBoxItem) JMenuItem(javax.swing.JMenuItem) RulesTableModel(wekapar.table.RulesTableModel) ArrayList(java.util.ArrayList) List(java.util.List) RulesTableColumnModel(wekapar.table.RulesTableColumnModel) RowSorterListener(javax.swing.event.RowSorterListener) Enumeration(java.util.Enumeration) RulesTableCellRenderer(wekapar.table.RulesTableCellRenderer) TableColumn(javax.swing.table.TableColumn) IOException(java.io.IOException) Point(java.awt.Point) AssociationRule(weka.associations.AssociationRule) ComboBoxItem(wekapar.ComboBoxItem) RowSorterEvent(javax.swing.event.RowSorterEvent) RulesTableModel(wekapar.table.RulesTableModel) TableModel(javax.swing.table.TableModel)

Example 3 with RowSorterEvent

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());
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) ActionEvent(java.awt.event.ActionEvent) DefaultTableModel(javax.swing.table.DefaultTableModel) KeyAdapter(java.awt.event.KeyAdapter) BoxLayout(javax.swing.BoxLayout) JTextField(javax.swing.JTextField) RolloverButton(org.jivesoftware.spark.component.RolloverButton) KeyEvent(java.awt.event.KeyEvent) BorderLayout(java.awt.BorderLayout) RowSorterListener(javax.swing.event.RowSorterListener) EmptyBorder(javax.swing.border.EmptyBorder) TableRowSorter(javax.swing.table.TableRowSorter) JScrollPane(javax.swing.JScrollPane) SoftPhoneManager(net.java.sipmack.softphone.SoftPhoneManager) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable) RowSorterEvent(javax.swing.event.RowSorterEvent) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel)

Aggregations

RowSorterEvent (javax.swing.event.RowSorterEvent)3 RowSorterListener (javax.swing.event.RowSorterListener)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SortKey (javax.swing.RowSorter.SortKey)2 TableModel (javax.swing.table.TableModel)2 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 KeyAdapter (java.awt.event.KeyAdapter)1 KeyEvent (java.awt.event.KeyEvent)1 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 BoxLayout (javax.swing.BoxLayout)1 ImageIcon (javax.swing.ImageIcon)1 JLabel (javax.swing.JLabel)1 JMenuItem (javax.swing.JMenuItem)1 JPanel (javax.swing.JPanel)1