Crossfire JXClient, Trunk  R20561
ShortcutSpell.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.shortcuts;
23 
28 import org.jetbrains.annotations.NotNull;
29 
34 public class ShortcutSpell extends Shortcut {
35 
39  @NotNull
40  private static final String CAST = "cast ";
41 
45  @NotNull
46  private static final String INVOKE = "invoke ";
47 
51  @NotNull
52  private final CommandQueue commandQueue;
53 
57  @NotNull
58  private final Spell spell;
59 
63  @NotNull
64  private String command = CAST;
65 
69  @NotNull
70  private final SpellListener spellListener = this::fireModifiedEvent;
71 
77  public ShortcutSpell(@NotNull final CommandQueue commandQueue, @NotNull final Spell spell) {
78  this.commandQueue = commandQueue;
79  this.spell = spell;
80  spell.addSpellListener(spellListener);
81  }
82 
87  @NotNull
88  public Spell getSpell() {
89  return spell;
90  }
91 
96  public boolean isCast() {
97  return command == CAST;
98  }
99 
104  public void setCast(final boolean cast) {
105  final String newCommand = cast ? CAST : INVOKE;
106  if (command == newCommand) {
107  return;
108  }
109 
110  command = newCommand;
112  }
113 
117  @Override
118  public void dispose() {
119  spell.removeSpellListener(spellListener);
120  }
121 
125  @Override
126  public void execute() {
127  if (!spell.isUnknown()) {
128  commandQueue.sendNcom(false, command+spell.getTag());
129  }
130  }
131 
135  @NotNull
136  @Override
137  public String getTooltipText() {
138  return command+spell.getTooltipText();
139  }
140 
144  @Override
145  public void visit(@NotNull final ShortcutVisitor visitor) {
146  visitor.visit(this);
147  }
148 
152  @Override
153  public boolean displaysFace(final Face face) {
154  return face.getFaceNum() == spell.getFaceNum();
155  }
156 
157 }
static final String CAST
Command prefix to "cast" a spell.
String command
The command for casting the spell.
void setCast(final boolean cast)
Sets whether the spell should be "cast" or "invoked".
void sendNcom(final boolean mustSend, @NotNull final String command)
Sends an "ncom" command to the server.
static final String INVOKE
Command prefix to "invoke" a spell.
Manages image information ("faces") needed to display the map view, items, and spell icons...
int getFaceNum()
Returns the face number.
Definition: Spell.java:214
void visit(@NotNull final ShortcutVisitor visitor)
boolean isUnknown()
Returns whether this spell is unknown to the character.
Definition: Spell.java:222
void addSpellListener(@NotNull final SpellListener listener)
Adds a SpellListener to be notified of changes.
Definition: Spell.java:425
Describes a Crossfire spell.
Definition: Spell.java:36
int getTag()
Returns the tag ID.
Definition: Spell.java:132
void fireModifiedEvent()
Notifies all listeners about a modification.
Definition: Shortcut.java:69
String getTooltipText()
Returns a description for this spell to be used in tooltips.
Definition: Spell.java:359
final SpellListener spellListener
The SpellListener attached to spell.
ShortcutSpell(@NotNull final CommandQueue commandQueue, @NotNull final Spell spell)
Creates a new instance.
Interface for visitors of Shortcut instances.
final CommandQueue commandQueue
The command queue for executing commands.
boolean isCast()
Returns whether the spell should be "cast" or "invoked".
void removeSpellListener(@NotNull final SpellListener listener)
Removes a SpellListener to be notified of changes.
Definition: Spell.java:433
Abstract base class for shortcut commands.
Definition: Shortcut.java:32
Maintains the pending (ncom) commands sent to the server.
int getFaceNum()
Returns the unique face id.
Definition: Face.java:105
Interface for listeners interested in Spell related events.