1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Font ; 26 import java.awt.FontMetrics ; 27 import java.awt.Graphics ; 28 import java.awt.Graphics2D ; 29 import java.awt.Insets ; 30 import java.awt.Rectangle ; 31 32 import javax.swing.plaf.basic.BasicToggleButtonUI ; 33 import java.awt.geom.AffineTransform ; 34 import javax.swing.AbstractButton ; 35 import javax.swing.BorderFactory ; 36 import javax.swing.ButtonModel ; 37 import javax.swing.Icon ; 38 import javax.swing.JButton ; 39 import javax.swing.JComponent ; 40 import javax.swing.UIDefaults ; 41 import javax.swing.UIManager ; 42 import javax.swing.border.Border ; 43 import javax.swing.border.CompoundBorder ; 44 import javax.swing.plaf.ComponentUI ; 45 import javax.swing.plaf.basic.BasicGraphicsUtils ; 46 import org.netbeans.swing.tabcontrol.SlidingButton; 47 import org.netbeans.swing.tabcontrol.SlidingButtonUI; 48 49 55 public class WindowsSlidingButtonUI extends SlidingButtonUI { 56 57 private static final SlidingButtonUI INSTANCE = new WindowsSlidingButtonUI(); 59 60 private boolean defaults_initialized = false; 62 protected Color focusColor; 63 protected static int dashedRectGapX; 64 protected static int dashedRectGapY; 65 protected static int dashedRectGapWidth; 66 protected static int dashedRectGapHeight; 67 68 69 70 protected WindowsSlidingButtonUI() { 71 } 72 73 public static ComponentUI createUI(JComponent c) { 74 return INSTANCE; 75 } 76 77 78 protected void installBorder (AbstractButton b) { 79 b.setBorder ( BorderFactory.createEmptyBorder(2, 2, 2, 2)); 82 } 83 84 public void installDefaults (AbstractButton b) { 85 super.installDefaults(b); 86 if(!defaults_initialized) { 87 try { 88 Integer in = ((Integer )UIManager.get("Button.dashedRectGapX")); 90 dashedRectGapX = in == null ? 3 : in.intValue(); 91 in = ((Integer )UIManager.get("Button.dashedRectGapY")); 92 dashedRectGapY = in == null ? 3 : in.intValue(); 93 in = ((Integer )UIManager.get("Button.dashedRectGapWidth")); 94 dashedRectGapWidth = in == null ? 3 : in.intValue(); 95 in = ((Integer )UIManager.get("Button.dashedRectGapHeight")); 96 dashedRectGapHeight = in == null ? 3 : in.intValue(); 97 focusColor = UIManager.getColor(getPropertyPrefix() + "focus"); 98 defaults_initialized = true; 99 } catch (NullPointerException npe) { 100 dashedRectGapX = 2; 103 dashedRectGapY = 2; 104 dashedRectGapWidth = 2; 105 dashedRectGapHeight = 2; 106 } 107 } 108 } 109 110 protected void uninstallDefaults(AbstractButton b) { 111 super.uninstallDefaults(b); 112 defaults_initialized = false; 113 } 114 115 protected void paintBackground(Graphics2D g, AbstractButton b) { 116 if (((SlidingButton) b).isBlinkState()) { 117 g.setColor(WinClassicEditorTabCellRenderer.ATTENTION_COLOR); 118 g.fillRect (0, 0, b.getWidth(), b.getHeight()); 119 } else { 120 super.paintBackground(g, b); 121 } 122 } 123 124 protected void paintButtonPressed(Graphics g, AbstractButton b) { 125 Color oldColor = g.getColor(); 128 129 if (((SlidingButton) b).isBlinkState()) { 130 g.setColor(WinClassicEditorTabCellRenderer.ATTENTION_COLOR); 131 g.fillRect (0, 0, b.getWidth(), b.getHeight()); 132 } 133 134 int w = b.getWidth(); 135 int h = b.getHeight(); 136 UIDefaults table = UIManager.getLookAndFeelDefaults(); 137 if (b.getModel().isRollover() && (! b.getModel().isPressed() && ! b.getModel().isSelected())) { 138 g.setColor(table.getColor("ToggleButton.highlight")); 139 g.drawRect(0, 0, w-1, h-1); 140 g.drawRect(0, 0, 0, h-1); 141 142 Color shade = table.getColor("ToggleButton.shadow"); 143 Component p = b.getParent(); 144 if (p != null && p.getBackground().equals(shade)) { 145 shade = table.getColor("ToggleButton.darkShadow"); 146 } 147 g.setColor(shade); 148 g.drawLine(w-1, 0, w-1, h-1); 149 g.drawLine(0, h-1, w-1, h-1); 150 } else { 151 Color shade = table.getColor("ToggleButton.shadow"); 152 Component p = b.getParent(); 153 if (p != null && p.getBackground().equals(shade)) { 154 shade = table.getColor("ToggleButton.darkShadow"); 155 } 156 g.setColor(shade); 157 g.drawRect(0, 0, w-1, h-1); 158 g.setColor(table.getColor("ToggleButton.highlight")); 159 g.drawLine(w-1, 0, w-1, h-1); 160 g.drawLine(0, h-1, w-1, h-1); 161 } 162 g.setColor(oldColor); 163 } 164 165 protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ 166 167 int width = b.getWidth(); 168 int height = b.getHeight(); 169 g.setColor(getFocusColor()); 170 BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY, 171 width - dashedRectGapWidth, height - dashedRectGapHeight); 172 } 173 174 175 protected Color getFocusColor() { 176 return focusColor; 177 } 178 179 180 181 182 public Dimension getPreferredSize(JComponent c) { 186 Dimension d = super.getPreferredSize(c); 187 188 191 AbstractButton b = (AbstractButton )c; 192 if (b.isFocusPainted()) { 193 if(d.width % 2 == 0) { d.width += 1; } 194 if(d.height % 2 == 0) { d.height += 1; } 195 } 196 return d; 197 } 198 199 200 } 201 | Popular Tags |