Java Code Examples for sun.swing.DefaultLookup#getColor()

The following examples show how to use sun.swing.DefaultLookup#getColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicListUI.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void paintDropLine(Graphics g) {
    JList.DropLocation loc = list.getDropLocation();
    if (loc == null || !loc.isInsert()) {
        return;
    }

    Color c = DefaultLookup.getColor(list, this, "List.dropLineColor", null);
    if (c != null) {
        g.setColor(c);
        Rectangle rect = getDropLineRect(loc);
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    }
}
 
Example 2
Source File: BasicTextUI.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Updates the background of the text component based on whether the
 * text component is editable and/or enabled.
 *
 * @param c the JTextComponent that needs its background color updated
 */
private void updateBackground(JTextComponent c) {
    // This is a temporary workaround.
    // This code does not correctly deal with Synth (Synth doesn't use
    // properties like this), nor does it deal with the situation where
    // the developer grabs the color from a JLabel and sets it as
    // the background for a JTextArea in all look and feels. The problem
    // scenario results if the Color obtained for the Label and TextArea
    // is ==, which is the case for the windows look and feel.
    // Until an appropriate solution is found, the code is being
    // reverted to what it was before the original fix.
    if (this instanceof SynthUI || (c instanceof JTextArea)) {
        return;
    }
    Color background = c.getBackground();
    if (background instanceof UIResource) {
        String prefix = getPropertyPrefix();

        Color disabledBG =
            DefaultLookup.getColor(c, this, prefix + ".disabledBackground", null);
        Color inactiveBG =
            DefaultLookup.getColor(c, this, prefix + ".inactiveBackground", null);
        Color bg =
            DefaultLookup.getColor(c, this, prefix + ".background", null);

        /* In an ideal situation, the following check would not be necessary
         * and we would replace the color any time the previous color was a
         * UIResouce. However, it turns out that there is existing code that
         * uses the following inadvisable pattern to turn a text area into
         * what appears to be a multi-line label:
         *
         * JLabel label = new JLabel();
         * JTextArea area = new JTextArea();
         * area.setBackground(label.getBackground());
         * area.setEditable(false);
         *
         * JLabel's default background is a UIResource. As such, just
         * checking for UIResource would have us always changing the
         * background away from what the developer wanted.
         *
         * Therefore, for JTextArea/JEditorPane, we'll additionally check
         * that the color we're about to replace matches one that was
         * installed by us from the UIDefaults.
         */
        if ((c instanceof JTextArea || c instanceof JEditorPane)
                && background != disabledBG
                && background != inactiveBG
                && background != bg) {

            return;
        }

        Color newColor = null;
        if (!c.isEnabled()) {
            newColor = disabledBG;
        }
        if (newColor == null && !c.isEditable()) {
            newColor = inactiveBG;
        }
        if (newColor == null) {
            newColor = bg;
        }
        if (newColor != null && newColor != background) {
            c.setBackground(newColor);
        }
    }
}
 
Example 3
Source File: BasicTextUI.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Updates the background of the text component based on whether the
 * text component is editable and/or enabled.
 *
 * @param c the JTextComponent that needs its background color updated
 */
private void updateBackground(JTextComponent c) {
    // This is a temporary workaround.
    // This code does not correctly deal with Synth (Synth doesn't use
    // properties like this), nor does it deal with the situation where
    // the developer grabs the color from a JLabel and sets it as
    // the background for a JTextArea in all look and feels. The problem
    // scenario results if the Color obtained for the Label and TextArea
    // is ==, which is the case for the windows look and feel.
    // Until an appropriate solution is found, the code is being
    // reverted to what it was before the original fix.
    if (this instanceof SynthUI || (c instanceof JTextArea)) {
        return;
    }
    Color background = c.getBackground();
    if (background instanceof UIResource) {
        String prefix = getPropertyPrefix();

        Color disabledBG =
            DefaultLookup.getColor(c, this, prefix + ".disabledBackground", null);
        Color inactiveBG =
            DefaultLookup.getColor(c, this, prefix + ".inactiveBackground", null);
        Color bg =
            DefaultLookup.getColor(c, this, prefix + ".background", null);

        /* In an ideal situation, the following check would not be necessary
         * and we would replace the color any time the previous color was a
         * UIResouce. However, it turns out that there is existing code that
         * uses the following inadvisable pattern to turn a text area into
         * what appears to be a multi-line label:
         *
         * JLabel label = new JLabel();
         * JTextArea area = new JTextArea();
         * area.setBackground(label.getBackground());
         * area.setEditable(false);
         *
         * JLabel's default background is a UIResource. As such, just
         * checking for UIResource would have us always changing the
         * background away from what the developer wanted.
         *
         * Therefore, for JTextArea/JEditorPane, we'll additionally check
         * that the color we're about to replace matches one that was
         * installed by us from the UIDefaults.
         */
        if ((c instanceof JTextArea || c instanceof JEditorPane)
                && background != disabledBG
                && background != inactiveBG
                && background != bg) {

            return;
        }

        Color newColor = null;
        if (!c.isEnabled()) {
            newColor = disabledBG;
        }
        if (newColor == null && !c.isEditable()) {
            newColor = inactiveBG;
        }
        if (newColor == null) {
            newColor = bg;
        }
        if (newColor != null && newColor != background) {
            c.setBackground(newColor);
        }
    }
}
 
