Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avatar Overhaul #191

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions .idea/codeStyles/Project.xml

This file was deleted.

5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

21 changes: 2 additions & 19 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions .idea/encodings.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions .idea/modules.xml

This file was deleted.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
Expand All @@ -34,14 +35,16 @@ public AvatarService(FafApiCommunicationService fafApi, AvatarAssignmentMapper a
this.avatarAssignmentMapper = avatarAssignmentMapper;
}

public List<Avatar> getAll() {
log.debug("Retrieving all avatars");
List<Avatar> result = fafApi.getAll(Avatar.class, ElideNavigator.of(Avatar.class)
.collection()
.addInclude("assignments")
.addInclude("assignments.player"));
log.trace("found {} avatars", result.size());
return result;
public CompletableFuture<List<Avatar>> getAll() {
return CompletableFuture.supplyAsync(() -> {
log.debug("Retrieving all avatars");
List<Avatar> result = fafApi.getAll(Avatar.class, ElideNavigator.of(Avatar.class)
.collection()
.addInclude("assignments")
.addInclude("assignments.player"));
log.trace("found {} avatars", result.size());
return result;
});
}

private List<Avatar> findAvatarsByAttribute(@NotNull String attribute, @NotNull String pattern) {
Expand All @@ -65,19 +68,21 @@ public List<Avatar> findAvatarsByTooltip(@NotNull String pattern) {
return findAvatarsByAttribute("tooltip", pattern);
}

public List<Avatar> findAvatarsByAssignedUser(@NotNull String pattern) {
log.debug("Searching for avatars by assigned player with pattern: {}", pattern);
boolean isNumeric = pattern.matches("^[0-9]+$");
public CompletableFuture<List<Avatar>> findAvatarsByAssignedUser(@NotNull String pattern) {
return CompletableFuture.supplyAsync(() -> {
log.debug("Searching for avatars by assigned player with pattern: {}", pattern);
boolean isNumeric = pattern.matches("^[0-9]+$");

ElideNavigatorOnCollection<Avatar> navigator = ElideNavigator.of(Avatar.class)
.collection()
.addInclude("assignments")
.addInclude("assignments.player")
.setFilter(ElideNavigator.qBuilder().string(isNumeric ? "assignments.player.id" : "assignments.player.login").eq(pattern));
ElideNavigatorOnCollection<Avatar> navigator = ElideNavigator.of(Avatar.class)
.collection()
.addInclude("assignments")
.addInclude("assignments.player")
.setFilter(ElideNavigator.qBuilder().string(isNumeric ? "assignments.player.id" : "assignments.player.login").eq(pattern));

List<Avatar> result = fafApi.getAll(Avatar.class, navigator);
log.trace("found {} avatars", result.size());
return result;
List<Avatar> result = fafApi.getAll(Avatar.class, navigator);
log.trace("found {} avatars", result.size());
return result;
});
}

public void uploadAvatar(String name, File avatarImageFile) {
Expand Down
29 changes: 25 additions & 4 deletions src/main/java/com/faforever/moderatorclient/ui/ViewHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static void buildAvatarTableView(TableView<AvatarFX> tableView, Observabl
TableColumn<AvatarFX, String> idColumn = new TableColumn<>("ID");
idColumn.setCellValueFactory(o -> o.getValue().idProperty());
idColumn.setComparator(Comparator.comparingInt(Integer::parseInt));
idColumn.setMinWidth(50);
idColumn.setMinWidth(25);
tableView.getColumns().add(idColumn);
extractors.put(idColumn, AvatarFX::getId);

Expand All @@ -188,7 +188,7 @@ public static void buildAvatarTableView(TableView<AvatarFX> tableView, Observabl

TableColumn<AvatarFX, String> tooltipColumn = new TableColumn<>("Tooltip");
tooltipColumn.setCellValueFactory(o -> o.getValue().tooltipProperty());
tooltipColumn.setMinWidth(50);
tooltipColumn.setMinWidth(250);
tableView.getColumns().add(tooltipColumn);
extractors.put(tooltipColumn, AvatarFX::getTooltip);

Expand All @@ -199,7 +199,7 @@ public static void buildAvatarTableView(TableView<AvatarFX> tableView, Observabl

TableColumn<AvatarFX, String> urlColumn = new TableColumn<>("URL");
urlColumn.setCellValueFactory(o -> o.getValue().urlProperty());
urlColumn.setMinWidth(50);
urlColumn.setMinWidth(500);
tableView.getColumns().add(urlColumn);
extractors.put(urlColumn, AvatarFX::getUrl);

Expand All @@ -221,7 +221,7 @@ public static void bindMapTreeViewToImageView(TreeTableView<MapTableItemAdapter>
});
}

public static void buildAvatarAssignmentTableView(TableView<AvatarAssignmentFX> tableView, ObservableList<AvatarAssignmentFX> data) {
public static void buildAvatarAssignmentTableView(TableView<AvatarAssignmentFX> tableView, ObservableList<AvatarAssignmentFX> data, @Nullable Consumer<AvatarAssignmentFX> onRemove) {
tableView.setItems(data);
HashMap<TableColumn<AvatarAssignmentFX, ?>, Function<AvatarAssignmentFX, ?>> extractors = new HashMap<>();

Expand Down Expand Up @@ -265,6 +265,27 @@ public static void buildAvatarAssignmentTableView(TableView<AvatarAssignmentFX>
assignedAtColumn.setMinWidth(180);
tableView.getColumns().add(assignedAtColumn);

TableColumn<AvatarAssignmentFX, AvatarAssignmentFX> removeColumn = new TableColumn<>("Remove");
removeColumn.setMinWidth(90);
removeColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue()));
removeColumn.setCellFactory(param -> new TableCell<AvatarAssignmentFX, AvatarAssignmentFX>() {

@Override
protected void updateItem(AvatarAssignmentFX item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
Button button = new Button("Remove");
button.setOnMouseClicked(event -> onRemove.accept(item));
button.setTextFill(Color.rgb(200, 10, 10));

setGraphic(button);
return;
}
setGraphic(null);
}
});
tableView.getColumns().add(removeColumn);
Comment on lines +268 to +287
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this only be added if onRemove != null?


applyCopyContextMenus(tableView, extractors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.faforever.moderatorclient.ui.caches.AvatarCache;
import com.faforever.moderatorclient.ui.domain.AvatarAssignmentFX;
import com.faforever.moderatorclient.ui.domain.AvatarFX;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.TableCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
Expand All @@ -18,27 +20,44 @@ public class UrlImageViewTableCell<T> extends TableCell<T, String> {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if (item != null) {
if (!Objects.equals(currentUrl, item)) {
currentUrl = item;
Image img;
if (getTableRow() != null && getTableRow().getItem() != null) {
String cacheKey = cacheKeyFrom(item, getTableRow().getItem());
if (AvatarCache.getInstance().containsKey(cacheKey)) {
img = AvatarCache.getInstance().get(cacheKey);
} else {
img = new Image(item, true);
AvatarCache.getInstance().put(cacheKey, img);

if (item == null) return;
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
if (item != null) {
if (!Objects.equals(currentUrl, item)) {
currentUrl = item;
Image img;
if (getTableRow() != null && getTableRow().getItem() != null) {
String cacheKey = cacheKeyFrom(item, getTableRow().getItem());
if (AvatarCache.getInstance().containsKey(cacheKey)) {
img = AvatarCache.getInstance().get(cacheKey);
} else {
img = new Image(item, true);
AvatarCache.getInstance().put(cacheKey, img);
}
} else {
img = new Image(item);
}
imageView.setImage(img);
}
Platform.runLater(() -> {
if (Objects.equals(currentUrl, item)) {
setGraphic(imageView);
}

});

} else {
img = new Image(item);
setGraphic(null);
}
imageView.setImage(img);
return null;
}
setGraphic(imageView);
} else {
setGraphic(null);
}
};

new Thread(task).start();

}

private String cacheKeyFrom(String item, Object rawData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.faforever.moderatorclient.ui.data_cells;

import com.faforever.moderatorclient.ui.caches.AvatarCache;
import com.faforever.moderatorclient.ui.domain.AvatarAssignmentFX;
import com.faforever.moderatorclient.ui.domain.AvatarFX;
import javafx.scene.control.TableCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.time.OffsetDateTime;
import java.util.Objects;

public class UrlImageViewTableCellSync<T> extends TableCell<T, String> {
private ImageView imageView = new ImageView();
private String currentUrl;

@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if (item != null) {
if (!Objects.equals(currentUrl, item)) {
currentUrl = item;
Image img;
if (getTableRow() != null && getTableRow().getItem() != null) {
String cacheKey = cacheKeyFrom(item, getTableRow().getItem());
if (AvatarCache.getInstance().containsKey(cacheKey)) {
img = AvatarCache.getInstance().get(cacheKey);
} else {
img = new Image(item, true);
AvatarCache.getInstance().put(cacheKey, img);
}
} else {
img = new Image(item);
}
imageView.setImage(img);
}

setGraphic(imageView);


} else {
setGraphic(null);
}

}

private String cacheKeyFrom(String item, Object rawData) {
OffsetDateTime updateTime = null;
if (rawData instanceof AvatarFX) {
updateTime = ((AvatarFX)rawData).getUpdateTime();
} else if (rawData instanceof AvatarAssignmentFX) {
updateTime = ((AvatarAssignmentFX)rawData).getUpdateTime();
}
return updateTime != null ? item+updateTime.toString() : item;
}
}