Gridarta Editor
DialogAttributeBitmask.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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 java.awt.Component;
23 import javax.swing.text.Document;
24 import javax.swing.text.JTextComponent;
25 import javax.swing.text.Style;
32 import org.apache.log4j.Category;
33 import org.apache.log4j.Logger;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class DialogAttributeBitmask<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends DialogAttribute<G, A, R, ArchetypeAttributeBitmask> {
42 
46  @NotNull
47  private static final Category LOG = Logger.getLogger(DialogAttributeBitmask.class);
48 
52  @NotNull
53  private final JTextComponent input;
54 
58  private int value;
59 
63  @Nullable
65 
71  public DialogAttributeBitmask(@NotNull final ArchetypeAttributeBitmask ref, @NotNull final JTextComponent input) {
72  super(ref);
73  this.input = input;
74  }
75 
76  @NotNull
77  @Override
78  public String getObjectText(@NotNull final G gameObject, @NotNull final Archetype<G, A, R> archetype, @NotNull final String @NotNull [] newMsg, @NotNull final ArchetypeType archetypeType) {
79  final String encodedValue = getEncodedValue(); // get bitmask value
80  final String archetypeAttributeName = getRef().getArchetypeAttributeName();
81  String oldValue = archetype.getAttributeString(archetypeAttributeName);
82  if (oldValue.isEmpty()) {
83  oldValue = "0";
84  }
85 
86  return oldValue.equals(encodedValue) ? "" : archetypeAttributeName + " " + encodedValue;
87  }
88 
89  @Override
90  public void appendSummary(@NotNull final Document doc, @NotNull final Style style) {
91  final String text = input.getText();
92  if (text == null) {
93  return;
94  }
95 
96  final String tmp = text.trim();
97  if (tmp.isEmpty() || tmp.startsWith("<")) {
98  return;
99  }
100 
101  addLine(doc, style, "", " = " + tmp);
102  }
103 
108  public int getValue() {
109  return value;
110  }
111 
116  @NotNull
117  private String getEncodedValue() {
118  return bitmask.encodeValue(value);
119  }
120 
125  public void setValue(final int value) {
126  this.value = value;
127  if (bitmask == null) {
128  LOG.warn("null bitmask");
129  } else {
130  input.setText(bitmask.getText(value));
131  }
132  }
133 
139  public void setEncodedValue(@NotNull final String encodedValue) {
140  assert bitmask != null;
141  setValue(bitmask.decodeValue(encodedValue));
142  }
143 
144  @Nullable
146  return bitmask;
147  }
148 
149  public void setBitmask(@Nullable final AttributeBitmask bitmask) {
150  this.bitmask = bitmask;
151  }
152 
157  @NotNull
158  public Component getInput() {
159  return input;
160  }
161 
162 }
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttribute< G, A, R, ArchetypeAttributeBitmask >::ref
final T ref
Reference to the attribute data.
Definition: DialogAttribute.java:53
net.sf.gridarta.model.archetypetype
Defines types of GameObjects with corresponding attributes.
Definition: AbstractArchetypeAttributeInvSpell.java:20
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttribute< G, A, R, ArchetypeAttributeBitmask >::getRef
T getRef()
Returns the attribute data.
Definition: DialogAttribute.java:68
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.getBitmask
AttributeBitmask getBitmask()
Definition: DialogAttributeBitmask.java:145
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.getObjectText
String getObjectText(@NotNull final G gameObject, @NotNull final Archetype< G, A, R > archetype, @NotNull final String @NotNull[] newMsg, @NotNull final ArchetypeType archetypeType)
Definition: DialogAttributeBitmask.java:78
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.DialogAttributeBitmask
DialogAttributeBitmask(@NotNull final ArchetypeAttributeBitmask ref, @NotNull final JTextComponent input)
Creates a new instance.
Definition: DialogAttributeBitmask.java:71
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.LOG
static final Category LOG
The Logger for printing log messages.
Definition: DialogAttributeBitmask.java:47
net.sf
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.setValue
void setValue(final int value)
Set the active bitmask value.
Definition: DialogAttributeBitmask.java:125
net.sf.gridarta.model.archetypetype.AttributeBitmask.getText
String getText(final int value)
Generate the text to be displayed for a given bitmask value.
Definition: AttributeBitmask.java:102
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttribute< G, A, R, ArchetypeAttributeBitmask >::addLine
void addLine( @NotNull final Document doc, @NotNull final AttributeSet style, @NotNull final String prefix, @NotNull final String postfix)
Appends a line to a Document.
Definition: DialogAttribute.java:101
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.value
int value
Active bitmask value.
Definition: DialogAttributeBitmask.java:58
net.sf.gridarta.model.archetypetype.AttributeBitmask.decodeValue
int decodeValue(@NotNull final String encodedValue)
Convert a value from external representation.
Definition: AttributeBitmask.java:196
net.sf.gridarta.model.archetypetype.AttributeBitmask.encodeValue
String encodeValue(final int value)
Convert a value to external representation.
Definition: AttributeBitmask.java:120
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttribute
A single Attribute, combining an ArchetypeAttribute with its input component(s).
Definition: DialogAttribute.java:41
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.appendSummary
void appendSummary(@NotNull final Document doc, @NotNull final Style style)
Definition: DialogAttributeBitmask.java:90
net
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.getEncodedValue
String getEncodedValue()
Get the active bitmask value in external file representation.
Definition: DialogAttributeBitmask.java:117
net.sf.gridarta.model.archetypetype.AttributeBitmask
This class manages bitmask values which appear in Gridarta archetype attributes.
Definition: AttributeBitmask.java:42
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.bitmask
AttributeBitmask bitmask
Reference to the bitmask data.
Definition: DialogAttributeBitmask.java:64
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.getValue
int getValue()
Get the active bitmask value.
Definition: DialogAttributeBitmask.java:108
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.archetypetype.ArchetypeType
Contains the data of one Gridarta Object-Type.
Definition: ArchetypeType.java:35
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.archetypetype.ArchetypeAttributeBitmask
An ArchetypeAttribute for selecting bitmask values.
Definition: ArchetypeAttributeBitmask.java:29
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.setEncodedValue
void setEncodedValue(@NotNull final String encodedValue)
Set the active bitmask value in external file representation.
Definition: DialogAttributeBitmask.java:139
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.getInput
Component getInput()
Returns the input ui component for editing the value.
Definition: DialogAttributeBitmask.java:158
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask
DialogAttribute for types with bitmasks to choose from.
Definition: DialogAttributeBitmask.java:41
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.setBitmask
void setBitmask(@Nullable final AttributeBitmask bitmask)
Definition: DialogAttributeBitmask.java:149
net.sf.gridarta.gui.dialog.gameobjectattributes.DialogAttributeBitmask.input
final JTextComponent input
The input ui component for editing the value.
Definition: DialogAttributeBitmask.java:53