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.scripts;
021
022import java.awt.event.ActionEvent;
023import java.awt.event.ActionListener;
024import javax.swing.JDialog;
025import net.sf.gridarta.model.archetype.Archetype;
026import net.sf.gridarta.model.gameobject.GameObject;
027import net.sf.gridarta.model.maparchobject.MapArchObject;
028import net.sf.gridarta.model.scripts.ScriptArchData;
029import net.sf.gridarta.model.scripts.ScriptedEvent;
030import org.jetbrains.annotations.NotNull;
031import org.jetbrains.annotations.Nullable;
032
033/**
034 * Small class, listening for button-press events in the popup frame for script
035 * paths or create-new-event frame.
036 * @author Andreas Kirschbaum
037 */
038public class PathButtonListener<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements ActionListener {
039
040    @NotNull
041    private final JDialog frame;
042
043    private final boolean isOkButton;
044
045    /**
046     * The target event to script.
047     */
048    @Nullable
049    private ScriptedEvent<G, A, R> scriptedEvent;
050
051    /**
052     * The ScriptArchData to operate on.
053     */
054    @Nullable
055    private ScriptArchData<G, A, R> scriptArchData;
056
057    /**
058     * The affected game object.
059     */
060    @Nullable
061    private G gameObject;
062
063    /**
064     * The {@link ScriptArchEditor} instance to use.
065     */
066    @NotNull
067    private final ScriptArchEditor<G, A, R> scriptArchEditor;
068
069    /**
070     * Create a PathButtonListener.
071     * @param isOkButton true for ok-buttons
072     * @param frame frame this listener belongs to
073     * @param scriptArchData this is only set for the ok-button of "create new"
074     * frame, otherwise null
075     * @param gameObject the affected game object
076     * @param scriptArchEditor the script arch editor to use
077     * @param scriptedEvent the target event to script
078     */
079    public PathButtonListener(final boolean isOkButton, @NotNull final JDialog frame, @Nullable final ScriptArchData<G, A, R> scriptArchData, @Nullable final G gameObject, @NotNull final ScriptArchEditor<G, A, R> scriptArchEditor, @Nullable final ScriptedEvent<G, A, R> scriptedEvent) {
080        this.isOkButton = isOkButton;
081        this.frame = frame;
082        this.scriptArchData = scriptArchData;
083        this.gameObject = gameObject;
084        this.scriptArchEditor = scriptArchEditor;
085        this.scriptedEvent = scriptedEvent;
086    }
087
088    /**
089     * Set the target event to script.
090     * @param scriptedEvent the new target event to script
091     */
092    public void setTargetEvent(@NotNull final ScriptedEvent<G, A, R> scriptedEvent) {
093        this.scriptedEvent = scriptedEvent;
094    }
095
096    /**
097     * Set the ScriptArchData to operate on.
098     * @param scriptArchData the script arch data to operate on
099     * @param gameObject the affected game object
100     */
101    public void setScriptArchData(@NotNull final ScriptArchData<G, A, R> scriptArchData, @NotNull final G gameObject) {
102        this.scriptArchData = scriptArchData;
103        this.gameObject = gameObject;
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public void actionPerformed(@NotNull final ActionEvent e) {
111        if (isOkButton && scriptArchData == null && scriptedEvent != null) {
112            scriptedEvent.modifyEventPath(); // ok button for modifying path
113        }
114
115        if (isOkButton && scriptArchData != null && gameObject != null) {
116            // ok button for creating a new event/script
117            scriptArchEditor.createNewEvent(frame, scriptArchData, gameObject);
118        } else {
119            frame.setVisible(false); // hide dialog
120        }
121    }
122
123}