001package jmri.jmrit.display; 002 003import java.awt.Color; 004import java.awt.Font; 005import java.awt.Graphics2D; 006import java.awt.font.TextLayout; 007import java.awt.geom.Rectangle2D; 008 009/** 010 * Implements Tooltips for Positionable objects. 011 * 012 * @author Pete Cressman Copyright (c) 2010 013 */ 014public class ToolTip { 015 016 private Positionable _positionable; 017 private Color _backgroundColor; 018 private Color _fontColor; 019 private Color _borderColor; 020 private Font _tFont; 021 private boolean _showDisplayName; 022 private String _tip; 023 private int _tx, _ty; // location of Positionable 024 025 /** 026 * @param text tooltip text 027 * @param x x coord of Positionable's screen location 028 * @param y y coord of Positionable's screen location 029 * @param pos Positionable of this Tooltip 030 */ 031 public ToolTip(String text, int x, int y, Positionable pos) { 032 _positionable = pos; 033 _tip = text; 034 _tx = x; 035 _ty = y; 036 _tFont = new Font("SansSerif", Font.PLAIN, 12); 037 _fontColor = Color.black; 038 _backgroundColor = new Color(255, 250, 210); 039 _borderColor = Color.blue; 040 } 041 042 /** 043 * @param tooltip toolTip to clone 044 * @param pos Positionable of this Tooltip 045 */ 046 public ToolTip(ToolTip tooltip, Positionable pos) { 047 setLocation(pos); 048 _positionable = pos; 049 _tFont = new Font(tooltip._tFont.getFamily(), tooltip._tFont.getStyle(), tooltip._tFont.getSize()); 050 _fontColor = tooltip._fontColor; 051 _backgroundColor = tooltip._backgroundColor; 052 _borderColor = tooltip._borderColor; 053 } 054 055 /** 056 * @param text tooltip text 057 * @param x x coord of Positionable's screen location 058 * @param y y coord of Positionable's screen location 059 * @param font tooltip font 060 * @param fontColor tooltip font color 061 * @param backgroundColor tooltip background color 062 * @param borderColor tooltip border color 063 * @param pos Positionable of this Tooltip 064 */ 065 public ToolTip(String text, int x, int y, Font font, 066 Color fontColor, Color backgroundColor, Color borderColor, 067 Positionable pos) { 068 _positionable = pos; 069 _tip = text; 070 _tx = x; 071 _ty = y; 072 _tFont = font; 073 _fontColor = fontColor; 074 _backgroundColor = backgroundColor; 075 _borderColor = borderColor; 076 } 077 078 public final void setPositionable(Positionable pos) { 079 _positionable = pos; 080 setLocation(pos.getX() + pos.getWidth() / 2, pos.getY() + pos.getHeight() / 2); 081 } 082 083 public final Positionable getPositionable() { 084 return _positionable; 085 } 086 087 public final void setText(String text) { 088 _tip = text; 089 } 090 091 public final String getText() { 092 return _tip; 093 } 094 095 public final void setPrependToolTipWithDisplayName(boolean value) { 096 _showDisplayName = value; 097 } 098 099 public final boolean getPrependToolTipWithDisplayName() { 100 return _showDisplayName; 101 } 102 103 public final void setLocation(int x, int y) { 104 _tx = x; 105 _ty = y; 106 } 107 108 private final void setLocation(Positionable pos) { 109 setLocation(pos.getX() + pos.getWidth() / 2, pos.getY() + pos.getHeight() / 2); 110 } 111 112 public final void setFontSize(int size) { 113 _tFont = _tFont.deriveFont(size); 114 } 115 116 public final int getFontSize() { 117 return _tFont.getSize(); 118 } 119 120 public final void setFontColor(Color fontColor) { 121 _fontColor = fontColor; 122 } 123 124 public final Color getFontColor() { 125 return _fontColor; 126 } 127 128 public final void setBackgroundColor(Color backgroundColor) { 129 _backgroundColor = backgroundColor; 130 } 131 132 public final Color getBackgroundColor() { 133 return _backgroundColor; 134 } 135 136 public final void setBoderColor(Color borderColor) { 137 _borderColor = borderColor; 138 } 139 140 public final Color getBorderColor() { 141 return _borderColor; 142 } 143 144 public String getTextToDisplay() { 145 String tipText = _tip != null ? _tip.trim() : ""; 146 var displayName = ""; 147 if (_positionable != null) { // LE turnout tooltips do not have a positional 148 displayName = _positionable.getNameString(); 149 } 150 151 if (tipText.isEmpty()) { 152 tipText = displayName; 153 } else if (_showDisplayName) { 154 tipText = displayName + ": " + tipText; 155 } 156 157 return tipText; 158 } 159 160 public void paint(Graphics2D g2d, double scale) { 161 String tipText = getTextToDisplay(); 162 if (tipText.isEmpty()) return; 163 164 Color color = g2d.getColor(); 165 Font font = g2d.getFont(); 166 TextLayout tl = new TextLayout(tipText, _tFont, g2d.getFontRenderContext()); 167 Rectangle2D bds = tl.getBounds(); 168 int x0 = Math.max((int) (bds.getX() + _tx - bds.getWidth() / 2 - 9), 0); 169 bds.setRect(x0, _ty + (bds.getHeight() - 9) / scale, 170 bds.getWidth() + 9, bds.getHeight() + 8); 171 g2d.setColor(_backgroundColor); 172 g2d.fill(bds); 173 g2d.setColor(_borderColor); 174 g2d.draw(bds); 175 g2d.setColor(_fontColor); 176 tl.draw(g2d, x0 + 3f, (float) (_ty + bds.getHeight() - 4)); 177 g2d.setColor(color); 178 g2d.setFont(font); 179 } 180 181// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ToolTip.class); 182}