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;
}
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;
}
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();
}
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"));
}
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;
}
Aggregations