package jalview.gui; import jalview.fts.api.FTSData; import jalview.fts.api.FTSDataColumnI; import jalview.fts.core.DecimalFormatTableCellRenderer; import jalview.fts.core.FTSRestRequest; import java.awt.event.MouseEvent; import java.util.Collection; import java.util.Map; import javax.swing.JTable; import javax.swing.event.ChangeEvent; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; @SuppressWarnings("serial") public class JvSummaryTable extends JTable { private Map tempUserPrefs; public JvSummaryTable(Map tempUserPrefs) { this.tempUserPrefs = tempUserPrefs; } private boolean inLayout; @Override public boolean getScrollableTracksViewportWidth() { return hasExcessWidth(); } @Override public void doLayout() { if (hasExcessWidth()) { autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS; } inLayout = true; super.doLayout(); inLayout = false; autoResizeMode = AUTO_RESIZE_OFF; } protected boolean hasExcessWidth() { return getPreferredSize().width < getParent().getWidth(); } @Override public void columnMarginChanged(ChangeEvent e) { if (isEditing()) { removeEditor(); } TableColumn resizingColumn = getTableHeader().getResizingColumn(); // Need to do this here, before the parent's // layout manager calls getPreferredSize(). if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF && !inLayout) { resizingColumn.setPreferredWidth(resizingColumn.getWidth()); String colHeader = resizingColumn.getHeaderValue().toString(); tempUserPrefs.put(colHeader, resizingColumn.getWidth()); } resizeAndRepaint(); } @Override public String getToolTipText(MouseEvent evt) { String toolTipText = null; java.awt.Point pnt = evt.getPoint(); int rowIndex = rowAtPoint(pnt); int colIndex = columnAtPoint(pnt); try { if (getValueAt(rowIndex, colIndex) == null) { return null; } toolTipText = getValueAt(rowIndex, colIndex).toString(); } catch (Exception e) { // e.printStackTrace(); } toolTipText = (toolTipText == null ? null : (toolTipText.length() > 500 ? JvSwingUtils.wrapTooltip(true, "\"" + toolTipText.subSequence(0, 500) + "...\"") : JvSwingUtils.wrapTooltip(true, toolTipText))); return toolTipText; } /** * Convenience method to obtain a Table model for a given summary List based * on the request parameters * * @param request * the FTSRestRequest object which holds useful information for * creating a table model * @param summariesList * the summary list which contains the data for populating the * table's rows * @return the table model which was dynamically generated */ public static DefaultTableModel getTableModel(FTSRestRequest request, Collection summariesList) { final FTSDataColumnI[] cols = request.getWantedFields().toArray( new FTSDataColumnI[0]); final int colOffset = request.getAssociatedSequence() == null ? 0 : 1; DefaultTableModel tableModel = new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) { return false; } @Override public Class getColumnClass(int columnIndex) { if (colOffset == 1 && columnIndex == 0) { return String.class; } return cols[columnIndex - colOffset].getDataType() .getDataTypeClass(); } }; if (request.getAssociatedSequence() != null) { tableModel.addColumn("Ref Sequence"); // Create sequence column header if // exists in the request } for (FTSDataColumnI field : request.getWantedFields()) { tableModel.addColumn(field.getName()); // Create sequence column header if // exists in the request } for (FTSData res : summariesList) { tableModel.addRow(res.getSummaryData()); // Populate table rows with // summary list } return tableModel; } public static void configureTableColumn(JTable tbl_summary, Collection wantedFields, Map columnPrefs) { for (FTSDataColumnI wantedField : wantedFields) { try { tbl_summary.getColumn(wantedField.getName()).setMinWidth( wantedField.getMinWidth()); tbl_summary.getColumn(wantedField.getName()).setMaxWidth( wantedField.getMaxWidth()); int prefedWidth = columnPrefs.get(wantedField.getName()) == null ? wantedField .getPreferredWidth() : columnPrefs.get(wantedField .getName()); tbl_summary.getColumn(wantedField.getName()).setPreferredWidth( prefedWidth); } catch (Exception e) { e.printStackTrace(); } if (wantedField.getDataType().getDataTypeClass() == Double.class) { DecimalFormatTableCellRenderer dfr = new DecimalFormatTableCellRenderer( wantedField.getDataType().isFormtted(), wantedField .getDataType().getSignificantFigures()); tbl_summary.getColumn(wantedField.getName()).setCellRenderer(dfr); } else if (wantedField.getDataType().getDataTypeClass() == Integer.class) { DecimalFormatTableCellRenderer dfr = new DecimalFormatTableCellRenderer( wantedField.getDataType().isFormtted(), wantedField .getDataType().getSignificantFigures()); tbl_summary.getColumn(wantedField.getName()).setCellRenderer(dfr); } } } }