Example 4
Source File: BasicTextUI.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Updates the background of the text component based on whether the
 * text component is editable and/or enabled.
 *
 * @param c the JTextComponent that needs its background color updated
 */
private void updateBackground(JTextComponent c) {
    // This is a temporary workaround.
    // This code does not correctly deal with Synth (Synth doesn't use
    // properties like this), nor does it deal with the situation where
    // the developer grabs the color from a JLabel and sets it as
    // the background for a JTextArea in all look and feels. The problem
    // scenario results if the Color obtained for the Label and TextArea
    // is ==, which is the case for the windows look and feel.
    // Until an appropriate solution is found, the code is being
    // reverted to what it was before the original fix.
    if (this instanceof SynthUI || (c instanceof JTextArea)) {
        return;
    }
    Color background = c.getBackground();
    if (background instanceof UIResource) {
        String prefix = getPropertyPrefix();

        Color disabledBG =
            DefaultLookup.getColor(c, this, prefix + ".disabledBackground", null);
        Color inactiveBG =
            DefaultLookup.getColor(c, this, prefix + ".inactiveBackground", null);
        Color bg =
            DefaultLookup.getColor(c, this, prefix + ".background", null);

        /* In an ideal situation, the following check would not be necessary
         * and we would replace the color any time the previous color was a
         * UIResouce. However, it turns out that there is existing code that
         * uses the following inadvisable pattern to turn a text area into
         * what appears to be a multi-line label:
         *
         * JLabel label = new JLabel();
         * JTextArea area = new JTextArea();
         * area.setBackground(label.getBackground());
         * area.setEditable(false);
         *
         * JLabel's default background is a UIResource. As such, just
         * checking for UIResource would have us always changing the
         * background away from what the developer wanted.
         *
         * Therefore, for JTextArea/JEditorPane, we'll additionally check
         * that the color we're about to replace matches one that was
         * installed by us from the UIDefaults.
         */
        if ((c instanceof JTextArea || c instanceof JEditorPane)
                && background != disabledBG
                && background != inactiveBG
                && background != bg) {

            return;
        }

        Color newColor = null;
        if (!c.isEnabled()) {
            newColor = disabledBG;
        }
        if (newColor == null && !c.isEditable()) {
            newColor = inactiveBG;
        }
        if (newColor == null) {
            newColor = bg;
        }
        if (newColor != null && newColor != background) {
            c.setBackground(newColor);
        }
    }
}
 
Example 5
Source File: DefaultListCellRenderer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public Component getListCellRendererComponent(
    JList<?> list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus)
{
    setComponentOrientation(list.getComponentOrientation());

    Color bg = null;
    Color fg = null;

    JList.DropLocation dropLocation = list.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsert()
            && dropLocation.getIndex() == index) {

        bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");
        fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");

        isSelected = true;
    }

    if (isSelected) {
        setBackground(bg == null ? list.getSelectionBackground() : bg);
        setForeground(fg == null ? list.getSelectionForeground() : fg);
    }
    else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }

    if (value instanceof Icon) {
        setIcon((Icon)value);
        setText("");
    }
    else {
        setIcon(null);
        setText((value == null) ? "" : value.toString());
    }

    setEnabled(list.isEnabled());
    setFont(list.getFont());

    Border border = null;
    if (cellHasFocus) {
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
        }
    } else {
        border = getNoFocusBorder();
    }
    setBorder(border);

    return this;
}
 
Example 6
Source File: DefaultTreeCellRenderer.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
  * Configures the renderer based on the passed in components.
  * The value is set from messaging the tree with
  * <code>convertValueToText</code>, which ultimately invokes
  * <code>toString</code> on <code>value</code>.
  * The foreground color is set based on the selection and the icon
  * is set based on the <code>leaf</code> and <code>expanded</code>
  * parameters.
  */
