Gridarta Editor
GameObjectTextEditor.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.panel.gameobjecttexteditor;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import javax.swing.JPanel;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTextPane;
31 import javax.swing.JViewport;
32 import javax.swing.ScrollPaneConstants;
33 import javax.swing.text.BadLocationException;
34 import javax.swing.text.Document;
35 import javax.swing.text.MutableAttributeSet;
36 import javax.swing.text.StyleConstants;
37 import javax.swing.text.StyleContext;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
47 
53 public class GameObjectTextEditor extends JPanel {
54 
58  private static final long serialVersionUID = 1L;
59 
63  @NotNull
65 
69  @NotNull
70  private final JTextPane archEdit = new ScrollingTextPane();
71 
75  @NotNull
76  private String defaultArchEditValue = "";
77 
83  super(new BorderLayout());
84  this.archetypeTypeSet = archetypeTypeSet;
85  final JScrollPane scrollPane = new JScrollPane();
86  add(scrollPane, BorderLayout.CENTER);
87  scrollPane.setViewportView(archEdit);
88  scrollPane.setBackground(archEdit.getBackground());
89  scrollPane.getViewport().add(archEdit);
90  scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
91  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
92  scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
93  }
94 
99  public boolean canApplyChanges() {
100  return !archEdit.getText().equals(defaultArchEditValue);
101  }
102 
107  public void applyChanges(@NotNull final GameObject<?, ?, ?> gameObject) {
108  gameObject.setObjectText(AttributeListUtils.diffArchTextValues(gameObject.getArchetype(), archEdit.getText()));
109  }
110 
117  @NotNull
118  public Severity refreshDisplay(@Nullable final GameObject<?, ?, ?> gameObject) {
119  archEdit.setEnabled(gameObject != null);
120  archEdit.setText("");
121  Severity severity = Severity.DEFAULT;
122  if (gameObject != null) {
123  final MutableAttributeSet currentAttributes = archEdit.getStyle(StyleContext.DEFAULT_STYLE);
124  try {
125  final Document document = archEdit.getDocument();
126 
127  final ArchetypeType typeStruct = archetypeTypeSet.getArchetypeTypeByBaseObject(gameObject);
128  final String errorText = GameObjectUtils.getSyntaxErrors(gameObject, typeStruct);
129 
130  final String objectText = gameObject.getObjectText();
131  if (errorText == null) {
132  StyleConstants.setForeground(currentAttributes, Color.blue);
133  document.insertString(document.getLength(), objectText, currentAttributes);
134  if (!objectText.isEmpty()) {
135  severity = Severity.MODIFIED;
136  }
137  } else {
138  severity = Severity.ERROR;
139 
140  StyleConstants.setForeground(currentAttributes, Color.red);
141  document.insertString(document.getLength(), errorText, currentAttributes);
142 
143  final Collection<String> errors = new HashSet<>(Arrays.asList(StringUtils.PATTERN_END_OF_LINE.split(errorText)));
144  StyleConstants.setForeground(currentAttributes, Color.blue);
145  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(objectText)) {
146  if (!errors.contains(line)) {
147  document.insertString(document.getLength(), line + '\n', currentAttributes);
148  }
149  }
150  }
151 
152  StyleConstants.setForeground(currentAttributes, Color.black);
153  document.insertString(document.getLength(), AttributeListUtils.diffArchTextKeys(gameObject, gameObject.getArchetype()), currentAttributes);
154  } catch (final BadLocationException ex) {
155  throw new AssertionError(ex);
156  }
157  }
158  archEdit.setCaretPosition(0);
159  defaultArchEditValue = archEdit.getText();
160  return severity;
161  }
162 
167  @NotNull
168  public Component getTextPane() {
169  return archEdit;
170  }
171 
175  public void activate() {
176  archEdit.requestFocusInWindow();
177  final Document document = archEdit.getDocument();
178  if (document != null) {
179  archEdit.setCaretPosition(document.getLength());
180  }
181  }
182 
183 }
net.sf.gridarta.model.archetypetype
Defines types of GameObjects with corresponding attributes.
Definition: AbstractArchetypeAttributeInvSpell.java:20
net.sf.gridarta.gui.utils.Severity.MODIFIED
MODIFIED
The tab contents are modified from defaults.
Definition: Severity.java:39
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.defaultArchEditValue
String defaultArchEditValue
The initial value of archEdit.
Definition: GameObjectTextEditor.java:76
net.sf
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor
Implements the "Game Object Text Editor".
Definition: GameObjectTextEditor.java:53
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.GameObjectTextEditor
GameObjectTextEditor(@NotNull final ArchetypeTypeSet archetypeTypeSet)
Creates a new instance.
Definition: GameObjectTextEditor.java:82
net.sf.gridarta.model.io.AttributeListUtils
Utility class for archetype attribute related functions.
Definition: AttributeListUtils.java:30
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.applyChanges
void applyChanges(@NotNull final GameObject<?, ?, ?> gameObject)
Updates a GameObject's attributes from the input field.
Definition: GameObjectTextEditor.java:107
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet
Manages ArchetypeType instances, list, and bitmask definitions.
Definition: ArchetypeTypeSet.java:40
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.getTextPane
Component getTextPane()
Returns the text input pane.
Definition: GameObjectTextEditor.java:168
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.canApplyChanges
boolean canApplyChanges()
Returns whether the current text field differs from the initial value.
Definition: GameObjectTextEditor.java:99
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.utils.Severity
Severity levels for colors of tabs.
Definition: Severity.java:29
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.model.io.AttributeListUtils.diffArchTextKeys
static String diffArchTextKeys(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final BaseObject<?, ?, ?, ?> archetype)
Returns all attributes from the given game object that don't exist in an archetype.
Definition: AttributeListUtils.java:72
net
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.archetypeTypeSet
final ArchetypeTypeSet archetypeTypeSet
The ArchetypeTypeSet.
Definition: GameObjectTextEditor.java:64
net.sf.gridarta.utils.StringUtils.PATTERN_END_OF_LINE
static final Pattern PATTERN_END_OF_LINE
The pattern to match end of line characters separating lines.
Definition: StringUtils.java:61
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getArchetypeTypeByBaseObject
ArchetypeType getArchetypeTypeByBaseObject(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Returns the ArchetypeType for the given BaseObject.
Definition: ArchetypeTypeSet.java:142
errors
errors
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:1
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.activate
void activate()
Activates this component.
Definition: GameObjectTextEditor.java:175
net.sf.gridarta.model.io.AttributeListUtils.diffArchTextValues
static String diffArchTextValues(@NotNull final BaseObject<?, ?, ?, ?> archetype, @NotNull final CharSequence attributes)
Returns all entries from the given attributes that don't exist in an archetype.
Definition: AttributeListUtils.java:93
net.sf.gridarta.model.gameobject.GameObjectUtils.getSyntaxErrors
static String getSyntaxErrors(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final ArchetypeType type)
This method checks the objectText for syntax errors.
Definition: GameObjectUtils.java:56
net.sf.gridarta.gui.utils.Severity.ERROR
ERROR
The tab contents are invalid.
Definition: Severity.java:44
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.io
Reading and writing of maps, handling of paths.
Definition: AbstractAnimationObjectsReader.java:20
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.refreshDisplay
Severity refreshDisplay(@Nullable final GameObject<?, ?, ?> gameObject)
Refreshes the input field to show the attributes of a {}.
Definition: GameObjectTextEditor.java:118
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.archEdit
final JTextPane archEdit
The JTextPane that contains the attributes of the game object.
Definition: GameObjectTextEditor.java:70
net.sf.gridarta.model
net.sf.gridarta.gui.panel.gameobjecttexteditor.GameObjectTextEditor.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: GameObjectTextEditor.java:58
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.model.archetypetype.ArchetypeType
Contains the data of one Gridarta Object-Type.
Definition: ArchetypeType.java:35
net.sf.gridarta.model.gameobject.GameObjectUtils
Utility class for GameObject related functions.
Definition: GameObjectUtils.java:32
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.panel.gameobjecttexteditor.ScrollingTextPane
A JTextPane that always extends horizontally.
Definition: ScrollingTextPane.java:30
net.sf.gridarta.gui.utils.Severity.DEFAULT
DEFAULT
The tab contents are unchanged from defaults.
Definition: Severity.java:34
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20