Search in sources :

Example 6 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project adempiere by adempiere.

the class CTable method packColumn.

//	autoSize
public void packColumn(TableColumn column) {
    int width = 0;
    //	Header
    TableCellRenderer renderer = column.getHeaderRenderer();
    if (renderer == null)
        renderer = new DefaultTableCellRenderer();
    Component comp = null;
    if (renderer != null)
        comp = renderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, 0, 0);
    //
    if (comp != null) {
        width = comp.getPreferredSize().width;
        width = Math.max(width, comp.getWidth());
        //	Cells
        int col = column.getModelIndex();
        int maxRow = Math.min(20, getRowCount());
        try {
            for (int row = 0; row < maxRow; row++) {
                renderer = getCellRenderer(row, col);
                comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
                if (comp != null) {
                    int rowWidth = comp.getPreferredSize().width;
                    width = Math.max(width, rowWidth);
                }
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, column.getIdentifier().toString(), e);
        }
        //	Width not greater than 250
        width = Math.min(MAXSIZE, width + SLACK);
    }
    //
    column.setPreferredWidth(width);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JComponent(javax.swing.JComponent) Component(java.awt.Component) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 7 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project adempiere by adempiere.

the class CTable method autoSize.

//  stopEditor
/**************************************************************************
	 *	Size Columns.
	 *	@param useColumnIdentifier if false uses plain content -
	 *  otherwise uses Column Identifier to indicate displayed columns
	 */
public void autoSize(boolean useColumnIdentifier) {
    TableModel model = this.getModel();
    int size = model.getColumnCount();
    //	for all columns
    for (int c = 0; c < size; c++) {
        TableColumn column = getColumnModel().getColumn(c);
        //	Not displayed columns
        if (useColumnIdentifier && (column.getIdentifier() == null || column.getMaxWidth() == 0 || column.getIdentifier().toString().length() == 0))
            continue;
        int width = 0;
        //	Header
        TableCellRenderer renderer = column.getHeaderRenderer();
        if (renderer == null)
            renderer = new DefaultTableCellRenderer();
        Component comp = null;
        if (renderer != null)
            comp = renderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, 0, 0);
        //
        if (comp != null) {
            width = comp.getPreferredSize().width;
            width = Math.max(width, comp.getWidth());
            //	Cells
            int col = column.getModelIndex();
            int maxRow = Math.min(20, getRowCount());
            try {
                for (int row = 0; row < maxRow; row++) {
                    renderer = getCellRenderer(row, col);
                    comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
                    if (comp != null) {
                        int rowWidth = comp.getPreferredSize().width;
                        width = Math.max(width, rowWidth);
                    }
                }
            } catch (Exception e) {
                log.log(Level.SEVERE, column.getIdentifier().toString(), e);
            }
            //	Width not greater than 250
            width = Math.min(MAXSIZE, width + SLACK);
        }
        //
        column.setPreferredWidth(width);
    }
//	for all columns
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JComponent(javax.swing.JComponent) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 8 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project adempiere by adempiere.

the class VBrowserTable method autoSize.

/**
     * Size Columns.
     * Uses Mimimun Column Size
     */
