Gridarta Editor
JFileField.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.utils;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Insets;
25 import java.io.File;
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;
39 
45 public class JFileField extends JComponent {
46 
50  private static final long serialVersionUID = 1L;
51 
55  @NotNull
56  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
57 
61  @NotNull
62  private final Component parent;
63 
68  @Nullable
69  private final File baseDir;
70 
74  @NotNull
75  private final JTextField textField;
76 
80  @NotNull
81  private final JFileChooser fileChooser = new JFileChooser();
82 
92  public JFileField(@NotNull final Component parent, @NotNull final String key, @Nullable final File baseDir, @NotNull final File file, final int fileSelectionMode) {
93  this.parent = parent;
94  this.baseDir = baseDir;
95  textField = new JTextField(getRelativeFile(file), 20);
96 
97  fileChooser.setFileSelectionMode(fileSelectionMode);
98  textField.setToolTipText(ActionBuilderUtils.getString(ACTION_BUILDER, key + ".shortdescription"));
99 
100  final AbstractButton fileChooserButton = new JButton(ACTION_BUILDER.createAction(false, "fileChooserButton", this));
101  fileChooserButton.setMargin(new Insets(0, 0, 0, 0));
102 
103  setLayout(new BorderLayout());
104  add(textField, BorderLayout.CENTER);
105  add(fileChooserButton, BorderLayout.EAST);
106  }
107 
112  @NotNull
113  public File getFile() {
114  final String text = textField.getText();
115  if (text.isEmpty()) {
116  return baseDir == null ? new File("/") : baseDir;
117  }
118  return new File(baseDir, text);
119  }
120 
125  public void setFile(@NotNull final File file) {
126  textField.setText(getRelativeFile(file));
127  }
128 
134  public boolean isFile(@NotNull final File file) {
135  return getRelativeFile(file).equals(textField.getText());
136  }
137 
143  @NotNull
144  private String getRelativeFile(@NotNull final File file) {
145  return baseDir == null ? file.getPath() : PathManagerUtils.getMapPath(file, baseDir);
146  }
147 
152  public void addDocumentListener(@NotNull final DocumentListener documentListener) {
153  textField.getDocument().addDocumentListener(documentListener);
154  }
155 
159  @ActionMethod
160  public void fileChooserButton() {
161  final File file = getFile();
162  fileChooser.setSelectedFile(file);
163  //if (file.isDirectory()) {
164  //FileChooserUtils.setCurrentDirectory(fileChooser, file);
165  //fileChooser.setSelectedFile(null);
166  //} else {
167  //FileChooserUtils.setCurrentDirectory(fileChooser, file.getParentFile());
168  //fileChooser.setSelectedFile(file);
169  //}
170  if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
171  setFile(fileChooser.getSelectedFile());
172  }
173  }
174 
175  @Override
176  public void setEnabled(final boolean enabled) {
177  super.setEnabled(enabled);
178  textField.setEnabled(enabled);
179  fileChooser.setEnabled(enabled);
180  }
181 
182 }
net.sf.gridarta.gui.utils.JFileField.isFile
boolean isFile(@NotNull final File file)
Returns whether the input field contains the given file.
Definition: JFileField.java:134
net.sf.gridarta.gui.utils.JFileField.baseDir
final File baseDir
The base directory.
Definition: JFileField.java:69
net.sf.gridarta.gui.utils.JFileField.getRelativeFile
String getRelativeFile(@NotNull final File file)
Returns the contents for the text input field for a file.
Definition: JFileField.java:144
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.utils.JFileField.addDocumentListener
void addDocumentListener(@NotNull final DocumentListener documentListener)
Adds a DocumentListener to the text input field.
Definition: JFileField.java:152
net.sf.gridarta.gui.utils.JFileField.fileChooserButton
void fileChooserButton()
The action method for the button.
Definition: JFileField.java:160
net.sf
net.sf.gridarta.gui.utils.JFileField.parent
final Component parent
The parent component.
Definition: JFileField.java:62
net.sf.gridarta.gui.utils.JFileField.JFileField
JFileField(@NotNull final Component parent, @NotNull final String key, @Nullable final File baseDir, @NotNull final File file, final int fileSelectionMode)
Creates a new instance.
Definition: JFileField.java:92
net
net.sf.gridarta.utils.PathManagerUtils.getMapPath
static String getMapPath(@NotNull final File file, @NotNull final File baseDir)
Returns a relative path path for a File.
Definition: PathManagerUtils.java:58
net.sf.gridarta.gui.utils.JFileField
A component for selecting files.
Definition: JFileField.java:45
net.sf.gridarta.gui.utils.JFileField.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: JFileField.java:50
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.gui.utils.JFileField.fileChooser
final JFileChooser fileChooser
The JFileChooser for selecting the file.
Definition: JFileField.java:81
net.sf.gridarta.utils.PathManagerUtils
Utility class for converting relative map paths to absolute map paths and vice versa.
Definition: PathManagerUtils.java:31
net.sf.gridarta.gui.utils.JFileField.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Definition: JFileField.java:56
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.gui.utils.JFileField.textField
final JTextField textField
The text file that contains the currently selected file.
Definition: JFileField.java:75
net.sf.gridarta.gui.utils.JFileField.setFile
void setFile(@NotNull final File file)
Sets the currently selected file.
Definition: JFileField.java:125
net.sf.gridarta.gui.utils.JFileField.setEnabled
void setEnabled(final boolean enabled)
Definition: JFileField.java:176
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.utils.JFileField.getFile
File getFile()
Returns the currently selected file.
Definition: JFileField.java:113