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.DefaultListCellRenderer;
024import javax.swing.JList;
025import net.sf.gridarta.model.mapcontrol.MapControl;
026import net.sf.gridarta.model.mapmanager.MapManager;
027import org.jetbrains.annotations.NotNull;
028
029public class MapParameterCellRenderer extends DefaultListCellRenderer {
030
031    /**
032     * The serial version UID.
033     */
034    private static final long serialVersionUID = 1L;
035
036    /**
037     * The map manager to use.
038     */
039    @NotNull
040    private final MapManager<?, ?, ?> mapManager;
041
042    /**
043     * Create a new instance.
044     * @param mapManager the map manager to use
045     */
046    public MapParameterCellRenderer(@NotNull final MapManager<?, ?, ?> mapManager) {
047        this.mapManager = mapManager;
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
055        final String newVal;
056        if (value instanceof MapControl) {
057            newVal = ((MapControl<?, ?, ?>) value).getMapModel().getMapArchObject().getMapName();
058        } else {
059            final MapControl<?, ?, ?> mapControl = mapManager.getCurrentMap();
060            final String mapName = mapControl != null ? mapControl.getMapModel().getMapArchObject().getMapName() : "";
061            newVal = "Current - " + mapName;
062        }
063        return super.getListCellRendererComponent(list, newVal, index, isSelected, cellHasFocus);
064    }
065
066}