public void autoSize() {
    if (!autoResize)
        return;
    long start = System.currentTimeMillis();
    //
    //	making sure it fits in a column
    final int SLACK = 8;
    //	max size of a column
    final int MAXSIZE = 300;
    //
    TableModel model = this.getModel();
    int size = model.getColumnCount();
    //	for all columns
    for (int col = 0; col < size; col++) {
        //  Column & minimum width
        TableColumn tc = this.getColumnModel().getColumn(col);
        int width = 0;
        if (m_minWidth.size() > col)
            width = ((Integer) m_minWidth.get(col)).intValue();
        //  log.config( "Column=" + col + " " + column.getHeaderValue());
        //	Header
        TableCellRenderer renderer = tc.getHeaderRenderer();
        if (renderer == null)
            renderer = new DefaultTableCellRenderer();
        Component comp = renderer.getTableCellRendererComponent(this, tc.getHeaderValue(), false, false, 0, 0);
        //	log.fine( "Hdr - preferred=" + comp.getPreferredSize().width + ", width=" + comp.getWidth());
        width = Math.max(width, comp.getPreferredSize().width + SLACK);
        //	Cells
        //  first 30 rows
        int maxRow = Math.min(30, getRowCount());
        for (int row = 0; row < maxRow; row++) {
            renderer = getCellRenderer(row, col);
            comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
            if (comp != null) {
                int rowWidth = comp.getPreferredSize().width + SLACK;
                width = Math.max(width, rowWidth);
            }
        }
        //	Width not greater ..
        width = Math.min(MAXSIZE, width);
        tc.setPreferredWidth(width);
    //	log.fine( "width=" + width);
    }
    //	for all columns
    log.finer("Cols=" + size + " - " + (System.currentTimeMillis() - start) + "ms");
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 9 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project jmeter by apache.

the class HeaderAsPropertyRendererWrapper method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (delegate instanceof DefaultTableCellRenderer) {
        DefaultTableCellRenderer tr = (DefaultTableCellRenderer) delegate;
        if (table != null) {
            JTableHeader header = table.getTableHeader();
            if (header != null) {
                tr.setForeground(header.getForeground());
                tr.setBackground(header.getBackground());
                tr.setFont(header.getFont());
            }
        }
        tr.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        tr.setHorizontalAlignment(SwingConstants.CENTER);
    }
    return delegate.getTableCellRendererComponent(table, HeaderAsPropertyRenderer.getText(value, row, column, columnsMsgParameters), isSelected, hasFocus, row, column);
}
Also used : JTableHeader(javax.swing.table.JTableHeader) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 10 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project intellij-community by JetBrains.

