Gridarta Editor
ManageBookmarksDialog.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.dialog.bookmarks;
21 
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.dnd.DnDConstants;
27 import javax.swing.Action;
28 import javax.swing.Icon;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JLabel;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JScrollPane;
35 import javax.swing.JTree;
36 import javax.swing.JViewport;
37 import javax.swing.ScrollPaneConstants;
38 import javax.swing.SwingConstants;
39 import javax.swing.WindowConstants;
40 import javax.swing.event.TreeSelectionEvent;
41 import javax.swing.event.TreeSelectionListener;
42 import javax.swing.tree.DefaultMutableTreeNode;
43 import javax.swing.tree.TreePath;
44 import javax.swing.tree.TreeSelectionModel;
59 import net.sf.japi.swing.action.ActionBuilder;
60 import net.sf.japi.swing.action.ActionBuilderFactory;
61 import net.sf.japi.swing.action.ActionMethod;
62 import org.jetbrains.annotations.NotNull;
63 import org.jetbrains.annotations.Nullable;
64 
69 public class ManageBookmarksDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JOptionPane {
70 
74  private static final long serialVersionUID = 1L;
75 
79  @NotNull
80  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
81 
85  @NotNull
87 
91  @NotNull
92  private final MapMenu mapMenu;
93 
97  @NotNull
99 
104  @NotNull
105  private final JTree bookmarksTree;
106 
111  @NotNull
112  private final JLabel preview = new JLabel();
113 
118  @NotNull
119  private final JDialog dialog;
120 
125  @NotNull
126  private final Action editAction = ACTION_BUILDER.createAction(false, "manageBookmarksEdit", this);
127 
132  @NotNull
133  private final Action removeAction = ACTION_BUILDER.createAction(false, "manageBookmarksRemove", this);
134 
139  @NotNull
140  private final Action unDeleteAction = ACTION_BUILDER.createAction(false, "manageBookmarksUnDelete", this);
141 
146  @NotNull
147  private final Action newDirectoryAction = ACTION_BUILDER.createAction(false, "manageBookmarksNewDirectory", this);
148 
153  @NotNull
154  private final JButton closeButton = new JButton(ACTION_BUILDER.createAction(false, "manageBookmarksClose", this));
155 
160  @Nullable
161  private TreePath selectedTreePath;
162 
169  public ManageBookmarksDialog(@NotNull final Component parentComponent, @NotNull final MapImageCache<G, A, R> mapImageCache, @NotNull final MapMenu mapMenu) {
170  this.mapImageCache = mapImageCache;
171  this.mapMenu = mapMenu;
172  mapMenuEntryIcons = new MapMenuEntryIcons(mapImageCache);
173  bookmarksTree = mapMenu.newTree();
174  //noinspection ResultOfObjectAllocationIgnored
175  new TreeDragSource(bookmarksTree, DnDConstants.ACTION_COPY_OR_MOVE);
176  //noinspection ResultOfObjectAllocationIgnored
177  new TreeDropTarget(bookmarksTree);
178  final JButton editButton = new JButton(editAction);
179  final JButton removeButton = new JButton(removeAction);
180  final JButton unDeleteButton = new JButton(unDeleteAction);
181  final JButton newDirectoryButton = new JButton(newDirectoryAction);
182  setOptions(new Object[] { editButton, removeButton, unDeleteButton, newDirectoryButton, closeButton });
183 
184  setMessage(createPanel());
185 
186  dialog = createDialog(parentComponent, ActionBuilderUtils.getString(ACTION_BUILDER, "manageBookmarks.title"));
187  dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
188  dialog.setModal(false);
189  dialog.setResizable(true);
190  dialog.pack();
191  }
192 
197  public void showDialog(@NotNull final Component parentComponent) {
198  setInitialValue(bookmarksTree);
199  bookmarksTree.setRootVisible(false);
200  bookmarksTree.setSelectionRow(0);
201  dialog.setLocationRelativeTo(parentComponent);
202  dialog.setMinimumSize(new Dimension(600, 300));
203  dialog.setPreferredSize(new Dimension(800, 600));
204  dialog.setVisible(true);
205  dialog.toFront();
206  }
207 
212  @NotNull
213  private JPanel createPanel() {
214  preview.setHorizontalAlignment(SwingConstants.CENTER);
215  preview.setPreferredSize(new Dimension(200, 200));
216 
217  final JPanel mainPanel = new JPanel(new GridBagLayout());
218 
219  mainPanel.setBorder(GUIConstants.DIALOG_BORDER);
220 
221  final JScrollPane bookmarksScrollPane = new JScrollPane();
222  bookmarksScrollPane.setViewportView(bookmarksTree);
223  bookmarksScrollPane.setBackground(bookmarksTree.getBackground());
224  bookmarksScrollPane.getViewport().add(bookmarksTree);
225  bookmarksScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
226  bookmarksScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
227  bookmarksScrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
228 
229  final GridBagConstraints gbc = new GridBagConstraints();
230  gbc.fill = GridBagConstraints.BOTH;
231  gbc.weightx = 1.0;
232  gbc.weighty = 1.0;
233  gbc.gridx = 0;
234  gbc.gridy = 0;
235  mainPanel.add(bookmarksScrollPane, gbc);
236 
237  gbc.weightx = 0.0;
238  gbc.gridx = 1;
239  mainPanel.add(preview, gbc);
240 
241  bookmarksTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
242  bookmarksTree.setCellRenderer(new MapMenuEntryTreeCellRenderer(mapImageCache));
243 
244  final TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
245 
246  @Override
247  public void valueChanged(final TreeSelectionEvent e) {
249  }
250 
251  };
252  bookmarksTree.addTreeSelectionListener(treeSelectionListener);
254 
255  return mainPanel;
256  }
257 
261  private void updateSelectedBookmark() {
262  selectedTreePath = bookmarksTree.getSelectionPath();
263  updateActions();
264  }
265 
269  @ActionMethod
270  public void manageBookmarksEdit() {
271  doEditBookmark(true);
272  }
273 
277  @ActionMethod
278  public void manageBookmarksRemove() {
279  doRemoveBookmark(true);
280  }
281 
285  @ActionMethod
286  public void manageBookmarksUnDelete() {
287  doUnDeleteBookmark(true);
288  }
289 
293  @ActionMethod
295  doNewDirectory(true);
296  }
297 
301  @ActionMethod
302  public void manageBookmarksClose() {
303  setValue(closeButton);
304  }
305 
306  @Override
307  public void setValue(@Nullable final Object newValue) {
308  super.setValue(newValue);
309  if (newValue != UNINITIALIZED_VALUE) {
310  mapMenu.save();
311  }
312  }
313 
319  private boolean doEditBookmark(final boolean performAction) {
320  final TreePath tmpSelectedTreePath = selectedTreePath;
321  if (tmpSelectedTreePath == null) {
322  return false;
323  }
324 
325  final DefaultMutableTreeNode selectedTreeNode = (DefaultMutableTreeNode) tmpSelectedTreePath.getLastPathComponent();
326  if (selectedTreeNode == mapMenu.getRoot()) {
327  return false;
328  }
329 
330  if (performAction) {
331  final DefaultMutableTreeNode tmpSelectedTreeNode = (DefaultMutableTreeNode) tmpSelectedTreePath.getLastPathComponent();
332  final MapMenuEntryVisitor mapMenuEntryVisitor = new MapMenuEntryVisitor() {
333 
334  @Override
335  public void visit(@NotNull final MapMenuEntryDir mapMenuEntry) {
336  final BookmarkDirectoryDialog bookmarkDirectoryDialog = new BookmarkDirectoryDialog(ManageBookmarksDialog.this, mapMenuEntry.getTitle());
337  if (bookmarkDirectoryDialog.showDialog()) {
338  mapMenuEntry.setTitle(bookmarkDirectoryDialog.getDirectory());
339  bookmarksTree.getModel().valueForPathChanged(tmpSelectedTreePath, mapMenuEntry);
340  }
341  }
342 
343  @Override
344  public void visit(@NotNull final MapMenuEntryMap mapMenuEntry) {
345  final EditBookmarkDialog editBookmarkDialog = new EditBookmarkDialog(ManageBookmarksDialog.this, mapMenuEntry.getTitle());
346  if (editBookmarkDialog.showDialog()) {
347  mapMenuEntry.setTitle(editBookmarkDialog.getDescription());
348  bookmarksTree.getModel().valueForPathChanged(tmpSelectedTreePath, mapMenuEntry);
349  }
350  }
351 
352  };
353  ((MapMenuEntry) tmpSelectedTreeNode.getUserObject()).visit(mapMenuEntryVisitor);
354  }
355 
356  return true;
357  }
358 
364  private boolean doRemoveBookmark(final boolean performAction) {
365  final TreePath tmpSelectedTreePath = selectedTreePath;
366  if (tmpSelectedTreePath == null) {
367  return false;
368  }
369 
370  final DefaultMutableTreeNode selectedTreeNode = (DefaultMutableTreeNode) tmpSelectedTreePath.getLastPathComponent();
371  if (selectedTreeNode == mapMenu.getRoot()) {
372  return false;
373  }
374 
375  if (performAction) {
376  final int[] selectionRows = bookmarksTree.getSelectionRows();
377  mapMenu.removeNode(selectedTreeNode);
378  final int index = selectionRows == null ? 0 : selectionRows[0];
379  if (index > 0) {
380  bookmarksTree.setSelectionRow(index - 1);
381  }
382  }
383 
384  return true;
385  }
386 
392  private boolean doUnDeleteBookmark(final boolean performAction) {
393  final MapMenu.DeletedNode deletedNode = mapMenu.getDeletedNode(performAction);
394  if (deletedNode == null) {
395  return false;
396  }
397 
398  if (performAction) {
399  final TreePath treePath = mapMenu.addMapMenuEntry(deletedNode.getDirectory(), deletedNode.getTreeNode());
400  bookmarksTree.setSelectionPath(treePath);
401  }
402 
403  return true;
404  }
405 
411  private boolean doNewDirectory(final boolean performAction) {
412  final TreePath tmpSelectedTreePath = selectedTreePath;
413  if (tmpSelectedTreePath == null) {
414  return false;
415  }
416 
417  if (performAction) {
418  final DefaultMutableTreeNode selectedTreeNode = (DefaultMutableTreeNode) tmpSelectedTreePath.getLastPathComponent();
419  final BookmarkDirectoryDialog bookmarkDirectoryDialog = new BookmarkDirectoryDialog(this, "");
420  if (bookmarkDirectoryDialog.showDialog()) {
421  final String directory = bookmarkDirectoryDialog.getDirectory();
422  final MapMenuEntry mapEntry = new MapMenuEntryDir(directory);
423  final DefaultMutableTreeNode parent;
424  final int index;
425  if (selectedTreeNode == mapMenu.getRoot()) {
426  parent = selectedTreeNode;
427  index = 0;
428  } else {
429  parent = (DefaultMutableTreeNode) selectedTreeNode.getParent();
430  assert parent != null;
431  index = parent.getIndex(selectedTreeNode) + 1;
432  assert index != 0;
433  }
434  final TreePath treePath = mapMenu.insertNodeInto(mapEntry, parent, index);
435  bookmarksTree.setSelectionPath(treePath);
436 
437  }
438  }
439 
440  return true;
441  }
442 
446  private void updateActions() {
447  editAction.setEnabled(doEditBookmark(false));
448  removeAction.setEnabled(doRemoveBookmark(false));
449  unDeleteAction.setEnabled(doUnDeleteBookmark(false));
450  newDirectoryAction.setEnabled(doNewDirectory(false));
451  @Nullable final Icon previewIcon;
452  if (selectedTreePath == null) {
453  previewIcon = null;
454  } else {
455  final DefaultMutableTreeNode tmpSelectedTreeNode = (DefaultMutableTreeNode) selectedTreePath.getLastPathComponent();
456  final MapMenuEntry mapMenuEntry = (MapMenuEntry) tmpSelectedTreeNode.getUserObject();
457  previewIcon = mapMenuEntryIcons.getIcon(mapMenuEntry);
458  }
459  preview.setIcon(previewIcon);
460  }
461 
462 }
void manageBookmarksClose()
Action method for "close bookmarks dialog".
A MapMenuEntry that represents a directory.
static final long serialVersionUID
The serial Version UID.
Graphical User Interface of Gridarta.
A DefaultTreeCellRenderer for rendering MapMenuEntry instances.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
boolean doUnDeleteBookmark(final boolean performAction)
Restores the last deleted bookmark.
DeletedNode getDeletedNode(final boolean delete)
Returns the last deleted node.
Definition: MapMenu.java:343
void manageBookmarksUnDelete()
Action method for "undo deletion".
final MapImageCache< G, A, R > mapImageCache
The MapImageCache for creating map previews.
void manageBookmarksRemove()
Action method for "remove bookmark".
A dialog that displays existing bookmarks and allows to edit or remove them.
Manages the contents of a recent or bookmark menu.
Definition: MapMenu.java:44
final MapMenuEntryIcons mapMenuEntryIcons
The MapMenuEntryIcons for looking up icons.
void manageBookmarksEdit()
Action method for "edit bookmark".
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
boolean doRemoveBookmark(final boolean performAction)
Removes the selected bookmark.
boolean doNewDirectory(final boolean performAction)
Create a new directory.
Base package of all Gridarta classes.
DefaultMutableTreeNode getRoot()
Returns the root node.
Definition: MapMenu.java:390
Reflects a game object (object on a map).
Definition: GameObject.java:36
void updateActions()
Updates the actions&#39; states to reflect the current selection.
Implements a drag source for JTree instances.
final Action editAction
The Action for "edit bookmark".
A dialog that displays one bookmark and allows to edit the values.
final Action unDeleteAction
The Action for "remove bookmark".
GameObjects are the objects based on Archetypes found on maps.
void save()
Saves the contents to preferences if modified since last save.
Definition: MapMenu.java:149
Caches icon and preview images for map files.
final Action removeAction
The Action for "remove bookmark".
void showDialog(@NotNull final Component parentComponent)
Opens the dialog.
void addMapMenuEntry(@NotNull final String directory, @NotNull final MapMenuEntry mapMenuEntry)
Adds a MapMenuEntry to this menu.
Definition: MapMenu.java:209
Utility class for ActionBuilder related functions.
void updateSelectedBookmark()
Updates selectedTreePath from bookmarksTree.
Result value consisting of a TreeNode and its location (directory).
Definition: MapMenu.java:399
final Action newDirectoryAction
The Action for "new directory".
A dialog that queries a bookmark directory name.
TreePath insertNodeInto(@NotNull final MapMenuEntry mapEntry, @NotNull final DefaultMutableTreeNode parent, final int index)
Inserts a new node into the tree.
Definition: MapMenu.java:280
ManageBookmarksDialog(@NotNull final Component parentComponent, @NotNull final MapImageCache< G, A, R > mapImageCache, @NotNull final MapMenu mapMenu)
Creates a new instance.
Abstract base class for recent and bookmark menu entries.
boolean doEditBookmark(final boolean performAction)
Edits the selected bookmark.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
A MapMenuEntry that represents a map.
void manageBookmarksNewDirectory()
Action method for "new directory".
Tracks JTree instances for drop targets.
Generates icons for MapMenuEntry instances.
void removeNode(@NotNull final DefaultMutableTreeNode treeNode)
Removes a node from the tree.
Definition: MapMenu.java:295
Icon getIcon(@NotNull final MapMenuEntry mapMenuEntry)
Returns an Icon for a MapMenuEntry instance.
Defines common UI constants used in different dialogs.
Interface for classes operating on MapMenuEntry instances.