Crossfire JXClient, Trunk  R20561
GUISpellList.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.list;
23 
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
44 public class GUISpellList extends GUIItemList<GUIItemSpell> {
45 
49  private static final long serialVersionUID = 1L;
50 
54  @NotNull
56 
60  @NotNull
62 
66  @NotNull
67  private final String name;
68 
72  @NotNull
73  private final ItemView itemView;
74 
78  @NotNull
80 
84  @NotNull
86 
90  @NotNull
91  private final CommandQueue commandQueue;
92 
96  @NotNull
97  private final ItemPainter itemPainter;
98 
102  @NotNull
103  private final FacesManager facesManager;
104 
108  @NotNull
110 
129  public GUISpellList(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final ItemView itemView, @Nullable final AbstractLabel currentItem, @NotNull final SpellsManager spellsManager, @NotNull final KeybindingsManager keybindingsManager, @NotNull final CommandQueue commandQueue, @NotNull final ItemPainter itemPainter, @NotNull final FacesManager facesManager, @NotNull final CurrentSpellManager currentSpellManager) {
130  super(tooltipManager, elementListener, name, cellWidth, cellHeight, itemView, currentItem, new GUIItemSpell(tooltipManager, elementListener, commandQueue, name+"_template", itemPainter, -1, facesManager, spellsManager, currentSpellManager, itemView, cellHeight));
131  this.tooltipManager = tooltipManager;
132  this.elementListener = elementListener;
133  this.name = name;
134  this.itemView = itemView;
135  this.spellsManager = spellsManager;
136  this.keybindingsManager = keybindingsManager;
137  this.commandQueue = commandQueue;
138  this.itemPainter = itemPainter;
139  this.facesManager = facesManager;
140  this.currentSpellManager = currentSpellManager;
141  }
142 
151  @NotNull
152  private String getBindings(@NotNull final Spell spell, @NotNull final String prefix, @NotNull final String legend) {
153  final String search = prefix+spell.getName().charAt(0);
154  final String match = prefix+spell.getName();
155 
156  // because key bindings can specify partial names, we search in 2 steps:
157  // - search all bindings with the first spell letter
158  // - from those bindings only keep the ones the spell's command matches
159  final Iterable<KeyBinding> bindings = keybindingsManager.getBindingsForPartialCommand(search, true);
160  boolean first = true;
161  final StringBuilder sb = new StringBuilder();
162 
163  for (final KeyBinding binding : bindings) {
164  if (match.startsWith(binding.getCommandString())) {
165  if (first) {
166  sb.append(legend);
167  first = false;
168  } else {
169  sb.append(" ; ");
170  }
171 
172  sb.append(binding.getBindingDescription());
173  }
174  }
175 
176  return sb.toString();
177  }
178 
179  @Override
180  protected void updateTooltip(final int index, final int x, final int y, final int w, final int h) {
181  final Spell spell = spellsManager.getSpell(index);
182  if (spell == null) {
183  setTooltipText(null, x, y, w, h);
184  return;
185  }
186 
187  //noinspection StringBufferReplaceableByString
188  final StringBuilder sb = new StringBuilder(spell.getTooltipText());
189 
190  // find bindings to cast or invoke the spell
191  sb.append(getBindings(spell, "cast ", "<br>Cast shortcut: "));
192  sb.append(getBindings(spell, "invoke ", "<br>Invoke shortcut:"));
193 
194  setTooltipText(sb.toString(), x, y, w, h);
195  }
196 
200  @NotNull
201  @Override
202  protected GUIItemSpell newItem(final int index) {
203  return new GUIItemSpell(tooltipManager, elementListener, commandQueue, name+index, itemPainter, index, facesManager, spellsManager, currentSpellManager, itemView, 0);
204  }
205 
206 }
Abstract base class for all label classes.
Iterable< KeyBinding > getBindingsForPartialCommand(@NotNull final String command, final boolean startOnly)
Searches bindings having a command text starting with the specified value.
void updateTooltip(final int index, final int x, final int y, final int w, final int h)
final GUIElementListener elementListener
The GUIElementListener to notify.
final TooltipManager tooltipManager
The TooltipManager to update.
final ItemPainter itemPainter
The ItemPainter for painting the icon.
Manages image information ("faces") needed to display the map view, items, and spell icons...
GUISpellList(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final ItemView itemView, @Nullable final AbstractLabel currentItem, @NotNull final SpellsManager spellsManager, @NotNull final KeybindingsManager keybindingsManager, @NotNull final CommandQueue commandQueue, @NotNull final ItemPainter itemPainter, @NotNull final FacesManager facesManager, @NotNull final CurrentSpellManager currentSpellManager)
Creates a new instance.
String getBindings(@NotNull final Spell spell, @NotNull final String prefix, @NotNull final String legend)
Return a text with the keybindings for the spell.
final CommandQueue commandQueue
The CommandQueue for sending commands.
A GUIItemItem that represents an entry in a GUISpellList.
Describes a Crossfire spell.
Definition: Spell.java:36
A GUIList instance that displays GUIItemItem instances.
String getTooltipText()
Returns a description for this spell to be used in tooltips.
Definition: Spell.java:359
final KeybindingsManager keybindingsManager
The keybinding for displaying shortcuts.
final ItemView itemView
The ItemView to use.
final String name
The base name for created elements.
static final long serialVersionUID
The serial version UID.
final CurrentSpellManager currentSpellManager
The CurrentSpellManager to update when a spell is selected.
Maintains the pending (ncom) commands sent to the server.
final SpellsManager spellsManager
The spells to display.
Spell getSpell(@NotNull final String spellName)
Returns a Spell instance by spell name.
final AbstractLabel currentItem
The label to update with information about the selected item.
Maintains a mapping of face numbers to face data.
final FacesManager facesManager
The FacesManager to use.