Gridarta Editor
MapMenuEntryTreeCellRenderer.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.mapmenu;
21 
22 import java.awt.Component;
23 import java.awt.Image;
24 import javax.swing.ImageIcon;
25 import javax.swing.JTree;
26 import javax.swing.tree.DefaultMutableTreeNode;
27 import javax.swing.tree.DefaultTreeCellRenderer;
30 import org.jetbrains.annotations.NotNull;
31 
37 public class MapMenuEntryTreeCellRenderer extends DefaultTreeCellRenderer implements MapMenuEntryVisitor {
38 
42  private static final long serialVersionUID = 1L;
43 
47  @NotNull
49 
54  private boolean expanded;
55 
60  public MapMenuEntryTreeCellRenderer(@NotNull final MapImageCache<?, ?, ?> mapImageCache) {
61  this.mapImageCache = mapImageCache;
62  }
63 
64  @Override
65  public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
66  super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
67  final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
68  final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
69  setText(mapMenuEntry.getTitle());
70  this.expanded = expanded;
71  mapMenuEntry.visit(this);
72  return this;
73  }
74 
75  @Override
76  public void visit(@NotNull final MapMenuEntryDir mapMenuEntry) {
77  setIcon(expanded ? getOpenIcon() : getClosedIcon());
78  }
79 
80  @Override
81  public void visit(@NotNull final MapMenuEntryMap mapMenuEntry) {
82  final MapFile mapFile = mapMenuEntry.getMapFile();
83  setToolTipText(mapFile.getMapPath().toString());
84  final Image icon = mapImageCache.getOrCreateIcon(mapFile.getFile());
85  if (icon != null) {
86  setIcon(new ImageIcon(icon));
87  }
88  }
89 
90 }
Image getOrCreateIcon(@NotNull final File mapFile)
Returns an icon Image for a given map file.
A MapMenuEntry that represents a directory.
Graphical User Interface of Gridarta.
A DefaultTreeCellRenderer for rendering MapMenuEntry instances.
String getTitle()
Returns the entry&#39;s title.
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
Base package of all Gridarta classes.
MapMenuEntryTreeCellRenderer(@NotNull final MapImageCache<?, ?, ?> mapImageCache)
Creates a new instance.
abstract void visit(@NotNull MapMenuEntryVisitor visitor)
Calls the.
void visit(@NotNull final MapMenuEntryDir mapMenuEntry)
Called for MapMenuEntryDir instances.
final MapImageCache<?, ?, ?> mapImageCache
The MapImageCache queried for icons.
Caches icon and preview images for map files.
void visit(@NotNull final MapMenuEntryMap mapMenuEntry)
Called for MapMenuEntryMap instances.
Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus)
Abstract base class for recent and bookmark menu entries.
A MapMenuEntry that represents a map.
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
The location of a map file with a map directory.
Definition: MapFile.java:31
boolean expanded
Parameter for passing the expanded state into the MapMenuEntryVisitor function.
Interface for classes operating on MapMenuEntry instances.