Gridarta Editor
DialogAttributeTreasure.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.text.Document;
23 import javax.swing.text.JTextComponent;
24 import javax.swing.text.Style;
32 import org.jetbrains.annotations.NotNull;
33 
34 // a treasurelist attribute
35 
36 public class DialogAttributeTreasure<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends DialogAttribute<G, A, R, ArchetypeAttributeTreasure> {
37 
41  @NotNull
42  private final JTextComponent input;
43 
47  @NotNull
48  private final TreasureTree treasureTree;
49 
56  public DialogAttributeTreasure(@NotNull final ArchetypeAttributeTreasure ref, @NotNull final JTextComponent input, @NotNull final TreasureTree treasureTree) {
57  super(ref);
58  this.input = input;
59  this.treasureTree = treasureTree;
60  }
61 
62  @NotNull
63  @Override
64  public String getObjectText(@NotNull final G gameObject, @NotNull final Archetype<G, A, R> archetype, @NotNull final String[] newMsg, @NotNull final ArchetypeType archetypeType) throws InvalidAttributeException {
65  final String inline = input.getText().trim(); // input string
66  final boolean isNone = inline.equals(CFTreasureListTree.NONE_SYM) || inline.isEmpty();
67  final String archetypeAttributeName = getRef().getArchetypeAttributeName();
68  if (!isNone && treasureTree.get(inline) == null && !inline.equalsIgnoreCase(archetype.getAttributeString(archetypeAttributeName))) {
69  // The user has specified a WRONG treasurelist name, and it does not come
70  // from the default gameObject. -> Error and out.
71  throw new InvalidAttributeException("In attribute '" + getRef().getAttributeName() + "':\n" + "'" + inline + "' is not a known treasurelist name!");
72  }
73 
74  if (!inline.equalsIgnoreCase(archetype.getAttributeString(archetypeAttributeName)) && !(isNone && !archetype.hasAttribute(archetypeAttributeName))) {
75  if (isNone) {
76  return archetypeAttributeName + " none";
77  } else {
78  return archetypeAttributeName + " " + inline;
79  }
80  }
81 
82  return "";
83  }
84 
85  @Override
86  public void appendSummary(@NotNull final Document doc, @NotNull final Style style) {
87  final String text = input.getText();
88  if (text == null) {
89  return;
90  }
91 
92  final String value = text.trim();
93  if (value.isEmpty() || value.equals(CFTreasureListTree.NONE_SYM)) {
94  return;
95  }
96 
97  addLine(doc, style, "", " = " + value);
98  }
99 
100 }
TreasureTreeNode get(@NotNull final String name)
Returns a treasure list by name.
Graphical User Interface of Gridarta.
DialogAttributeTreasure(@NotNull final ArchetypeAttributeTreasure ref, @NotNull final JTextComponent input, @NotNull final TreasureTree treasureTree)
Creates a new instance.
Contains the data of one Gridarta Object-Type.
void appendSummary(@NotNull final Document doc, @NotNull final Style style)
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
The CFTreasureListTree class fully manages treasurelists.
String getObjectText(@NotNull final G gameObject, @NotNull final Archetype< G, A, R > archetype, @NotNull final String[] newMsg, @NotNull final ArchetypeType archetypeType)
static final String NONE_SYM
The string displayed in attribute dialog for "none".
A single Attribute, combining an ArchetypeAttribute with its input component(s).
final JTextComponent input
The input ui component for editing the value.
Stores all defined treasure lists.
void addLine( @NotNull final Document doc, @NotNull final AttributeSet style, @NotNull final String prefix, @NotNull final String postfix)
Appends a line to a Document.
Defines types of GameObjects with corresponding attributes.