public void setMetricsResults(MetricDisplaySpecification displaySpecification, MetricsRun run) { final MetricCategory[] categories = MetricCategory.values(); for (final MetricCategory category : categories) { final JTable table = tables.get(category); final String type = MetricsCategoryNameUtil.getShortNameForCategory(category); final MetricTableSpecification tableSpecification = displaySpecification.getSpecification(category); final MetricsResult results = run.getResultsForCategory(category); final MetricTableModel model = new MetricTableModel(results, type, tableSpecification); table.setModel(model); final Container tab = table.getParent().getParent(); if (model.getRowCount() == 0) { tabbedPane.remove(tab); continue; } final String longName = MetricsCategoryNameUtil.getLongNameForCategory(category); tabbedPane.add(tab, longName); final MyColumnListener columnListener = new MyColumnListener(tableSpecification, table); final TableColumnModel columnModel = table.getColumnModel(); columnModel.addColumnModelListener(columnListener); final int columnCount = columnModel.getColumnCount(); for (int i = 0; i < columnCount; i++) { final TableColumn column = columnModel.getColumn(i); column.addPropertyChangeListener(columnListener); } setRenderers(table, type); setColumnWidths(table, tableSpecification); } }
private void saveColumnSpecification() { final TableColumnModel columnModel = table.getColumnModel(); final int numColumns = table.getColumnCount(); final List<String> columns = new ArrayList<String>(numColumns); final List<Integer> columnWidths = new ArrayList<Integer>(numColumns); for (int i = 0; i < numColumns; i++) { final String columnName = table.getColumnName(i); columns.add(columnName); final TableColumn column = columnModel.getColumn(i); final int columnWidth = column.getWidth(); columnWidths.add(Integer.valueOf(columnWidth)); } tableSpecification.setColumnOrder(columns); tableSpecification.setColumnWidths(columnWidths); MetricsProfileRepository.getInstance().persistCurrentProfile(); }
private static void setColumnWidths(JTable table, MetricTableSpecification tableSpecification) { final TableModel model = table.getModel(); final TableColumnModel columnModel = table.getColumnModel(); final List<Integer> columnWidths = tableSpecification.getColumnWidths(); final List<String> columnOrder = tableSpecification.getColumnOrder(); if (columnWidths != null && !columnWidths.isEmpty()) { final int columnCount = model.getColumnCount(); for (int i = 0; i < columnCount; i++) { final String columnName = model.getColumnName(i); final int index = columnOrder.indexOf(columnName); if (index != -1) { final Integer width = columnWidths.get(index); final TableColumn column = columnModel.getColumn(i); column.setPreferredWidth(width.intValue()); } } } else { final Graphics graphics = table.getGraphics(); final Font font = table.getFont(); final FontMetrics fontMetrics = table.getFontMetrics(font); final int rowCount = model.getRowCount(); int maxFirstColumnWidth = 100; for (int i = 0; i < rowCount; i++) { final String name = (String) model.getValueAt(i, 0); if (name != null) { final Rectangle2D stringBounds = fontMetrics.getStringBounds(name, graphics); final double stringWidth = stringBounds.getWidth(); if (stringWidth > maxFirstColumnWidth) { maxFirstColumnWidth = (int) stringWidth; } } } final int allocatedFirstColumnWidth = Math.min(300, maxFirstColumnWidth + 5); final TableColumn column = columnModel.getColumn(0); column.setPreferredWidth(allocatedFirstColumnWidth); } }
private static void setRenderers(JTable table, String type) { final MetricTableModel model = (MetricTableModel) table.getModel(); final MetricInstance[] metrics = model.getMetricsInstances(); Arrays.sort(metrics, new MetricInstanceAbbreviationComparator()); final TableColumnModel columnModel = table.getColumnModel(); for (int i = 0; i < model.getColumnCount(); i++) { final String columnName = model.getColumnName(i); final TableColumn column = columnModel.getColumn(i); if (columnName.equals(type)) { column.setCellRenderer(new MetricCellRenderer(null)); column.setHeaderRenderer(new HeaderRenderer(null, model, SwingConstants.LEFT)); } else { final MetricInstance metricInstance = model.getMetricForColumn(i); final TableCellRenderer renderer = new MetricCellRenderer(metricInstance); column.setCellRenderer(renderer); final Metric metric = metricInstance.getMetric(); final String displayName = metric.getDisplayName(); column.setHeaderRenderer(new HeaderRenderer(displayName, model, SwingConstants.RIGHT)); } } }