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.border.*; |
| 30 | import javax.swing.event.*; |
| 31 | import javax.swing.plaf.*; |
| 32 | import javax.swing.plaf.basic.*; |
| 33 | import javax.swing.text.Position; |
| 34 | |
| 35 | import java.awt.*; |
| 36 | import java.awt.event.*; |
| 37 | import java.awt.datatransfer.Transferable; |
| 38 | import java.awt.dnd.*; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.TooManyListenersException; |
| 42 | |
| 43 | import java.beans.PropertyChangeListener; |
| 44 | import java.beans.PropertyChangeEvent; |
| 45 | import sun.swing.plaf.synth.SynthUI; |
| 46 | |
| 47 | /** |
| 48 | * Synth's ListUI. |
| 49 | * |
| 50 | * @author Scott Violet |
| 51 | */ |
| 52 | class SynthListUI extends BasicListUI implements PropertyChangeListener, |
| 53 | SynthUI { |
| 54 | private SynthStyle style; |
| 55 | private boolean useListColors; |
| 56 | private boolean useUIBorder; |
| 57 | |
| 58 | public static ComponentUI createUI(JComponent list) { |
| 59 | return new SynthListUI(); |
| 60 | } |
| 61 | |
| 62 | public void update(Graphics g, JComponent c) { |
| 63 | SynthContext context = getContext(c); |
| 64 | |
| 65 | SynthLookAndFeel.update(context, g); |
| 66 | context.getPainter().paintListBackground(context, |
| 67 | g, 0, 0, c.getWidth(), c.getHeight()); |
| 68 | context.dispose(); |
| 69 | paint(g, c); |
| 70 | } |
| 71 | |
| 72 | public void paintBorder(SynthContext context, Graphics g, int x, |
| 73 | int y, int w, int h) { |
| 74 | context.getPainter().paintListBorder(context, g, x, y, w, h); |
| 75 | } |
| 76 | |
| 77 | protected void installListeners() { |
| 78 | super.installListeners(); |
| 79 | list.addPropertyChangeListener(this); |
| 80 | } |
| 81 | |
| 82 | public void propertyChange(PropertyChangeEvent e) { |
| 83 | if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
| 84 | updateStyle((JList)e.getSource()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | protected void uninstallListeners() { |
| 89 | super.uninstallListeners(); |
| 90 | list.removePropertyChangeListener(this); |
| 91 | } |
| 92 | |
| 93 | protected void installDefaults() { |
| 94 | if (list.getCellRenderer() == null || |
| 95 | (list.getCellRenderer() instanceof UIResource)) { |
| 96 | list.setCellRenderer(new SynthListCellRenderer()); |
| 97 | } |
| 98 | updateStyle(list); |
| 99 | } |
| 100 | |
| 101 | private void updateStyle(JComponent c) { |
| 102 | SynthContext context = getContext(list, ENABLED); |
| 103 | SynthStyle oldStyle = style; |
| 104 | |
| 105 | style = SynthLookAndFeel.updateStyle(context, this); |
| 106 | |
| 107 | if (style != oldStyle) { |
| 108 | context.setComponentState(SELECTED); |
| 109 | Color sbg = list.getSelectionBackground(); |
| 110 | if (sbg == null || sbg instanceof UIResource) { |
| 111 | list.setSelectionBackground(style.getColor( |
| 112 | context, ColorType.TEXT_BACKGROUND)); |
| 113 | } |
| 114 | |
| 115 | Color sfg = list.getSelectionForeground(); |
| 116 | if (sfg == null || sfg instanceof UIResource) { |
| 117 | list.setSelectionForeground(style.getColor( |
| 118 | context, ColorType.TEXT_FOREGROUND)); |
| 119 | } |
| 120 | |
| 121 | useListColors = style.getBoolean(context, |
| 122 | "List.rendererUseListColors", true); |
| 123 | useUIBorder = style.getBoolean(context, |
| 124 | "List.rendererUseUIBorder", true); |
| 125 | |
| 126 | int height = style.getInt(context, "List.cellHeight", -1); |
| 127 | if (height != -1) { |
| 128 | list.setFixedCellHeight(height); |
| 129 | } |
| 130 | if (oldStyle != null) { |
| 131 | uninstallKeyboardActions(); |
| 132 | installKeyboardActions(); |
| 133 | } |
| 134 | } |
| 135 | context.dispose(); |
| 136 | } |
| 137 | |
| 138 | protected void uninstallDefaults() { |
| 139 | super.uninstallDefaults(); |
| 140 | |
| 141 | SynthContext context = getContext(list, ENABLED); |
| 142 | |
| 143 | style.uninstallDefaults(context); |
| 144 | context.dispose(); |
| 145 | style = null; |
| 146 | } |
| 147 | |
| 148 | public SynthContext getContext(JComponent c) { |
| 149 | return getContext(c, getComponentState(c)); |
| 150 | } |
| 151 | |
| 152 | private SynthContext getContext(JComponent c, int state) { |
| 153 | return SynthContext.getContext(SynthContext.class, c, |
| 154 | SynthLookAndFeel.getRegion(c), style, state); |
| 155 | } |
| 156 | |
| 157 | private Region getRegion(JComponent c) { |
| 158 | return SynthLookAndFeel.getRegion(c); |
| 159 | } |
| 160 | |
| 161 | private int getComponentState(JComponent c) { |
| 162 | return SynthLookAndFeel.getComponentState(c); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | private class SynthListCellRenderer extends DefaultListCellRenderer.UIResource { |
| 167 | public String getName() { |
| 168 | return "List.cellRenderer"; |
| 169 | } |
| 170 | |
| 171 | public void setBorder(Border b) { |
| 172 | if (useUIBorder || b instanceof SynthBorder) { |
| 173 | super.setBorder(b); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public Component getListCellRendererComponent(JList list, Object value, |
| 178 | int index, boolean isSelected, boolean cellHasFocus) { |
| 179 | if (!useListColors && (isSelected || cellHasFocus)) { |
| 180 | SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel. |
| 181 | getUIOfType(getUI(), SynthLabelUI.class), |
| 182 | isSelected, cellHasFocus, list.isEnabled(), false); |
| 183 | } |
| 184 | else { |
| 185 | SynthLookAndFeel.resetSelectedUI(); |
| 186 | } |
| 187 | |
| 188 | super.getListCellRendererComponent(list, value, index, |
| 189 | isSelected, cellHasFocus); |
| 190 | return this; |
| 191 | } |
| 192 | |
| 193 | public void paint(Graphics g) { |
| 194 | super.paint(g); |
| 195 | SynthLookAndFeel.resetSelectedUI(); |
| 196 | } |
| 197 | } |
| 198 | } |