use of org.knime.core.data.renderer.DataValueRendererFamily in project knime-core by knime.
the class TableContentView method addColumn.
/**
* Overridden to set proper header content and apply renderer. The
* header of the column will be set to the <code>DataTable</code>'s
* <code>DataColumnSpec</code> and for the renderer the
* type's <code>getNewRenderer()</code> is used
*
* @param aColumn column to be added
* @see javax.swing.JTable#addColumn(javax.swing.table.TableColumn)
* @see org.knime.core.data.DataType#getRenderer(DataColumnSpec)
* @see DataColumnSpec
*/
@Override
public void addColumn(final TableColumn aColumn) {
assert (hasData());
int i = aColumn.getModelIndex();
aColumn.sizeWidthToFit();
DataTable data = getContentModel().getDataTable();
DataColumnSpec headerValue = data.getDataTableSpec().getColumnSpec(i);
aColumn.setHeaderValue(headerValue);
DataValueRendererFamily renderer = getRendererFamily(headerValue);
String[] descs = renderer.getRendererDescriptions();
// setting a certain column property will set a preferred renderer
String preferredRenderer = headerValue.getProperties().getProperty(DataValueRenderer.PROPERTY_PREFERRED_RENDERER);
if (Arrays.asList(descs).contains(preferredRenderer) && renderer.accepts(preferredRenderer, headerValue)) {
renderer.setActiveRenderer(preferredRenderer);
} else {
for (String s : descs) {
if (renderer.accepts(s, headerValue)) {
renderer.setActiveRenderer(s);
break;
}
}
}
aColumn.setCellRenderer(renderer);
super.addColumn(aColumn);
}
use of org.knime.core.data.renderer.DataValueRendererFamily in project knime-core by knime.
the class TableContentView method getPopUpMenu.
/**
* Create a custom popup menu when the mouse was clicked in a column header.
* This popup menu will contain the possible values in that column (when
* available) and a set of buttons which let the user change the renderer
* (again: when available).
*
* @param column column for which to create the popup menu
* @return a popup menu displaying these properties
* @see #onMouseClickInHeader(MouseEvent)
*/
protected JPopupMenu getPopUpMenu(final int column) {
final TableColumn tableColumn = getColumnModel().getColumn(column);
Object value = tableColumn.getHeaderValue();
if (!(value instanceof DataColumnSpec)) {
// only occurs if someone overrides the addColumn method.
return null;
}
final DataColumnSpec spec = (DataColumnSpec) value;
JPopupMenu popup = new JPopupMenu("Column Context Menu");
JMenuItem menuItem;
// first menu item will allow to show all possible values
final Set<DataCell> valueList = spec.getDomain().getValues();
if (valueList != null && !valueList.isEmpty()) {
menuItem = new JMenuItem("Show possible values");
final String[] columnValues = new String[valueList.size()];
int i = 0;
for (DataCell cell : valueList) {
columnValues[i++] = cell.toString();
}
menuItem.addActionListener(new ActionListener() {
// TODO: must be put in a scroll pane?
@Override
public void actionPerformed(final ActionEvent action) {
JOptionPane.showMessageDialog(TableContentView.this.getRootPane(), columnValues, "Possible Values", JOptionPane.INFORMATION_MESSAGE);
}
});
popup.add(menuItem);
}
// try to figure out the set of available renderers
TableCellRenderer curRen = tableColumn.getCellRenderer();
String renderID = null;
DataValueRendererFamily renFamily = null;
// should always be true unless someone overrides addColumn
if (curRen instanceof DataValueRendererFamily) {
renFamily = (DataValueRendererFamily) curRen;
renderID = renFamily.getDescription();
}
String[] availRender = getAvailableRenderers(column);
if (availRender != null && availRender.length > 1) {
JMenu subMenu = new JMenu("Available Renderers");
popup.add(subMenu);
// actionlistener which changes the renderer according to the
// action command
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent action) {
changeRenderer(column, action.getActionCommand());
}
};
ButtonGroup buttonGroup = new ButtonGroup();
for (int i = 0; i < availRender.length; i++) {
String thisID = availRender[i];
menuItem = new JRadioButtonMenuItem(thisID);
menuItem.setEnabled(renFamily != null && renFamily.accepts(thisID, spec));
buttonGroup.add(menuItem);
menuItem.setActionCommand(thisID);
menuItem.addActionListener(actionListener);
menuItem.setSelected(thisID.equals(renderID));
subMenu.add(menuItem);
}
}
return popup;
}
use of org.knime.core.data.renderer.DataValueRendererFamily in project knime-core by knime.
the class TableContentView method changeRenderer.
/**
* Changes the renderer in a given column. The column's renderer is
* retrieved and checked if it is instance of
* {@link DataValueRendererFamily} (which it is unless a subclass
* overrides <code>addColumn</code>). In this renderer family the renderer
* matching the description <code>rendererID</code> is set active.
* <br>
* If the description is not valid (<code>null</code> or unknown), this
* method does nothing.
*
* @param column the column of interest
* @param rendererID the name of the renderer
* @see DataValueRendererFamily#getRendererDescriptions()
* @throws IndexOutOfBoundsException if <code>column</code> violates its
* range
*/
public void changeRenderer(final int column, final String rendererID) {
final TableColumn aColumn = getColumnModel().getColumn(column);
TableCellRenderer curRen = aColumn.getCellRenderer();
if (!(curRen instanceof DataValueRendererFamily)) {
return;
}
DataValueRendererFamily renFamily = (DataValueRendererFamily) curRen;
renFamily.setActiveRenderer(rendererID);
repaint();
}
use of org.knime.core.data.renderer.DataValueRendererFamily in project knime-core by knime.
the class TableContentView method changeRenderer.
/**
* Changes the renderer in all columns whose type is
* equal to <code>type</code>. This is a convenient way to change the
* renderer of several columns at once. This method does nothing if
* the type is unknown or the identifier is invalid.
* @param type the target type
* @param ident The identifier for the renderer to use
* @see #getTypeRendererMap()
*/
public void changeRenderer(final DataType type, final String ident) {
for (Enumeration<TableColumn> e = getColumnModel().getColumns(); e.hasMoreElements(); ) {
TableColumn tc = e.nextElement();
Object headerValue = tc.getHeaderValue();
TableCellRenderer ren = tc.getCellRenderer();
if (headerValue instanceof DataColumnSpec && ren instanceof DataValueRendererFamily) {
DataColumnSpec c = (DataColumnSpec) headerValue;
DataValueRendererFamily r = (DataValueRendererFamily) ren;
DataType t = c.getType();
if (t.equals(type)) {
r.setActiveRenderer(ident);
}
}
}
repaint();
}
use of org.knime.core.data.renderer.DataValueRendererFamily in project knime-core by knime.
the class TableContentView method getTypeRendererMap.
/**
* Creates a new map containing DataType<->available renderer
* identifiers. The size of this map is equal to the number of different
* {@link DataColumnSpec#getType() column types}, i.e. if the table
* only contains, e.g. double values (represented by
* {@link org.knime.core.data.def.DoubleCell}), this map will have only one
* entry. The values in this map correspond to the renderer descriptions
* that are {@link DataType#getRenderer(DataColumnSpec) available for the
* type at hand}.
*
* <p>This map is used to switch the renderer for a set of columns.
* @return Such a (new) map.
*/
public Map<DataType, String[]> getTypeRendererMap() {
LinkedHashMap<DataType, String[]> result = new LinkedHashMap<DataType, String[]>();
for (Enumeration<TableColumn> e = getColumnModel().getColumns(); e.hasMoreElements(); ) {
TableColumn tc = e.nextElement();
Object headerValue = tc.getHeaderValue();
TableCellRenderer ren = tc.getCellRenderer();
if (headerValue instanceof DataColumnSpec && ren instanceof DataValueRendererFamily) {
DataColumnSpec c = (DataColumnSpec) headerValue;
DataValueRendererFamily r = (DataValueRendererFamily) ren;
DataType t = c.getType();
if (!result.containsKey(t)) {
result.put(t, r.getRendererDescriptions());
}
}
}
return result;
}
Aggregations