Gridarta Editor
MapPreviewAccessory.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.map;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.Image;
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27 import java.io.File;
28 import java.util.prefs.Preferences;
29 import javax.swing.Action;
30 import javax.swing.Icon;
31 import javax.swing.ImageIcon;
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JFileChooser;
35 import javax.swing.JLabel;
36 import javax.swing.JScrollPane;
37 import javax.swing.SwingConstants;
38 import javax.swing.filechooser.FileView;
39 import net.sf.gridarta.MainControl;
41 import net.sf.japi.swing.action.ActionBuilder;
42 import net.sf.japi.swing.action.ActionBuilderFactory;
43 import net.sf.japi.swing.action.ActionMethod;
44 import net.sf.japi.swing.action.ToggleAction;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
47 
53 public class MapPreviewAccessory extends JComponent {
54 
58  private static final long serialVersionUID = 1L;
59 
63  @NotNull
64  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
65 
69  @NotNull
70  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
71 
75  @NotNull
76  private static final String PREFERENCES_AUTO_GENERATE_PREVIEW = "autoGeneratePreviews";
77 
81  @NotNull
83 
87  @NotNull
88  private final JLabel preview = new JLabel();
89 
93  @NotNull
94  private final ToggleAction autoGeneratePreview = (ToggleAction) ACTION_BUILDER.createToggle(false, "autoGeneratePreviews", this);
95 
99  @NotNull
100  private final Action genPreview = ACTION_BUILDER.createAction(false, "genPreview", this);
101 
105  @NotNull
106  private final JFileChooser fileChooser;
107 
111  @Nullable
112  private File[] selectedFiles;
113 
117  private boolean autoGeneratePreviews;
118 
124  public MapPreviewAccessory(@NotNull final MapImageCache<?, ?, ?> mapImageCache, @NotNull final JFileChooser fileChooser) {
125  this.mapImageCache = mapImageCache;
126  this.fileChooser = fileChooser;
127  buildUI();
128  setPreview("genPreviewNone.text");
129 
130  final PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
131 
132  @Override
133  public void propertyChange(@NotNull final PropertyChangeEvent evt) {
134  final String prop = evt.getPropertyName();
135  if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
136  setPreview("genPreviewNone.text");
137  } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
138  final File selectedFile = (File) evt.getNewValue();
139  if (selectedFile != null) {
140  final Image previewImage = getMapPreview(selectedFile);
141  if (previewImage != null) {
142  setPreview(previewImage);
143  } else {
144  setPreview("genPreviewNone.text");
145  }
146  } else {
147  setPreview("genPreviewNone.text");
148  }
149  } else if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(prop)) {
150  selectedFiles = (File[]) evt.getNewValue();
151  }
152  }
153 
154  };
155  fileChooser.addPropertyChangeListener(propertyChangeListener);
156  fileChooser.setFileView(new MapFileView());
157  }
158 
162  private void buildUI() {
163  setAutoGeneratePreviews(PREFERENCES.getBoolean(PREFERENCES_AUTO_GENERATE_PREVIEW, false));
164  setPreferredSize(new Dimension(240, 115));
165  setLayout(new BorderLayout());
166  add(autoGeneratePreview.createCheckBox(), BorderLayout.NORTH);
167  add(new JButton(genPreview), BorderLayout.SOUTH);
168  preview.setHorizontalAlignment(SwingConstants.CENTER);
169  add(new JScrollPane(preview), BorderLayout.CENTER);
170  }
171 
176  @SuppressWarnings("NullableProblems")
177  private void setPreview(@Nullable final Image image) {
178  preview.setIcon(image == null ? null : new ImageIcon(image));
179  preview.setText(null);
180  }
181 
186  private void setPreview(@NotNull final String text) {
187  preview.setIcon(null);
188  preview.setText(ACTION_BUILDER.getString(text));
189  }
190 
196  @ActionMethod
197  public void setAutoGeneratePreviews(final boolean autoGeneratePreviews) {
198  this.autoGeneratePreviews = autoGeneratePreviews;
199  autoGeneratePreview.setSelected(autoGeneratePreviews);
200  PREFERENCES.putBoolean(PREFERENCES_AUTO_GENERATE_PREVIEW, autoGeneratePreviews);
201  if (autoGeneratePreviews) {
202  fileChooser.validate();
203  fileChooser.repaint();
204  }
205  }
206 
212  @ActionMethod
213  public boolean isAutoGeneratePreviews() {
214  return autoGeneratePreviews;
215  }
216 
220  @ActionMethod
221  public void genPreview() {
222  final File[] files = selectedFiles;
223  if (files == null || files.length == 0) {
224  return;
225  }
226 
227  for (final File file : files) {
228  mapImageCache.flush(file);
229 
230  final Image image = mapImageCache.getOrCreatePreview(file);
231  setPreview(image);
232  }
233  fileChooser.validate();
234  fileChooser.repaint();
235  }
236 
243  @Nullable
244  public Image getMapIcon(@NotNull final File mapFile) {
245  return autoGeneratePreview.isSelected() ? mapImageCache.getOrCreateIcon(mapFile) : mapImageCache.getIcon(mapFile);
246  }
247 
254  @Nullable
255  private Image getMapPreview(@NotNull final File mapFile) {
256  return autoGeneratePreview.isSelected() ? mapImageCache.getOrCreatePreview(mapFile) : mapImageCache.getPreview(mapFile);
257  }
258 
262  private class MapFileView extends FileView {
263 
264  @Override
265  public Icon getIcon(@NotNull final File f) {
266  @Nullable final Image image;
267  if (fileChooser.getCurrentDirectory().equals(f.getParentFile())) {
268  image = getMapIcon(f);
269  } else {
270  image = null;
271  }
272  return image != null ? new ImageIcon(image) : super.getIcon(f);
273  }
274 
275  }
276 
277 }
boolean isAutoGeneratePreviews()
Get whether to automatically generate previews.
Image getOrCreateIcon(@NotNull final File mapFile)
Returns an icon Image for a given map file.
static final Preferences PREFERENCES
Preferences.
final JFileChooser fileChooser
The file chooser instance.
MapPreviewAccessory(@NotNull final MapImageCache<?, ?, ?> mapImageCache, @NotNull final JFileChooser fileChooser)
Creates an MapPreviewAccessory.
Graphical User Interface of Gridarta.
final ToggleAction autoGeneratePreview
ToggleAction for auto-generate preview.
Image getMapPreview(@NotNull final File mapFile)
Get a preview.
Image getMapIcon(@NotNull final File mapFile)
Get an icon.
Image getPreview(@NotNull final File mapFile)
Returns a preview Image for a given map file.
Base package of all Gridarta classes.
static final long serialVersionUID
The serial version UID.
void setPreview(@Nullable final Image image)
Updates the text and icon of preview.
void setAutoGeneratePreviews(final boolean autoGeneratePreviews)
Switch automatic preview generation.
void flush(@NotNull final File mapFile)
Removes the cached images for a map file.
Interface used as preferences location.
boolean autoGeneratePreviews
Whether previews should be auto-generated.
Image getOrCreatePreview(@NotNull final File mapFile)
Returns a preview Image for a given map file.
Caches icon and preview images for map files.
final MapImageCache<?, ?, ?> mapImageCache
The cache for map images.
void setPreview(@NotNull final String text)
Updates the text and icon of preview.
Image getIcon(@NotNull final File mapFile)
Returns the icon Image for a given map file.
static final ActionBuilder ACTION_BUILDER
Action Builder.
Abstract base class for MapPreviewAccessories which are used for previewing maps in JFileChoosers...
File [] selectedFiles
The currently selected files, or.
static final String PREFERENCES_AUTO_GENERATE_PREVIEW
Preferences key for automatic preview generation.