J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2003 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.awt.*; |
| 29 | import javax.swing.*; |
| 30 | import javax.swing.border.*; |
| 31 | import javax.swing.plaf.*; |
| 32 | import javax.swing.plaf.basic.BasicPanelUI; |
| 33 | import java.awt.*; |
| 34 | import java.awt.event.*; |
| 35 | import java.beans.*; |
| 36 | import sun.swing.plaf.synth.SynthUI; |
| 37 | |
| 38 | /** |
| 39 | * Synth's PanelUI. |
| 40 | * |
| 41 | * @author Steve Wilson |
| 42 | */ |
| 43 | class SynthPanelUI extends BasicPanelUI implements PropertyChangeListener, |
| 44 | SynthUI { |
| 45 | private SynthStyle style; |
| 46 | |
| 47 | public static ComponentUI createUI(JComponent c) { |
| 48 | return new SynthPanelUI(); |
| 49 | } |
| 50 | |
| 51 | public void installUI(JComponent c) { |
| 52 | JPanel p = (JPanel)c; |
| 53 | |
| 54 | super.installUI(c); |
| 55 | installListeners(p); |
| 56 | } |
| 57 | |
| 58 | public void uninstallUI(JComponent c) { |
| 59 | JPanel p = (JPanel)c; |
| 60 | |
| 61 | uninstallListeners(p); |
| 62 | super.uninstallUI(c); |
| 63 | } |
| 64 | |
| 65 | protected void installListeners(JPanel p) { |
| 66 | p.addPropertyChangeListener(this); |
| 67 | } |
| 68 | |
| 69 | protected void uninstallListeners(JPanel p) { |
| 70 | p.removePropertyChangeListener(this); |
| 71 | } |
| 72 | |
| 73 | protected void installDefaults(JPanel p) { |
| 74 | updateStyle(p); |
| 75 | } |
| 76 | |
| 77 | protected void uninstallDefaults(JPanel p) { |
| 78 | SynthContext context = getContext(p, ENABLED); |
| 79 | |
| 80 | style.uninstallDefaults(context); |
| 81 | context.dispose(); |
| 82 | style = null; |
| 83 | } |
| 84 | |
| 85 | private void updateStyle(JPanel c) { |
| 86 | SynthContext context = getContext(c, ENABLED); |
| 87 | style = SynthLookAndFeel.updateStyle(context, this); |
| 88 | context.dispose(); |
| 89 | } |
| 90 | |
| 91 | public SynthContext getContext(JComponent c) { |
| 92 | return getContext(c, getComponentState(c)); |
| 93 | } |
| 94 | |
| 95 | private SynthContext getContext(JComponent c, int state) { |
| 96 | return SynthContext.getContext(SynthContext.class, c, |
| 97 | SynthLookAndFeel.getRegion(c), style, state); |
| 98 | } |
| 99 | |
| 100 | private Region getRegion(JComponent c) { |
| 101 | return SynthLookAndFeel.getRegion(c); |
| 102 | } |
| 103 | |
| 104 | private int getComponentState(JComponent c) { |
| 105 | return SynthLookAndFeel.getComponentState(c); |
| 106 | } |
| 107 | |
| 108 | public void update(Graphics g, JComponent c) { |
| 109 | SynthContext context = getContext(c); |
| 110 | |
| 111 | SynthLookAndFeel.update(context, g); |
| 112 | context.getPainter().paintPanelBackground(context, |
| 113 | g, 0, 0, c.getWidth(), c.getHeight()); |
| 114 | paint(context, g); |
| 115 | context.dispose(); |
| 116 | } |
| 117 | |
| 118 | public void paint(Graphics g, JComponent c) { |
| 119 | SynthContext context = getContext(c); |
| 120 | |
| 121 | paint(context, g); |
| 122 | context.dispose(); |
| 123 | } |
| 124 | |
| 125 | protected void paint(SynthContext context, Graphics g) { |
| 126 | // do actual painting |
| 127 | } |
| 128 | |
| 129 | public void paintBorder(SynthContext context, Graphics g, int x, |
| 130 | int y, int w, int h) { |
| 131 | context.getPainter().paintPanelBorder(context, g, x, y, w, h); |
| 132 | } |
| 133 | |
| 134 | public void propertyChange(PropertyChangeEvent pce) { |
| 135 | if (SynthLookAndFeel.shouldUpdateStyle(pce)) { |
| 136 | updateStyle((JPanel)pce.getSource()); |
| 137 | } |
| 138 | } |
| 139 | } |