1 7 8 package javax.swing.plaf.metal; 9 10 import javax.swing.*; 11 import java.awt.Color ; 12 import java.awt.Component ; 13 import java.awt.Container ; 14 import java.awt.Dimension ; 15 import java.awt.Frame ; 16 import java.awt.Graphics ; 17 import java.awt.GraphicsEnvironment ; 18 import java.awt.Insets ; 19 import java.awt.Point ; 20 import java.awt.Rectangle ; 21 import java.awt.event.*; 22 import java.lang.ref.WeakReference ; 23 import java.util.*; 24 25 import java.beans.PropertyChangeListener ; 26 27 import javax.swing.event.*; 28 import javax.swing.border.*; 29 import javax.swing.plaf.*; 30 import javax.swing.plaf.basic.*; 31 32 40 public class MetalToolBarUI extends BasicToolBarUI 41 { 42 47 private static java.util.List components = new ArrayList(); 48 49 55 protected ContainerListener contListener; 56 57 63 protected PropertyChangeListener rolloverListener; 64 65 private static Border nonRolloverBorder; 66 67 68 71 synchronized static void register(JComponent c) { 72 if (c == null) { 73 throw new NullPointerException ("JComponent must be non-null"); 76 } 77 components.add(new WeakReference (c)); 78 } 79 80 83 synchronized static void unregister(JComponent c) { 84 for (int counter = components.size() - 1; counter >= 0; counter--) { 85 WeakReference ref = (WeakReference )components.get(counter); 88 Object target = ((WeakReference )components.get(counter)).get(); 89 90 if (target == c || target == null) { 91 components.remove(counter); 92 } 93 } 94 } 95 96 100 synchronized static Object findRegisteredComponentOfType(JComponent from, 101 Class target) { 102 JRootPane rp = SwingUtilities.getRootPane(from); 103 if (rp != null) { 104 for (int counter = components.size() - 1; counter >= 0; counter--){ 105 Object component = ((WeakReference )components.get(counter)). 106 get(); 107 108 if (component == null) { 109 components.remove(counter); 111 } 112 else if (target.isInstance(component) && SwingUtilities. 113 getRootPane((Component )component) == rp) { 114 return component; 115 } 116 } 117 } 118 return null; 119 } 120 121 125 static boolean doesMenuBarBorderToolBar(JMenuBar c) { 126 JToolBar tb = (JToolBar)MetalToolBarUI. 127 findRegisteredComponentOfType(c, JToolBar.class); 128 if (tb != null && tb.getOrientation() == JToolBar.HORIZONTAL) { 129 JRootPane rp = SwingUtilities.getRootPane(c); 130 Point point = new Point (0, 0); 131 point = SwingUtilities.convertPoint(c, point, rp); 132 int menuX = point.x; 133 int menuY = point.y; 134 point.x = point.y = 0; 135 point = SwingUtilities.convertPoint(tb, point, rp); 136 return (point.x == menuX && menuY + c.getHeight() == point.y && 137 c.getWidth() == tb.getWidth()); 138 } 139 return false; 140 } 141 142 public static ComponentUI createUI( JComponent c ) 143 { 144 return new MetalToolBarUI (); 145 } 146 147 public void installUI( JComponent c ) 148 { 149 super.installUI( c ); 150 register(c); 151 } 152 153 public void uninstallUI( JComponent c ) 154 { 155 super.uninstallUI( c ); 156 nonRolloverBorder = null; 157 unregister(c); 158 } 159 160 protected void installListeners() { 161 super.installListeners(); 162 163 contListener = createContainerListener(); 164 if (contListener != null) { 165 toolBar.addContainerListener(contListener); 166 } 167 rolloverListener = createRolloverListener(); 168 if (rolloverListener != null) { 169 toolBar.addPropertyChangeListener(rolloverListener); 170 } 171 } 172 173 protected void uninstallListeners() { 174 super.uninstallListeners(); 175 176 if (contListener != null) { 177 toolBar.removeContainerListener(contListener); 178 } 179 rolloverListener = createRolloverListener(); 180 if (rolloverListener != null) { 181 toolBar.removePropertyChangeListener(rolloverListener); 182 } 183 } 184 185 protected Border createRolloverBorder() { 186 return super.createRolloverBorder(); 187 } 188 189 protected Border createNonRolloverBorder() { 190 return super.createNonRolloverBorder(); 191 } 192 193 194 197 private Border createNonRolloverToggleBorder() { 198 return createNonRolloverBorder(); 199 } 200 201 protected void setBorderToNonRollover(Component c) { 202 if (c instanceof JToggleButton && !(c instanceof JCheckBox)) { 203 208 JToggleButton b = (JToggleButton)c; 212 Border border = b.getBorder(); 213 super.setBorderToNonRollover(c); 214 if (border instanceof UIResource) { 215 if (nonRolloverBorder == null) { 216 nonRolloverBorder = createNonRolloverToggleBorder(); 217 } 218 b.setBorder(nonRolloverBorder); 219 } 220 } else { 221 super.setBorderToNonRollover(c); 222 } 223 } 224 225 226 233 protected ContainerListener createContainerListener() { 234 return null; 235 } 236 237 244 protected PropertyChangeListener createRolloverListener() { 245 return null; 246 } 247 248 protected MouseInputListener createDockingListener( ) 249 { 250 return new MetalDockingListener( toolBar ); 251 } 252 253 protected void setDragOffset(Point p) { 254 if (!GraphicsEnvironment.isHeadless()) { 255 if (dragWindow == null) { 256 dragWindow = createDragWindow(toolBar); 257 } 258 dragWindow.setOffset(p); 259 } 260 } 261 262 274 public void update(Graphics g, JComponent c) { 275 if (c.isOpaque() && (c.getBackground() instanceof UIResource) && 276 ((JToolBar)c).getOrientation() == 277 JToolBar.HORIZONTAL && UIManager.get( 278 "MenuBar.gradient") != null) { 279 JRootPane rp = SwingUtilities.getRootPane(c); 280 JMenuBar mb = (JMenuBar)findRegisteredComponentOfType( 281 c, JMenuBar.class); 282 if (mb != null && mb.isOpaque() && 283 (mb.getBackground() instanceof UIResource)) { 284 Point point = new Point (0, 0); 285 point = SwingUtilities.convertPoint(c, point, rp); 286 int x = point.x; 287 int y = point.y; 288 point.x = point.y = 0; 289 point = SwingUtilities.convertPoint(mb, point, rp); 290 if (point.x == x && y == point.y + mb.getHeight() && 291 mb.getWidth() == c.getWidth() && 292 MetalUtils.drawGradient(c, g, "MenuBar.gradient", 293 0, -mb.getHeight(), c.getWidth(), c.getHeight() + 294 mb.getHeight(), true)) { 295 paint(g, c); 296 return; 297 } 298 } 299 if (MetalUtils.drawGradient(c, g, "MenuBar.gradient", 300 0, 0, c.getWidth(), c.getHeight(), true)) { 301 paint(g, c); 302 return; 303 } 304 } 305 super.update(g, c); 306 } 307 308 protected class MetalContainerListener 310 extends BasicToolBarUI.ToolBarContListener {} 311 312 protected class MetalRolloverListener 314 extends BasicToolBarUI.PropertyListener {} 315 316 protected class MetalDockingListener extends DockingListener { 317 private boolean pressedInBumps = false; 318 319 public MetalDockingListener(JToolBar t) { 320 super(t); 321 } 322 323 public void mousePressed(MouseEvent e) { 324 super.mousePressed(e); 325 if (!toolBar.isEnabled()) { 326 return; 327 } 328 pressedInBumps = false; 329 Rectangle bumpRect = new Rectangle (); 330 331 if (toolBar.getOrientation() == JToolBar.HORIZONTAL) { 332 int x = MetalUtils.isLeftToRight(toolBar) ? 0 : toolBar.getSize().width-14; 333 bumpRect.setBounds(x, 0, 14, toolBar.getSize().height); 334 } else { bumpRect.setBounds(0, 0, toolBar.getSize().width, 14); 336 } 337 if (bumpRect.contains(e.getPoint())) { 338 pressedInBumps = true; 339 Point dragOffset = e.getPoint(); 340 if (!MetalUtils.isLeftToRight(toolBar)) { 341 dragOffset.x -= (toolBar.getSize().width 342 - toolBar.getPreferredSize().width); 343 } 344 setDragOffset(dragOffset); 345 } 346 } 347 348 public void mouseDragged(MouseEvent e) { 349 if (pressedInBumps) { 350 super.mouseDragged(e); 351 } 352 } 353 } } 355 | Popular Tags |