public Component getTreeCellRendererComponent(JTree tree, Object value,
                                              boolean sel,
                                              boolean expanded,
                                              boolean leaf, int row,
                                              boolean hasFocus) {
    String         stringValue = tree.convertValueToText(value, sel,
                                      expanded, leaf, row, hasFocus);

    this.tree = tree;
    this.hasFocus = hasFocus;
    setText(stringValue);

    Color fg = null;
    isDropCell = false;

    JTree.DropLocation dropLocation = tree.getDropLocation();
    if (dropLocation != null
            && dropLocation.getChildIndex() == -1
            && tree.getRowForPath(dropLocation.getPath()) == row) {

        Color col = DefaultLookup.getColor(this, ui, "Tree.dropCellForeground");
        if (col != null) {
            fg = col;
        } else {
            fg = getTextSelectionColor();
        }

        isDropCell = true;
    } else if (sel) {
        fg = getTextSelectionColor();
    } else {
        fg = getTextNonSelectionColor();
    }

    setForeground(fg);

    Icon icon = null;
    if (leaf) {
        icon = getLeafIcon();
    } else if (expanded) {
        icon = getOpenIcon();
    } else {
        icon = getClosedIcon();
    }

    if (!tree.isEnabled()) {
        setEnabled(false);
        LookAndFeel laf = UIManager.getLookAndFeel();
        Icon disabledIcon = laf.getDisabledIcon(tree, icon);
        if (disabledIcon != null) icon = disabledIcon;
        setDisabledIcon(icon);
    } else {
        setEnabled(true);
        setIcon(icon);
    }
    setComponentOrientation(tree.getComponentOrientation());

    selected = sel;

    return this;
}
 
Example 7
Source File: DefaultTableCellRenderer.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * Returns the default table cell renderer.
 * <p>
 * During a printing operation, this method will be called with
 * <code>isSelected</code> and <code>hasFocus</code> values of
 * <code>false</code> to prevent selection and focus from appearing
 * in the printed output. To do other customization based on whether
 * or not the table is being printed, check the return value from
 * {@link javax.swing.JComponent#isPaintingForPrint()}.
 *
 * @param table  the <code>JTable</code>
 * @param value  the value to assign to the cell at
 *                  <code>[row, column]</code>
 * @param isSelected true if cell is selected
 * @param hasFocus true if cell has focus
 * @param row  the row of the cell to render
 * @param column the column of the cell to render
 * @return the default table cell renderer
 * @see javax.swing.JComponent#isPaintingForPrint()
 */
public Component getTableCellRendererComponent(JTable table, Object value,
                      boolean isSelected, boolean hasFocus, int row, int column) {
    if (table == null) {
        return this;
    }

    Color fg = null;
    Color bg = null;

    JTable.DropLocation dropLocation = table.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsertRow()
            && !dropLocation.isInsertColumn()
            && dropLocation.getRow() == row
            && dropLocation.getColumn() == column) {

        fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
        bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");

        isSelected = true;
    }

    if (isSelected) {
        super.setForeground(fg == null ? table.getSelectionForeground()
                                       : fg);
        super.setBackground(bg == null ? table.getSelectionBackground()
                                       : bg);
    } else {
        Color background = unselectedBackground != null
                                ? unselectedBackground
                                : table.getBackground();
        if (background == null || background instanceof javax.swing.plaf.UIResource) {
            Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
            if (alternateColor != null && row % 2 != 0) {
                background = alternateColor;
            }
        }
        super.setForeground(unselectedForeground != null
                                ? unselectedForeground
                                : table.getForeground());
        super.setBackground(background);
    }

    setFont(table.getFont());

    if (hasFocus) {
        Border border = null;
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
        }
        setBorder(border);

        if (!isSelected && table.isCellEditable(row, column)) {
            Color col;
            col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
            if (col != null) {
                super.setForeground(col);
            }
            col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
            if (col != null) {
                super.setBackground(col);
            }
        }
    } else {
        setBorder(getNoFocusBorder());
    }

    setValue(value);

    return this;
}
 
Example 8
Source File: DefaultTableCellHeaderRenderer.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    Icon sortIcon = null;

    boolean isPaintingForPrint = false;

    if (table != null) {
        JTableHeader header = table.getTableHeader();
        if (header != null) {
            Color fgColor = null;
            Color bgColor = null;
            if (hasFocus) {
                fgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellForeground");
                bgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellBackground");
            }
            if (fgColor == null) {
                fgColor = header.getForeground();
            }
            if (bgColor == null) {
                bgColor = header.getBackground();
            }
            setForeground(fgColor);
            setBackground(bgColor);

            setFont(header.getFont());

            isPaintingForPrint = header.isPaintingForPrint();
        }

        if (!isPaintingForPrint && table.getRowSorter() != null) {
            if (!horizontalTextPositionSet) {
                // There is a row sorter, and the developer hasn't
                // set a text position, change to leading.
                setHorizontalTextPosition(JLabel.LEADING);
            }
            SortOrder sortOrder = getColumnSortOrder(table, column);
            if (sortOrder != null) {
                switch(sortOrder) {
                case ASCENDING:
                    sortIcon = DefaultLookup.getIcon(
                        this, ui, "Table.ascendingSortIcon");
                    break;
                case DESCENDING:
                    sortIcon = DefaultLookup.getIcon(
                        this, ui, "Table.descendingSortIcon");
                    break;
                case UNSORTED:
                    sortIcon = DefaultLookup.getIcon(
                        this, ui, "Table.naturalSortIcon");
                    break;
                }
            }
        }
    }

    setText(value == null ? "" : value.toString());
    setIcon(sortIcon);
    sortArrow = sortIcon;

    Border border = null;
    if (hasFocus) {
        border = DefaultLookup.getBorder(this, ui, "TableHeader.focusCellBorder");
    }
    if (border == null) {
        border = DefaultLookup.getBorder(this, ui, "TableHeader.cellBorder");
    }
    setBorder(border);

    return this;
}
 
