1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.kernel.modules.context; 22 23 import java.lang.reflect.ParameterizedType; 24 import java.util.HashMap; 25 import java.util.Map; 26 27 import javax.swing.ImageIcon; 28 import javax.swing.JComponent; 29 import javax.swing.JTable; 30 31 import net.sourceforge.atunes.gui.AbstractTableCellRendererCode; 32 import net.sourceforge.atunes.model.IControlsBuilder; 33 34 /** 35 * Renderer for context table rows 36 * 37 * @author alex 38 * 39 * @param <T> 40 */ 41 public abstract class ContextTableRowPanelRendererCode<T> extends 42 AbstractTableCellRendererCode<JComponent, T> { 43 44 private final Class<?> clazz; 45 46 private ContextTable table; 47 48 private final ContextTableRowPanelFactory<T> factory; 49 50 private String cacheKeyControl; 51 52 private final Map<T, ContextTableRowPanel<T>> cachedPanels = new HashMap<T, ContextTableRowPanel<T>>(); 53 54 private IControlsBuilder controlsBuilder; 55 56 /** 57 * @param controlsBuilder 58 */ setControlsBuilder(final IControlsBuilder controlsBuilder)59 public void setControlsBuilder(final IControlsBuilder controlsBuilder) { 60 this.controlsBuilder = controlsBuilder; 61 } 62 63 /** 64 * @param lookAndFeel 65 */ 66 @SuppressWarnings("unchecked") ContextTableRowPanelRendererCode()67 public ContextTableRowPanelRendererCode() { 68 this.clazz = (Class<T>) ((ParameterizedType) getClass() 69 .getGenericSuperclass()).getActualTypeArguments()[0]; 70 this.factory = new ContextTableRowPanelFactory<T>(); 71 } 72 73 @Override getComponent(final JComponent superComponent, final JTable t, final T value, final boolean isSelected, final boolean hasFocus, final int row, final int column)74 public JComponent getComponent(final JComponent superComponent, 75 final JTable t, final T value, final boolean isSelected, 76 final boolean hasFocus, final int row, final int column) { 77 if (value != null) { 78 if (this.cacheKeyControl != null 79 && !this.cacheKeyControl.equals(getCacheKeyControl(value))) { 80 // Clean cached panels when similar artist changes 81 this.cachedPanels.clear(); 82 } 83 84 // Create if necessary 85 if (!this.cachedPanels.containsKey(value)) { 86 this.cachedPanels 87 .put(value, createPanel(superComponent, value)); 88 } 89 90 // Get panel 91 ContextTableRowPanel<T> panel = this.cachedPanels.get(value); 92 93 // Update panel 94 panel.setColors(superComponent.getBackground(), 95 superComponent.getForeground()); 96 panel.setFocus(hasFocus); 97 98 return panel; 99 } 100 return superComponent; 101 } 102 103 /** 104 * Binds as renderer and editor to table 105 * 106 * @param table 107 */ bind(final ContextTable table)108 public void bind(final ContextTable table) { 109 this.table = table; 110 this.table.setDefaultRenderer(this.clazz, getLookAndFeel() 111 .getTableCellRenderer(this)); 112 } 113 114 /** 115 * @return 116 */ getTable()117 protected ContextTable getTable() { 118 return this.table; 119 } 120 getPanelForTableRenderer( final ImageIcon image, final String text, final int imageMaxWidth)121 protected final ContextTableRowPanel<T> getPanelForTableRenderer( 122 final ImageIcon image, final String text, final int imageMaxWidth) { 123 124 return this.factory.getPanelForTableRenderer(this.table, image, text, 125 imageMaxWidth, this.controlsBuilder); 126 } 127 128 /** 129 * @param object 130 * @return cache key control value of this object 131 */ getCacheKeyControl(T object)132 public abstract String getCacheKeyControl(T object); 133 134 /** 135 * @param superComponent 136 * @param value 137 * @return 138 */ createPanel( final JComponent superComponent, final T value)139 public abstract ContextTableRowPanel<T> createPanel( 140 final JComponent superComponent, final T value); 141 142 } 143