Search in sources :

Example 1 with ProgressCellRenderer

use of wekapar.table.ProgressCellRenderer 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 2 with ProgressCellRenderer

use of wekapar.table.ProgressCellRenderer in project wekapar by gems-uff.

the class PostprocessAssociationsPanel method applyFilter.

/**
 * Apply filter for rules and metrics
 */
@SuppressWarnings("unchecked")
private static void applyFilter() {
    /* Lists for filters */
    List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();
    /* Rule's filter */
    RowFilter<Object, Object> rulesFilter = (RowFilter<Object, Object>) buildRulesFilter(comboFilterComponent.getText().trim());
    if (rulesFilter != null) {
        filters.add(rulesFilter);
    }
    /* Filter for metrics */
    RowFilter<Object, Object> metricsFilter = (RowFilter<Object, Object>) buildMetricsFilter();
    if (metricsFilter != null) {
        filters.add(metricsFilter);
    }
    /* Join rule and metric filters */
    RowFilter<Object, Object> andFilter = RowFilter.andFilter(filters);
    /* Apply filter to table */
    sorter.setRowFilter(andFilter);
    /* Update counting of filtered values */
    lblFilteredRulesValue.setText(String.valueOf(table.getRowCount()));
    /* Update max value reference for ProgressCellRenderer */
    List<?> sortKeys = sorter.getSortKeys();
    if (sortKeys.size() > 0) {
        RowSorter.SortKey key = (SortKey) sortKeys.get(0);
        TableModel tableModel = table.getModel();
        RulesTableColumnModel columnModel = table.getColumnModel();
        String columnName = tableModel.getColumnName(key.getColumn());
        if (columnModel.hasColumn(columnName)) {
            int columnIndex = columnModel.getColumnIndex(columnName);
            TableCellRenderer cellRenderer = columnModel.getColumn(columnIndex).getCellRenderer();
            if (cellRenderer instanceof ProgressCellRenderer) {
                ((ProgressCellRenderer) cellRenderer).updateMaxValue(table, columnIndex);
            }
        }
    }
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) RulesTableCellRenderer(wekapar.table.RulesTableCellRenderer) RowSorter(javax.swing.RowSorter) TableRowSorter(javax.swing.table.TableRowSorter) ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) SortKey(javax.swing.RowSorter.SortKey) ProgressCellRenderer(wekapar.table.ProgressCellRenderer) Point(java.awt.Point) RowFilter(javax.swing.RowFilter) RulesTableColumnModel(wekapar.table.RulesTableColumnModel) RulesTableModel(wekapar.table.RulesTableModel) TableModel(javax.swing.table.TableModel)

Aggregations

Point (java.awt.Point)2 ArrayList (java.util.ArrayList)2 SortKey (javax.swing.RowSorter.SortKey)2 TableModel (javax.swing.table.TableModel)2 ProgressCellRenderer (wekapar.table.ProgressCellRenderer)2 RulesTableCellRenderer (wekapar.table.RulesTableCellRenderer)2 RulesTableColumnModel (wekapar.table.RulesTableColumnModel)2 RulesTableModel (wekapar.table.RulesTableModel)2 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 JMenuItem (javax.swing.JMenuItem)1 RowFilter (javax.swing.RowFilter)1 RowSorter (javax.swing.RowSorter)1 RowSorterEvent (javax.swing.event.RowSorterEvent)1 RowSorterListener (javax.swing.event.RowSorterListener)1 TableCellRenderer (javax.swing.table.TableCellRenderer)1 TableColumn (javax.swing.table.TableColumn)1 TableRowSorter (javax.swing.table.TableRowSorter)1 AssociationRule (weka.associations.AssociationRule)1