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-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 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[] 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  if (!oldValue.equals(encodedValue)) {
87  return archetypeAttributeName + " " + encodedValue;
88  }
89 
90  return "";
91  }
92 
93  @Override
94  public void appendSummary(@NotNull final Document doc, @NotNull final Style style) {
95  final String text = input.getText();
96  if (text == null) {
97  return;
98  }
99 
100  final String tmp = text.trim();
101  if (tmp.isEmpty() || tmp.startsWith("<")) {
102  return;
103  }
104 
105  addLine(doc, style, "", " = " + tmp);
106  }
107 
112  public int getValue() {
113  return value;
114  }
115 
120  @NotNull
121  private String getEncodedValue() {
122  return bitmask.encodeValue(value);
123  }
124 
129  public void setValue(final int value) {
130  this.value = value;
131  if (bitmask != null) {
132  input.setText(bitmask.getText(value));
133  } else {
134  LOG.warn("null bitmask");
135  }
136  }
137 
143  public void setEncodedValue(@NotNull final String encodedValue) {
144  assert bitmask != null;
145  setValue(bitmask.decodeValue(encodedValue));
146  }
147 
148  @Nullable
150  return bitmask;
151  }
152 
153  public void setBitmask(@Nullable final AttributeBitmask bitmask) {
154  this.bitmask = bitmask;
155  }
156 
161  @NotNull
162  public Component getInput() {
163  return input;
164  }
165 
166 }
DialogAttribute for types with bitmasks to choose from.
String getObjectText(@NotNull final G gameObject, @NotNull final Archetype< G, A, R > archetype, @NotNull final String[] newMsg, @NotNull final ArchetypeType archetypeType)
void appendSummary(@NotNull final Document doc, @NotNull final Style style)
Contains the data of one Gridarta Object-Type.
void setEncodedValue(@NotNull final String encodedValue)
Set the active bitmask value in external file representation.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
String encodeValue(final int value)
Convert a value to external representation.
GameObjects are the objects based on Archetypes found on maps.
This class manages bitmask values which appear in Gridarta archetype attributes.
int decodeValue(@NotNull final String encodedValue)
Convert a value from external representation.
An ArchetypeAttribute for selecting bitmask values.
Component getInput()
Returns the input ui component for editing the value.
static final Category LOG
The Logger for printing log messages.
A single Attribute, combining an ArchetypeAttribute with its input component(s).
final JTextComponent input
The input ui component for editing the value.
DialogAttributeBitmask(@NotNull final ArchetypeAttributeBitmask ref, @NotNull final JTextComponent input)
Creates a new instance.
void addLine( @NotNull final Document doc, @NotNull final AttributeSet style, @NotNull final String prefix, @NotNull final String postfix)
Appends a line to a Document.
String getText(final int value)
Generate the text to be displayed for a given bitmask value.
Defines types of GameObjects with corresponding attributes.
String getEncodedValue()
Get the active bitmask value in external file representation.