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-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.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 
82  public GameObjectTextEditor(@NotNull final ArchetypeTypeSet archetypeTypeSet) {
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<>();
144  errors.addAll(Arrays.asList(StringUtils.PATTERN_END_OF_LINE.split(errorText)));
145  StyleConstants.setForeground(currentAttributes, Color.blue);
146  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(objectText)) {
147  if (!errors.contains(line)) {
148  document.insertString(document.getLength(), line + '\n', currentAttributes);
149  }
150  }
151  }
152 
153  StyleConstants.setForeground(currentAttributes, Color.black);
154  document.insertString(document.getLength(), AttributeListUtils.diffArchTextKeys(gameObject, gameObject.getArchetype()), currentAttributes);
155  } catch (final BadLocationException ex) {
156  throw new AssertionError(ex);
157  }
158  }
159  archEdit.setCaretPosition(0);
160  defaultArchEditValue = archEdit.getText();
161  return severity;
162  }
163 
168  @NotNull
169  public Component getTextPane() {
170  return archEdit;
171  }
172 
176  public void activate() {
177  archEdit.requestFocusInWindow();
178  final Document document = archEdit.getDocument();
179  if (document != null) {
180  archEdit.setCaretPosition(document.getLength());
181  }
182  }
183 
184 }
Utility class for string manipulation.
Graphical User Interface of Gridarta.
Reading and writing of maps, handling of paths.
Manages ArchetypeType instances, list, and bitmask definitions.
static String getSyntaxErrors(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final ArchetypeType type)
This method checks the objectText for syntax errors.
ArchetypeType getArchetypeTypeByBaseObject(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Returns the ArchetypeType for the given BaseObject.
void applyChanges(@NotNull final GameObject<?, ?, ?> gameObject)
Updates a GameObject&#39;s attributes from the input field.
Contains the data of one Gridarta Object-Type.
Severity refreshDisplay(@Nullable final GameObject<?, ?, ?> gameObject)
Refreshes the input field to show the attributes of a GameObject.
MODIFIED
The tab contents are modified from defaults.
Definition: Severity.java:39
Base package of all Gridarta classes.
boolean canApplyChanges()
Returns whether the current text field differs from the initial value.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
Utility class for GameObject related functions.
Utility class for archetype attribute related functions.
ERROR
The tab contents are invalid.
Definition: Severity.java:44
final JTextPane archEdit
The JTextPane that contains the attributes of the game object.
Severity levels for colors of tabs.
Definition: Severity.java:29
static final Pattern PATTERN_END_OF_LINE
The pattern to match end of line characters separating lines.
static String diffArchTextKeys(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final BaseObject<?, ?, ?, ?> archetype)
Returns all attributes from the given game object that don&#39;t exist in an archetype.
DEFAULT
The tab contents are unchanged from defaults.
Definition: Severity.java:34
GameObjectTextEditor(@NotNull final ArchetypeTypeSet archetypeTypeSet)
Creates a new instance.
Defines types of GameObjects with corresponding attributes.
static String diffArchTextValues(@NotNull final BaseObject<?, ?, ?, ?> archetype, @NotNull final CharSequence attributes)
Returns all entries from the given attributes that don&#39;t exist in an archetype.