Example #1
0
 private static int getComboBoxBaselineResizeBehavior(JComboBox cb) {
   if (cb.isEditable()) {
     return getBaselineResizeBehavior(cb.getEditor().getEditorComponent());
   }
   ListCellRenderer renderer = cb.getRenderer();
   if (renderer == null) {
     if (brbListCellRenderer == null) {
       brbListCellRenderer = new DefaultListCellRenderer();
     }
     renderer = brbListCellRenderer;
   }
   Object value = null;
   Object prototypeValue = cb.getPrototypeDisplayValue();
   if (prototypeValue != null) {
     value = prototypeValue;
   } else if (cb.getModel().getSize() > 0) {
     value = cb.getModel().getElementAt(0);
   }
   if (value != null) {
     if (brbList == null) {
       brbList = new JList();
     }
     Component component = renderer.getListCellRendererComponent(brbList, value, -1, false, false);
     return getBaselineResizeBehavior(component);
   }
   return BRB_OTHER;
 }
 public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) {
   ListCellRenderer renderer = comboBox.getRenderer();
   Component c;
   Dimension d;
   c =
       renderer.getListCellRendererComponent(
           listBox, comboBox.getSelectedItem(), -1, false, false);
   c.setFont(comboBox.getFont());
   if (comboBox.isEnabled()) {
     c.setForeground(comboBox.getForeground());
     c.setBackground(comboBox.getBackground());
   } else {
     c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
     c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
   }
   d = c.getPreferredSize();
   currentValuePane.paintComponent(g, c, comboBox, bounds.x, bounds.y, bounds.width, d.height);
 }
    /**
     * Calculates the placement and size of the popup portion of the combo box based on the combo
     * box location and the enclosing screen bounds. If no transformations are required, then the
     * returned rectangle will have the same values as the parameters.
     *
     * <p>In addition to the superclass behavior, this class offers to use the combo's popup
     * prototype display value to compute the popup menu width. This is an optional feature of the
     * JGoodies Plastic L&amp;fs implemented via a client property key.
     *
     * <p>If a prototype is set, the popup width is the maximum of the combobox width and the
     * prototype based popup width. For the latter the renderer is used to render the prototype. The
     * prototype based popup width is the prototype's width plus the scrollbar width - if any. The
     * scrollbar test checks if there are more items than the combo's maximum row count.
     *
     * @param px starting x location
     * @param py starting y location
     * @param pw starting width
     * @param ph starting height
     * @return a rectangle which represents the placement and size of the popup
     * @see Options#COMBO_POPUP_PROTOTYPE_DISPLAY_VALUE_KEY
     * @see JComboBox#getMaximumRowCount()
     */
    protected Rectangle computePopupBounds(int px, int py, int pw, int ph) {
      Rectangle defaultBounds = super.computePopupBounds(px, py, pw, ph);
      Object popupPrototypeDisplayValue =
          comboBox.getClientProperty(Options.COMBO_POPUP_PROTOTYPE_DISPLAY_VALUE_KEY);
      if (popupPrototypeDisplayValue == null) {
        return defaultBounds;
      }

      ListCellRenderer renderer = list.getCellRenderer();
      Component c =
          renderer.getListCellRendererComponent(list, popupPrototypeDisplayValue, -1, true, true);
      pw = c.getPreferredSize().width;
      boolean hasVerticalScrollBar = comboBox.getItemCount() > comboBox.getMaximumRowCount();
      if (hasVerticalScrollBar) {
        // Add the scrollbar width.
        JScrollBar verticalBar = scroller.getVerticalScrollBar();
        pw += verticalBar.getPreferredSize().width;
      }
      Rectangle prototypeBasedBounds = super.computePopupBounds(px, py, pw, ph);
      return prototypeBasedBounds.width > defaultBounds.width
          ? prototypeBasedBounds
          : defaultBounds;
    }
  public final Component getListCellRendererComponent(
      final JList list,
      final Object value,
      final int index,
      final boolean isSelected,
      final boolean cellHasFocus) {
    mySeparator = false;
    myIcon = null;
    myText = null;
    myForeground = null;
    myBackground = null;
    myFont = null;
    myToolTipText = null;

    @SuppressWarnings("unchecked")
    final T t = (T) value;
    customize(list, t, index, isSelected, cellHasFocus);

    if (mySeparator) {
      final TitledSeparator separator = new TitledSeparator(myText);
      separator.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
      if (!UIUtil.isUnderGTKLookAndFeel()) {
        separator.setOpaque(false);
        separator.setBackground(UIUtil.TRANSPARENT_COLOR);
        separator.getLabel().setOpaque(false);
        separator.getLabel().setBackground(UIUtil.TRANSPARENT_COLOR);
      }
      return separator;
    }

    final Component component =
        myOriginalRenderer.getListCellRendererComponent(
            list, value, index, isSelected, cellHasFocus);
    if (component instanceof JLabel) {
      final JLabel label = (JLabel) component;
      label.setIcon(myIcon);
      if (myText != null) label.setText(myText);
      if (myForeground != null) label.setForeground(myForeground);
      if (myBackground != null && !isSelected) label.setBackground(myBackground);
      if (myFont != null) label.setFont(myFont);
      label.setToolTipText(myToolTipText);
    }
    return component;
  }
 public Component getListCellRendererComponent(
     JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
   String message;
   if (value == null) {
     message = ""; // NOI18N
   } else {
     assert value instanceof SourceLevelKey;
     SourceLevelKey key = (SourceLevelKey) value;
     if (key.isBroken()) {
       message =
           "<html><font color=\"#A40000\">" // NOI18N
               + NbBundle.getMessage(
                   PlatformUiSupport.class,
                   "TXT_InvalidSourceLevel", // NOI18N
                   key.getDisplayName());
     } else {
       message = key.getDisplayName();
     }
   }
   return delegate.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus);
 }