Crossfire JXClient, Trunk
CommandList.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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.commandlist;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
34 public class CommandList {
35 
39  @NotNull
41 
45  @NotNull
46  private final List<GUICommand> commandList = new ArrayList<>();
47 
52  public CommandList(@NotNull final CommandListType commandListType) {
53  this.commandListType = commandListType;
54  }
55 
60  public void add(@NotNull final GUICommand guiCommand) {
61  commandList.add(guiCommand);
62  }
63 
68  private boolean canExecute() {
69  switch (commandListType) {
70  case AND:
71  for (GUICommand command : commandList) {
72  if (!command.canExecute()) {
73  return false;
74  }
75  }
76  break;
77 
78  case OR:
79  boolean ok = false;
80  for (GUICommand command : commandList) {
81  if (command.canExecute()) {
82  ok = true;
83  break;
84  }
85  }
86  if (!ok) {
87  return false;
88  }
89  break;
90  }
91 
92  return true;
93  }
94 
99  public void execute() {
100  if (!canExecute()) {
101  return;
102  }
103 
105  }
106 
111  @NotNull
112  public String getCommandString() {
113  final StringBuilder sb = new StringBuilder();
114  boolean firstCommand = true;
115  for (GUICommand guiCommand : commandList) {
116  if (!(guiCommand instanceof GUICommand2)) {
117  throw new AssertionError("Cannot encode command of type "+guiCommand.getClass().getName());
118  }
119  final String commandString = ((GUICommand2)guiCommand).getCommandString();
120  if (firstCommand) {
121  firstCommand = false;
122  } else {
123  sb.append(';');
124  }
125  sb.append(commandString);
126  }
127  return sb.toString();
128  }
129 
135  public boolean containsCommand(@NotNull final Class<? extends GUICommand> command) {
136  for (final GUICommand tmp : commandList) {
137  if (command.isAssignableFrom(tmp.getClass())) {
138  return true;
139  }
140  }
141  return false;
142  }
143 
149  @Nullable
151  return commandList.size() == 1 ? commandList.get(0) : null;
152  }
153 
154 }
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.canExecute
boolean canExecute()
Definition: CommandList.java:68
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.CommandList
CommandList(@NotNull final CommandListType commandListType)
Definition: CommandList.java:52
com.realtime.crossfire.jxclient.gui.commandlist.CommandList
Definition: CommandList.java:34
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.commandListType
final CommandListType commandListType
Definition: CommandList.java:40
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.containsCommand
boolean containsCommand(@NotNull final Class<? extends GUICommand > command)
Definition: CommandList.java:135
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.getCommandString
String getCommandString()
Definition: CommandList.java:112
com.realtime.crossfire.jxclient.gui.commandlist.GUICommand.execute
void execute()
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.getSingleCommand
GUICommand getSingleCommand()
Definition: CommandList.java:150
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.execute
void execute()
Definition: CommandList.java:99
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.add
void add(@NotNull final GUICommand guiCommand)
Definition: CommandList.java:60
com.realtime.crossfire.jxclient.gui.commandlist.GUICommand2
Definition: GUICommand2.java:31
com.realtime.crossfire.jxclient.gui.commandlist.CommandListType
Definition: CommandListType.java:29
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.commandList
final List< GUICommand > commandList
Definition: CommandList.java:46
com.realtime.crossfire.jxclient.gui.commandlist.GUICommand
Definition: GUICommand.java:29