Search in sources :

Example 36 with CyTable

use of org.cytoscape.model.CyTable in project cytoscape-impl by cytoscape.

the class AbstractChart method getDataFromColumns.

public Map<String, List<Double>> getDataFromColumns(final CyNetwork network, final CyIdentifiable model, final List<CyColumnIdentifier> columnNames) {
    LinkedHashMap<String, List<Double>> data = new LinkedHashMap<>();
    final CyRow row = network.getRow(model);
    if (row == null)
        return data;
    final CyTable table = row.getTable();
    final List<Double> singleSeriesValues = new ArrayList<>();
    final StringBuilder singleSeriesKey = new StringBuilder();
    int singleSeriesIndex = -1;
    int count = 0;
    for (final CyColumnIdentifier colId : columnNames) {
        final CyColumn column = table.getColumn(colId.getColumnName());
        if (column == null)
            continue;
        final String colName = column.getName();
        final List<Double> values = new ArrayList<Double>();
        if (column.getType() == List.class) {
            // List Column: One column = one data series
            final Class<?> type = column.getListElementType();
            if (type == Double.class) {
                List<Double> list = row.getList(colName, Double.class);
                if (list != null)
                    values.addAll(list);
            } else if (type == Integer.class) {
                List<Integer> list = row.getList(colName, Integer.class);
                if (list != null) {
                    for (Integer i : list) values.add(i.doubleValue());
                }
            } else if (type == Long.class) {
                List<Long> list = row.getList(colName, Long.class);
                if (list != null) {
                    for (Long l : list) values.add(l.doubleValue());
                }
            } else if (type == Float.class) {
                List<Float> list = row.getList(colName, Float.class);
                if (list != null) {
                    for (Float f : list) values.add(f.doubleValue());
                }
            }
            data.put(colName, values);
        } else {
            // Single Column: All single columns together make only one data series
            final Class<?> type = column.getType();
            if (Number.class.isAssignableFrom(type)) {
                if (!row.isSet(colName)) {
                    singleSeriesValues.add(Double.NaN);
                } else if (type == Double.class) {
                    singleSeriesValues.add(row.get(colName, Double.class));
                } else if (type == Integer.class) {
                    Integer i = row.get(colName, Integer.class);
                    singleSeriesValues.add(i.doubleValue());
                } else if (type == Float.class) {
                    Float f = row.get(colName, Float.class);
                    singleSeriesValues.add(f.doubleValue());
                }
                singleSeriesKey.append(colName + ",");
                // The index of this data series is the index of the first single column
                if (singleSeriesIndex == -1)
                    singleSeriesIndex = count;
            }
        }
        count++;
    }
    if (!singleSeriesValues.isEmpty()) {
        singleSeriesKey.deleteCharAt(singleSeriesKey.length() - 1);
        // To add the series of single columns into the correct position, we have to rebuild the data map
        final Set<Entry<String, List<Double>>> entrySet = data.entrySet();
        data = new LinkedHashMap<>();
        int i = 0;
        for (final Entry<String, List<Double>> entry : entrySet) {
            if (i == singleSeriesIndex)
                data.put(singleSeriesKey.toString(), singleSeriesValues);
            data.put(entry.getKey(), entry.getValue());
            i++;
        }
        if (// (entrySet.isEmpty() || i >= entrySet.size())
        !data.containsKey(singleSeriesKey.toString()))
            data.put(singleSeriesKey.toString(), singleSeriesValues);
    }
    return data;
}
Also used : ArrayList(java.util.ArrayList) CyColumn(org.cytoscape.model.CyColumn) CyColumnIdentifier(org.cytoscape.view.presentation.property.values.CyColumnIdentifier) CyRow(org.cytoscape.model.CyRow) LinkedHashMap(java.util.LinkedHashMap) CyTable(org.cytoscape.model.CyTable) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List)

Example 37 with CyTable

use of org.cytoscape.model.CyTable in project cytoscape-impl by cytoscape.

the class AbstractChart method getLabelsFromColumn.

