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.event.ItemEvent;
023import java.awt.event.ItemListener;
024import java.io.File;
025import javax.swing.ComboBoxModel;
026import javax.swing.JComboBox;
027import javax.swing.JComponent;
028import javax.swing.JPanel;
029import net.sf.gridarta.model.archetype.Archetype;
030import net.sf.gridarta.model.gameobject.GameObject;
031import net.sf.gridarta.model.maparchobject.MapArchObject;
032import net.sf.gridarta.model.mapcontrol.MapControl;
033import net.sf.gridarta.model.mapmanager.MapManager;
034import net.sf.gridarta.plugin.parameter.MapParameter;
035import org.jetbrains.annotations.NotNull;
036
037public class MapParameterView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements PluginParameterView<G, A, R> {
038
039    @NotNull
040    private final MapParameter<G, A, R> parameter;
041
042    @NotNull
043    private final JComponent config = new JPanel();
044
045    @NotNull
046    private final JComboBox value;
047
048    public MapParameterView(@NotNull final MapParameter<G, A, R> param, @NotNull final MapManager<G, A, R> mapManager) {
049        parameter = param;
050        value = new JComboBox();
051        final ComboBoxModel myModel = new MapParameterComboBoxModel<G, A, R>(mapManager);
052        value.setModel(myModel);
053        value.setRenderer(new MapParameterCellRenderer(mapManager));
054        value.addItemListener(new ItemListener() {
055
056            @Override
057            public void itemStateChanged(final ItemEvent e) {
058                if (e.getStateChange() == ItemEvent.SELECTED) {
059                    if (value.getSelectedIndex() == 0) {
060                        parameter.setValueToCurrent();
061                    } else {
062                        final MapControl<?, ?, ?> mapControl = (MapControl<?, ?, ?>) value.getSelectedItem();
063                        final String stringValue;
064                        if (mapControl == null) {
065                            stringValue = "";
066                        } else {
067                            final File mapFile = mapControl.getMapModel().getMapFile();
068                            stringValue = mapFile == null ? "" : mapFile.getPath();
069                        }
070                        parameter.setStringValue(stringValue);
071                    }
072                }
073            }
074        });
075        value.setSelectedItem(parameter.getValue());
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @NotNull
082    @Override
083    public JComponent getValueComponent() {
084        return value;
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    @NotNull
091    @Override
092    public JComponent getConfigComponent() {
093        return config;
094    }
095
096}