/
ActionRow.java
306 lines (283 loc) · 9.46 KB
/
ActionRow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.interactions.components;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.interactions.component.ButtonImpl;
import net.dv8tion.jda.internal.interactions.component.SelectMenuImpl;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;
/**
* One row of components.
*
* @see ItemComponent
* @see LayoutComponent
*/
public class ActionRow implements LayoutComponent
{
private final List<ItemComponent> components = new ArrayList<>();
private ActionRow() {}
/**
* Load ActionRow from serialized representation.
* <br>Inverse of {@link #toData()}.
*
* @param data
* Serialized version of an action row
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided data is not a valid action row
* @throws IllegalArgumentException
* If the data is null or the type is not 1
*
* @return ActionRow instance
*/
@Nonnull
public static ActionRow fromData(@Nonnull DataObject data)
{
Checks.notNull(data, "Data");
ActionRow row = new ActionRow();
if (data.getInt("type", 0) != 1)
throw new IllegalArgumentException("Data has incorrect type. Expected: 1 Found: " + data.getInt("type"));
data.getArray("components")
.stream(DataArray::getObject)
.map(obj -> {
switch (Component.Type.fromKey(obj.getInt("type")))
{
case BUTTON:
return new ButtonImpl(obj);
case SELECT_MENU:
return new SelectMenuImpl(obj);
default:
return null;
}
})
.filter(Objects::nonNull)
.forEach(row.components::add);
return row;
}
/**
* Create one row of {@link ItemComponent components}.
* <br>You cannot currently mix different types of components and each type has its own maximum defined by {@link Component.Type#getMaxPerRow()}.
*
* @param components
* The components for this action row
*
* @throws IllegalArgumentException
* If anything is null, empty, or an invalid number of components are provided
*
* @return The action row
*/
@Nonnull
public static ActionRow of(@Nonnull Collection<? extends ItemComponent> components)
{
Checks.noneNull(components, "Components");
return of(components.toArray(new ItemComponent[0]));
}
/**
* Create one row of {@link ItemComponent components}.
* <br>You cannot currently mix different types of components and each type has its own maximum defined by {@link Component.Type#getMaxPerRow()}.
*
* @param components
* The components for this action row
*
* @throws IllegalArgumentException
* If anything is null, empty, or an invalid number of components are provided
*
* @return The action row
*/
@Nonnull
public static ActionRow of(@Nonnull ItemComponent... components)
{
Checks.noneNull(components, "Components");
Checks.check(components.length > 0, "Cannot have empty row!");
ActionRow row = new ActionRow();
Collections.addAll(row.components, components);
if (!row.isValid())
{
Map<Component.Type, List<ItemComponent>> grouped = Arrays.stream(components).collect(Collectors.groupingBy(Component::getType));
String provided = grouped.entrySet()
.stream()
.map(entry -> entry.getValue().size() + "/" + entry.getKey().getMaxPerRow() + " of " + entry.getKey())
.collect(Collectors.joining(", "));
throw new IllegalArgumentException("Cannot create action row with invalid component combinations. Provided: " + provided);
}
return row;
}
/**
* Partitions the provided {@link ItemComponent components} into a list of ActionRow instances.
* <br>This will split the provided components by {@link Type#getMaxPerRow()} and create homogeneously typed rows,
* meaning they will not have mixed component types.
*
* <p><b>Example</b>
* <pre>{@code
* List<ItemComponent> components = Arrays.asList(
* Button.primary("id1", "Hello"),
* Button.secondary("id2", "World"),
* SelectMenu.create("menu:id").build()
* );
*
* List<ActionRow> partitioned = ActionRow.partition(components);
* // partitioned[0] = ActionRow(button, button)
* // partitioned[1] = ActionRow(selectMenu)
* }</pre>
*
* @param components
* The components to partition
*
* @throws IllegalArgumentException
* If null is provided
*
* @return {@link List} of {@link ActionRow}
*/
@Nonnull
public static List<ActionRow> partitionOf(@Nonnull Collection<? extends ItemComponent> components)
{
Checks.noneNull(components, "Components");
List<ActionRow> rows = new ArrayList<>();
// The current action row we are building
List<ItemComponent> currentRow = null;
// The component types contained in that row (for now it can't have mixed types)
Component.Type type = null;
for (ItemComponent current : components)
{
if (type != current.getType() || currentRow.size() == type.getMaxPerRow())
{
type = current.getType();
ActionRow row = ActionRow.of(current);
currentRow = row.components;
rows.add(row);
}
else
{
currentRow.add(current);
}
}
return rows;
}
/**
* Partitions the provided {@link ItemComponent components} into a list of ActionRow instances.
* <br>This will split the provided components by {@link Type#getMaxPerRow()} and create homogeneously typed rows,
* meaning they will not have mixed component types.
*
* <p><b>Example</b>
* <pre>{@code
* List<ItemComponent> components = Arrays.asList(
* Button.primary("id1", "Hello"),
* Button.secondary("id2", "World"),
* SelectMenu.create("menu:id").build()
* );
*
* List<ActionRow> partitioned = ActionRow.partition(components);
* // partitioned[0] = ActionRow(button, button)
* // partitioned[1] = ActionRow(selectMenu)
* }</pre>
*
* @param components
* The components to partition
*
* @throws IllegalArgumentException
* If null is provided
*
* @return {@link List} of {@link ActionRow}
*/
@Nonnull
public static List<ActionRow> partitionOf(@Nonnull ItemComponent... components)
{
Checks.notNull(components, "Components");
return partitionOf(Arrays.asList(components));
}
/**
* Mutable list of components in this ActionRow.
* <br>ActionRows should not be empty and are limited to 5 buttons.
*
* @return The list of components
*/
@Nonnull
@Override
public List<ItemComponent> getComponents()
{
return components;
}
@Nonnull
@Override
@CheckReturnValue
public ActionRow withDisabled(boolean disabled)
{
return ActionRow.of(components.stream()
.map(c -> {
if (c instanceof ActionComponent)
return ((ActionComponent) c).withDisabled(disabled);
return c;
})
.collect(Collectors.toList()));
}
@Nonnull
@Override
@CheckReturnValue
public ActionRow asDisabled()
{
return withDisabled(true);
}
@Nonnull
@Override
@CheckReturnValue
public ActionRow asEnabled()
{
return withDisabled(false);
}
@Nonnull
@Override
public Component.Type getType()
{
return Component.Type.ACTION_ROW;
}
@Nonnull
@Override
public DataObject toData()
{
return DataObject.empty()
.put("type", 1)
.put("components", DataArray.fromCollection(components));
}
@Nonnull
@Override
public Iterator<ItemComponent> iterator()
{
return components.iterator();
}
@Override
public String toString()
{
return "ActionRow(" + components + ")";
}
@Override
public int hashCode()
{
return components.hashCode();
}
@Override
public boolean equals(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof ActionRow))
return false;
return components.equals(((ActionRow) obj).components);
}
}