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-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.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  } else {
118  return new File(baseDir, text);
119  }
120  }
121 
126  public void setFile(@NotNull final File file) {
127  textField.setText(getRelativeFile(file));
128  }
129 
135  @NotNull
136  private String getRelativeFile(@NotNull final File file) {
137  return baseDir == null ? file.getPath() : PathManagerUtils.getMapPath(file, baseDir);
138  }
139 
144  public void addDocumentListener(@NotNull final DocumentListener documentListener) {
145  textField.getDocument().addDocumentListener(documentListener);
146  }
147 
151  @ActionMethod
152  public void fileChooserButton() {
153  final File file = getFile();
154  fileChooser.setSelectedFile(file);
155  //if (file.isDirectory()) {
156  //FileChooserUtils.setCurrentDirectory(fileChooser, file);
157  //fileChooser.setSelectedFile(null);
158  //} else {
159  //FileChooserUtils.setCurrentDirectory(fileChooser, file.getParentFile());
160  //fileChooser.setSelectedFile(file);
161  //}
162  if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
163  setFile(fileChooser.getSelectedFile());
164  }
165  }
166 
167 }
void addDocumentListener(@NotNull final DocumentListener documentListener)
Adds a DocumentListener to the text input field.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Definition: JFileField.java:56
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.
Definition: JFileField.java:45
final JFileChooser fileChooser
The JFileChooser for selecting the file.
Definition: JFileField.java:81
void fileChooserButton()
The action method for the button.
final JTextField textField
The text file that contains the currently selected file.
Definition: JFileField.java:75
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.
Definition: JFileField.java:50
void setFile(@NotNull final File file)
Sets the currently selected file.
final Component parent
The parent component.
Definition: JFileField.java:62
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.
Definition: JFileField.java:92
File getFile()
Returns the currently selected file.
final File baseDir
The base directory.
Definition: JFileField.java:69