| | |
| | | * CDDL HEADER END |
| | | * |
| | | * |
| | | * Copyright 2008 Sun Microsystems, Inc. |
| | | * Copyright 2008-2009 Sun Microsystems, Inc. |
| | | */ |
| | | |
| | | package org.opends.guitools.controlpanel.util; |
| | |
| | | */ |
| | | public static void updateTableSizes(JTable table, int rows) |
| | | { |
| | | updateTableColumnWidth(table); |
| | | updateTableRowHeight(table, rows); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Updates the height of the table rows according to the size of the |
| | | * rendered component. |
| | | * @param table the table to handle. |
| | | */ |
| | | private static void updateTableRowHeight(JTable table, int rows) |
| | | { |
| | | int headerMaxHeight = 5; |
| | | int margin = table.getIntercellSpacing().height; |
| | | int horizontalMargin = table.getIntercellSpacing().width; |
| | | int verticalMargin = table.getIntercellSpacing().height; |
| | | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
| | | |
| | | int headerMaxHeight = 5; |
| | | int headerMaxWidth = 0; |
| | | |
| | | JTableHeader header = table.getTableHeader(); |
| | | if (header.isVisible()) |
| | | { |
| | |
| | | } |
| | | Component comp = renderer.getTableCellRendererComponent(table, |
| | | table.getModel().getColumnName(col), false, false, 0, col); |
| | | int colHeight = comp.getPreferredSize().height + (2 * margin); |
| | | int colHeight = comp.getPreferredSize().height + (2 * verticalMargin); |
| | | if (colHeight > screenSize.height) |
| | | { |
| | | // There are some issues on Mac OS and sometimes the preferred size |
| | |
| | | } |
| | | headerMaxHeight = Math.max(headerMaxHeight, colHeight); |
| | | } |
| | | } |
| | | |
| | | for (int col=0; col<table.getColumnCount(); col++) |
| | | { |
| | | int colMaxWidth = 8; |
| | | TableColumn tcol = table.getColumnModel().getColumn(col); |
| | | TableCellRenderer renderer = tcol.getHeaderRenderer(); |
| | | |
| | | if (renderer == null) |
| | | { |
| | | renderer = table.getTableHeader().getDefaultRenderer(); |
| | | } |
| | | |
| | | Component comp = renderer.getTableCellRendererComponent(table, |
| | | table.getModel().getColumnName(col), false, false, 0, col); |
| | | colMaxWidth = comp.getPreferredSize().width + 8; |
| | | |
| | | if (colMaxWidth > screenSize.width) |
| | | { |
| | | colMaxWidth = 8; |
| | | } |
| | | |
| | | for (int row=0; row<table.getRowCount(); row++) |
| | | { |
| | | renderer = table.getCellRenderer(row, col); |
| | | comp = table.prepareRenderer(renderer, row, col); |
| | | int colWidth = comp.getPreferredSize().width + (2 * horizontalMargin); |
| | | colMaxWidth = Math.max(colMaxWidth, colWidth); |
| | | } |
| | | tcol.setPreferredWidth(colMaxWidth); |
| | | headerMaxWidth += colMaxWidth; |
| | | } |
| | | |
| | | |
| | | if (header.isVisible()) |
| | | { |
| | | header.setPreferredSize(new Dimension( |
| | | header.getPreferredSize().width, |
| | | headerMaxWidth, |
| | | headerMaxHeight)); |
| | | } |
| | | |
| | | |
| | | int maxRow = 0; |
| | | for (int row=0; row<table.getRowCount(); row++) |
| | | { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Updates the height of the table columns according to the size of the |
| | | * rendered component. |
| | | * @param table the table to handle. |
| | | */ |
| | | private static void updateTableColumnWidth(JTable table) |
| | | { |
| | | int margin = table.getIntercellSpacing().width; |
| | | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
| | | for (int col=0; col<table.getColumnCount(); col++) |
| | | { |
| | | int colMaxWidth = 8; |
| | | TableColumn tcol = table.getColumnModel().getColumn(col); |
| | | TableCellRenderer renderer = tcol.getHeaderRenderer(); |
| | | |
| | | if (renderer == null) |
| | | { |
| | | renderer = table.getTableHeader().getDefaultRenderer(); |
| | | } |
| | | |
| | | Component comp = renderer.getTableCellRendererComponent(table, |
| | | table.getModel().getColumnName(col), false, false, 0, col); |
| | | colMaxWidth = comp.getPreferredSize().width + 8; |
| | | |
| | | if (colMaxWidth > screenSize.width) |
| | | { |
| | | colMaxWidth = 8; |
| | | } |
| | | |
| | | for (int row=0; row<table.getRowCount(); row++) |
| | | { |
| | | renderer = table.getCellRenderer(row, col); |
| | | comp = table.prepareRenderer(renderer, row, col); |
| | | int colWidth = comp.getPreferredSize().width + (2 * margin); |
| | | colMaxWidth = Math.max(colMaxWidth, colWidth); |
| | | } |
| | | tcol.setPreferredWidth(colMaxWidth); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Returns a String that contains the html passed as parameter with a span |
| | | * applied. The span style corresponds to the Font specified as parameter. |
| | | * The goal of this method is to be able to specify a font for an HTML string. |