/** * Returns the renderer. * * @param list The list of choices being rendered. * @param value The {@link Completion} being rendered. * @param index The index into <code>list</code> being rendered. * @param selected Whether the item is selected. * @param hasFocus Whether the item has focus. */ public Component getListCellRendererComponent( JList list, Object value, int index, boolean selected, boolean hasFocus) { super.getListCellRendererComponent(list, value, index, selected, hasFocus); setText("Foobar"); // Just something to give it proper height this.list = list; this.selected = selected; if (value instanceof JavaSourceCompletion) { jsc = (JavaSourceCompletion) value; nonJavaCompletion = null; setIcon(jsc.getIcon()); } else { jsc = null; nonJavaCompletion = (Completion) value; setIcon(null); // TODO: emptyIcon } evenRow = (index & 1) == 0; if (altBG != null && evenRow && !selected) { setBackground(altBG); } return this; }
protected void paintComponent(Graphics g) { // Set up rendering hints to look as close to native as possible Graphics2D g2d = (Graphics2D) g; Object old = null; // First, try to use the rendering hint set that is "native". Map hints = (Map) getToolkit().getDesktopProperty("awt.font.desktophints"); if (hints != null) { old = g2d.getRenderingHints(); g2d.addRenderingHints(hints); } // If a "native" set isn't found, just turn on standard text AA. else { old = g2d.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING); g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } // if (jsc!=null) { // setText(null); // Stop "Foobar" from being painted // } // We never paint "selection" around the icon, to imitate Eclipse final int iconW = 18; int h = getHeight(); if (!selected) { g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), h); } else { g.setColor(altBG != null && evenRow ? altBG : list.getBackground()); g.fillRect(0, 0, iconW, h); g.setColor(getBackground()); // Selection color g.fillRect(iconW, 0, getWidth() - iconW, h); } if (getIcon() != null) { int y = (h - getIcon().getIconHeight()) / 2; getIcon().paintIcon(this, g, 0, y); } int x = getX() + iconW + 2; g.setColor(selected ? list.getSelectionForeground() : list.getForeground()); if (jsc != null && !simpleText) { jsc.rendererText(g, x, g.getFontMetrics().getHeight(), selected); } else { Completion c = jsc != null ? (Completion) jsc : nonJavaCompletion; if (c != null) { g.drawString(c.toString(), x, g.getFontMetrics().getHeight()); } } // Restore rendering hints appropriately. if (hints != null) { g2d.addRenderingHints((Map) old); } else { g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, old); } }