Gridarta Editor
EventsTab.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.panel.gameobjectattributes;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.Frame;
26 import java.awt.GridLayout;
27 import javax.swing.Action;
28 import javax.swing.DefaultListModel;
29 import javax.swing.JButton;
30 import javax.swing.JList;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.ListModel;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.ScrollPaneConstants;
36 import javax.swing.border.EtchedBorder;
37 import javax.swing.event.ListSelectionEvent;
38 import javax.swing.event.ListSelectionListener;
51 import net.sf.japi.swing.action.ActionBuilder;
52 import net.sf.japi.swing.action.ActionBuilderFactory;
53 import net.sf.japi.swing.action.ActionMethod;
54 import org.jetbrains.annotations.NotNull;
55 import org.jetbrains.annotations.Nullable;
56 
62 public class EventsTab<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractGameObjectAttributesTab<G, A, R> {
63 
67  @NotNull
68  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
69 
73  @NotNull
74  private final Frame parent;
75 
79  @NotNull
81 
85  @NotNull
87 
91  @NotNull
93 
97  @NotNull
99 
103  @NotNull
105 
109  @NotNull
110  private final JPanel panel = new JPanel();
111 
115  @NotNull
116  private final JScrollPane scrollPane;
117 
121  @NotNull
122  private final Action aEventAddNew = ACTION_BUILDER.createAction(false, "eventAddNew", this);
123 
127  @NotNull
128  private final Action aEventEditData = ACTION_BUILDER.createAction(false, "eventEditData", this);
129 
133  @NotNull
134  private final Action aEventEdit = ACTION_BUILDER.createAction(false, "eventEdit", this);
135 
139  @NotNull
140  private final Action aEventRemove = ACTION_BUILDER.createAction(false, "eventRemove", this);
141 
145  @NotNull
146  private final JList<String> eventList = new JList<>();
147 
152  @Nullable
154 
165  public EventsTab(@NotNull final Frame parent, @NotNull final MapManager<G, A, R> mapManager, @NotNull final GameObjectAttributesModel<G, A, R> gameObjectAttributesModel, @NotNull final ScriptArchEditor<G, A, R> scriptArchEditor, @NotNull final ScriptArchData<G, A, R> scriptArchData, @NotNull final ScriptArchDataUtils<G, A, R> scriptArchDataUtils, @NotNull final ScriptArchUtils scriptArchUtils) {
166  super(gameObjectAttributesModel);
167  this.parent = parent;
168  this.mapManager = mapManager;
169  this.scriptArchEditor = scriptArchEditor;
170  this.scriptArchData = scriptArchData;
171  this.scriptArchDataUtils = scriptArchDataUtils;
172  this.scriptArchUtils = scriptArchUtils;
173 
174  scrollPane = new JScrollPane(eventList);
175  scrollPane.setBorder(new EtchedBorder());
176  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
177  scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
178  scrollPane.setPreferredSize(new Dimension(80, 40));
179 
180  final Container buttons = new JPanel(new GridLayout(4, 1));
181  buttons.add(new JButton(aEventAddNew));
182  buttons.add(new JButton(aEventEditData));
183  buttons.add(new JButton(aEventEdit));
184  buttons.add(new JButton(aEventRemove));
185 
186  panel.setLayout(new BorderLayout());
187  panel.add(scrollPane, BorderLayout.CENTER);
188  panel.add(buttons, BorderLayout.EAST);
189  panel.setPreferredSize(new Dimension(100, 40));
190 
191  eventList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
192  final ListSelectionListener listSelectionListener = new ListSelectionListener() {
193 
194  @Override
195  public void valueChanged(final ListSelectionEvent e) {
196  updateActions();
197  }
198 
199  };
200  eventList.addListSelectionListener(listSelectionListener);
201  refresh(gameObjectAttributesModel.getSelectedGameObject());
202  }
203 
204  @NotNull
205  @Override
206  public JPanel getPanel() {
207  return panel;
208  }
209 
210  @Override
211  public boolean canApply() {
212  return false;
213  }
214 
215  @Override
216  public void activate() {
217  scrollPane.requestFocusInWindow();
218  }
219 
220  @Override
221  protected final void refresh(@Nullable final G gameObject) {
222  selectedGameObject = gameObject;
223  if (gameObject == null || !gameObject.isScripted()) {
225  final ListModel<?> listModel = eventList.getModel();
226  if (listModel != null && listModel.getSize() > 0) {
227  eventList.setModel(new DefaultListModel<>());
228  }
229  } else {
231  eventList.removeAll();
232  scriptArchUtils.addEventsToJList(eventList, gameObject);
233  }
234  updateActions();
235  }
236 
240  private void updateActions() {
241  aEventAddNew.setEnabled(doAddNewEvent(false));
242  aEventEditData.setEnabled(doEditEvent(ScriptTask.EVENT_EDIT_PATH, false));
243  aEventEdit.setEnabled(doEditEvent(ScriptTask.EVENT_OPEN, false));
244  aEventRemove.setEnabled(doEditEvent(ScriptTask.EVENT_REMOVE, false));
245  }
246 
247  @Override
248  protected void apply(@NotNull final G gameObject) {
249  }
250 
254  @ActionMethod
255  public void eventAddNew() {
256  doAddNewEvent(true);
257  }
258 
262  @ActionMethod
263  public void eventEditData() {
265  }
266 
270  @ActionMethod
271  public void eventEdit() {
273  }
274 
278  @ActionMethod
279  public void eventRemove() {
281  }
282 
288  private boolean doAddNewEvent(final boolean performAction) {
289  final GameObject<G, A, R> gameObject = selectedGameObject;
290  if (gameObject == null) {
291  return false;
292  }
293 
294  if (performAction) {
295  final G selectedHead = gameObject.getHead();
296  final MapSquare<G, A, R> mapSquare = selectedHead.getMapSquare();
297  assert mapSquare != null;
298  final MapModel<G, A, R> mapModel = mapSquare.getMapModel();
299  mapModel.beginTransaction("Add event");
300  try {
301  scriptArchEditor.addEventScript(selectedHead, scriptArchData, parent);
302 
303  if (scriptArchData.isEmpty(selectedHead)) {
304  setEventPanelButtonState(true, false, false, false);
305  } else {
306  setEventPanelButtonState(true, true, true, true);
307  scriptArchUtils.addEventsToJList(eventList, selectedHead);
308  }
309  } finally {
310  mapModel.endTransaction();
311  }
312  }
313 
314  return true;
315  }
316 
326  private boolean doEditEvent(@NotNull final ScriptTask task, final boolean performAction) {
327  final GameObject<G, A, R> gameObject = selectedGameObject;
328  if (gameObject == null) {
329  return false;
330  }
331 
332  final ListModel<?> listModel = eventList.getModel();
333  if (listModel == null || listModel.getSize() <= 0) {
334  return false;
335  }
336 
337  final int index = eventList.getSelectedIndex();
338  if (index < 0) {
339  return false;
340  }
341 
342  if (performAction) {
343  final G selectedHead = gameObject.getHead();
344  final MapSquare<G, A, R> mapSquare = selectedHead.getMapSquare();
345  assert mapSquare != null;
346  final MapModel<G, A, R> mapModel = mapSquare.getMapModel();
347  mapModel.beginTransaction("Modify event");
348  try {
349  scriptArchDataUtils.modifyEventScript(index, task, eventList, mapManager, parent, selectedHead);
350  if (scriptArchData.isEmpty(selectedHead)) {
351  setEventPanelButtonState(true, false, false, false);
352  }
353  } finally {
354  mapModel.endTransaction();
355  }
356  }
357 
358  return true;
359  }
360 
368  private void setEventPanelButtonState(final boolean newButton, final boolean modifyButton, final boolean pathButton, final boolean removeButton) {
369  aEventAddNew.setEnabled(newButton);
370  aEventEditData.setEnabled(pathButton);
371  aEventEdit.setEnabled(modifyButton);
372  aEventRemove.setEnabled(removeButton);
373  }
374 
375 }
boolean isEmpty(@NotNull G gameObject)
Returns whether this ScriptArchData is empty (contains no events).
T getHead()
Return the head part of a multi-part object.
Stores and manages information about scripted events.
void modifyEventScript(final int eventIndex, final ScriptTask task, @NotNull final JList< String > panelList, @NotNull final MapManager<?, ?, ?> mapManager, @NotNull final Frame parent, @NotNull final Iterable< G > gameObject)
If there is a scripted event of the specified type, the script pad is opened and the appropriate scri...
Package with common types for event archetypes.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
void addEventsToJList(@NotNull JList< String > list, @NotNull Iterable<? extends GameObject<?, ?, ?>> gameObject)
Set all ScriptedEvents to appear in the given JList This method should be fast because it may be exec...
void setEventPanelButtonState(final boolean newButton, final boolean modifyButton, final boolean pathButton, final boolean removeButton)
Sets the enable/disable states for the four buttons in the event panel.
Definition: EventsTab.java:368
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
Parameter for operation to perform in ScriptTask, javax.swing.JList, net.sf.gridarta.model.mapmanager.MapManager, java.awt.Frame, Iterable).
Definition: ScriptTask.java:28
void endTransaction()
End a transaction.
void eventEditData()
Action method for editing the data of an existing event.
Definition: EventsTab.java:263
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Definition: EventsTab.java:68
void eventAddNew()
Action method for creating a new event.
Definition: EventsTab.java:255
EVENT_EDIT_PATH
Opens a dialog to edit the script parameters.
Definition: ScriptTask.java:38
MODIFIED
The tab contents are modified from defaults.
Definition: Severity.java:39
final MapManager< G, A, R > mapManager
The MapManager.
Definition: EventsTab.java:80
final void refresh(@Nullable final G gameObject)
Definition: EventsTab.java:221
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final ScriptArchData< G, A, R > scriptArchData
The ScriptArchData instance to use.
Definition: EventsTab.java:92
GameObject< G, A, R > selectedGameObject
The currently selected game object.
Definition: EventsTab.java:153
GameObjects are the objects based on Archetypes found on maps.
The "Events" tab in the game object attributes panel.
Definition: EventsTab.java:62
final JList< String > eventList
The JList that shows all events.
Definition: EventsTab.java:146
EVENT_OPEN
Opens an editor for the script code.
Definition: ScriptTask.java:33
final ScriptArchDataUtils< G, A, R > scriptArchDataUtils
The ScriptArchDataUtils to use.
Definition: EventsTab.java:104
void eventEdit()
Action method for editing an existing event.
Definition: EventsTab.java:271
final ScriptArchUtils scriptArchUtils
The ScriptArchUtils to use.
Definition: EventsTab.java:98
void addEventScript(@NotNull final G gameObject, @NotNull final ScriptArchData< G, A, R > scriptArchData, @NotNull final Frame parent)
A popup is opened and the user can create a new scripting event which gets attached to this gameObjec...
final Frame parent
The parent frame for dialog boxes.
Definition: EventsTab.java:74
net.sf.gridarta.model.scripts.ScriptArchData related functions.
Severity levels for colors of tabs.
Definition: Severity.java:29
final JScrollPane scrollPane
The JScrollPane displaying all event.s.
Definition: EventsTab.java:116
Dialog to create events linked to item scripting.
final Action aEventEdit
The action for "edit event code".
Definition: EventsTab.java:134
final ScriptArchEditor< G, A, R > scriptArchEditor
The ScriptArchEditor to use.
Definition: EventsTab.java:86
DEFAULT
The tab contents are unchanged from defaults.
Definition: Severity.java:34
boolean doAddNewEvent(final boolean performAction)
This method is invoked when the user pressed the "new event" button.
Definition: EventsTab.java:288
void eventRemove()
Action method for removing an existing event.
Definition: EventsTab.java:279
void beginTransaction(@NotNull String name)
Starts a new transaction.
final Action aEventRemove
The action for "remove event".
Definition: EventsTab.java:140
EventsTab(@NotNull final Frame parent, @NotNull final MapManager< G, A, R > mapManager, @NotNull final GameObjectAttributesModel< G, A, R > gameObjectAttributesModel, @NotNull final ScriptArchEditor< G, A, R > scriptArchEditor, @NotNull final ScriptArchData< G, A, R > scriptArchData, @NotNull final ScriptArchDataUtils< G, A, R > scriptArchDataUtils, @NotNull final ScriptArchUtils scriptArchUtils)
Creates a new instance.
Definition: EventsTab.java:165
final Action aEventEditData
The action for "edit event parameters".
Definition: EventsTab.java:128
final Action aEventAddNew
The action for "add new event".
Definition: EventsTab.java:122
boolean doEditEvent(@NotNull final ScriptTask task, final boolean performAction)
This method is invoked when the user pressed the "edit event"/"path"/"remove" button from the event p...
Definition: EventsTab.java:326
void updateActions()
Updates the enabled state of all actions.
Definition: EventsTab.java:240
EVENT_REMOVE
Removes the event object.
Definition: ScriptTask.java:43