J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2005 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 java.beans.*; |
| 29 | import javax.swing.*; |
| 30 | import java.awt.Dimension; |
| 31 | import java.awt.Graphics; |
| 32 | import java.awt.Insets; |
| 33 | import javax.swing.plaf.ComponentUI; |
| 34 | import javax.swing.plaf.SeparatorUI; |
| 35 | import javax.swing.plaf.UIResource; |
| 36 | import javax.swing.plaf.DimensionUIResource; |
| 37 | import sun.swing.plaf.synth.SynthUI; |
| 38 | |
| 39 | /** |
| 40 | * A Synth L&F implementation of SeparatorUI. This implementation |
| 41 | * is a "combined" view/controller. |
| 42 | * |
| 43 | * @author Shannon Hickey |
| 44 | * @author Joshua Outwater |
| 45 | */ |
| 46 | class SynthSeparatorUI extends SeparatorUI implements PropertyChangeListener, |
| 47 | SynthUI { |
| 48 | private SynthStyle style; |
| 49 | |
| 50 | public static ComponentUI createUI(JComponent c) { |
| 51 | return new SynthSeparatorUI(); |
| 52 | } |
| 53 | |
| 54 | public void installUI(JComponent c) { |
| 55 | installDefaults((JSeparator)c); |
| 56 | installListeners((JSeparator)c); |
| 57 | } |
| 58 | |
| 59 | public void uninstallDefaults(JComponent c) { |
| 60 | uninstallListeners((JSeparator)c); |
| 61 | uninstallDefaults((JSeparator)c); |
| 62 | } |
| 63 | |
| 64 | public void installDefaults(JSeparator c) { |
| 65 | updateStyle(c); |
| 66 | } |
| 67 | |
| 68 | private void updateStyle(JSeparator sep) { |
| 69 | SynthContext context = getContext(sep, ENABLED); |
| 70 | SynthStyle oldStyle = style; |
| 71 | |
| 72 | style = SynthLookAndFeel.updateStyle(context, this); |
| 73 | |
| 74 | if (style != oldStyle) { |
| 75 | if (sep instanceof JToolBar.Separator) { |
| 76 | Dimension size = ((JToolBar.Separator)sep).getSeparatorSize(); |
| 77 | if (size == null || size instanceof UIResource) { |
| 78 | size = (DimensionUIResource)style.get( |
| 79 | context, "ToolBar.separatorSize"); |
| 80 | if (size == null) { |
| 81 | size = new DimensionUIResource(10, 10); |
| 82 | } |
| 83 | ((JToolBar.Separator)sep).setSeparatorSize(size); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | context.dispose(); |
| 89 | } |
| 90 | |
| 91 | public void uninstallDefaults(JSeparator c) { |
| 92 | SynthContext context = getContext(c, ENABLED); |
| 93 | |
| 94 | style.uninstallDefaults(context); |
| 95 | context.dispose(); |
| 96 | style = null; |
| 97 | } |
| 98 | |
| 99 | public void installListeners(JSeparator c) { |
| 100 | c.addPropertyChangeListener(this); |
| 101 | } |
| 102 | |
| 103 | public void uninstallListeners(JSeparator c) { |
| 104 | c.removePropertyChangeListener(this); |
| 105 | } |
| 106 | |
| 107 | public void update(Graphics g, JComponent c) { |
| 108 | SynthContext context = getContext(c); |
| 109 | |
| 110 | JSeparator separator = (JSeparator)context.getComponent(); |
| 111 | SynthLookAndFeel.update(context, g); |
| 112 | context.getPainter().paintSeparatorBackground(context, |
| 113 | g, 0, 0, c.getWidth(), c.getHeight(), |
| 114 | separator.getOrientation()); |
| 115 | paint(context, g); |
| 116 | context.dispose(); |
| 117 | } |
| 118 | |
| 119 | public void paint(Graphics g, JComponent c) { |
| 120 | SynthContext context = getContext(c); |
| 121 | |
| 122 | paint(context, g); |
| 123 | context.dispose(); |
| 124 | } |
| 125 | |
| 126 | protected void paint(SynthContext context, Graphics g) { |
| 127 | JSeparator separator = (JSeparator)context.getComponent(); |
| 128 | context.getPainter().paintSeparatorForeground(context, g, 0, 0, |
| 129 | separator.getWidth(), separator.getHeight(), |
| 130 | separator.getOrientation()); |
| 131 | } |
| 132 | |
| 133 | public void paintBorder(SynthContext context, Graphics g, int x, |
| 134 | int y, int w, int h) { |
| 135 | JSeparator separator = (JSeparator)context.getComponent(); |
| 136 | context.getPainter().paintSeparatorBorder(context, g, x, y, w, h, |
| 137 | separator.getOrientation()); |
| 138 | } |
| 139 | |
| 140 | public Dimension getPreferredSize(JComponent c) { |
| 141 | SynthContext context = getContext(c); |
| 142 | |
| 143 | int thickness = style.getInt(context, "Separator.thickness", 2); |
| 144 | Insets insets = c.getInsets(); |
| 145 | Dimension size; |
| 146 | |
| 147 | if (((JSeparator)c).getOrientation() == JSeparator.VERTICAL) { |
| 148 | size = new Dimension(insets.left + insets.right + thickness, |
| 149 | insets.top + insets.bottom); |
| 150 | } else { |
| 151 | size = new Dimension(insets.left + insets.right, |
| 152 | insets.top + insets.bottom + thickness); |
| 153 | } |
| 154 | context.dispose(); |
| 155 | return size; |
| 156 | } |
| 157 | |
| 158 | public Dimension getMinimumSize(JComponent c) { |
| 159 | return getPreferredSize(c); |
| 160 | } |
| 161 | |
| 162 | public Dimension getMaximumSize(JComponent c) { |
| 163 | return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); |
| 164 | } |
| 165 | |
| 166 | public SynthContext getContext(JComponent c) { |
| 167 | return getContext(c, getComponentState(c)); |
| 168 | } |
| 169 | |
| 170 | private SynthContext getContext(JComponent c, int state) { |
| 171 | return SynthContext.getContext(SynthContext.class, c, |
| 172 | SynthLookAndFeel.getRegion(c), style, state); |
| 173 | } |
| 174 | |
| 175 | private Region getRegion(JComponent c) { |
| 176 | return SynthLookAndFeel.getRegion(c); |
| 177 | } |
| 178 | |
| 179 | private int getComponentState(JComponent c) { |
| 180 | return SynthLookAndFeel.getComponentState(c); |
| 181 | } |
| 182 | |
| 183 | public void propertyChange(PropertyChangeEvent evt) { |
| 184 | if (SynthLookAndFeel.shouldUpdateStyle(evt)) { |
| 185 | updateStyle((JSeparator)evt.getSource()); |
| 186 | } |
| 187 | } |
| 188 | } |