@SuppressWarnings("unchecked")
public List<String> getLabelsFromColumn(final CyNetwork network, final CyIdentifiable model, final CyColumnIdentifier columnId) {
    final List<String> labels = new ArrayList<>();
    final CyRow row = network.getRow(model);
    if (row != null && columnId != null) {
        final CyTable table = row.getTable();
        final CyColumn column = table.getColumn(columnId.getColumnName());
        if (column != null && column.getType() == List.class) {
            final Class<?> type = column.getListElementType();
            final List<?> values = row.getList(columnId.getColumnName(), type);
            if (type == String.class) {
                labels.addAll((List<String>) values);
            } else {
                for (Object obj : values) labels.add(obj.toString());
            }
        }
    }
    return labels;
}
Also used : CyTable(org.cytoscape.model.CyTable) ArrayList(java.util.ArrayList) CyColumn(org.cytoscape.model.CyColumn) ArrayList(java.util.ArrayList) List(java.util.List) CyRow(org.cytoscape.model.CyRow)

Example 38 with CyTable

use of org.cytoscape.model.CyTable in project cytoscape-impl by cytoscape.

the class PopupMenuHelper method selectElementsFromSelectedRows.

private void selectElementsFromSelectedRows(final JTable table, final Class<? extends CyIdentifiable> tableType) {
    final Thread t = new Thread() {

        @Override
        public void run() {
            final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
            final CyNetwork net = applicationManager.getCurrentNetwork();
            if (net != null) {
                final BrowserTableModel tableModel = (BrowserTableModel) table.getModel();
                final int[] selectedRows = table.getSelectedRows();
                final Set<CyRow> targetRows = new HashSet<CyRow>();
                for (final int rowIndex : selectedRows) {
                    // Getting the row from data table solves the problem with hidden or moved SUID column.
                    // However, since the rows might be sorted we need to convert the index to model.
                    final ValidatedObjectAndEditString selected = (ValidatedObjectAndEditString) tableModel.getValueAt(table.convertRowIndexToModel(rowIndex), CyNetwork.SUID);
                    targetRows.add(tableModel.getRow(selected.getValidatedObject()));
                }
                final CyTable cyTable = tableType == CyNode.class ? net.getDefaultNodeTable() : net.getDefaultEdgeTable();
                for (final CyRow cyRow : cyTable.getAllRows()) cyRow.set(CyNetwork.SELECTED, targetRows.contains(cyRow));
                final CyNetworkView view = applicationManager.getCurrentNetworkView();
                if (view != null) {
                    final CyEventHelper eventHelper = serviceRegistrar.getService(CyEventHelper.class);
                    eventHelper.flushPayloadEvents();
                    view.updateView();
                }
            }
        }
    };
    t.start();
}
Also used : CyEventHelper(org.cytoscape.event.CyEventHelper) ValidatedObjectAndEditString(org.cytoscape.browser.internal.util.ValidatedObjectAndEditString) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) Point(java.awt.Point) CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyTable(org.cytoscape.model.CyTable) CyNode(org.cytoscape.model.CyNode) CyNetworkView(org.cytoscape.view.model.CyNetworkView) HashSet(java.util.HashSet)

Example 39 with CyTable

use of org.cytoscape.model.CyTable in project cytoscape-impl by cytoscape.

the class TableBrowserToolBar method updateEnableState.

protected void updateEnableState(final JComponent comp) {
    if (comp == null)
        return;
    boolean enabled = false;
    if (browserTableModel != null) {
        final CyTable attrs = browserTableModel.getDataTable();
        if (comp == deleteTableButton) {
            enabled = browserTableModel.getDataTable().getMutability() == Mutability.MUTABLE;
        } else if (comp == deleteAttributeButton) {
            for (final CyColumn column : attrs.getColumns()) {
                if (!column.isImmutable()) {
                    enabled = true;
                    break;
                }
            }
        } else if (comp == fnBuilderButton) {
            final int row = browserTable.getSelectedRow();
            final int column = browserTable.getSelectedColumn();
            enabled = row >= 0 && column >= 0 && browserTableModel.isCellEditable(row, column);
        } else if (comp == tableChooser) {
            enabled = tableChooser.getItemCount() > 0;
        } else {
            enabled = true;
        }
    }
    comp.setEnabled(enabled);
    // Unfortunately this is necessary on Nimbus!
    if (comp instanceof AbstractButton && LookAndFeelUtil.isNimbusLAF())
        comp.setForeground(UIManager.getColor(enabled ? "Button.foreground" : "Button.disabledForeground"));
}
Also used : CyTable(org.cytoscape.model.CyTable) AbstractButton(javax.swing.AbstractButton) CyColumn(org.cytoscape.model.CyColumn)