Example 9
Source File: DefaultTableCellRenderer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * Returns the default table cell renderer.
 * <p>
 * During a printing operation, this method will be called with
 * <code>isSelected</code> and <code>hasFocus</code> values of
 * <code>false</code> to prevent selection and focus from appearing
 * in the printed output. To do other customization based on whether
 * or not the table is being printed, check the return value from
 * {@link javax.swing.JComponent#isPaintingForPrint()}.
 *
 * @param table  the <code>JTable</code>
 * @param value  the value to assign to the cell at
 *                  <code>[row, column]</code>
 * @param isSelected true if cell is selected
 * @param hasFocus true if cell has focus
 * @param row  the row of the cell to render
 * @param column the column of the cell to render
 * @return the default table cell renderer
 * @see javax.swing.JComponent#isPaintingForPrint()
 */
public Component getTableCellRendererComponent(JTable table, Object value,
                      boolean isSelected, boolean hasFocus, int row, int column) {
    if (table == null) {
        return this;
    }

    Color fg = null;
    Color bg = null;

    JTable.DropLocation dropLocation = table.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsertRow()
            && !dropLocation.isInsertColumn()
            && dropLocation.getRow() == row
            && dropLocation.getColumn() == column) {

        fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
        bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");

        isSelected = true;
    }

    if (isSelected) {
        super.setForeground(fg == null ? table.getSelectionForeground()
                                       : fg);
        super.setBackground(bg == null ? table.getSelectionBackground()
                                       : bg);
    } else {
        Color background = unselectedBackground != null
                                ? unselectedBackground
                                : table.getBackground();
        if (background == null || background instanceof javax.swing.plaf.UIResource) {
            Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
            if (alternateColor != null && row % 2 != 0) {
                background = alternateColor;
            }
        }
        super.setForeground(unselectedForeground != null
                                ? unselectedForeground
                                : table.getForeground());
        super.setBackground(background);
    }

    setFont(table.getFont());

    if (hasFocus) {
        Border border = null;
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
        }
        setBorder(border);

        if (!isSelected && table.isCellEditable(row, column)) {
            Color col;
            col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
            if (col != null) {
                super.setForeground(col);
            }
            col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
            if (col != null) {
                super.setBackground(col);
            }
        }
    } else {
        setBorder(getNoFocusBorder());
    }

    setValue(value);

    return this;
}
 
Example 10
Source File: DefaultListCellRenderer.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
public Component getListCellRendererComponent(
    JList<?> list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus)
{
    setComponentOrientation(list.getComponentOrientation());

    Color bg = null;
    Color fg = null;

    JList.DropLocation dropLocation = list.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsert()
            && dropLocation.getIndex() == index) {

        bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");
        fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");

        isSelected = true;
    }

    if (isSelected) {
        setBackground(bg == null ? list.getSelectionBackground() : bg);
        setForeground(fg == null ? list.getSelectionForeground() : fg);
    }
    else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }

    if (value instanceof Icon) {
        setIcon((Icon)value);
        setText("");
    }
    else {
        setIcon(null);
        setText((value == null) ? "" : value.toString());
    }

    setEnabled(list.isEnabled());
    setFont(list.getFont());

    Border border = null;
    if (cellHasFocus) {
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
        }
    } else {
        border = getNoFocusBorder();
    }
    setBorder(border);

    return this;
}
 
