Gridarta Editor
TreasureCellRenderer.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.treasurelist;
21 
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Font;
25 import javax.swing.BorderFactory;
26 import javax.swing.JTree;
27 import javax.swing.tree.DefaultMutableTreeNode;
28 import javax.swing.tree.DefaultTreeCellRenderer;
42 import org.jetbrains.annotations.NotNull;
43 
49 public class TreasureCellRenderer extends DefaultTreeCellRenderer {
50 
54  private static final long serialVersionUID = 1L;
55 
59  @NotNull
61 
65  @NotNull
67 
71  @NotNull
73 
78  @NotNull
79  private final DefaultMutableTreeNode root;
80 
85 
86  @Override
87  public void visit(@NotNull final ArchTreasureObj treasureObj) {
88  try {
89  setIcon(faceObjectProviders.getFace(archetypeSet.getArchetype(treasureObj.getName())));
90  } catch (final UndefinedArchetypeException ignored) {
91  setIcon(resourceIcons.getResourceIcon(ResourceIcons.SQUARE_NO_ARCH));
92  }
93  setBorder(BorderFactory.createEmptyBorder(1, 0, 1, 0));
94  }
95 
96  @Override
97  public void visit(@NotNull final FolderTreasureObj treasureObj) {
98  }
99 
100  @Override
101  public void visit(@NotNull final NoTreasureObj treasureObj) {
102  setForeground(Color.gray);
103  setIcon(resourceIcons.getResourceIcon(ResourceIcons.TREASURE_NO));
104  }
105 
106  @Override
107  public void visit(@NotNull final TreasureListTreasureObj treasureObj) {
108  if (treasureObj.getListType() == TreasureListTreasureObjType.MULTI) {
109  if (treasureObj.getName().equalsIgnoreCase("none")) {
110  setIcon(null);
111  setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
112  } else {
113  setBorder(BorderFactory.createEmptyBorder(1, 0, 1, 0));
114  setIcon(resourceIcons.getResourceIcon(ResourceIcons.TREASURE_LIST));
115  }
116  } else {
117  if (treasureObj.getName().equalsIgnoreCase("none")) {
118  setIcon(null);
119  setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
120  } else {
121  setBorder(BorderFactory.createEmptyBorder(1, 0, 1, 0));
122  setIcon(resourceIcons.getResourceIcon(ResourceIcons.TREASUREONE_LIST));
123  }
124  }
125  }
126 
127  @Override
128  public void visit(@NotNull final YesTreasureObj treasureObj) {
129  setForeground(Color.gray);
130  setIcon(resourceIcons.getResourceIcon(ResourceIcons.TREASURE_YES));
131  }
132 
133  };
134 
143  public TreasureCellRenderer(@NotNull final ArchetypeSet<?, ?, ?> archetypeSet, @NotNull final DefaultMutableTreeNode root, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final ResourceIcons resourceIcons) {
144  this.archetypeSet = archetypeSet;
145  this.root = root;
146  this.faceObjectProviders = faceObjectProviders;
147  this.resourceIcons = resourceIcons;
148  }
149 
150  @Override
151  public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
152  super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
153 
154  final DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
155 
156  setFont(getFont().deriveFont(Font.PLAIN));
157  if (node.isRoot()) {
158  setForeground(Color.gray);
159  setIcon(null);
160  } else {
161  final TreasureObj content = ((TreasureTreeNode) value).getTreasureObj();
162  content.visit(treasureObjVisitor);
163 
164  if (node.getParent() == root) {
165  if (!(content instanceof FolderTreasureObj)) {
166  setFont(getFont().deriveFont(Font.BOLD));
167  }
168  } else {
169  final TreasureTreeNode parent = (TreasureTreeNode) node.getParent();
170  if (parent.getTreasureObj() instanceof FolderTreasureObj) {
171  setFont(getFont().deriveFont(Font.BOLD));
172  }
173  }
174  }
175 
176  return this;
177  }
178 
179 }
A TreasureObj representing a "no" entry.
Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus)
TreasureCellRenderer(@NotNull final ArchetypeSet<?, ?, ?> archetypeSet, @NotNull final DefaultMutableTreeNode root, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final ResourceIcons resourceIcons)
Create a TreasureCellRenderer: load icons and initialize fonts.
Base package of all Gridarta classes.
final TreasureObjVisitor treasureObjVisitor
The TreasureObjVisitor for setting rendering parameters.
final ResourceIcons resourceIcons
The ResourceIcons for creating icons.
R getArchetype(@NotNull String archetypeName)
Returns an Archetype by its name.
final ArchetypeSet<?, ?, ?> archetypeSet
The ArchetypeSet.
abstract void visit(@NotNull TreasureObjVisitor visitor)
Calls the TreasureObjVisitor callback function appropriate for this instance.
final FaceObjectProviders faceObjectProviders
The FaceObjectProviders for looking up faces.
A TreasureObj representing an "arch" entry.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
Creates ImageIcon instances from resources.
This cell renderer is responsible for drawing the treasure-object cells in the JTree.
A TreasureObj representing a "yes" entry.
Subclass: Nodes in the CFTreasureListTree.
final DefaultMutableTreeNode root
The root node.
Interface that captures similarities between different ArchetypeSet implementations.
Subclass: UserObject (= content object) for nodes in the CFTreasureListTree These can be either treas...
ImageIcon getResourceIcon(@NotNull final String iconName)
Returns the image icon for the given icon name.
Interface for visitors of TreasureObj instances.
static final long serialVersionUID
Serial Version UID.