| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.component; |
| 16 | |
| 17 | import java.util.HashMap; |
| 18 | import java.util.HashSet; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import javax.swing.Icon; |
| 23 | import javax.swing.JLabel; |
| 24 | |
| 25 | public class IconMapRendererDelegate extends AbstractRendererDelegate |
| 26 | implements RendererInformation |
| 27 | { |
| 28 | @Override protected void init() |
| 29 | { |
| 30 | for (Object value: _keys) |
| 31 | { |
| 32 | String key = getKey(value); |
| 33 | addIcon(value, _map.getIcon(key)); |
| 34 | } |
| 35 | _keys = null; |
| 36 | } |
| 37 | |
| 38 | public void addIcon(Object value) |
| 39 | { |
| 40 | if (_map != null) |
| 41 | { |
| 42 | addIcon(value, _map.getIcon(getKey(value))); |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | _keys.add(value); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public void addIcon(Object value, Icon icon) |
| 51 | { |
| 52 | _icons.put(value, icon); |
| 53 | } |
| 54 | |
| 55 | public int getCellHeight() |
| 56 | { |
| 57 | if (_height == 0) |
| 58 | { |
| 59 | for (Icon icon: _icons.values()) |
| 60 | { |
| 61 | if (icon != null && icon.getIconHeight() > _height) |
| 62 | { |
| 63 | _height = icon.getIconHeight(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return _height; |
| 68 | } |
| 69 | |
| 70 | @Override public void setValue(JLabel renderer, Object value) |
| 71 | { |
| 72 | renderer.setIcon(_icons.get(value)); |
| 73 | renderer.setText(null); |
| 74 | } |
| 75 | |
| 76 | private Set<Object> _keys = new HashSet<Object>(); |
| 77 | private final Map<Object, Icon> _icons = new HashMap<Object, Icon>(); |
| 78 | private int _height; |
| 79 | } |