Gridarta Editor
DialogAttributeInvSpell.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.dialog.gameobjectattributes;
21 
22 import javax.swing.JComboBox;
23 import javax.swing.text.Document;
24 import javax.swing.text.Style;
32 import org.jetbrains.annotations.NotNull;
33 
34 public class DialogAttributeInvSpell<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends DialogAttribute<G, A, R, ArchetypeAttributeInvSpell> {
35 
39  private final boolean isOptionalSpell;
40 
44  @NotNull
45  private final JComboBox<?> input;
46 
50  @NotNull
52 
60  public DialogAttributeInvSpell(final boolean isOptionalSpell, @NotNull final ArchetypeAttributeInvSpell ref, @NotNull final JComboBox<?> input, @NotNull final Spells<GameObjectSpell<G, A, R>> gameObjectSpells) {
61  super(ref);
62  this.isOptionalSpell = isOptionalSpell;
63  this.input = input;
64  this.gameObjectSpells = gameObjectSpells;
65  }
66 
67  @NotNull
68  @Override
69  public String getObjectText(@NotNull final G gameObject, @NotNull final Archetype<G, A, R> archetype, @NotNull final String[] newMsg, @NotNull final ArchetypeType archetypeType) {
70  final int index = input.getSelectedIndex();
71  if (index < gameObjectSpells.size() + (isOptionalSpell ? 1 : 0)) {
72  final boolean isModified;
73  switch (gameObject.countInvObjects()) {
74  case 0:
75  // game object has no inventory ==> isModified if
76  // anything other than <none> is selected
77  isModified = index != gameObjectSpells.size();
78  break;
79 
80  default:
81  // game object has multiple inventories ==> always isModified
82  isModified = true;
83  break;
84 
85  case 1:
86  if (index >= gameObjectSpells.size()) {
87  // game object has one inventory, <none> is
88  // selected ==> isModified
89  isModified = true;
90  } else {
91  // game object has one inventory, a spell is
92  // selected ==> isModified if a different spell is
93  // selected
94  final GameObject<G, A, R> invObject = gameObject.iterator().next();
95  if (invObject.isDefaultGameObject()) {
96  final String invObjectArchetypeName = invObject.getArchetype().getArchetypeName();
97  final GameObjectSpell<G, A, R> spellObject = gameObjectSpells.getSpell(index);
98  isModified = !invObjectArchetypeName.equals(spellObject.getArchetypeName());
99  } else {
100  isModified = true;
101  }
102  }
103  break;
104  }
105  if (isModified) {
106  gameObject.removeAll();
107  if (index < gameObjectSpells.size()) {
108  final G spellObject = gameObjectSpells.getSpell(index).createGameObject();
109  gameObject.addLast(spellObject);
110  }
111 
112  // remove the entry for a customized spell
113  final int modelSize = input.getModel().getSize();
114  if (modelSize > gameObjectSpells.size() + (isOptionalSpell ? 1 : 0)) {
115  input.removeItemAt(modelSize - 1);
116  }
117  }
118  }
119 
120  return "";
121  }
122 
123  @Override
124  public void appendSummary(@NotNull final Document doc, @NotNull final Style style) {
125  final Object selectedItem = input.getSelectedItem();
126  if (selectedItem == null) {
127  return;
128  }
129 
130  final String value = selectedItem.toString().trim();
131  if (value.isEmpty() || value.startsWith("<")) {
132  return;
133  }
134 
135  addLine(doc, style, "", " = " + value);
136  }
137 
138 }
void appendSummary(@NotNull final Document doc, @NotNull final Style style)
Iterator< G > iterator()
The Iterator returned does not recurse, it only contains objects on the first level.
Contains the data of one Gridarta Object-Type.
String getArchetypeName()
Return the archetype name of the spell object.
String getObjectText(@NotNull final G gameObject, @NotNull final Archetype< G, A, R > archetype, @NotNull final String[] newMsg, @NotNull final ArchetypeType archetypeType)
Base package of all Gridarta classes.
Common base class for spells and spell lists.
Definition: Spells.java:33
Reflects a game object (object on a map).
Definition: GameObject.java:36
final Spells< GameObjectSpell< G, A, R > > gameObjectSpells
The game object spells to display.
GameObjects are the objects based on Archetypes found on maps.
Describes a numbered in-game spell.
R getArchetype()
Returns the Archetype this GameObject is based on.
A single Attribute, combining an ArchetypeAttribute with its input component(s).
void addLine( @NotNull final Document doc, @NotNull final AttributeSet style, @NotNull final String prefix, @NotNull final String postfix)
Appends a line to a Document.
S getSpell(final int index)
Return one spell object by index.
Definition: Spells.java:91
boolean isDefaultGameObject()
Returns whether this game object is unmodified from its underlying archetype.
final JComboBox<?> input
The input ui component for editing the value.
DialogAttributeInvSpell(final boolean isOptionalSpell, @NotNull final ArchetypeAttributeInvSpell ref, @NotNull final JComboBox<?> input, @NotNull final Spells< GameObjectSpell< G, A, R >> gameObjectSpells)
Creates a new instance.
final boolean isOptionalSpell
Whether the spell game object is optional.
An ArchetypeAttribute for selecting a spell encoded as an inventory game object.
Defines types of GameObjects with corresponding attributes.
int size()
Return the number of existing spell objects.
Definition: Spells.java:64