20 package net.sf.gridarta.gui.utils;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Insets;
26 import javax.swing.AbstractButton;
27 import javax.swing.JButton;
28 import javax.swing.JComponent;
29 import javax.swing.JFileChooser;
30 import javax.swing.JTextField;
31 import javax.swing.event.DocumentListener;
34 import net.
sf.japi.swing.action.ActionBuilder;
35 import net.
sf.japi.swing.action.ActionBuilderFactory;
36 import net.
sf.japi.swing.action.ActionMethod;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
56 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
92 public JFileField(@NotNull
final Component parent, @NotNull
final String key, @Nullable
final File baseDir, @NotNull
final File file,
final int fileSelectionMode) {
97 fileChooser.setFileSelectionMode(fileSelectionMode);
100 final AbstractButton
fileChooserButton =
new JButton(ACTION_BUILDER.createAction(
false,
"fileChooserButton",
this));
101 fileChooserButton.setMargin(
new Insets(0, 0, 0, 0));
103 setLayout(
new BorderLayout());
104 add(textField, BorderLayout.CENTER);
105 add(fileChooserButton, BorderLayout.EAST);
114 final String text = textField.getText();
115 if (text.isEmpty()) {
116 return baseDir == null ?
new File(
"/") :
baseDir;
118 return new File(baseDir, text);
126 public void setFile(@NotNull
final File file) {
145 textField.getDocument().addDocumentListener(documentListener);
154 fileChooser.setSelectedFile(file);
162 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
163 setFile(fileChooser.getSelectedFile());
void addDocumentListener(@NotNull final DocumentListener documentListener)
Adds a DocumentListener to the text input field.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Reading and writing of maps, handling of paths.
Utility class for converting relative map paths to absolute map paths and vice versa.
A component for selecting files.
final JFileChooser fileChooser
The JFileChooser for selecting the file.
void fileChooserButton()
The action method for the button.
final JTextField textField
The text file that contains the currently selected file.
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 final long serialVersionUID
The serial version UID.
void setFile(@NotNull final File file)
Sets the currently selected file.
final Component parent
The parent component.
Utility class for ActionBuilder related functions.
static String getMapPath(@NotNull final File file, @NotNull final File baseDir)
Returns a relative path path for a File.
String getRelativeFile(@NotNull final File file)
Returns the contents for the text input field for a file.
JFileField(@NotNull final Component parent, @NotNull final String key, @Nullable final File baseDir, @NotNull final File file, final int fileSelectionMode)
Creates a new instance.
File getFile()
Returns the currently selected file.
final File baseDir
The base directory.