Example 11
Source File: DefaultTableCellRenderer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * Returns the default table cell renderer.
 * <p>
 * During a printing operation, this method will be called with
 * <code>isSelected</code> and <code>hasFocus</code> values of
 * <code>false</code> to prevent selection and focus from appearing
 * in the printed output. To do other customization based on whether
 * or not the table is being printed, check the return value from
 * {@link javax.swing.JComponent#isPaintingForPrint()}.
 *
 * @param table  the <code>JTable</code>
 * @param value  the value to assign to the cell at
 *                  <code>[row, column]</code>
 * @param isSelected true if cell is selected
 * @param hasFocus true if cell has focus
 * @param row  the row of the cell to render
 * @param column the column of the cell to render
 * @return the default table cell renderer
 * @see javax.swing.JComponent#isPaintingForPrint()
 */
public Component getTableCellRendererComponent(JTable table, Object value,
                      boolean isSelected, boolean hasFocus, int row, int column) {
    if (table == null) {
        return this;
    }

    Color fg = null;
    Color bg = null;

    JTable.DropLocation dropLocation = table.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsertRow()
            && !dropLocation.isInsertColumn()
            && dropLocation.getRow() == row
            && dropLocation.getColumn() == column) {

        fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
        bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");

        isSelected = true;
    }

    if (isSelected) {
        super.setForeground(fg == null ? table.getSelectionForeground()
                                       : fg);
        super.setBackground(bg == null ? table.getSelectionBackground()
                                       : bg);
    } else {
        Color background = unselectedBackground != null
                                ? unselectedBackground
                                : table.getBackground();
        if (background == null || background instanceof javax.swing.plaf.UIResource) {
            Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
            if (alternateColor != null && row % 2 != 0) {
                background = alternateColor;
            }
        }
        super.setForeground(unselectedForeground != null
                                ? unselectedForeground
                                : table.getForeground());
        super.setBackground(background);
    }

    setFont(table.getFont());

    if (hasFocus) {
        Border border = null;
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
        }
        setBorder(border);

        if (!isSelected && table.isCellEditable(row, column)) {
            Color col;
            col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
            if (col != null) {
                super.setForeground(col);
            }
            col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
            if (col != null) {
                super.setBackground(col);
            }
        }
    } else {
        setBorder(getNoFocusBorder());
    }

    setValue(value);

    return this;
}
 
Example 12
Source File: DefaultTreeCellRenderer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
  * Paints the value.  The background is filled based on selected.
  */
public void paint(Graphics g) {
    Color bColor;

    if (isDropCell) {
        bColor = DefaultLookup.getColor(this, ui, "Tree.dropCellBackground");
        if (bColor == null) {
            bColor = getBackgroundSelectionColor();
        }
    } else if (selected) {
        bColor = getBackgroundSelectionColor();
    } else {
        bColor = getBackgroundNonSelectionColor();
        if (bColor == null) {
            bColor = getBackground();
        }
    }

    int imageOffset = -1;
    if (bColor != null && fillBackground) {
        imageOffset = getLabelStart();
        g.setColor(bColor);
        if(getComponentOrientation().isLeftToRight()) {
            g.fillRect(imageOffset, 0, getWidth() - imageOffset,
                       getHeight());
        } else {
            g.fillRect(0, 0, getWidth() - imageOffset,
                       getHeight());
        }
    }

    if (hasFocus) {
        if (drawsFocusBorderAroundIcon) {
            imageOffset = 0;
        }
        else if (imageOffset == -1) {
            imageOffset = getLabelStart();
        }
        if(getComponentOrientation().isLeftToRight()) {
            paintFocus(g, imageOffset, 0, getWidth() - imageOffset,
                       getHeight(), bColor);
        } else {
            paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight(), bColor);
        }
    }
    super.paint(g);
}
 
Example 13
Source File: DefaultTreeCellRenderer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
  * Configures the renderer based on the passed in components.
  * The value is set from messaging the tree with
  * <code>convertValueToText</code>, which ultimately invokes
  * <code>toString</code> on <code>value</code>.
  * The foreground color is set based on the selection and the icon
  * is set based on the <code>leaf</code> and <code>expanded</code>
  * parameters.
  */
public Component getTreeCellRendererComponent(JTree tree, Object value,
                                              boolean sel,
                                              boolean expanded,
                                              boolean leaf, int row,
                                              boolean hasFocus) {
    String         stringValue = tree.convertValueToText(value, sel,
                                      expanded, leaf, row, hasFocus);

    this.tree = tree;
    this.hasFocus = hasFocus;
    setText(stringValue);

    Color fg = null;
    isDropCell = false;

    JTree.DropLocation dropLocation = tree.getDropLocation();
    if (dropLocation != null
            && dropLocation.getChildIndex() == -1
            && tree.getRowForPath(dropLocation.getPath()) == row) {

        Color col = DefaultLookup.getColor(this, ui, "Tree.dropCellForeground");
        if (col != null) {
            fg = col;
        } else {
            fg = getTextSelectionColor();
        }

        isDropCell = true;
    } else if (sel) {
        fg = getTextSelectionColor();
    } else {
        fg = getTextNonSelectionColor();
    }

    setForeground(fg);

    Icon icon = null;
    if (leaf) {
        icon = getLeafIcon();
    } else if (expanded) {
        icon = getOpenIcon();
    } else {
        icon = getClosedIcon();
    }

    if (!tree.isEnabled()) {
        setEnabled(false);
        LookAndFeel laf = UIManager.getLookAndFeel();
        Icon disabledIcon = laf.getDisabledIcon(tree, icon);
        if (disabledIcon != null) icon = disabledIcon;
        setDisabledIcon(icon);
    } else {
        setEnabled(true);
        setIcon(icon);
    }
    setComponentOrientation(tree.getComponentOrientation());

    selected = sel;

    return this;
}
 
