Gridarta Editor
ScriptedEventEditor.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.scripts;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.FlowLayout;
25 import java.awt.Frame;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.io.File;
29 import javax.swing.AbstractButton;
30 import javax.swing.BorderFactory;
31 import javax.swing.Box;
32 import javax.swing.BoxLayout;
33 import javax.swing.JButton;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.JTextField;
38 import javax.swing.WindowConstants;
39 import javax.swing.text.JTextComponent;
48 import net.sf.japi.swing.action.ActionBuilder;
49 import net.sf.japi.swing.action.ActionBuilderFactory;
50 import org.jetbrains.annotations.NotNull;
51 
56 public class ScriptedEventEditor<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
57 
61  @NotNull
62  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
63 
67  @NotNull
69 
73  @NotNull
75 
76  @NotNull
77  private JTextComponent inputScriptPath;
78 
79  @NotNull
80  private JTextComponent inputPluginName;
81 
82  @NotNull
83  private JTextComponent inputOptions;
84 
90  public ScriptedEventEditor(@NotNull final ProjectSettings projectSettings, @NotNull final ScriptEditControl scriptEditControl) {
91  this.projectSettings = projectSettings;
92  this.scriptEditControl = scriptEditControl;
93  }
94 
101  public void openScript(@NotNull final MapManager<?, ?, ?> mapManager, @NotNull final String scriptPath, @NotNull final Component parent) {
102  // trying to get the absolute path to script file:
103  final File scriptFile;
104  if (scriptPath.startsWith("/")) {
105  // file path is absolute (to map base directory):
106  scriptFile = new File(projectSettings.getMapsDirectory(), scriptPath.substring(1));
107  } else {
108  // file path is relative to map dir
109  scriptFile = new File(mapManager.getLocalMapDir(), scriptPath);
110  }
111 
112  // now see if that file really exists:
113  if (scriptFile.exists() && scriptFile.isFile()) {
114  final String path = scriptFile.getAbsolutePath();
115  scriptEditControl.openScriptFile(path);
116  } else {
117  // file does not exist!
118  ACTION_BUILDER.showMessageDialog(parent, "openScriptNotFound", scriptFile);
119  }
120  }
121 
128  public void editParameters(@NotNull final ScriptedEvent<G, A, R> scriptedEvent, @NotNull final Frame parent) {
129  final JDialog pathFrame = new JDialog(parent, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptedEventTitle"), true);
130  pathFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
131 
132  final JPanel mainPanel = new JPanel();
133  mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
134  mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 2, 5));
135 
136  // input line: script path
137  final Container line1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
138  final Component text1 = new JLabel(ActionBuilderUtils.getString(ACTION_BUILDER, "scriptedEventScript"));
139  line1.add(text1);
140  inputScriptPath = new JTextField(scriptedEvent.getScriptPath(), 20);
141  line1.add(inputScriptPath);
142  mainPanel.add(line1);
143 
144  // input line: plugin options
145  final Container line2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
146  final Component text2 = new JLabel(ActionBuilderUtils.getString(ACTION_BUILDER, "scriptedEventOptions"));
147  line2.add(text2);
148  inputOptions = new JTextField(scriptedEvent.getOptions(), 20);
149  line2.add(inputOptions);
150  mainPanel.add(line2);
151  mainPanel.add(Box.createVerticalStrut(5));
152 
153  // input line: plugin name
154  final Container line3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
155  final Component text3 = ActionBuilderUtils.newLabel(ACTION_BUILDER, "scriptedEventPlugin");
156  line3.add(text3);
157  inputPluginName = new JTextField(scriptedEvent.getPluginName(), 20);
158  line3.add(inputPluginName);
159  mainPanel.add(line3);
160  mainPanel.add(Box.createVerticalStrut(5));
161 
162  // button panel:
163  final Container line4 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
164  final AbstractButton okButton = new JButton(ActionBuilderUtils.getString(ACTION_BUILDER, "scriptedEventOk"));
165  okButton.addActionListener(new ActionListener() {
166 
167  @Override
168  public void actionPerformed(@NotNull final ActionEvent e) {
169  scriptedEvent.modifyEventPath();
170  pathFrame.dispose();
171  }
172 
173  });
174  line4.add(okButton);
175 
176  final AbstractButton cancelButton = new JButton(ActionBuilderUtils.getString(ACTION_BUILDER, "scriptedEventCancel"));
177  cancelButton.addActionListener(new ActionListener() {
178 
179  @Override
180  public void actionPerformed(@NotNull final ActionEvent e) {
181  pathFrame.dispose();
182  }
183 
184  });
185  line4.add(cancelButton);
186  mainPanel.add(line4);
187 
188  pathFrame.getContentPane().add(mainPanel);
189  pathFrame.pack();
190  pathFrame.setLocationRelativeTo(parent);
191  pathFrame.setVisible(true);
192  }
193 
194  @NotNull
195  public String getInputScriptPath() {
196  return inputScriptPath.getText().trim();
197  }
198 
199  @NotNull
200  public String getInputPluginName() {
201  return inputPluginName.getText().trim();
202  }
203 
204  @NotNull
205  public String getInputOptions() {
206  return inputOptions.getText().trim();
207  }
208 
209 }
Dialog to edit events linked to item scripting.
Package with common types for event archetypes.
A MapManager manages all opened maps.
Definition: MapManager.java:37
final ProjectSettings projectSettings
The project settings instance.
Settings that apply to a project.
void openScriptFile(@NotNull final String pathName)
Open a new empty script document.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
This package contains the classes for the script editor used within the editor to create and modify P...
Definition: Actions.java:20
Reflects a game object (object on a map).
Definition: GameObject.java:36
void openScript(@NotNull final MapManager<?, ?, ?> mapManager, @NotNull final String scriptPath, @NotNull final Component parent)
Opens the script pad to display a script.
static final ActionBuilder ACTION_BUILDER
Action Builder.
GameObjects are the objects based on Archetypes found on maps.
ScriptedEventEditor(@NotNull final ProjectSettings projectSettings, @NotNull final ScriptEditControl scriptEditControl)
Creates a new instance.
final ScriptEditControl scriptEditControl
The ScriptEditControl to use.
ScriptEditControl - Manages events and data flow for the script editor entity.
Utility class for ActionBuilder related functions.
void editParameters(@NotNull final ScriptedEvent< G, A, R > scriptedEvent, @NotNull final Frame parent)
Edit path and plugin name for an event.
File getMapsDirectory()
Returns the default maps directory.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
Class which stores information about one scripted event.