Example 40 with CyTable

use of org.cytoscape.model.CyTable in project cytoscape-impl by cytoscape.

the class TableBrowserToolBar method getFnBuilderButton.

private JButton getFnBuilderButton() {
    if (fnBuilderButton == null) {
        fnBuilderButton = new JButton("f(x)");
        fnBuilderButton.setToolTipText("Function Builder");
        Font iconFont = null;
        try {
            iconFont = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/fonts/jsMath-cmti10.ttf"));
        } catch (Exception e) {
            throw new RuntimeException("Error loading font", e);
        }
        styleButton(fnBuilderButton, iconFont.deriveFont(18.0f));
        final JFrame rootFrame = (JFrame) SwingUtilities.getRoot(this);
        fnBuilderButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                // Do not allow opening of the formula builder dialog while a cell is being edited!
                if (browserTableModel == null || browserTable.getCellEditor() != null)
                    return;
                final int cellRow = browserTable.getSelectedRow();
                final int cellColumn = browserTable.getSelectedColumn();
                int colIndex = -1;
                // Map the screen index of column to internal index of the table model
                if (cellRow >= 0 && cellColumn >= 0) {
                    String colName = browserTable.getColumnName(cellColumn);
                    colIndex = browserTableModel.mapColumnNameToColumnIndex(colName);
                }
                if (cellRow == -1 || cellColumn == -1 || !browserTableModel.isCellEditable(cellRow, colIndex)) {
                    JOptionPane.showMessageDialog(rootFrame, "Can't enter a formula w/o a selected cell.", "Information", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    final String attrName = getAttribName(cellRow, cellColumn);
                    final Map<String, Class<?>> attribNameToTypeMap = new HashMap<>();
                    final CyTable dataTable = browserTableModel.getDataTable();
                    initAttribNameToTypeMap(dataTable, attrName, attribNameToTypeMap);
                    final EquationCompiler compiler = serviceRegistrar.getService(EquationCompiler.class);
                    final FormulaBuilderDialog formulaBuilderDialog = new FormulaBuilderDialog(compiler, browserTable, rootFrame, attrName);
                    formulaBuilderDialog.setLocationRelativeTo(rootFrame);
                    formulaBuilderDialog.setVisible(true);
                }
            }

            private void initAttribNameToTypeMap(final CyTable dataTable, final String attrName, final Map<String, Class<?>> attribNameToTypeMap) {
                for (final CyColumn column : dataTable.getColumns()) attribNameToTypeMap.put(column.getName(), column.getType());
                attribNameToTypeMap.remove(attrName);
            }
        });
    }
    return fnBuilderButton;
}
Also used : ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) CyColumn(org.cytoscape.model.CyColumn) Font(java.awt.Font) CyTable(org.cytoscape.model.CyTable) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame) EquationCompiler(org.cytoscape.equations.EquationCompiler) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CyTable (org.cytoscape.model.CyTable)282 CyNetwork (org.cytoscape.model.CyNetwork)79 CyRow (org.cytoscape.model.CyRow)73 CyColumn (org.cytoscape.model.CyColumn)71 ArrayList (java.util.ArrayList)55 CyNode (org.cytoscape.model.CyNode)43 CyEdge (org.cytoscape.model.CyEdge)35 Test (org.junit.Test)29 List (java.util.List)26 HashMap (java.util.HashMap)24 CyApplicationManager (org.cytoscape.application.CyApplicationManager)19 HashSet (java.util.HashSet)18 CyIdentifiable (org.cytoscape.model.CyIdentifiable)14 CyNetworkView (org.cytoscape.view.model.CyNetworkView)14 CyNetworkTableManager (org.cytoscape.model.CyNetworkTableManager)13 RowSetRecord (org.cytoscape.model.events.RowSetRecord)11 CyRootNetwork (org.cytoscape.model.subnetwork.CyRootNetwork)11 CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)11 CyEventHelper (org.cytoscape.event.CyEventHelper)10 CyTableManager (org.cytoscape.model.CyTableManager)10