Gridarta Editor
MsgTextTab.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.gameobjectattributes;
21 
22 import java.awt.Color;
23 import java.awt.GridLayout;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTextArea;
27 import javax.swing.ScrollPaneConstants;
28 import javax.swing.border.EtchedBorder;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
43 public class MsgTextTab<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractGameObjectAttributesTab<G, A, R> {
44 
48  private final JTextArea archTextArea = new JTextArea(4, 25);
49 
53  @NotNull
54  private final JPanel textPanel = new JPanel(new GridLayout(1, 1));
55 
59  @NotNull
60  private String defaultArchTextAreaValue = "";
61 
66  public MsgTextTab(@NotNull final GameObjectAttributesModel<G, A, R> gameObjectAttributesModel) {
67  super(gameObjectAttributesModel);
68  archTextArea.setLineWrap(true);
69 
70  // create ScrollPane for text scrolling
71  final JScrollPane sta = new JScrollPane(archTextArea);
72  sta.setBorder(new EtchedBorder());
73  sta.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
74  sta.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
75 
76  //textPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
77  textPanel.add(sta);
78 
80  refresh(gameObjectAttributesModel.getSelectedGameObject());
81  }
82 
83  @NotNull
84  @Override
85  public JPanel getPanel() {
86  return textPanel;
87  }
88 
89  @Override
90  protected final void refresh(@Nullable final G gameObject) {
91  final boolean hasMessage;
92  if (gameObject == null) {
93  setArchTextArea("", Color.black);
94  hasMessage = false;
95  } else {
96  final String msgText = gameObject.getMsgText(false);
97  if (msgText == null) {
98  final String archMsgText = gameObject.getArchetype().getMsgText(false);
99  if (archMsgText == null) {
100  setArchTextArea("", Color.black);
101  hasMessage = false;
102  } else {
103  setArchTextArea(archMsgText, Color.black);
104  hasMessage = true;
105  }
106  } else {
107  setArchTextArea(msgText, Color.blue);
108  hasMessage = true;
109  }
110  }
112  }
113 
114  @Override
115  public boolean canApply() {
116  return !archTextArea.getText().equals(defaultArchTextAreaValue);
117  }
118 
119  @Override
120  public void activate() {
121  archTextArea.requestFocusInWindow();
122  }
123 
124  @Override
125  protected void apply(@NotNull final G gameObject) {
126  gameObject.setMsgText(getMsgText(gameObject));
127  }
128 
134  @Nullable
135  private String getMsgText(@NotNull final GameObject<G, A, R> gameObject) {
136  final BaseObject<G, A, R, ?> archetype = gameObject.getArchetype();
137  // the msg TEXT!! ("msg ... endmsg")
138  // if there is an entry in the archTextArea (msg window), this
139  // overrules the default text (if any)
140  final String msgText = StringUtils.removeTrailingWhitespaceFromLines(archTextArea.getText());
141  if (msgText.isEmpty()) { // there is nothing in the msg win
142  if (archetype.getMsgText(false) == null) {
143  return null; // always delete this...
144  }
145  return ""; // but here we must overrule default with msg/endmsg (empty msg)
146  } // there is something in the msg win
147  final String archetypeMsgText = archetype.getMsgText(false);
148  if (archetypeMsgText == null) {
149  return msgText;
150  }
151  if (msgText.equals(archetypeMsgText)) {
152  return null; // yes, we don't need it in map
153  }
154  return msgText;
155  }
156 
162  private void setArchTextArea(@NotNull final String objText, @NotNull final Color color) {
163  archTextArea.setForeground(color);
164  archTextArea.setText(objText);
165  archTextArea.setCaretPosition(0);
166  defaultArchTextAreaValue = objText;
167  }
168 
169 }
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.gameobjectattributes.MsgTextTab.refresh
final void refresh(@Nullable final G gameObject)
Definition: MsgTextTab.java:90
net.sf.gridarta.model.baseobject.BaseObject.getMsgText
String getMsgText(boolean queryArchetype)
Returns the message bound to this object.
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.getMsgText
String getMsgText(@NotNull final GameObject< G, A, R > gameObject)
Returns the message text to be set.
Definition: MsgTextTab.java:135
net.sf
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.panel.gameobjectattributes.AbstractGameObjectAttributesTab
Base class for GameObjectAttributesTab implementations.
Definition: AbstractGameObjectAttributesTab.java:38
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.defaultArchTextAreaValue
String defaultArchTextAreaValue
The initial value of the archTextArea.
Definition: MsgTextTab.java:60
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.model.baseobject.BaseObject.getArchetype
R getArchetype()
Returns the Archetype this GameObject is based on.
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
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.panel.gameobjectattributes.GameObjectAttributesModel< G, A, R >
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.MsgTextTab
MsgTextTab(@NotNull final GameObjectAttributesModel< G, A, R > gameObjectAttributesModel)
Creates a new instance.
Definition: MsgTextTab.java:66
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.setArchTextArea
void setArchTextArea(@NotNull final String objText, @NotNull final Color color)
Updates the value of the archTextArea.
Definition: MsgTextTab.java:162
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.gui.panel.gameobjectattributes.AbstractGameObjectAttributesTab< G, A, R >::addAutoApply
void addAutoApply( @NotNull final Component component)
Registers a component that auto-applies when the focus is lost.
Definition: AbstractGameObjectAttributesTab.java:161
net.sf.gridarta.gui.panel.gameobjectattributes.AbstractGameObjectAttributesTab< G, A, R >::setTabSeverity
void setTabSeverity( @NotNull final Severity tabSeverity)
Sets the tab severity.
Definition: AbstractGameObjectAttributesTab.java:102
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.apply
void apply(@NotNull final G gameObject)
Definition: MsgTextTab.java:125
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.getPanel
JPanel getPanel()
Definition: MsgTextTab.java:85
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab
The "Msg Text" tab in the game object attributes panel.
Definition: MsgTextTab.java:43
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.activate
void activate()
Definition: MsgTextTab.java:120
net.sf.gridarta.utils.StringUtils.removeTrailingWhitespaceFromLines
static String removeTrailingWhitespaceFromLines(@NotNull final CharSequence str)
Removes trailing whitespace from all lines of a string.
Definition: StringUtils.java:133
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.textPanel
final JPanel textPanel
The content panel.
Definition: MsgTextTab.java:54
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
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.archTextArea
final JTextArea archTextArea
Arch text field.
Definition: MsgTextTab.java:48
net.sf.gridarta.gui.panel.gameobjectattributes.MsgTextTab.canApply
boolean canApply()
Definition: MsgTextTab.java:115