Example 14
Source File: DefaultTableCellHeaderRenderer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    Icon sortIcon = null;

    boolean isPaintingForPrint = false;

    if (table != null) {
        JTableHeader header = table.getTableHeader();
        if (header != null) {
            Color fgColor = null;
            Color bgColor = null;
            if (hasFocus) {
                fgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellForeground");
                bgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellBackground");
            }
            if (fgColor == null) {
                fgColor = header.getForeground();
            }
            if (bgColor == null) {
                bgColor = header.getBackground();
            }
            setForeground(fgColor);
            setBackground(bgColor);

            setFont(header.getFont());

            isPaintingForPrint = header.isPaintingForPrint();
        }

        if (!isPaintingForPrint && table.getRowSorter() != null) {
            if (!horizontalTextPositionSet) {
                // There is a row sorter, and the developer hasn't
                // set a text position, change to leading.
                setHorizontalTextPosition(JLabel.LEADING);
            }
            SortOrder sortOrder = getColumnSortOrder(table, column);
            if (sortOrder != null) {
                switch(sortOrder) {
                case ASCENDING:
                    sortIcon = DefaultLookup.getIcon(
                        this, ui, "Table.ascendingSortIcon");
                    break;
                case DESCENDING:
                    sortIcon = DefaultLookup.getIcon(
                        this, ui, "Table.descendingSortIcon");
                    break;
                case UNSORTED:
                    sortIcon = DefaultLookup.getIcon(
                        this, ui, "Table.naturalSortIcon");
                    break;
                }
            }
        }
    }

    setText(value == null ? "" : value.toString());
    setIcon(sortIcon);
    sortArrow = sortIcon;

    Border border = null;
    if (hasFocus) {
        border = DefaultLookup.getBorder(this, ui, "TableHeader.focusCellBorder");
    }
    if (border == null) {
        border = DefaultLookup.getBorder(this, ui, "TableHeader.cellBorder");
    }
    setBorder(border);

    return this;
}
 
Example 15
Source File: DefaultTreeCellRenderer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
  * Paints the value.  The background is filled based on selected.
  */
public void paint(Graphics g) {
    Color bColor;

    if (isDropCell) {
        bColor = DefaultLookup.getColor(this, ui, "Tree.dropCellBackground");
        if (bColor == null) {
            bColor = getBackgroundSelectionColor();
        }
    } else if (selected) {
        bColor = getBackgroundSelectionColor();
    } else {
        bColor = getBackgroundNonSelectionColor();
        if (bColor == null) {
            bColor = getBackground();
        }
    }

    int imageOffset = -1;
    if (bColor != null && fillBackground) {
        imageOffset = getLabelStart();
        g.setColor(bColor);
        if(getComponentOrientation().isLeftToRight()) {
            g.fillRect(imageOffset, 0, getWidth() - imageOffset,
                       getHeight());
        } else {
            g.fillRect(0, 0, getWidth() - imageOffset,
                       getHeight());
        }
    }

    if (hasFocus) {
        if (drawsFocusBorderAroundIcon) {
            imageOffset = 0;
        }
        else if (imageOffset == -1) {
            imageOffset = getLabelStart();
        }
        if(getComponentOrientation().isLeftToRight()) {
            paintFocus(g, imageOffset, 0, getWidth() - imageOffset,
                       getHeight(), bColor);
        } else {
            paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight(), bColor);
        }
    }
    super.paint(g);
}
 
