20 package net.sf.gridarta.gui.scripts;
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.awt.FlowLayout;
26 import java.awt.Frame;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
31 import java.io.IOException;
32 import javax.swing.AbstractButton;
33 import javax.swing.BorderFactory;
34 import javax.swing.Box;
35 import javax.swing.BoxLayout;
36 import javax.swing.JButton;
37 import javax.swing.JComboBox;
38 import javax.swing.JDialog;
39 import javax.swing.JFileChooser;
40 import javax.swing.JLabel;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPanel;
43 import javax.swing.JTextField;
44 import javax.swing.WindowConstants;
45 import javax.swing.filechooser.FileFilter;
46 import javax.swing.text.JTextComponent;
62 import net.
sf.japi.swing.action.ActionBuilder;
63 import net.
sf.japi.swing.action.ActionBuilderFactory;
64 import net.
sf.japi.util.Arrays2;
65 import org.jetbrains.annotations.NotNull;
77 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
148 public ScriptArchEditor(@NotNull
final ScriptedEventFactory<G, A, R> scriptedEventFactory, @NotNull
final String scriptEnding, @NotNull
final String name, @NotNull
final ScriptArchUtils scriptArchUtils, @NotNull
final FileFilter scriptFileFilter, @NotNull
final ProjectSettings projectSettings, @NotNull
final MapManager<?, ?, ?> mapManager, @NotNull
final PathManager pathManager, @NotNull
final ScriptEditControl scriptEditControl) {
155 pluginNameBox =
new JComboBox<>(
new String[] { name });
156 pluginNameBox.setSelectedIndex(0);
166 final String[] valuesArray = scriptArchUtils.getEventNames();
167 final JComboBox<?> tmpEventTypeBox =
new JComboBox<>(valuesArray);
168 tmpEventTypeBox.setSelectedIndex(Arrays2.linearEqualitySearch(
"say", valuesArray));
169 return tmpEventTypeBox;
180 final String archName = gameObject.getBestName();
186 newScriptFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
188 final JPanel mainPanel =
new JPanel();
189 mainPanel.setLayout(
new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
190 mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 2, 5));
193 final Container line1 =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
194 final Component headingLabel =
new JLabel(
ActionBuilderUtils.
format(ACTION_BUILDER,
"scriptedEventHeading", archName));
195 headingLabel.setForeground(Color.black);
196 line1.add(headingLabel);
197 mainPanel.add(line1);
200 mainPanel.add(Box.createVerticalStrut(10));
201 final Container line2 =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
203 line2.add(typeLabel);
204 line2.add(eventTypeBox);
206 line2.add(Box.createHorizontalStrut(10));
210 line2.add(pluginLabel);
211 line2.add(pluginNameBox);
212 mainPanel.add(line2);
215 mainPanel.add(Box.createVerticalStrut(5));
216 final Container line3 =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
218 line3.add(scriptFileLabel);
219 mainPanel.add(line3);
220 inputScriptPath =
new JTextField(defScriptName, 20);
221 final AbstractButton browseButton =
new JButton(
"...");
222 browseButton.setMargin(
new Insets(0, 10, 0, 10));
223 browseButton.addActionListener(
new ActionListener() {
226 public void actionPerformed(@NotNull
final ActionEvent e) {
229 final JFileChooser fileChooser =
new JFileChooser();
231 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
233 fileChooser.setMultiSelectionEnabled(
false);
234 fileChooser.setFileFilter(scriptFileFilter);
236 if (fileChooser.showOpenDialog(newScriptFrame) == JFileChooser.APPROVE_OPTION) {
238 final File f = fileChooser.getSelectedFile();
243 line3.add(inputScriptPath);
244 line3.add(browseButton);
245 mainPanel.add(line3);
248 mainPanel.add(Box.createVerticalStrut(5));
249 final Container line4 =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
251 inputOptions =
new JTextField(
"", 20);
252 line4.add(inputOptions);
253 mainPanel.add(line4);
256 final Container line5 =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
257 final JPanel textPanel =
new JPanel();
258 textPanel.setLayout(
new BoxLayout(textPanel, BoxLayout.Y_AXIS));
260 textPanel.add(label1);
262 textPanel.add(label2);
263 line5.add(textPanel);
264 mainPanel.add(line5);
267 mainPanel.add(Box.createVerticalStrut(10));
268 final Container line6 =
new JPanel(
new FlowLayout(FlowLayout.RIGHT));
270 nsOkButton.addActionListener(
new ActionListener() {
273 public void actionPerformed(@NotNull
final ActionEvent e) {
278 line6.add(nsOkButton);
281 cancelButton.addActionListener(
new ActionListener() {
284 public void actionPerformed(@NotNull
final ActionEvent e) {
285 newScriptFrame.setVisible(
false);
289 line6.add(cancelButton);
290 mainPanel.add(line6);
292 newScriptFrame.getContentPane().add(mainPanel);
293 newScriptFrame.pack();
294 newScriptFrame.setLocationRelativeTo(parent);
295 newScriptFrame.setVisible(
true);
299 final StringBuilder scriptPath =
new StringBuilder(inputScriptPath.getText().trim().replace(
'\\',
'/'));
300 final String options = inputOptions.getText().trim();
301 final int eventType = scriptArchUtils.
indexToEventType(eventTypeBox.getSelectedIndex());
302 final String pluginName = ((String) pluginNameBox.getSelectedItem()).trim();
307 final GameObject<G, A, R> replaceObject = scriptArchData.getScriptedEvent(eventType, gameObject);
308 if (replaceObject != null) {
310 if (JOptionPane.showConfirmDialog(frame,
ActionBuilderUtils.
format(ACTION_BUILDER,
"scriptedEventReplace", scriptArchUtils.
typeName(eventType)),
ActionBuilderUtils.
getString(ACTION_BUILDER,
"scriptedEventReplaceTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.NO_OPTION) {
316 String absScriptPath;
317 if (scriptPath.length() > 0 && scriptPath.charAt(0) ==
'/') {
320 if (!mapDir.exists()) {
322 frame.setVisible(
false);
323 ACTION_BUILDER.showMessageDialog(frame,
"mapDirDoesntExist", mapDir);
327 absScriptPath = mapDir.getAbsolutePath() + scriptPath;
330 absScriptPath = localMapDir.getAbsolutePath() +
"/" + scriptPath;
334 File newScriptFile =
new File(absScriptPath);
335 if (!newScriptFile.exists() && !absScriptPath.endsWith(scriptEnding)) {
337 scriptPath.append(scriptEnding);
338 newScriptFile =
new File(absScriptPath);
341 if (newScriptFile.exists()) {
342 if (newScriptFile.isFile()) {
346 event = scriptedEventFactory.
newScriptedEvent(eventType, pluginName, scriptPath.toString(), options);
351 if (replaceObject != null) {
355 frame.setVisible(
false);
361 if (!absScriptPath.endsWith(scriptEnding)) {
363 scriptPath.append(scriptEnding);
364 newScriptFile =
new File(absScriptPath);
368 if (JOptionPane.showConfirmDialog(frame,
ActionBuilderUtils.
format(ACTION_BUILDER,
"scriptedEventCreate", newScriptFile.getName()),
ActionBuilderUtils.
getString(ACTION_BUILDER,
"scriptedEventCreateTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) != JOptionPane.YES_OPTION) {
372 boolean couldCreateFile =
false;
375 couldCreateFile = newScriptFile.createNewFile();
376 }
catch (
final IOException e) {
380 if (!couldCreateFile) {
388 event = scriptedEventFactory.
newScriptedEvent(eventType, pluginName, scriptPath.toString(), options);
393 if (replaceObject != null) {
397 frame.setVisible(
false);
400 scriptEditControl.
openScriptFile(newScriptFile.getAbsolutePath());
final MapManager<?, ?, ?> mapManager
The MapManager to use.
Stores and manages information about scripted events.
Package with common types for event archetypes.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapManager manages all opened maps.
Reading and writing of maps, handling of paths.
Settings that apply to a project.
File getLocalMapDir()
Returns a guess for a script directory to use.
Factory for creating ScriptedEvent instances.
Utility class for JFileChooser related functions.
final String scriptEnding
The ending for scripts.
ScriptArchEditor(@NotNull final ScriptedEventFactory< G, A, R > scriptedEventFactory, @NotNull final String scriptEnding, @NotNull final String name, @NotNull final ScriptArchUtils scriptArchUtils, @NotNull final FileFilter scriptFileFilter, @NotNull final ProjectSettings projectSettings, @NotNull final MapManager<?, ?, ?> mapManager, @NotNull final PathManager pathManager, @NotNull final ScriptEditControl scriptEditControl)
Creates a new instance.
final ProjectSettings projectSettings
The ProjectSettings to use.
final ScriptEditControl scriptEditControl
The ScriptEditControl to use.
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.
static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir)
Calls JFileChooser#setCurrentDirectory(File).
This package contains the classes for the script editor used within the editor to create and modify P...
Reflects a game object (object on a map).
final ScriptedEventFactory< G, A, R > scriptedEventFactory
The ScriptedEventFactory instance to use.
GameObjects are the objects based on Archetypes found on maps.
This exception is thrown if an event game object cannot be created.
ScriptedEvent< G, A, R > newScriptedEvent(int eventType, @NotNull String pluginName, @NotNull String scriptPath, @NotNull String options)
Creates a new ScriptedEvent instance.
final JComboBox< String > pluginNameBox
final PathManager pathManager
The PathManager for converting path names.
ScriptEditControl - Manages events and data flow for the script editor entity.
void addEventScript(@NotNull final G gameObject, @NotNull final ScriptArchData< G, A, R > scriptArchData, @NotNull final Frame parent)
A popup is opened and the user can create a new scripting event which gets attached to this gameObjec...
static String chooseDefaultScriptName(@NotNull final File baseDir, final String archetypeName, final String scriptEnding, @NotNull final PathManager pathManager)
Try to create a reasonable default script name for lazy users.
Utility class for ActionBuilder related functions.
G getEventArch()
Returns the underlying event game object.
static String localizeEventPath(@NotNull final File localMapDir, final File f, @NotNull final File mapDir)
This method is called when the user selects a new event to be created.
Dialog to create events linked to item scripting.
static JComboBox<?> createEventTypeBox(@NotNull final ScriptArchUtils scriptArchUtils)
JTextComponent inputOptions
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.
static String format(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final Object... args)
Returns the value of a key.
final JComboBox<?> eventTypeBox
JTextComponent inputScriptPath
void createNewEvent(@NotNull final Component frame, @NotNull final ScriptArchData< G, A, R > scriptArchData, @NotNull final G gameObject)
final FileFilter scriptFileFilter
final ScriptArchUtils scriptArchUtils
The ScriptArchUtils instance to use.
static final ActionBuilder ACTION_BUILDER
Action Builder.
String typeName(int eventType)
Returns a human readable name for an event type.
Interface for MapArchObjects.
Class which stores information about one scripted event.
void remove()
Remove this GameObject from its container.
int indexToEventType(int index)
Converts a combo box index to an event type.