J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.swing.plaf.synth; |
| 27 | |
| 28 | import javax.swing.*; |
| 29 | import javax.swing.event.*; |
| 30 | import java.awt.*; |
| 31 | import java.awt.event.*; |
| 32 | |
| 33 | import java.beans.*; |
| 34 | |
| 35 | import javax.swing.border.*; |
| 36 | import javax.swing.plaf.*; |
| 37 | import javax.swing.plaf.basic.BasicToolBarUI; |
| 38 | import sun.swing.plaf.synth.*; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * A Synth L&F implementation of ToolBarUI. This implementation |
| 43 | * is a "combined" view/controller. |
| 44 | * <p> |
| 45 | * |
| 46 | */ |
| 47 | class SynthToolBarUI extends BasicToolBarUI implements PropertyChangeListener, |
| 48 | SynthUI { |
| 49 | protected Icon handleIcon = null; |
| 50 | protected Rectangle contentRect = new Rectangle(); |
| 51 | |
| 52 | private SynthStyle style; |
| 53 | private SynthStyle contentStyle; |
| 54 | private SynthStyle dragWindowStyle; |
| 55 | |
| 56 | public static ComponentUI createUI(JComponent c) { |
| 57 | return new SynthToolBarUI(); |
| 58 | } |
| 59 | |
| 60 | protected void installDefaults() { |
| 61 | toolBar.setLayout(createLayout()); |
| 62 | updateStyle(toolBar); |
| 63 | } |
| 64 | |
| 65 | protected void installListeners() { |
| 66 | super.installListeners(); |
| 67 | toolBar.addPropertyChangeListener(this); |
| 68 | } |
| 69 | |
| 70 | protected void uninstallListeners() { |
| 71 | super.uninstallListeners(); |
| 72 | toolBar.removePropertyChangeListener(this); |
| 73 | } |
| 74 | |
| 75 | private void updateStyle(JToolBar c) { |
| 76 | SynthContext context = getContext(c, ENABLED); |
| 77 | SynthStyle oldStyle = style; |
| 78 | |
| 79 | style = SynthLookAndFeel.updateStyle(context, this); |
| 80 | if (oldStyle != style) { |
| 81 | handleIcon = |
| 82 | style.getIcon(context, "ToolBar.handleIcon"); |
| 83 | if (oldStyle != null) { |
| 84 | uninstallKeyboardActions(); |
| 85 | installKeyboardActions(); |
| 86 | } |
| 87 | } |
| 88 | context.dispose(); |
| 89 | |
| 90 | context = getContext(c, Region.TOOL_BAR_CONTENT, ENABLED); |
| 91 | contentStyle = SynthLookAndFeel.updateStyle(context, this); |
| 92 | context.dispose(); |
| 93 | |
| 94 | context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, ENABLED); |
| 95 | dragWindowStyle = SynthLookAndFeel.updateStyle(context, this); |
| 96 | context.dispose(); |
| 97 | } |
| 98 | |
| 99 | protected void uninstallDefaults() { |
| 100 | SynthContext context = getContext(toolBar, ENABLED); |
| 101 | |
| 102 | style.uninstallDefaults(context); |
| 103 | context.dispose(); |
| 104 | style = null; |
| 105 | |
| 106 | handleIcon = null; |
| 107 | |
| 108 | context = getContext(toolBar, Region.TOOL_BAR_CONTENT, ENABLED); |
| 109 | contentStyle.uninstallDefaults(context); |
| 110 | context.dispose(); |
| 111 | contentStyle = null; |
| 112 | |
| 113 | context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, ENABLED); |
| 114 | dragWindowStyle.uninstallDefaults(context); |
| 115 | context.dispose(); |
| 116 | dragWindowStyle = null; |
| 117 | |
| 118 | toolBar.setLayout(null); |
| 119 | } |
| 120 | |
| 121 | protected void installComponents() { |
| 122 | } |
| 123 | |
| 124 | protected void uninstallComponents() { |
| 125 | } |
| 126 | |
| 127 | protected LayoutManager createLayout() { |
| 128 | return new SynthToolBarLayoutManager(); |
| 129 | } |
| 130 | |
| 131 | public SynthContext getContext(JComponent c) { |
| 132 | return getContext(c, getComponentState(c)); |
| 133 | } |
| 134 | |
| 135 | private SynthContext getContext(JComponent c, int state) { |
| 136 | return SynthContext.getContext(SynthContext.class, c, |
| 137 | SynthLookAndFeel.getRegion(c), style, state); |
| 138 | } |
| 139 | |
| 140 | private SynthContext getContext(JComponent c, Region region) { |
| 141 | return getContext(c, region, getComponentState(c, region)); |
| 142 | } |
| 143 | |
| 144 | private SynthContext getContext(JComponent c, Region region, int state) { |
| 145 | return SynthContext.getContext(SynthContext.class, c, region, |
| 146 | dragWindowStyle, state); |
| 147 | } |
| 148 | |
| 149 | private Region getRegion(JComponent c) { |
| 150 | return SynthLookAndFeel.getRegion(c); |
| 151 | } |
| 152 | |
| 153 | private int getComponentState(JComponent c) { |
| 154 | return SynthLookAndFeel.getComponentState(c); |
| 155 | } |
| 156 | |
| 157 | private int getComponentState(JComponent c, Region region) { |
| 158 | return SynthLookAndFeel.getComponentState(c); |
| 159 | } |
| 160 | |
| 161 | public void update(Graphics g, JComponent c) { |
| 162 | SynthContext context = getContext(c); |
| 163 | |
| 164 | SynthLookAndFeel.update(context, g); |
| 165 | context.getPainter().paintToolBarBackground(context, |
| 166 | g, 0, 0, c.getWidth(), c.getHeight(), |
| 167 | toolBar.getOrientation()); |
| 168 | paint(context, g); |
| 169 | context.dispose(); |
| 170 | } |
| 171 | |
| 172 | public void paint(Graphics g, JComponent c) { |
| 173 | SynthContext context = getContext(c); |
| 174 | |
| 175 | paint(context, g); |
| 176 | context.dispose(); |
| 177 | } |
| 178 | |
| 179 | public void paintBorder(SynthContext context, Graphics g, int x, |
| 180 | int y, int w, int h) { |
| 181 | context.getPainter().paintToolBarBorder(context, g, x, y, w, h, |
| 182 | toolBar.getOrientation()); |
| 183 | } |
| 184 | |
| 185 | // Overloaded to do nothing so we can share listeners. |
| 186 | protected void setBorderToNonRollover(Component c) {} |
| 187 | |
| 188 | // Overloaded to do nothing so we can share listeners. |
| 189 | protected void setBorderToRollover(Component c) {} |
| 190 | |
| 191 | // Overloaded to do nothing so we can share listeners. |
| 192 | protected void setBorderToNormal(Component c) {} |
| 193 | |
| 194 | protected void paint(SynthContext context, Graphics g) { |
| 195 | if (handleIcon != null && toolBar.isFloatable()) { |
| 196 | int startX = toolBar.getComponentOrientation().isLeftToRight() ? |
| 197 | 0 : toolBar.getWidth() - |
| 198 | SynthIcon.getIconWidth(handleIcon, context); |
| 199 | SynthIcon.paintIcon(handleIcon, context, g, startX, 0, |
| 200 | SynthIcon.getIconWidth(handleIcon, context), |
| 201 | SynthIcon.getIconHeight(handleIcon, context)); |
| 202 | } |
| 203 | |
| 204 | SynthContext subcontext = getContext(toolBar, Region.TOOL_BAR_CONTENT); |
| 205 | paintContent(subcontext, g, contentRect); |
| 206 | subcontext.dispose(); |
| 207 | } |
| 208 | |
| 209 | public void paintContent(SynthContext context, Graphics g, |
| 210 | Rectangle bounds) { |
| 211 | SynthLookAndFeel.updateSubregion(context, g, bounds); |
| 212 | context.getPainter().paintToolBarContentBackground(context, g, |
| 213 | bounds.x, bounds.y, bounds.width, bounds.height, |
| 214 | toolBar.getOrientation()); |
| 215 | context.getPainter().paintToolBarContentBorder(context, g, |
| 216 | bounds.x, bounds.y, bounds.width, bounds.height, |
| 217 | toolBar.getOrientation()); |
| 218 | } |
| 219 | |
| 220 | protected void paintDragWindow(Graphics g) { |
| 221 | int w = dragWindow.getWidth(); |
| 222 | int h = dragWindow.getHeight(); |
| 223 | SynthContext context = getContext(toolBar,Region.TOOL_BAR_DRAG_WINDOW); |
| 224 | SynthLookAndFeel.updateSubregion(context, g, new Rectangle( |
| 225 | 0, 0, w, h)); |
| 226 | context.getPainter().paintToolBarDragWindowBackground(context, |
| 227 | g, 0, 0, w, h, |
| 228 | dragWindow.getOrientation()); |
| 229 | context.getPainter().paintToolBarDragWindowBorder(context, g, 0, 0, w, h, |
| 230 | dragWindow.getOrientation()); |
| 231 | context.dispose(); |
| 232 | } |
| 233 | |
| 234 | // |
| 235 | // PropertyChangeListener |
| 236 | // |
| 237 | |
| 238 | public void propertyChange(PropertyChangeEvent e) { |
| 239 | if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
| 240 | updateStyle((JToolBar)e.getSource()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | class SynthToolBarLayoutManager implements LayoutManager { |
| 246 | public void addLayoutComponent(String name, Component comp) {} |
| 247 | |
| 248 | public void removeLayoutComponent(Component comp) {} |
| 249 | |
| 250 | public Dimension minimumLayoutSize(Container parent) { |
| 251 | JToolBar tb = (JToolBar)parent; |
| 252 | Insets insets = tb.getInsets(); |
| 253 | Dimension dim = new Dimension(); |
| 254 | SynthContext context = getContext(tb); |
| 255 | |
| 256 | if (tb.getOrientation() == JToolBar.HORIZONTAL) { |
| 257 | dim.width = tb.isFloatable() ? |
| 258 | SynthIcon.getIconWidth(handleIcon, context) : 0; |
| 259 | Dimension compDim; |
| 260 | for (int i = 0; i < tb.getComponentCount(); i++) { |
| 261 | compDim = tb.getComponent(i).getMinimumSize(); |
| 262 | dim.width += compDim.width; |
| 263 | dim.height = Math.max(dim.height, compDim.height); |
| 264 | } |
| 265 | } else { |
| 266 | dim.height = tb.isFloatable() ? |
| 267 | SynthIcon.getIconHeight(handleIcon, context) : 0; |
| 268 | Dimension compDim; |
| 269 | for (int i = 0; i < tb.getComponentCount(); i++) { |
| 270 | compDim = tb.getComponent(i).getMinimumSize(); |
| 271 | dim.width = Math.max(dim.width, compDim.width); |
| 272 | dim.height += compDim.height; |
| 273 | } |
| 274 | } |
| 275 | dim.width += insets.left + insets.right; |
| 276 | dim.height += insets.top + insets.bottom; |
| 277 | |
| 278 | context.dispose(); |
| 279 | return dim; |
| 280 | } |
| 281 | |
| 282 | public Dimension preferredLayoutSize(Container parent) { |
| 283 | JToolBar tb = (JToolBar)parent; |
| 284 | Insets insets = tb.getInsets(); |
| 285 | Dimension dim = new Dimension(); |
| 286 | SynthContext context = getContext(tb); |
| 287 | |
| 288 | if (tb.getOrientation() == JToolBar.HORIZONTAL) { |
| 289 | dim.width = tb.isFloatable() ? |
| 290 | SynthIcon.getIconWidth(handleIcon, context) : 0; |
| 291 | Dimension compDim; |
| 292 | for (int i = 0; i < tb.getComponentCount(); i++) { |
| 293 | compDim = tb.getComponent(i).getPreferredSize(); |
| 294 | dim.width += compDim.width; |
| 295 | dim.height = Math.max(dim.height, compDim.height); |
| 296 | } |
| 297 | } else { |
| 298 | dim.height = tb.isFloatable() ? |
| 299 | SynthIcon.getIconHeight(handleIcon, context) : 0; |
| 300 | Dimension compDim; |
| 301 | for (int i = 0; i < tb.getComponentCount(); i++) { |
| 302 | compDim = tb.getComponent(i).getPreferredSize(); |
| 303 | dim.width = Math.max(dim.width, compDim.width); |
| 304 | dim.height += compDim.height; |
| 305 | } |
| 306 | } |
| 307 | dim.width += insets.left + insets.right; |
| 308 | dim.height += insets.top + insets.bottom; |
| 309 | |
| 310 | context.dispose(); |
| 311 | return dim; |
| 312 | } |
| 313 | |
| 314 | public void layoutContainer(Container parent) { |
| 315 | JToolBar tb = (JToolBar)parent; |
| 316 | Insets insets = tb.getInsets(); |
| 317 | boolean ltr = tb.getComponentOrientation().isLeftToRight(); |
| 318 | SynthContext context = getContext(tb); |
| 319 | |
| 320 | Component c; |
| 321 | Dimension d; |
| 322 | if (tb.getOrientation() == JToolBar.HORIZONTAL) { |
| 323 | int handleWidth = tb.isFloatable() ? |
| 324 | SynthIcon.getIconWidth(handleIcon, context) : 0; |
| 325 | |
| 326 | // Note: contentRect does not take insets into account |
| 327 | // since it is used for determining the bounds that are |
| 328 | // passed to paintToolBarContentBackground(). |
| 329 | contentRect.x = ltr ? handleWidth : 0; |
| 330 | contentRect.y = 0; |
| 331 | contentRect.width = tb.getWidth() - handleWidth; |
| 332 | contentRect.height = tb.getHeight(); |
| 333 | |
| 334 | // However, we do take the insets into account here for |
| 335 | // the purposes of laying out the toolbar child components. |
| 336 | int x = ltr ? |
| 337 | handleWidth + insets.left : |
| 338 | tb.getWidth() - handleWidth - insets.right; |
| 339 | int baseY = insets.top; |
| 340 | int baseH = tb.getHeight() - insets.top - insets.bottom; |
| 341 | |
| 342 | for (int i = 0; i < tb.getComponentCount(); i++) { |
| 343 | c = tb.getComponent(i); |
| 344 | d = c.getPreferredSize(); |
| 345 | int y, h; |
| 346 | if (d.height >= baseH || c instanceof JSeparator) { |
| 347 | // Fill available height |
| 348 | y = baseY; |
| 349 | h = baseH; |
| 350 | } else { |
| 351 | // Center component vertically in the available space |
| 352 | y = baseY + (baseH / 2) - (d.height / 2); |
| 353 | h = d.height; |
| 354 | } |
| 355 | c.setBounds(ltr ? x : x - d.width, y, d.width, h); |
| 356 | x = ltr ? x + d.width : x - d.width; |
| 357 | } |
| 358 | } else { |
| 359 | int handleHeight = tb.isFloatable() ? |
| 360 | SynthIcon.getIconHeight(handleIcon, context) : 0; |
| 361 | |
| 362 | // See notes above regarding the use of insets |
| 363 | contentRect.x = 0; |
| 364 | contentRect.y = handleHeight; |
| 365 | contentRect.width = tb.getWidth(); |
| 366 | contentRect.height = tb.getHeight() - handleHeight; |
| 367 | |
| 368 | int baseX = insets.left; |
| 369 | int baseW = tb.getWidth() - insets.left - insets.right; |
| 370 | int y = handleHeight + insets.top; |
| 371 | |
| 372 | for (int i = 0; i < tb.getComponentCount(); i++) { |
| 373 | c = tb.getComponent(i); |
| 374 | d = c.getPreferredSize(); |
| 375 | int x, w; |
| 376 | if (d.width >= baseW || c instanceof JSeparator) { |
| 377 | // Fill available width |
| 378 | x = baseX; |
| 379 | w = baseW; |
| 380 | } else { |
| 381 | // Center component horizontally in the available space |
| 382 | x = baseX + (baseW / 2) - (d.width / 2); |
| 383 | w = d.width; |
| 384 | } |
| 385 | c.setBounds(x, y, w, d.height); |
| 386 | y += d.height; |
| 387 | } |
| 388 | } |
| 389 | context.dispose(); |
| 390 | } |
| 391 | } |
| 392 | } |