Example 16
Source File: DefaultTableCellRenderer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * Returns the default table cell renderer.
 * <p>
 * During a printing operation, this method will be called with
 * <code>isSelected</code> and <code>hasFocus</code> values of
 * <code>false</code> to prevent selection and focus from appearing
 * in the printed output. To do other customization based on whether
 * or not the table is being printed, check the return value from
 * {@link javax.swing.JComponent#isPaintingForPrint()}.
 *
 * @param table  the <code>JTable</code>
 * @param value  the value to assign to the cell at
 *                  <code>[row, column]</code>
 * @param isSelected true if cell is selected
 * @param hasFocus true if cell has focus
 * @param row  the row of the cell to render
 * @param column the column of the cell to render
 * @return the default table cell renderer
 * @see javax.swing.JComponent#isPaintingForPrint()
 */
public Component getTableCellRendererComponent(JTable table, Object value,
                      boolean isSelected, boolean hasFocus, int row, int column) {
    if (table == null) {
        return this;
    }

    Color fg = null;
    Color bg = null;

    JTable.DropLocation dropLocation = table.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsertRow()
            && !dropLocation.isInsertColumn()
            && dropLocation.getRow() == row
            && dropLocation.getColumn() == column) {

        fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
        bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");

        isSelected = true;
    }

    if (isSelected) {
        super.setForeground(fg == null ? table.getSelectionForeground()
                                       : fg);
        super.setBackground(bg == null ? table.getSelectionBackground()
                                       : bg);
    } else {
        Color background = unselectedBackground != null
                                ? unselectedBackground
                                : table.getBackground();
        if (background == null || background instanceof javax.swing.plaf.UIResource) {
            Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
            if (alternateColor != null && row % 2 != 0) {
                background = alternateColor;
            }
        }
        super.setForeground(unselectedForeground != null
                                ? unselectedForeground
                                : table.getForeground());
        super.setBackground(background);
    }

    setFont(table.getFont());

    if (hasFocus) {
        Border border = null;
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
        }
        setBorder(border);

        if (!isSelected && table.isCellEditable(row, column)) {
            Color col;
            col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
            if (col != null) {
                super.setForeground(col);
            }
            col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
            if (col != null) {
                super.setBackground(col);
            }
        }
    } else {
        setBorder(getNoFocusBorder());
    }

    setValue(value);

    return this;
}
 
Example 17
Source File: DefaultTreeCellRenderer.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
  * Configures the renderer based on the passed in components.
  * The value is set from messaging the tree with
  * <code>convertValueToText</code>, which ultimately invokes
  * <code>toString</code> on <code>value</code>.
  * The foreground color is set based on the selection and the icon
  * is set based on the <code>leaf</code> and <code>expanded</code>
  * parameters.
  */
public Component getTreeCellRendererComponent(JTree tree, Object value,
                                              boolean sel,
                                              boolean expanded,
                                              boolean leaf, int row,
                                              boolean hasFocus) {
    String         stringValue = tree.convertValueToText(value, sel,
                                      expanded, leaf, row, hasFocus);

    this.tree = tree;
    this.hasFocus = hasFocus;
    setText(stringValue);

    Color fg = null;
    isDropCell = false;

    JTree.DropLocation dropLocation = tree.getDropLocation();
    if (dropLocation != null
            && dropLocation.getChildIndex() == -1
            && tree.getRowForPath(dropLocation.getPath()) == row) {

        Color col = DefaultLookup.getColor(this, ui, "Tree.dropCellForeground");
        if (col != null) {
            fg = col;
        } else {
            fg = getTextSelectionColor();
        }

        isDropCell = true;
    } else if (sel) {
        fg = getTextSelectionColor();
    } else {
        fg = getTextNonSelectionColor();
    }

    setForeground(fg);

    Icon icon = null;
    if (leaf) {
        icon = getLeafIcon();
    } else if (expanded) {
        icon = getOpenIcon();
    } else {
        icon = getClosedIcon();
    }

    if (!tree.isEnabled()) {
        setEnabled(false);
        LookAndFeel laf = UIManager.getLookAndFeel();
        Icon disabledIcon = laf.getDisabledIcon(tree, icon);
        if (disabledIcon != null) icon = disabledIcon;
        setDisabledIcon(icon);
    } else {
        setEnabled(true);
        setIcon(icon);
    }
    setComponentOrientation(tree.getComponentOrientation());

    selected = sel;

    return this;
}
 
Example 18
Source File: BasicTextUI.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the background of the text component based on whether the
 * text component is editable and/or enabled.
 *
 * @param c the JTextComponent that needs its background color updated
 */
