001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2011 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.gui.dialog.plugin.parameter;
021
022import java.awt.Component;
023import javax.swing.ComboBoxEditor;
024import javax.swing.DefaultListCellRenderer;
025import javax.swing.JList;
026import net.sf.gridarta.model.baseobject.BaseObject;
027import net.sf.gridarta.model.face.FaceObjectProviders;
028import org.jetbrains.annotations.NotNull;
029
030public class ArchComboBoxCellRenderer extends DefaultListCellRenderer {
031
032    private static final String SIZE_TESTER = "**SizeTester**";
033
034    private static final long serialVersionUID = 1L;
035
036    @NotNull
037    private final ComboBoxEditor archComboBoxEditor;
038
039    /**
040     * The {@link FaceObjectProviders} for looking up faces.
041     */
042    @NotNull
043    private final FaceObjectProviders faceObjectProviders;
044
045    /**
046     * Creates a new instance.
047     * @param archComboBoxEditor the instance to use
048     * @param faceObjectProviders the face object providers for looking up
049     * faces
050     */
051    public ArchComboBoxCellRenderer(@NotNull final ComboBoxEditor archComboBoxEditor, @NotNull final FaceObjectProviders faceObjectProviders) {
052        this.archComboBoxEditor = archComboBoxEditor;
053        this.faceObjectProviders = faceObjectProviders;
054    }
055
056    /* This is the only method defined by ListCellRenderer.  We just
057     * reconfigure the label each time we're called.
058     */
059
060    @Override
061    public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
062        if (SIZE_TESTER.equals(value)) {
063            return archComboBoxEditor.getEditorComponent();
064        }
065        /* The DefaultListCellRenderer class will take care of
066         * the JLabels text property, it's foreground and background
067         *colors, and so on.
068         */
069        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
070
071        /* We additionally set the JLabels icon property here.
072         */
073        final BaseObject<?, ?, ?, ?> arch = (BaseObject<?, ?, ?, ?>) value;
074        if (arch == null) {
075            setText("");
076            setIcon(null);
077            return this;
078        }
079        setText(arch.getArchetype().getArchetypeName());
080
081        setIcon(faceObjectProviders.getFace(arch));
082
083        return this;
084    }
085
086}