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.io.Serializable;
023import java.util.Comparator;
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 net.sf.gridarta.model.match.GameObjectMatcher;
031import org.jetbrains.annotations.NotNull;
032
033/**
034 * The view of the connection view control. It holds information about the
035 * connections of the selected connection value on the selected map.
036 * @author <a href="mailto:cher@riedquat.de">Christian Hujer</a>
037 * @author Andreas Kirschbaum
038 */
039public class MonsterView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends View<GameObject<G, A, R>, G, A, R> {
040
041    /**
042     * The serial version UID.
043     */
044    private static final long serialVersionUID = 1L;
045
046    /**
047     * The {@link GameObjectMatcher} for matching monster objects.
048     */
049    @NotNull
050    private final GameObjectMatcher monsterMatcher;
051
052    /**
053     * Create a MonsterView.
054     * @param mapViewManager the map view manager
055     * @param delayedMapModelListenerManager the delayed map model listener
056     * manager to use
057     * @param monsterMatcher the <code>GameObjectMatcher</code> for matching
058     * monster objects
059     */
060    public MonsterView(@NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final DelayedMapModelListenerManager<G, A, R> delayedMapModelListenerManager, @NotNull final GameObjectMatcher monsterMatcher) {
061        super(new MonsterComparator<G, A, R>(), new MonsterCellRenderer<G, A, R>(), mapViewManager, delayedMapModelListenerManager);
062        this.monsterMatcher = monsterMatcher;
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    protected void scanGameObjectForConnections(@NotNull final G gameObject) {
070        scanGameObject(gameObject);
071        for (final GameObject<G, A, R> invObject : gameObject.recursive()) {
072            scanGameObject(invObject);
073        }
074    }
075
076    /**
077     * Add the given game object as a connection if it is a monster object.
078     * @param gameObject the game object to process
079     */
080    private void scanGameObject(@NotNull final GameObject<G, A, R> gameObject) {
081        if (gameObject.isHead() && monsterMatcher.isMatching(gameObject)) {
082            addConnection(gameObject, gameObject);
083        }
084    }
085
086    /**
087     * A {@link Comparator} for ordering the values of this view.
088     */
089    private static class MonsterComparator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Comparator<GameObject<G, A, R>>, Serializable {
090
091        /**
092         * The serial version UID.
093         */
094        private static final long serialVersionUID = 1L;
095
096        @Override
097        public int compare(@NotNull final GameObject<G, A, R> o1, @NotNull final GameObject<G, A, R> o2) {
098            final int level1 = o1.getAttributeInt(BaseObject.LEVEL);
099            final int level2 = o2.getAttributeInt(BaseObject.LEVEL);
100            if (level1 < level2) {
101                return +1;
102            }
103            if (level1 > level2) {
104                return -1;
105            }
106
107            final String name1 = o1.getBestName();
108            final String name2 = o2.getBestName();
109            return String.CASE_INSENSITIVE_ORDER.compare(name1, name2);
110        }
111
112    }
113}