20 package net.sf.gridarta.gui.map.maptilepane;
22 import java.awt.Component;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
29 import java.io.IOException;
30 import javax.swing.AbstractButton;
31 import javax.swing.Action;
32 import javax.swing.JButton;
33 import javax.swing.JFileChooser;
34 import javax.swing.JPanel;
35 import javax.swing.JTextField;
36 import javax.swing.filechooser.FileFilter;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import net.
sf.japi.swing.action.ActionMethod;
43 import org.jetbrains.annotations.NotNull;
44 import org.jetbrains.annotations.Nullable;
63 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
111 public TilePanel(@NotNull
final FileFilter fileFilter, @NotNull
final String original, @Nullable
final File relativeReference, @NotNull
final File absoluteReference) {
112 super(
new GridBagLayout());
115 this.relativeReference = relativeReference == null ? null :
getAbsolutePath(relativeReference);
117 final GridBagConstraints gbc =
new GridBagConstraints();
118 textField =
new JTextField();
119 textField.setColumns(16);
121 gbc.fill = GridBagConstraints.NONE;
122 add(
iconButton(ACTION_BUILDER.createAction(
false,
"mapTileRevert",
this)), gbc);
123 add(
iconButton(ACTION_BUILDER.createAction(
false,
"mapTileClear",
this)), gbc);
124 gbc.fill = GridBagConstraints.HORIZONTAL;
128 gbc.fill = GridBagConstraints.NONE;
131 add(
iconButton(ACTION_BUILDER.createAction(
false,
"mapTileChoose",
this)), gbc);
133 textField.setText(original);
145 return file.getCanonicalFile();
146 }
catch (
final IOException ignored) {
147 return file.getAbsoluteFile();
157 private static Component
iconButton(@NotNull
final Action action) {
158 final AbstractButton button =
new JButton(action);
159 button.setMargin(
new Insets(0, 0, 0, 0));
185 final JFileChooser chooser = tmpRelativeReference == null ?
new JFileChooser() :
new JFileChooser(tmpRelativeReference.getParentFile());
186 final String oldFilename =
getText();
187 if (!oldFilename.isEmpty()) {
190 if (tmpRelativeReference == null || oldFilename.startsWith(
"/")) {
191 oldFile =
new File(absoluteReference, oldFilename.substring(1));
193 oldFile =
new File(tmpRelativeReference.getParentFile(), oldFilename);
196 chooser.setSelectedFile(oldFile);
198 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
199 chooser.setMultiSelectionEnabled(
false);
200 chooser.setFileFilter(fileFilter);
202 final int returnVal = chooser.showOpenDialog(
this);
203 if (returnVal == JFileChooser.APPROVE_OPTION) {
205 final File selected = chooser.getSelectedFile();
206 final String relPath = selected.getCanonicalPath().substring(absoluteReference.getCanonicalPath().length()).replace(
'\\',
'/');
208 }
catch (
final IOException ex) {
219 public void setText(@NotNull
final String text,
final boolean keepRA) {
220 textField.setText(text);
234 final String text = textField.getText();
243 textField.requestFocusInWindow();
251 textField.addActionListener(actionListener);
264 private class RASwitch extends JButton implements ActionListener {
269 private static final long serialVersionUID = 1L;
280 setMargin(
new Insets(0, 0, 0, 0));
283 addActionListener(
this);
284 setPreferredSize(getMinimumSize());
295 if (path.isEmpty() || tmpRelativeReference == null) {
298 if (toggleRelative) {
302 final String relRef =
new StringBuilder().append(
'/').append(
PathManagerUtils.
absoluteToRelative(absoluteReference.getPath() +
'/', tmpRelativeReference.getPath())).toString();
323 setText(isRelative ?
"R" :
"A");
final File absoluteReference
The context reference for absolute paths.
void mapTileChoose()
Action method for choosing the path.
void mapTileClear()
Action method for deleting the path.
Reading and writing of maps, handling of paths.
final FileFilter fileFilter
The FileFilter to use.
Utility class for converting relative map paths to absolute map paths and vice versa.
RASwitch()
Creates a new instance.
A tile panel displays exactly one direction for map tiling.
Utility class for JFileChooser related functions.
A JButton that converts relative to absolute paths and vice versa.
static String relativeToAbsolute(@NotNull final String reference, @NotNull final String relative)
Converts a relative path to an absolute path.
final JTextField textField
The JTextField with the text.
void actionPerformed(final boolean toggleRelative)
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).
boolean isRelative
Whether the current mode is relative.
TilePanel(@NotNull final FileFilter fileFilter, @NotNull final String original, @Nullable final File relativeReference, @NotNull final File absoluteReference)
Creates a new instance.
final String original
The original value.
String getText()
Returns the text.
static File getAbsolutePath(@NotNull final File file)
Returns the absolute path of a File.
void activateTextField()
Activates the text input field.
void setText(@NotNull final String text, final boolean keepRA)
Sets the text.
void addTextFieldActionListener(@NotNull final ActionListener actionListener)
Adds an ActionListener to the text input field.
void updateRAState()
Updates the state of the raSwitch.
static String absoluteToRelative(@NotNull final String reference, @NotNull final String absolute)
Converts an absolute path to a relative path.
final File relativeReference
The context reference for relative paths.
Utility class for ActionBuilder related functions.
void actionPerformed(@NotNull final ActionEvent e)
void mapTileRevert()
Action method for reverting to stored path.
static boolean isRelative(@NotNull final String path)
Check whether a path is relative.
static Component iconButton(@NotNull final Action action)
Creates a new button for reverting a path entry.
final RASwitch raSwitch
The TilePanel.RASwitch.
static final long serialVersionUID
The serial version UID.
static void sanitizeCurrentDirectory(@NotNull final JFileChooser fileChooser)
Makes sure the current directory of a JFileChooser is valid.
void updateRAState()
Updates the state of the button.
void updateText()
Updates the button text to reflect the current state.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.