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.Comparator;
023import net.sf.gridarta.gui.delayedmapmodel.DelayedMapModelListenerManager;
024import net.sf.gridarta.gui.map.mapview.MapViewManager;
025import net.sf.gridarta.model.archetype.Archetype;
026import net.sf.gridarta.model.baseobject.BaseObject;
027import net.sf.gridarta.model.connectionview.Connections;
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 connection view control. It holds information about the
034 * connections of the selected connection value on the selected map.
035 * @author <a href="mailto:cher@riedquat.de">Christian Hujer</a>
036 * @author Andreas Kirschbaum
037 */
038public class ConnectionView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends View<Integer, G, A, R> {
039
040    /**
041     * The serial version UID.
042     */
043    private static final long serialVersionUID = 1L;
044
045    /**
046     * Create a ConnectionView.
047     * @param mapViewManager the map view manager
048     * @param delayedMapModelListenerManager the delayed map model listener
049     * manager to use
050     */
051    public ConnectionView(@NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final DelayedMapModelListenerManager<G, A, R> delayedMapModelListenerManager) {
052        super(new Comparator<Integer>() {
053
054                @Override
055                public int compare(@NotNull final Integer o1, @NotNull final Integer o2) {
056                    if (o1 < o2) {
057                        return -1;
058                    }
059                    if (o1 > o2) {
060                        return +1;
061                    }
062                    return 0;
063                }
064            }, new ConnectionCellRenderer(), mapViewManager, delayedMapModelListenerManager);
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    protected void scanGameObjectForConnections(@NotNull final G gameObject) {
072        scanGameObject(gameObject);
073        for (final GameObject<G, A, R> invObject : gameObject.recursive()) {
074            scanGameObject(invObject);
075        }
076    }
077
078    /**
079     * Add the given game object as a connection if it has a "connected" field.
080     * @param gameObject the game object to process
081     */
082    private void scanGameObject(@NotNull final GameObject<G, A, R> gameObject) {
083        final int[] values = Connections.parseConnections(gameObject);
084        if (values == null) {
085            return;
086        }
087
088        for (final int value : values) {
089            addConnection(value, gameObject);
090        }
091    }
092
093    /**
094     * A {@link CellRenderer} for the connection view.
095     * @author Andreas Kirschbaum
096     */
097    private static class ConnectionCellRenderer extends CellRenderer<Integer> {
098
099        /**
100         * The serial version UID.
101         */
102        private static final long serialVersionUID = 1L;
103
104        @NotNull
105        @Override
106        protected String formatKey(@NotNull final Integer key) {
107            return key.toString();
108        }
109
110        @NotNull
111        @Override
112        protected String formatValue(@NotNull final BaseObject<?, ?, ?, ?> gameObject) {
113            return gameObject.getBestName();
114        }
115
116    }
117
118}