1 /* 2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.plaf.synth; 27 28 import java.awt.*; 29 import java.beans.PropertyChangeEvent; 30 import java.beans.PropertyChangeListener; 31 32 import javax.swing.*; 33 import javax.swing.plaf.basic.BasicHTML; 34 import javax.swing.plaf.basic.BasicToolTipUI; 35 import javax.swing.plaf.ComponentUI; 36 import javax.swing.text.View; 37 38 39 /** 40 * Provides the Synth L&F UI delegate for 41 * {@link javax.swing.JToolTip}. 42 * 43 * @author Joshua Outwater 44 * @since 1.7 45 */ 46 public class SynthToolTipUI extends BasicToolTipUI 47 implements PropertyChangeListener, SynthUI { 48 private SynthStyle style; 49 50 /** 51 * Creates a new UI object for the given component. 52 * 53 * @param c component to create UI object for 54 * @return the UI object 55 */ 56 public static ComponentUI createUI(JComponent c) { 57 return new SynthToolTipUI(); 58 } 59 60 /** 61 * {@inheritDoc} 62 */ 63 @Override 64 protected void installDefaults(JComponent c) { 65 updateStyle(c); 66 } 67 68 private void updateStyle(JComponent c) { 69 SynthContext context = getContext(c, ENABLED); 70 style = SynthLookAndFeel.updateStyle(context, this); 71 } 72 73 /** 74 * {@inheritDoc} 75 */ 76 @Override 77 protected void uninstallDefaults(JComponent c) { 78 SynthContext context = getContext(c, ENABLED); 79 style.uninstallDefaults(context); 80 style = null; 81 } 82 83 /** 84 * {@inheritDoc} 85 */ 86 @Override 87 protected void installListeners(JComponent c) { 88 c.addPropertyChangeListener(this); 89 } 90 91 /** 92 * {@inheritDoc} 93 */ 94 @Override 95 protected void uninstallListeners(JComponent c) { 96 c.removePropertyChangeListener(this); 97 } 98 99 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 public SynthContext getContext(JComponent c) { 104 return getContext(c, getComponentState(c)); 105 } 106 107 private SynthContext getContext(JComponent c, int state) { 108 return SynthContext.getContext(c, style, state); 109 } 110 111 private int getComponentState(JComponent c) { 112 JComponent comp = ((JToolTip)c).getComponent(); 113 114 if (comp != null && !comp.isEnabled()) { 115 return DISABLED; 116 } 117 return SynthLookAndFeel.getComponentState(c); 118 } 119 120 /** 121 * Notifies this UI delegate to repaint the specified component. 122 * This method paints the component background, then calls 123 * the {@link #paint(SynthContext,Graphics)} method. 124 * 125 * <p>In general, this method does not need to be overridden by subclasses. 126 * All Look and Feel rendering code should reside in the {@code paint} method. 127 * 128 * @param g the {@code Graphics} object used for painting 129 * @param c the component being painted 130 * @see #paint(SynthContext,Graphics) 131 */ 132 @Override 133 public void update(Graphics g, JComponent c) { 134 SynthContext context = getContext(c); 135 136 SynthLookAndFeel.update(context, g); 137 context.getPainter().paintToolTipBackground(context, 138 g, 0, 0, c.getWidth(), c.getHeight()); 139 paint(context, g); 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override 146 public void paintBorder(SynthContext context, Graphics g, int x, 147 int y, int w, int h) { 148 context.getPainter().paintToolTipBorder(context, g, x, y, w, h); 149 } 150 151 /** 152 * Paints the specified component according to the Look and Feel. 153 * <p>This method is not used by Synth Look and Feel. 154 * Painting is handled by the {@link #paint(SynthContext,Graphics)} method. 155 * 156 * @param g the {@code Graphics} object used for painting 157 * @param c the component being painted 158 * @see #paint(SynthContext,Graphics) 159 */ 160 @Override 161 public void paint(Graphics g, JComponent c) { 162 SynthContext context = getContext(c); 163 164 paint(context, g); 165 } 166 167 /** 168 * Paints the specified component. 169 * 170 * @param context context for the component being painted 171 * @param g the {@code Graphics} object used for painting 172 * @see #update(Graphics,JComponent) 173 */ 174 protected void paint(SynthContext context, Graphics g) { 175 JToolTip tip = (JToolTip)context.getComponent(); 176 177 Insets insets = tip.getInsets(); 178 View v = (View)tip.getClientProperty(BasicHTML.propertyKey); 179 if (v != null) { 180 Rectangle paintTextR = new Rectangle(insets.left, insets.top, 181 tip.getWidth() - (insets.left + insets.right), 182 tip.getHeight() - (insets.top + insets.bottom)); 183 v.paint(g, paintTextR); 184 } else { 185 g.setColor(context.getStyle().getColor(context, 186 ColorType.TEXT_FOREGROUND)); 187 g.setFont(style.getFont(context)); 188 context.getStyle().getGraphicsUtils(context).paintText( 189 context, g, tip.getTipText(), insets.left, insets.top, -1); 190 } 191 } 192 193 /** 194 * {@inheritDoc} 195 */ 196 @Override 197 public Dimension getPreferredSize(JComponent c) { 198 SynthContext context = getContext(c); 199 Insets insets = c.getInsets(); 200 Dimension prefSize = new Dimension(insets.left+insets.right, 201 insets.top+insets.bottom); 202 String text = ((JToolTip)c).getTipText(); 203 204 if (text != null) { 205 View v = (c != null) ? (View) c.getClientProperty("html") : null; 206 if (v != null) { 207 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS); 208 prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS); 209 } else { 210 Font font = context.getStyle().getFont(context); 211 FontMetrics fm = c.getFontMetrics(font); 212 prefSize.width += context.getStyle().getGraphicsUtils(context). 213 computeStringWidth(context, font, fm, text); 214 prefSize.height += fm.getHeight(); 215 } 216 } 217 return prefSize; 218 } 219 220 /** 221 * {@inheritDoc} 222 */ 223 @Override 224 public void propertyChange(PropertyChangeEvent e) { 225 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 226 updateStyle((JToolTip)e.getSource()); 227 } 228 String name = e.getPropertyName(); 229 if (name.equals("tiptext") || "font".equals(name) || 230 "foreground".equals(name)) { 231 // remove the old html view client property if one 232 // existed, and install a new one if the text installed 233 // into the JLabel is html source. 234 JToolTip tip = ((JToolTip) e.getSource()); 235 String text = tip.getTipText(); 236 BasicHTML.updateRenderer(tip, text); 237 } 238 } 239 }