@Override protected void customizeCellRenderer( JList list, Object value, int index, boolean selected, boolean hasFocus) { setIcon(myListEntryIcon); if (myUseIdeaEditor) { int max = list.getModel().getSize(); String indexString = String.valueOf(index + 1); int count = String.valueOf(max).length() - indexString.length(); char[] spaces = new char[count]; Arrays.fill(spaces, ' '); String prefix = indexString + new String(spaces) + " "; append(prefix, SimpleTextAttributes.GRAYED_ATTRIBUTES); } else if (UIUtil.isUnderGTKLookAndFeel()) { // Fix GTK background Color background = selected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground(); UIUtil.changeBackGround(this, background); } String text = ((Item) value).shortText; FontMetrics metrics = list.getFontMetrics(list.getFont()); int charWidth = metrics.charWidth('m'); int maxLength = list.getParent().getParent().getWidth() * 3 / charWidth / 2; text = StringUtil.first(text, maxLength, true); // do not paint long strings append(text, SimpleTextAttributes.REGULAR_ATTRIBUTES); }
private void setFixedColumnWidth(final int columnIndex, String sampleText) { final TableColumn column = myEntryTable.getTableHeader().getColumnModel().getColumn(columnIndex); final FontMetrics fontMetrics = myEntryTable.getFontMetrics(myEntryTable.getFont()); final int width = fontMetrics.stringWidth(" " + sampleText + " ") + JBUI.scale(4); column.setPreferredWidth(width); column.setMinWidth(width); column.setResizable(false); }
private String cutString(final String text, final double value) { final FontMetrics m = getFontMetrics(getFont()); final Graphics g = getGraphics(); if (m.getStringBounds(text, g).getWidth() < value) return text; final String dots = "..."; final double dotsWidth = m.getStringBounds(dots, g).getWidth(); if (dotsWidth >= value) { return dots; } for (int i = 1; i < text.length(); i++) { if ((m.getStringBounds(text, 0, i, g).getWidth() + dotsWidth) >= value) { if (i < 2) return dots; return text.substring(0, i - 1) + dots; } } return text; }
private int setTypeTextLabel( LookupElement item, final Color background, Color foreground, final LookupElementPresentation presentation, int allowedWidth, boolean selected, boolean nonFocusedSelection, FontMetrics normalMetrics) { final String givenText = presentation.getTypeText(); final String labelText = trimLabelText( StringUtil.isEmpty(givenText) ? "" : " " + givenText, allowedWidth, normalMetrics); int used = RealLookupElementPresentation.getStringWidth(labelText, normalMetrics); final Icon icon = presentation.getTypeIcon(); if (icon != null) { myTypeLabel.setIcon(icon); used += icon.getIconWidth(); } Color sampleBackground = background; Object o = item.isValid() ? item.getObject() : null; //noinspection deprecation if (o instanceof LookupValueWithUIHint && StringUtil.isEmpty(labelText)) { //noinspection deprecation Color proposedBackground = ((LookupValueWithUIHint) o).getColorHint(); if (proposedBackground != null) { sampleBackground = proposedBackground; } myTypeLabel.append(" "); used += normalMetrics.stringWidth("WW"); } else { myTypeLabel.append(labelText); } myTypeLabel.setBackground(sampleBackground); myTypeLabel.setForeground( getTypeTextColor(item, foreground, presentation, selected, nonFocusedSelection)); return used; }
private void paintRowData(Tree tree, Object data, Rectangle bounds, Graphics2D g) { Shortcut[] shortcuts = null; Set<String> abbreviations = null; if (data instanceof String) { final String actionId = (String) data; shortcuts = myKeymap.getShortcuts(actionId); abbreviations = AbbreviationManager.getInstance().getAbbreviations(actionId); } else if (data instanceof QuickList) { shortcuts = myKeymap.getShortcuts(((QuickList) data).getActionId()); } final GraphicsConfig config = GraphicsUtil.setupAAPainting(g); int totalWidth = 0; final FontMetrics metrics = tree.getFontMetrics(tree.getFont()); if (shortcuts != null && shortcuts.length > 0) { for (Shortcut shortcut : shortcuts) { totalWidth += metrics.stringWidth(KeymapUtil.getShortcutText(shortcut)); totalWidth += 10; } totalWidth -= 5; int x = bounds.x + bounds.width - totalWidth; int fontHeight = (int) metrics.getMaxCharBounds(g).getHeight(); Color c1 = new Color(234, 200, 162); Color c2 = new Color(208, 200, 66); g.translate(0, bounds.y - 1); for (Shortcut shortcut : shortcuts) { int width = metrics.stringWidth(KeymapUtil.getShortcutText(shortcut)); UIUtil.drawSearchMatch(g, x, x + width, bounds.height, c1, c2); g.setColor(Gray._50); g.drawString(KeymapUtil.getShortcutText(shortcut), x, fontHeight); x += width; x += 10; } g.translate(0, -bounds.y + 1); } if (Registry.is("actionSystem.enableAbbreviations") && abbreviations != null && abbreviations.size() > 0) { for (String abbreviation : abbreviations) { totalWidth += metrics.stringWidth(abbreviation); totalWidth += 10; } totalWidth -= 5; int x = bounds.x + bounds.width - totalWidth; int fontHeight = (int) metrics.getMaxCharBounds(g).getHeight(); Color c1 = new Color(206, 234, 176); Color c2 = new Color(126, 208, 82); g.translate(0, bounds.y - 1); for (String abbreviation : abbreviations) { int width = metrics.stringWidth(abbreviation); UIUtil.drawSearchMatch(g, x, x + width, bounds.height, c1, c2); g.setColor(Gray._50); g.drawString(abbreviation, x, fontHeight); x += width; x += 10; } g.translate(0, -bounds.y + 1); } config.restore(); }