1 19 20 package org.netbeans.swing.plaf.gtk; 21 22 import javax.swing.*; 23 import javax.swing.event.ChangeEvent ; 24 import javax.swing.event.ChangeListener ; 25 import javax.swing.plaf.ButtonUI ; 26 import javax.swing.plaf.basic.BasicButtonListener ; 27 import java.awt.*; 28 29 33 class GtkToolBarButtonUI extends ButtonUI implements ChangeListener { 34 private static BasicButtonListener listener = 35 new BasicButtonListener (null); 36 37 38 public GtkToolBarButtonUI() { 39 } 40 41 public void installUI (JComponent c) { 42 AbstractButton b = (AbstractButton) c; 43 b.addMouseListener (listener); 44 b.addChangeListener(this); 45 b.setContentAreaFilled(false); 46 b.setOpaque(false); 47 b.setFocusable(false); 48 b.setBorderPainted(false); 49 b.setBorder (BorderFactory.createEmptyBorder()); 50 b.putClientProperty("hideActionText", Boolean.TRUE); } 52 53 public void uninstallUI(JComponent c) { 54 c.removeMouseListener (listener); 55 } 56 57 public void stateChanged(ChangeEvent e) { 58 ((AbstractButton) e.getSource()).repaint(); 59 } 60 61 private final Rectangle scratch = new Rectangle(); 62 public void paint (Graphics g, JComponent c) { 63 Rectangle r = c.getBounds(scratch); 64 AbstractButton b = (AbstractButton) c; 65 r.x = 0; 66 r.y = 0; 67 Paint temp = ((Graphics2D) g).getPaint(); 68 paintBackground ((Graphics2D)g, b, r); 69 paintIcon (g, b, r); 70 ((Graphics2D) g).setPaint(temp); 71 } 72 73 private void paintBackground (Graphics2D g, AbstractButton b, Rectangle r) { 74 if (!b.isEnabled()) { 75 } else if (b.getModel().isPressed()) { 76 compositeColor (g, r, Color.BLUE, 0.3f); 77 } else if (b.getModel().isSelected()) { 78 compositeColor (g, r, new Color (0, 120, 255), 0.2f);; 79 } 80 } 81 82 private void compositeColor (Graphics2D g, Rectangle r, Color c, float alpha) { 83 g.setColor (c); 84 Composite comp = g.getComposite(); 85 86 g.setComposite(AlphaComposite.getInstance( 87 AlphaComposite.SRC_OVER, alpha)); 88 89 g.fillRect (r.x, r.y, r.width, r.height); 90 g.setComposite(comp); 91 } 92 93 private static boolean isFirst (AbstractButton b) { 94 if (b.getParent() != null && b.getParent().getComponentCount() > 1) { 95 return b == b.getParent().getComponent(1); 98 } else { 99 return false; 100 } 101 } 102 103 private void paintIcon (Graphics g, AbstractButton b, Rectangle r) { 104 Icon ic = getIconForState (b); 105 if (ic != null) { 106 int iconX = 0; 107 int iconY = 0; 108 int iconW = ic.getIconWidth(); 109 int iconH = ic.getIconHeight(); 110 111 if (iconW <= r.width) { 112 iconX = (r.width / 2) - (iconW / 2); 113 } 114 if (iconH <= r.height) { 115 iconY = (r.height / 2) - (iconH / 2); 116 } 117 ic.paintIcon(b, g, iconX, iconY); 118 } 119 } 120 121 private Icon getIconForState (AbstractButton b) { 122 ButtonModel mdl = b.getModel(); 123 Icon result = null; 124 if (!b.isEnabled()) { 125 result = mdl.isSelected() ? b.getDisabledSelectedIcon() : b.getDisabledIcon(); 126 if (result == null && mdl.isSelected()) { 127 result = b.getDisabledIcon(); 128 } 129 } else { 130 if (mdl.isArmed() && !mdl.isPressed()) { 131 result = mdl.isSelected() ? b.getRolloverSelectedIcon() : b.getRolloverIcon(); 132 if (result == null & mdl.isSelected()) { 133 result = b.getRolloverIcon(); 134 } 135 } 136 if (mdl.isPressed()) { 137 result = b.getPressedIcon(); 138 } else if (mdl.isSelected()) { 139 result = b.getSelectedIcon(); 140 } 141 } 142 if (result == null) { 143 result = b.getIcon(); 144 } 145 return result; 146 } 147 148 private static final int minButtonSize = 32; 149 public Dimension getPreferredSize(JComponent c) { 150 if (c instanceof AbstractButton) { 151 Icon ic = getIconForState((AbstractButton) c); 152 Dimension result; 153 int minSize = isFirst((AbstractButton)c) ? 0 : minButtonSize; 154 if (ic != null) { 155 result = new Dimension(Math.max(minSize, ic.getIconWidth()+1), 156 Math.max(minButtonSize,ic.getIconHeight() + 1)); 157 } else { 158 result = new Dimension (minButtonSize, minButtonSize); 159 } 160 result.width += 4; 161 return result; 162 } else { 163 if (c.getLayout() != null) { 164 return c.getLayout().preferredLayoutSize(c); 165 } 166 } 167 return null; 168 } 169 } 170 | Popular Tags |