1 /* 2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.plaf.synth; 27 28 import javax.swing.*; 29 import javax.swing.border.*; 30 import javax.swing.plaf.*; 31 import javax.swing.plaf.basic.*; 32 import java.awt.*; 33 import java.beans.PropertyChangeListener; 34 import java.beans.PropertyChangeEvent; 35 36 /** 37 * Provides the Synth L&F UI delegate for 38 * {@link javax.swing.JList}. 39 * 40 * @author Scott Violet 41 * @since 1.7 42 */ 43 public class SynthListUI extends BasicListUI 44 implements PropertyChangeListener, SynthUI { 45 private SynthStyle style; 46 private boolean useListColors; 47 private boolean useUIBorder; 48 49 /** 50 * Creates a new UI object for the given component. 51 * 52 * @param list component to create UI object for 53 * @return the UI object 54 */ 55 public static ComponentUI createUI(JComponent list) { 56 return new SynthListUI(); 57 } 58 59 /** 60 * Notifies this UI delegate to repaint the specified component. 61 * This method paints the component background, then calls 62 * the {@link #paint} method. 63 * 64 * <p>In general, this method does not need to be overridden by subclasses. 65 * All Look and Feel rendering code should reside in the {@code paint} method. 66 * 67 * @param g the {@code Graphics} object used for painting 68 * @param c the component being painted 69 * @see #paint 70 */ 71 @Override 72 public void update(Graphics g, JComponent c) { 73 SynthContext context = getContext(c); 74 75 SynthLookAndFeel.update(context, g); 76 context.getPainter().paintListBackground(context, 77 g, 0, 0, c.getWidth(), c.getHeight()); 78 context.dispose(); 79 paint(g, c); 80 } 81 82 /** 83 * {@inheritDoc} 84 */ 85 @Override 86 public void paintBorder(SynthContext context, Graphics g, int x, 87 int y, int w, int h) { 88 context.getPainter().paintListBorder(context, g, x, y, w, h); 89 } 90 91 /** 92 * {@inheritDoc} 93 */ 94 @Override 95 protected void installListeners() { 96 super.installListeners(); 97 list.addPropertyChangeListener(this); 98 } 99 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public void propertyChange(PropertyChangeEvent e) { 105 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 106 updateStyle((JList)e.getSource()); 107 } 108 } 109 110 /** 111 * {@inheritDoc} 112 */ 113 @Override 114 protected void uninstallListeners() { 115 super.uninstallListeners(); 116 list.removePropertyChangeListener(this); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 @Override 123 protected void installDefaults() { 124 if (list.getCellRenderer() == null || 125 (list.getCellRenderer() instanceof UIResource)) { 126 list.setCellRenderer(new SynthListCellRenderer()); 127 } 128 updateStyle(list); 129 } 130 131 private void updateStyle(JComponent c) { 132 SynthContext context = getContext(list, ENABLED); 133 SynthStyle oldStyle = style; 134 135 style = SynthLookAndFeel.updateStyle(context, this); 136 137 if (style != oldStyle) { 138 context.setComponentState(SELECTED); 139 Color sbg = list.getSelectionBackground(); 140 if (sbg == null || sbg instanceof UIResource) { 141 list.setSelectionBackground(style.getColor( 142 context, ColorType.TEXT_BACKGROUND)); 143 } 144 145 Color sfg = list.getSelectionForeground(); 146 if (sfg == null || sfg instanceof UIResource) { 147 list.setSelectionForeground(style.getColor( 148 context, ColorType.TEXT_FOREGROUND)); 149 } 150 151 useListColors = style.getBoolean(context, 152 "List.rendererUseListColors", true); 153 useUIBorder = style.getBoolean(context, 154 "List.rendererUseUIBorder", true); 155 156 int height = style.getInt(context, "List.cellHeight", -1); 157 if (height != -1) { 158 list.setFixedCellHeight(height); 159 } 160 if (oldStyle != null) { 161 uninstallKeyboardActions(); 162 installKeyboardActions(); 163 } 164 } 165 context.dispose(); 166 } 167 168 /** 169 * {@inheritDoc} 170 */ 171 @Override 172 protected void uninstallDefaults() { 173 super.uninstallDefaults(); 174 175 SynthContext context = getContext(list, ENABLED); 176 177 style.uninstallDefaults(context); 178 context.dispose(); 179 style = null; 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 @Override 186 public SynthContext getContext(JComponent c) { 187 return getContext(c, getComponentState(c)); 188 } 189 190 private SynthContext getContext(JComponent c, int state) { 191 return SynthContext.getContext(SynthContext.class, c, 192 SynthLookAndFeel.getRegion(c), style, state); 193 } 194 195 private int getComponentState(JComponent c) { 196 return SynthLookAndFeel.getComponentState(c); 197 } 198 199 200 private class SynthListCellRenderer extends DefaultListCellRenderer.UIResource { 201 @Override public String getName() { 202 return "List.cellRenderer"; 203 } 204 205 @Override public void setBorder(Border b) { 206 if (useUIBorder || b instanceof SynthBorder) { 207 super.setBorder(b); 208 } 209 } 210 211 @Override public Component getListCellRendererComponent(JList list, Object value, 212 int index, boolean isSelected, boolean cellHasFocus) { 213 if (!useListColors && (isSelected || cellHasFocus)) { 214 SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel. 215 getUIOfType(getUI(), SynthLabelUI.class), 216 isSelected, cellHasFocus, list.isEnabled(), false); 217 } 218 else { 219 SynthLookAndFeel.resetSelectedUI(); 220 } 221 222 super.getListCellRendererComponent(list, value, index, 223 isSelected, cellHasFocus); 224 return this; 225 } 226 227 @Override public void paint(Graphics g) { 228 super.paint(g); 229 SynthLookAndFeel.resetSelectedUI(); 230 } 231 } 232 }