private void updateBackground(JTextComponent c) {
    // This is a temporary workaround.
    // This code does not correctly deal with Synth (Synth doesn't use
    // properties like this), nor does it deal with the situation where
    // the developer grabs the color from a JLabel and sets it as
    // the background for a JTextArea in all look and feels. The problem
    // scenario results if the Color obtained for the Label and TextArea
    // is ==, which is the case for the windows look and feel.
    // Until an appropriate solution is found, the code is being
    // reverted to what it was before the original fix.
    if (this instanceof SynthUI || (c instanceof JTextArea)) {
        return;
    }
    Color background = c.getBackground();
    if (background instanceof UIResource) {
        String prefix = getPropertyPrefix();

        Color disabledBG =
            DefaultLookup.getColor(c, this, prefix + ".disabledBackground", null);
        Color inactiveBG =
            DefaultLookup.getColor(c, this, prefix + ".inactiveBackground", null);
        Color bg =
            DefaultLookup.getColor(c, this, prefix + ".background", null);

        /* In an ideal situation, the following check would not be necessary
         * and we would replace the color any time the previous color was a
         * UIResouce. However, it turns out that there is existing code that
         * uses the following inadvisable pattern to turn a text area into
         * what appears to be a multi-line label:
         *
         * JLabel label = new JLabel();
         * JTextArea area = new JTextArea();
         * area.setBackground(label.getBackground());
         * area.setEditable(false);
         *
         * JLabel's default background is a UIResource. As such, just
         * checking for UIResource would have us always changing the
         * background away from what the developer wanted.
         *
         * Therefore, for JTextArea/JEditorPane, we'll additionally check
         * that the color we're about to replace matches one that was
         * installed by us from the UIDefaults.
         */
        if ((c instanceof JTextArea || c instanceof JEditorPane)
                && background != disabledBG
                && background != inactiveBG
                && background != bg) {

            return;
        }

        Color newColor = null;
        if (!c.isEnabled()) {
            newColor = disabledBG;
        }
        if (newColor == null && !c.isEditable()) {
            newColor = inactiveBG;
        }
        if (newColor == null) {
            newColor = bg;
        }
        if (newColor != null && newColor != background) {
            c.setBackground(newColor);
        }
    }
}
 
Example 19
Source File: BasicTextUI.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the background of the text component based on whether the
 * text component is editable and/or enabled.
 *
 * @param c the JTextComponent that needs its background color updated
 */
private void updateBackground(JTextComponent c) {
    // This is a temporary workaround.
    // This code does not correctly deal with Synth (Synth doesn't use
    // properties like this), nor does it deal with the situation where
    // the developer grabs the color from a JLabel and sets it as
    // the background for a JTextArea in all look and feels. The problem
    // scenario results if the Color obtained for the Label and TextArea
    // is ==, which is the case for the windows look and feel.
    // Until an appropriate solution is found, the code is being
    // reverted to what it was before the original fix.
    if (this instanceof SynthUI || (c instanceof JTextArea)) {
        return;
    }
    Color background = c.getBackground();
    if (background instanceof UIResource) {
        String prefix = getPropertyPrefix();

        Color disabledBG =
            DefaultLookup.getColor(c, this, prefix + ".disabledBackground", null);
        Color inactiveBG =
            DefaultLookup.getColor(c, this, prefix + ".inactiveBackground", null);
        Color bg =
            DefaultLookup.getColor(c, this, prefix + ".background", null);

        /* In an ideal situation, the following check would not be necessary
         * and we would replace the color any time the previous color was a
         * UIResouce. However, it turns out that there is existing code that
         * uses the following inadvisable pattern to turn a text area into
         * what appears to be a multi-line label:
         *
         * JLabel label = new JLabel();
         * JTextArea area = new JTextArea();
         * area.setBackground(label.getBackground());
         * area.setEditable(false);
         *
         * JLabel's default background is a UIResource. As such, just
         * checking for UIResource would have us always changing the
         * background away from what the developer wanted.
         *
         * Therefore, for JTextArea/JEditorPane, we'll additionally check
         * that the color we're about to replace matches one that was
         * installed by us from the UIDefaults.
         */
        if ((c instanceof JTextArea || c instanceof JEditorPane)
                && background != disabledBG
                && background != inactiveBG
                && background != bg) {

            return;
        }

        Color newColor = null;
        if (!c.isEnabled()) {
            newColor = disabledBG;
        }
        if (newColor == null && !c.isEditable()) {
            newColor = inactiveBG;
        }
        if (newColor == null) {
            newColor = bg;
        }
        if (newColor != null && newColor != background) {
            c.setBackground(newColor);
        }
    }
}
 
Example 20
Source File: DefaultListCellRenderer.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public Component getListCellRendererComponent(
    JList<?> list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus)
{
    setComponentOrientation(list.getComponentOrientation());

    Color bg = null;
    Color fg = null;

    JList.DropLocation dropLocation = list.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsert()
            && dropLocation.getIndex() == index) {

        bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");
        fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");

        isSelected = true;
    }

    if (isSelected) {
        setBackground(bg == null ? list.getSelectionBackground() : bg);
        setForeground(fg == null ? list.getSelectionForeground() : fg);
    }
    else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }

    if (value instanceof Icon) {
        setIcon((Icon)value);
        setText("");
    }
    else {
        setIcon(null);
        setText((value == null) ? "" : value.toString());
    }

    setEnabled(list.isEnabled());
    setFont(list.getFont());

    Border border = null;
    if (cellHasFocus) {
        if (isSelected) {
            border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
            border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
        }
    } else {
        border = getNoFocusBorder();
    }
    setBorder(border);

    return this;
}