00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.shortcuts;
00023
00024 import com.realtime.crossfire.jxclient.queue.CommandQueue;
00025 import com.realtime.crossfire.jxclient.spells.Spell;
00026 import com.realtime.crossfire.jxclient.spells.SpellsManager;
00027 import com.realtime.crossfire.jxclient.util.EventListenerList2;
00028 import java.io.File;
00029 import java.util.ArrayList;
00030 import java.util.Iterator;
00031 import java.util.List;
00032 import org.jetbrains.annotations.NotNull;
00033 import org.jetbrains.annotations.Nullable;
00034
00039 public class Shortcuts implements Iterable<Shortcut> {
00040
00045 @NotNull
00046 private final List<Shortcut> shortcuts = new ArrayList<Shortcut>();
00047
00052 private boolean modified = false;
00053
00057 @Nullable
00058 private File file = null;
00059
00063 @NotNull
00064 private final EventListenerList2<ShortcutsListener> listeners = new EventListenerList2<ShortcutsListener>(ShortcutsListener.class);
00065
00069 @NotNull
00070 private final CommandQueue commandQueue;
00071
00075 @NotNull
00076 private final SpellsManager spellsManager;
00077
00083 public Shortcuts(@NotNull final CommandQueue commandQueue, @NotNull final SpellsManager spellsManager) {
00084 this.commandQueue = commandQueue;
00085 this.spellsManager = spellsManager;
00086 }
00087
00091 public void clearShortcuts() {
00092 if (shortcuts.isEmpty()) {
00093 return;
00094 }
00095
00096 for (int i = 0; i < shortcuts.size(); i++) {
00097 final Shortcut shortcut = shortcuts.get(i);
00098 if (shortcut != null) {
00099 for (final ShortcutsListener listener : listeners.getListeners()) {
00100 listener.shortcutRemoved(i, shortcut);
00101 }
00102 shortcut.dispose();
00103 }
00104 }
00105 shortcuts.clear();
00106 modified = true;
00107 }
00108
00114 @Nullable
00115 private Shortcut getShortcut(final int index) {
00116 try {
00117 return shortcuts.get(index);
00118 } catch (final IndexOutOfBoundsException ignored) {
00119 return null;
00120 }
00121 }
00122
00128 public void setShortcut(final int index, @Nullable final Shortcut shortcut) {
00129 while (shortcuts.size() <= index) {
00130 shortcuts.add(null);
00131 }
00132
00133 final Shortcut oldShortcut = shortcuts.get(index);
00134 if (oldShortcut != null) {
00135 for (final ShortcutsListener listener : listeners.getListeners()) {
00136 listener.shortcutRemoved(index, oldShortcut);
00137 }
00138 oldShortcut.dispose();
00139 }
00140 shortcuts.set(index, shortcut);
00141 modified = true;
00142 if (shortcut != null) {
00143 for (final ShortcutsListener listener : listeners.getListeners()) {
00144 listener.shortcutAdded(index, shortcut);
00145 }
00146 }
00147 }
00148
00153 public void unsetShortcut(final int index) {
00154 setShortcut(index, null);
00155 }
00156
00164 public void setSpellShortcut(final int index, @NotNull final String spellName, final boolean cast) {
00165 final Spell spell = spellsManager.getSpell(spellName);
00166 setSpellShortcut(index, spell, cast);
00167 }
00168
00176 public void setSpellShortcut(final int index, @NotNull final Spell spell, final boolean cast) {
00177 final ShortcutSpell shortcutSpell = new ShortcutSpell(commandQueue, spell);
00178 shortcutSpell.setCast(cast);
00179 setShortcut(index, shortcutSpell);
00180 }
00181
00187 public void setCommandShortcut(final int index, @NotNull final String command) {
00188 if (command.length() <= 0) {
00189 System.err.println("shortcut: ignoring empty command");
00190 return;
00191 }
00192
00193 if (command.contains("\n")) {
00194 System.err.println("shortcut: ignoring multi-line command");
00195 return;
00196 }
00197
00198 final Shortcut shortcutCommand = new ShortcutCommand(commandQueue, command);
00199 setShortcut(index, shortcutCommand);
00200 }
00201
00206 public void executeShortcut(final int index) {
00207 final Shortcut shortcut = getShortcut(index);
00208 if (shortcut != null) {
00209 shortcut.execute();
00210 }
00211 }
00212
00217 public void addShortcutsListener(@NotNull final ShortcutsListener listener) {
00218 listeners.add(listener);
00219 }
00220
00225 public void removeShortcutsListener(@NotNull final ShortcutsListener listener) {
00226 listeners.remove(listener);
00227 }
00228
00234 public boolean isModified() {
00235 return modified;
00236 }
00237
00241 public void resetModified() {
00242 modified = false;
00243 }
00244
00249 @Nullable
00250 public File getFile() {
00251 return file;
00252 }
00253
00258 public void setFile(@Nullable final File file) {
00259 this.file = file;
00260 }
00261
00265 @Override
00266 public Iterator<Shortcut> iterator() {
00267 return shortcuts.iterator();
00268 }
00269
00270 }