the class ShowUIDefaultsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final UIDefaults defaults = UIManager.getDefaults();
    Enumeration keys = defaults.keys();
    final Object[][] data = new Object[defaults.size()][2];
    int i = 0;
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        data[i][0] = key;
        data[i][1] = defaults.get(key);
        i++;
    }
    Arrays.sort(data, (o1, o2) -> StringUtil.naturalCompare(o1[0].toString(), o2[0].toString()));
    final Project project = getEventProject(e);
    new DialogWrapper(project) {

        {
            setTitle("Edit LaF Defaults");
            setModal(false);
            init();
        }

        public JBTable myTable;

        @Nullable
        @Override
        public JComponent getPreferredFocusedComponent() {
            return myTable;
        }

        @Nullable
        @Override
        protected String getDimensionServiceKey() {
            return project == null ? null : "UI.Defaults.Dialog";
        }

        @Override
        protected JComponent createCenterPanel() {
            final JBTable table = new JBTable(new DefaultTableModel(data, new Object[] { "Name", "Value" }) {

                @Override
                public boolean isCellEditable(int row, int column) {
                    return column == 1 && getValueAt(row, column) instanceof Color;
                }
            }) {

                @Override
                public boolean editCellAt(int row, int column, EventObject e) {
                    if (isCellEditable(row, column) && e instanceof MouseEvent) {
                        final Object color = getValueAt(row, column);
                        final Color newColor = ColorPicker.showDialog(this, "Choose Color", (Color) color, true, null, true);
                        if (newColor != null) {
                            final ColorUIResource colorUIResource = new ColorUIResource(newColor);
                            final Object key = getValueAt(row, 0);
                            UIManager.put(key, colorUIResource);
                            setValueAt(colorUIResource, row, column);
                        }
                    }
                    return false;
                }
            };
            table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    final JPanel panel = new JPanel(new BorderLayout());
                    final JLabel label = new JLabel(value == null ? "" : value.toString());
                    panel.add(label, BorderLayout.CENTER);
                    if (value instanceof Color) {
                        final Color c = (Color) value;
                        label.setText(String.format("[r=%d,g=%d,b=%d] hex=0x%s", c.getRed(), c.getGreen(), c.getBlue(), ColorUtil.toHex(c)));
                        label.setForeground(ColorUtil.isDark(c) ? Color.white : Color.black);
                        panel.setBackground(c);
                        return panel;
                    } else if (value instanceof Icon) {
                        try {
                            final Icon icon = new IconWrap((Icon) value);
                            if (icon.getIconHeight() <= 20) {
                                label.setIcon(icon);
                            }
                            label.setText(String.format("(%dx%d) %s)", icon.getIconWidth(), icon.getIconHeight(), label.getText()));
                        } catch (Throwable e1) {
                        //
                        }
                        return panel;
                    } else if (value instanceof Border) {
                        try {
                            final Insets i = ((Border) value).getBorderInsets(null);
                            label.setText(String.format("[%d, %d, %d, %d] %s", i.top, i.left, i.bottom, i.right, label.getText()));
                            return panel;
                        } catch (Exception ignore) {
                        }
                    }
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                }
            });
            final JBScrollPane pane = new JBScrollPane(table);
            new TableSpeedSearch(table, (o, cell) -> cell.column == 1 ? null : String.valueOf(o));
            table.setShowGrid(false);
            final JPanel panel = new JPanel(new BorderLayout());
            panel.add(pane, BorderLayout.CENTER);
            myTable = table;
            TableUtil.ensureSelectionExists(myTable);
            return panel;
        }
    }.show();
}
Also used : EmptyIcon(com.intellij.util.ui.EmptyIcon) Arrays(java.util.Arrays) DefaultTableModel(javax.swing.table.DefaultTableModel) Enumeration(java.util.Enumeration) StringUtil(com.intellij.openapi.util.text.StringUtil) AnAction(com.intellij.openapi.actionSystem.AnAction) ColorUIResource(javax.swing.plaf.ColorUIResource) MouseEvent(java.awt.event.MouseEvent) JBScrollPane(com.intellij.ui.components.JBScrollPane) Border(javax.swing.border.Border) EventObject(java.util.EventObject) java.awt(java.awt) JBTable(com.intellij.ui.table.JBTable) Nullable(org.jetbrains.annotations.Nullable) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) PairFunction(com.intellij.util.PairFunction) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Project(com.intellij.openapi.project.Project) DumbAware(com.intellij.openapi.project.DumbAware) Comparator(java.util.Comparator) javax.swing(javax.swing) DefaultTableModel(javax.swing.table.DefaultTableModel) JBTable(com.intellij.ui.table.JBTable) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) Enumeration(java.util.Enumeration) MouseEvent(java.awt.event.MouseEvent) ColorUIResource(javax.swing.plaf.ColorUIResource) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) EventObject(java.util.EventObject) Project(com.intellij.openapi.project.Project) EventObject(java.util.EventObject) EmptyIcon(com.intellij.util.ui.EmptyIcon) Border(javax.swing.border.Border) Nullable(org.jetbrains.annotations.Nullable) JBScrollPane(com.intellij.ui.components.JBScrollPane)

Aggregations

DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)27 TableCellRenderer (javax.swing.table.TableCellRenderer)12 Component (java.awt.Component)10 TableColumn (javax.swing.table.TableColumn)10 JComponent (javax.swing.JComponent)7 DefaultTableModel (javax.swing.table.DefaultTableModel)7 Project (com.intellij.openapi.project.Project)3 JTable (javax.swing.JTable)3 JTableHeader (javax.swing.table.JTableHeader)3 JBTable (com.intellij.ui.table.JBTable)2 AbstractTableCellEditor (com.intellij.util.ui.AbstractTableCellEditor)2 java.awt (java.awt)2 MouseEvent (java.awt.event.MouseEvent)2 ArrayList (java.util.ArrayList)2 EventObject (java.util.EventObject)2 javax.swing (javax.swing)2 TableColumnModel (javax.swing.table.TableColumnModel)2 TableModel (javax.swing.table.TableModel)2 Nullable (org.jetbrains.annotations.Nullable)2 AttributesTableModel (com.android.tools.idea.editors.theme.attributes.AttributesTableModel)1