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.util.List;
023import javax.swing.DefaultComboBoxModel;
024import net.sf.gridarta.model.archetype.Archetype;
025import net.sf.gridarta.model.gameobject.GameObject;
026import net.sf.gridarta.model.maparchobject.MapArchObject;
027import net.sf.gridarta.model.mapcontrol.MapControl;
028import net.sf.gridarta.model.mapmanager.MapManager;
029import org.jetbrains.annotations.NotNull;
030
031public class MapParameterComboBoxModel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends DefaultComboBoxModel {
032
033    private final Object currentMap = new Object();
034
035    private Object selected;
036
037    private static final long serialVersionUID = 1L;
038
039    /**
040     * The map manager to use.
041     */
042    @NotNull
043    private final MapManager<G, A, R> mapManager;
044
045    /**
046     * Creates a new instance.
047     * @param mapManager the map manager to use
048     */
049    public MapParameterComboBoxModel(@NotNull final MapManager<G, A, R> mapManager) {
050        this.mapManager = mapManager;
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public Object getElementAt(final int index) {
058        if (index == 0) {
059            return currentMap;
060        }
061
062        return mapManager.getOpenedMaps().get(index - 1);
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public int getIndexOf(final Object anObject) {
070        final List<MapControl<G, A, R>> maps = mapManager.getOpenedMaps();
071        if (anObject == currentMap) {
072            return 0;
073        }
074
075        for (int i = 0; i < maps.size(); i++) {
076            if (maps.get(i) == anObject) {
077                return i + 1;
078            }
079        }
080
081        return -1;
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public Object getSelectedItem() {
089        return selected;
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public int getSize() {
097        return mapManager.getOpenedMaps().size() + 1;
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public void setSelectedItem(final Object anObject) {
105        selected = anObject;
106    }
107
108}