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.panel.connectionview;
021
022import java.util.Collection;
023import java.util.HashSet;
024import net.sf.gridarta.gui.delayedmapmodel.DelayedMapModelListenerManager;
025import net.sf.gridarta.gui.map.mapview.MapViewManager;
026import net.sf.gridarta.model.archetype.Archetype;
027import net.sf.gridarta.model.baseobject.BaseObject;
028import net.sf.gridarta.model.gameobject.GameObject;
029import net.sf.gridarta.model.maparchobject.MapArchObject;
030import org.jetbrains.annotations.NotNull;
031
032/**
033 * The view of the locked items view control. It holds information about the
034 * connections of the selected locked items key value on the selected map.
035 * @author <a href="mailto:cher@riedquat.de">Christian Hujer</a>
036 * @author Andreas Kirschbaum
037 */
038public class LockedItemsView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends View<String, G, A, R> {
039
040    /**
041     * The serial version UID.
042     */
043    private static final long serialVersionUID = 1L;
044
045    /**
046     * The type numbers to consider.
047     */
048    private final Collection<Integer> typeNumbers = new HashSet<Integer>();
049
050    /**
051     * Create a LockedItemsView.
052     * @param mapViewManager the map view manager
053     * @param delayedMapModelListenerManager the delayed map model listener
054     * manager to use
055     * @param typeNumbers the type numbers to consider
056     */
057    public LockedItemsView(@NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final DelayedMapModelListenerManager<G, A, R> delayedMapModelListenerManager, @NotNull final int... typeNumbers) {
058        super(String.CASE_INSENSITIVE_ORDER, new LockedItemsCellRenderer(), mapViewManager, delayedMapModelListenerManager);
059        for (final int typeNumber : typeNumbers) {
060            this.typeNumbers.add(typeNumber);
061        }
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    protected void scanGameObjectForConnections(@NotNull final G gameObject) {
069        scanGameObject(gameObject);
070        for (final GameObject<G, A, R> invObject : gameObject.recursive()) {
071            scanGameObject(invObject);
072        }
073    }
074
075    /**
076     * Add the given game object as a connection if it has a "slaying" field.
077     * @param gameObject the game object to process
078     */
079    private void scanGameObject(@NotNull final GameObject<G, A, R> gameObject) {
080        if (typeNumbers.contains(gameObject.getTypeNo())) {
081            final String slayingSpec = gameObject.getAttributeString(BaseObject.SLAYING);
082            if (!slayingSpec.isEmpty()) {
083                addConnection(slayingSpec, gameObject);
084            }
085        }
086    }
087
088    /**
089     * A {@link CellRenderer} for the locked items view.
090     * @author Andreas Kirschbaum
091     */
092    private static class LockedItemsCellRenderer extends CellRenderer<String> {
093
094        /**
095         * The serial version UID.
096         */
097        private static final long serialVersionUID = 1L;
098
099        @NotNull
100        @Override
101        protected String formatKey(@NotNull final String key) {
102            return key;
103        }
104
105        @NotNull
106        @Override
107        protected String formatValue(@NotNull final BaseObject<?, ?, ?, ?> gameObject) {
108            return gameObject.getBestName();
109        }
110
111    }
112
113}