1 | package org.ngikm;
|
---|
2 |
|
---|
3 | import java.util.Optional;
|
---|
4 | import java.util.function.Consumer;
|
---|
5 | import java.util.function.Function;
|
---|
6 |
|
---|
7 | import org.expeditee.gio.EcosystemManager;
|
---|
8 | import org.expeditee.gui.Frame;
|
---|
9 | import org.expeditee.items.Text;
|
---|
10 |
|
---|
11 | public class Util {
|
---|
12 |
|
---|
13 | @FunctionalInterface
|
---|
14 | public interface ThrowingConsumer<T, E extends Exception> extends Consumer<T> {
|
---|
15 | @Override
|
---|
16 | default void accept(final T t) {
|
---|
17 | try {
|
---|
18 | acceptThrows(t);
|
---|
19 | } catch (final Exception e) {
|
---|
20 | throw new RuntimeException(e);
|
---|
21 | }
|
---|
22 | }
|
---|
23 | void acceptThrows(final T t) throws E;
|
---|
24 | }
|
---|
25 |
|
---|
26 | @FunctionalInterface
|
---|
27 | public interface ThrowingFunction<T, R, E extends Exception> extends Function<T, R> {
|
---|
28 | @Override
|
---|
29 | default R apply(T t) {
|
---|
30 | try {
|
---|
31 | return applyThrows(t);
|
---|
32 | } catch (final Exception e) {
|
---|
33 | throw new RuntimeException(e);
|
---|
34 | }
|
---|
35 | }
|
---|
36 | R applyThrows(final T t) throws E;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public static Text AddText(final Frame toFrame, final int x, final int y, final String content) {
|
---|
40 | final int windowHeight = EcosystemManager.getGraphicsManager().getWindowSize().height;
|
---|
41 | final Optional<Text> item = toFrame.getTextItems().stream().filter(i -> {
|
---|
42 | final org.expeditee.core.Point p = i.getPosition();
|
---|
43 | return (p.getX() == x) && (p.getY() == y);
|
---|
44 | }).findAny();
|
---|
45 | if (item.isPresent()) {
|
---|
46 | if (y >= (windowHeight * 0.9)) {
|
---|
47 | return AddText(toFrame, x + 100, y, content);
|
---|
48 | }
|
---|
49 | return AddText(toFrame, x, y + 100, content);
|
---|
50 | } else {
|
---|
51 | final Text text = toFrame.addText(x, y, content, null, null);
|
---|
52 | text.setOwner(toFrame.getOwner());
|
---|
53 | return text;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|