1 19 20 package org.netbeans.spi.editor.completion.support; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.awt.FontMetrics ; 25 import java.awt.Graphics ; 26 import javax.swing.ImageIcon ; 27 import org.netbeans.modules.editor.completion.PatchedHtmlRenderer; 28 29 36 37 public final class CompletionUtilities { 38 39 42 private static final int BEFORE_ICON_GAP = 1; 43 44 47 private static final int AFTER_ICON_GAP = 4; 48 49 52 private static final int ICON_HEIGHT = 16; 53 private static final int ICON_WIDTH = 16; 54 55 58 private static final int BEFORE_RIGHT_TEXT_GAP = 5; 59 60 63 private static final int AFTER_RIGHT_TEXT_GAP = 3; 64 65 private CompletionUtilities() { 66 } 68 69 81 public static int getPreferredWidth(String leftHtmlText, String rightHtmlText, 82 Graphics g, Font defaultFont) { 83 int width = BEFORE_ICON_GAP + ICON_WIDTH + AFTER_ICON_GAP + AFTER_RIGHT_TEXT_GAP; 84 if (leftHtmlText != null && leftHtmlText.length() > 0) { 85 width += (int)PatchedHtmlRenderer.renderHTML(leftHtmlText, g, 0, 0, Integer.MAX_VALUE, 0, 86 defaultFont, Color.black, PatchedHtmlRenderer.STYLE_CLIP, false, true); 87 } 88 if (rightHtmlText != null && rightHtmlText.length() > 0) { 89 if (leftHtmlText != null) { 90 width += BEFORE_RIGHT_TEXT_GAP; 91 } 92 width += (int)PatchedHtmlRenderer.renderHTML(rightHtmlText, g, 0, 0, Integer.MAX_VALUE, 0, 93 defaultFont, Color.black, PatchedHtmlRenderer.STYLE_CLIP, false, true); 94 } 95 return width; 96 } 97 98 135 public static void renderHtml(ImageIcon icon, String leftHtmlText, String rightHtmlText, 136 Graphics g, Font defaultFont, Color defaultColor, 137 int width, int height, boolean selected) { 138 if (icon != null) { 139 boolean done = g.drawImage(icon.getImage(), BEFORE_ICON_GAP, 0, null); 142 assert (done); 143 } 144 int iconWidth = BEFORE_ICON_GAP + ICON_WIDTH + AFTER_ICON_GAP; 145 int rightTextX = width - AFTER_RIGHT_TEXT_GAP; 146 FontMetrics fm = g.getFontMetrics(defaultFont); 147 int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent(); 148 if (rightHtmlText != null && rightHtmlText.length() > 0) { 149 int rightTextWidth = (int)PatchedHtmlRenderer.renderHTML(rightHtmlText, g, 0, 0, Integer.MAX_VALUE, 0, 150 defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_CLIP, false, true); 151 rightTextX = Math.max(iconWidth, rightTextX - rightTextWidth); 152 PatchedHtmlRenderer.renderHTML(rightHtmlText, g, rightTextX, textY, rightTextWidth, textY, 154 defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_CLIP, true, selected); 155 rightTextX = Math.max(iconWidth, rightTextX - BEFORE_RIGHT_TEXT_GAP); 156 } 157 158 if (leftHtmlText != null && leftHtmlText.length() > 0 && rightTextX > iconWidth) { PatchedHtmlRenderer.renderHTML(leftHtmlText, g, iconWidth, textY, rightTextX - iconWidth, textY, 161 defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_TRUNCATE, true, selected); 162 } 163 } 164 165 } 166 | Popular Tags |