Search in sources :

Example 46 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project vcell by virtualcell.

the class InitialConditionsPanel method initialize.

/**
 * Initialize the class.
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
    try {
        // user code begin {1}
        // user code end
        setName("InitialConditionsPanel");
        setLayout(new BorderLayout());
        add(getRadioButtonAndCheckboxPanel(), BorderLayout.NORTH);
        add(getScrollPaneTable().getEnclosingScrollPane(), BorderLayout.CENTER);
        getScrollPaneTable().getSelectionModel().addListSelectionListener(ivjEventHandler);
        getJMenuItemPaste().addActionListener(ivjEventHandler);
        getJMenuItemCopy().addActionListener(ivjEventHandler);
        getJMenuItemCopyAll().addActionListener(ivjEventHandler);
        getJMenuItemPasteAll().addActionListener(ivjEventHandler);
        getAmountRadioButton().addActionListener(ivjEventHandler);
        getConcentrationRadioButton().addActionListener(ivjEventHandler);
        getRandomizeInitCondnCheckbox().addActionListener(ivjEventHandler);
        DefaultTableCellRenderer renderer = new DefaultScrollTableCellRenderer() {

            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                setIcon(null);
                defaultToolTipText = null;
                if (value instanceof Species) {
                    setText(((Species) value).getCommonName());
                    defaultToolTipText = getText();
                    setToolTipText(defaultToolTipText);
                } else if (value instanceof SpeciesContext) {
                    setText(((SpeciesContext) value).getName());
                    defaultToolTipText = getText();
                    setToolTipText(defaultToolTipText);
                } else if (value instanceof Structure) {
                    setText(((Structure) value).getName());
                    defaultToolTipText = getText();
                    setToolTipText(defaultToolTipText);
                } else if (value instanceof ScopedExpression) {
                    SpeciesContextSpec scSpec = tableModel.getValueAt(row);
                    VCUnitDefinition unit = null;
                    if (table.getColumnName(column).equals(SpeciesContextSpecsTableModel.ColumnType.COLUMN_INITIAL.label)) {
                        SpeciesContextSpecParameter initialConditionParameter = scSpec.getInitialConditionParameter();
                        unit = initialConditionParameter.getUnitDefinition();
                    } else if (table.getColumnName(column).equals(SpeciesContextSpecsTableModel.ColumnType.COLUMN_DIFFUSION.label)) {
                        SpeciesContextSpecParameter diffusionParameter = scSpec.getDiffusionParameter();
                        unit = diffusionParameter.getUnitDefinition();
                    }
                    if (unit != null) {
                        setHorizontalTextPosition(JLabel.LEFT);
                        setIcon(new TextIcon("[" + unit.getSymbolUnicode() + "]", DefaultScrollTableCellRenderer.uneditableForeground));
                    }
                    int rgb = 0x00ffffff & DefaultScrollTableCellRenderer.uneditableForeground.getRGB();
                    defaultToolTipText = "<html>" + StringEscapeUtils.escapeHtml4(getText()) + " <font color=#" + Integer.toHexString(rgb) + "> [" + unit.getSymbolUnicode() + "] </font></html>";
                    setToolTipText(defaultToolTipText);
                    if (unit != null) {
                        setText(defaultToolTipText);
                    }
                }
                TableModel tableModel = table.getModel();
                if (tableModel instanceof SortTableModel) {
                    DefaultScrollTableCellRenderer.issueRenderer(this, defaultToolTipText, table, row, column, (SortTableModel) tableModel);
                    setHorizontalTextPosition(JLabel.TRAILING);
                }
                return this;
            }
        };
        DefaultTableCellRenderer rbmSpeciesShapeDepictionCellRenderer = new DefaultScrollTableCellRenderer() {

            SpeciesPatternSmallShape spss = null;

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (table.getModel() instanceof VCellSortTableModel<?>) {
                    Object selectedObject = null;
                    if (table.getModel() == tableModel) {
                        selectedObject = tableModel.getValueAt(row);
                    }
                    if (selectedObject != null) {
                        if (selectedObject instanceof SpeciesContextSpec) {
                            SpeciesContextSpec scs = (SpeciesContextSpec) selectedObject;
                            SpeciesContext sc = scs.getSpeciesContext();
                            // sp may be null for "plain" species contexts
                            SpeciesPattern sp = sc.getSpeciesPattern();
                            Graphics panelContext = table.getGraphics();
                            spss = new SpeciesPatternSmallShape(4, 2, sp, shapeManager, panelContext, sc, isSelected, issueManager);
                        }
                    } else {
                        spss = null;
                    }
                }
                setText("");
                return this;
            }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (spss != null) {
                    spss.paintSelf(g);
                }
            }
        };
        DefaultScrollTableCellRenderer rulesTableCellRenderer = new DefaultScrollTableCellRenderer() {

            final Color lightBlueBackground = new Color(214, 234, 248);

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (table.getModel() instanceof SpeciesContextSpecsTableModel) {
                    Icon icon = VCellIcons.issueGoodIcon;
                    Object selectedObject = null;
                    if (table.getModel() == tableModel) {
                        selectedObject = tableModel.getValueAt(row);
                    }
                    if (selectedObject != null) {
                        if (isSelected) {
                            setBackground(lightBlueBackground);
                        }
                        if (selectedObject instanceof SpeciesContextSpec) {
                            SpeciesContextSpec scs = (SpeciesContextSpec) selectedObject;
                            SpeciesContext sc = scs.getSpeciesContext();
                            boolean foundRuleMatch = false;
                            if (fieldSimulationContext.getRateRules() != null && fieldSimulationContext.getRateRules().length > 0) {
                                for (RateRule rr : fieldSimulationContext.getRateRules()) {
                                    if (rr.getRateRuleVar() == null) {
                                        continue;
                                    }
                                    if (sc.getName().equals(rr.getRateRuleVar().getName())) {
                                        foundRuleMatch = true;
                                        icon = VCellIcons.ruleRateIcon;
                                        break;
                                    }
                                }
                            }
                            if (!foundRuleMatch && fieldSimulationContext.getAssignmentRules() != null && fieldSimulationContext.getAssignmentRules().length > 0) {
                                for (AssignmentRule rr : fieldSimulationContext.getAssignmentRules()) {
                                    if (rr.getAssignmentRuleVar() == null) {
                                        continue;
                                    }
                                    if (sc.getName().equals(rr.getAssignmentRuleVar().getName())) {
                                        icon = VCellIcons.ruleAssignIcon;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    setIcon(icon);
                }
                return this;
            }
        };
        getScrollPaneTable().setDefaultRenderer(SpeciesContext.class, renderer);
        getScrollPaneTable().setDefaultRenderer(Structure.class, renderer);
        // depiction icons
        getScrollPaneTable().setDefaultRenderer(SpeciesPattern.class, rbmSpeciesShapeDepictionCellRenderer);
        getScrollPaneTable().setDefaultRenderer(Species.class, renderer);
        getScrollPaneTable().setDefaultRenderer(ScopedExpression.class, renderer);
        getScrollPaneTable().setDefaultRenderer(Boolean.class, new ScrollTableBooleanCellRenderer());
        // rules icons
        getScrollPaneTable().setDefaultRenderer(SpeciesContextSpecsTableModel.RulesProvenance.class, rulesTableCellRenderer);
    // TODO: find out why the code below is not working properly
    // int ordinal = SpeciesContextSpecsTableModel.ColumnType.COLUMN_RULES.ordinal();
    // getScrollPaneTable().getColumnModel().getColumn(ordinal).setCellRenderer(rulesTableCellRenderer);
    // final int rulesWidth = 50;		// fixed max size, there's no point to enlarge some columns
    // int index = SpeciesContextSpecsTableModel.ColumnType.COLUMN_RULES.ordinal();
    // getScrollPaneTable().getColumnModel().getColumn(index).setPreferredWidth(rulesWidth);
    // getScrollPaneTable().getColumnModel().getColumn(index).setMaxWidth(rulesWidth);
    } catch (java.lang.Throwable ivjExc) {
        handleException(ivjExc);
    }
}
Also used : VCellSortTableModel(cbit.vcell.client.desktop.biomodel.VCellSortTableModel) SortTableModel(org.vcell.util.gui.sorttable.SortTableModel) SpeciesPatternSmallShape(cbit.vcell.graph.SpeciesPatternSmallShape) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) BorderLayout(java.awt.BorderLayout) VCellSortTableModel(cbit.vcell.client.desktop.biomodel.VCellSortTableModel) RateRule(cbit.vcell.mapping.RateRule) Structure(cbit.vcell.model.Structure) Species(cbit.vcell.model.Species) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) TextIcon(cbit.vcell.mapping.gui.StructureMappingTableRenderer.TextIcon) AssignmentRule(cbit.vcell.mapping.AssignmentRule) Color(java.awt.Color) ScrollTableBooleanCellRenderer(org.vcell.util.gui.ScrollTable.ScrollTableBooleanCellRenderer) Graphics(java.awt.Graphics) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) ScopedExpression(cbit.gui.ScopedExpression) JTable(javax.swing.JTable) DefaultScrollTableCellRenderer(org.vcell.util.gui.DefaultScrollTableCellRenderer) Icon(javax.swing.Icon) TextIcon(cbit.vcell.mapping.gui.StructureMappingTableRenderer.TextIcon) TableModel(javax.swing.table.TableModel) VCellSortTableModel(cbit.vcell.client.desktop.biomodel.VCellSortTableModel) SortTableModel(org.vcell.util.gui.sorttable.SortTableModel)

Example 47 with DefaultTableCellRenderer

use of javax.swing.table.DefaultTableCellRenderer in project android by JetBrains.

the class IconPickerDialogTest method tableToString.

@NotNull
public static String tableToString(@NotNull JTable table, int startRow, int endRow, int startColumn, int endColumn, int cellWidth) {
    StringBuilder sb = new StringBuilder();
    String formatString = "%-" + Integer.toString(cellWidth) + "s";
    for (int row = Math.max(0, startRow); row < Math.min(endRow, table.getRowCount()); row++) {
        for (int column = Math.max(0, startColumn); column < Math.min(table.getColumnCount(), endColumn); column++) {
            Object value = table.getValueAt(row, column);
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component component = renderer.getTableCellRendererComponent(table, value, false, false, row, column);
            DefaultTableCellRenderer label = (DefaultTableCellRenderer) component;
            assertThat(label.getText()).isEmpty();
            String cell = label.getAccessibleContext().getAccessibleName();
            if (cell.length() > cellWidth) {
                cell = cell.substring(0, cellWidth - 3) + "...";
            }
            sb.append(String.format(formatString, cell));
        }
        sb.append('\n');
    }
    return sb.toString();
}
Also used : DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with DefaultTableCellRenderer

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

the class SwingHelper method setNoBorderCellRendererFor.

public static void setNoBorderCellRendererFor(@NotNull TableColumn column) {
    final TableCellRenderer previous = column.getCellRenderer();
    column.setCellRenderer(new DefaultTableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component component;
            if (previous != null) {
                component = previous.getTableCellRendererComponent(table, value, isSelected, false, row, column);
            } else {
                component = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
            }
            if (component instanceof JComponent) {
                ((JComponent) component).setBorder(null);
            }
            return component;
        }
    });
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 49 with DefaultTableCellRenderer

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

the class ParameterTablePanel method init.

public void init(VariableData[] variableData, Project project, PsiElement... scopeElements) {
    super.init(variableData);
    myParameterTypeSelectors = new TypeSelector[getVariableData().length];
    for (int i = 0; i < myParameterTypeSelectors.length; i++) {
        final VariableData data = variableData[i];
        final TypeSelector selector = createSelector(project, data, scopeElements);
        myParameterTypeSelectors[i] = selector;
        //reverse order
        getVariableData()[i].type = myParameterTypeSelectors[i].getSelectedType();
    }
    myTypeRendererCombo = new ComboBox<>(getVariableData());
    myTypeRendererCombo.setOpaque(true);
    myTypeRendererCombo.setBorder(null);
    myTypeRendererCombo.setRenderer(new ListCellRendererWrapper<VariableData>() {

        @Override
        public void customize(JList list, VariableData value, int index, boolean selected, boolean hasFocus) {
            if (value != null) {
                setText(value.type.getPresentableText());
            }
        }
    });
    final TableColumn typeColumn = myTable.getColumnModel().getColumn(1);
    typeColumn.setCellEditor(new AbstractTableCellEditor() {

        TypeSelector myCurrentSelector;

        final JBComboBoxTableCellEditorComponent myEditorComponent = new JBComboBoxTableCellEditorComponent();

        final JLabel myTypeLabel = new JLabel();

        @Nullable
        public Object getCellEditorValue() {
            if (myCurrentSelector.getComponent() instanceof JLabel) {
                return myCurrentSelector.getSelectedType();
            }
            return myEditorComponent.getEditorValue();
        }

        public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
            myCurrentSelector = myParameterTypeSelectors[row];
            if (myParameterTypeSelectors[row].getComponent() instanceof JLabel) {
                PsiType selectedType = myCurrentSelector.getSelectedType();
                if (selectedType != null) {
                    myTypeLabel.setText(selectedType.getPresentableText());
                }
                return myTypeLabel;
            }
            myEditorComponent.setCell(table, row, column);
            myEditorComponent.setOptions(myCurrentSelector.getTypes());
            myEditorComponent.setDefaultValue(getVariableData()[row].type);
            myEditorComponent.setToString(o -> ((PsiType) o).getPresentableText());
            return myEditorComponent;
        }
    });
    myTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {

        private final JBComboBoxLabel myLabel = new JBComboBoxLabel();

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value != null) {
                myLabel.setText(((PsiType) value).getPresentableText());
                myLabel.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
                myLabel.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
                if (isSelected) {
                    myLabel.setSelectionIcon();
                } else {
                    myLabel.setRegularIcon();
                }
            }
            return myLabel;
        }
    });
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) AbstractTableCellEditor(com.intellij.util.ui.AbstractTableCellEditor) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl) TableColumn(javax.swing.table.TableColumn) Predicate(java.util.function.Predicate) TypeSelector(com.intellij.refactoring.ui.TypeSelector) ColumnInfo(com.intellij.util.ui.ColumnInfo) TypeSelectorManager(com.intellij.refactoring.ui.TypeSelectorManager) ListCellRendererWrapper(com.intellij.ui.ListCellRendererWrapper) LanguageNamesValidation(com.intellij.lang.LanguageNamesValidation) ArrayList(java.util.ArrayList) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JBComboBoxTableCellEditorComponent(com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent) JBComboBoxLabel(com.intellij.ui.components.JBComboBoxLabel) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) JavaLanguage(com.intellij.lang.java.JavaLanguage) javax.swing(javax.swing) JBComboBoxLabel(com.intellij.ui.components.JBComboBoxLabel) TableColumn(javax.swing.table.TableColumn) JBComboBoxTableCellEditorComponent(com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) AbstractTableCellEditor(com.intellij.util.ui.AbstractTableCellEditor) JBComboBoxTableCellEditorComponent(com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent) TypeSelector(com.intellij.refactoring.ui.TypeSelector) Nullable(org.jetbrains.annotations.Nullable)

Example 50 with DefaultTableCellRenderer

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

the class MiniTable method autoSize.

/**
	 *	Size Columns.
	 *  Uses Mimimum 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) JComponent(javax.swing.JComponent) TableColumn(javax.swing.table.TableColumn) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Aggregations

DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)85 JTable (javax.swing.JTable)30 TableColumn (javax.swing.table.TableColumn)27 Component (java.awt.Component)21 JScrollPane (javax.swing.JScrollPane)20 DefaultTableModel (javax.swing.table.DefaultTableModel)17 TableCellRenderer (javax.swing.table.TableCellRenderer)17 JLabel (javax.swing.JLabel)14 MouseEvent (java.awt.event.MouseEvent)12 JComponent (javax.swing.JComponent)12 Dimension (java.awt.Dimension)11 Font (java.awt.Font)11 JTableHeader (javax.swing.table.JTableHeader)11 BorderLayout (java.awt.BorderLayout)9 Color (java.awt.Color)9 GridBagConstraints (java.awt.GridBagConstraints)9 Insets (java.awt.Insets)9 JPanel (javax.swing.JPanel)9 GridBagLayout (java.awt.GridBagLayout)7 Point (java.awt.Point)7