|
| 1 | +package de.geolykt.starloader.impl; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | +import org.jetbrains.annotations.Nullable; |
| 8 | + |
| 9 | +import de.geolykt.starloader.DebugNagException; |
| 10 | +import de.geolykt.starloader.api.gui.AutocloseableDialog; |
| 11 | +import de.geolykt.starloader.api.gui.BasicDialogCloseListener; |
| 12 | +import de.geolykt.starloader.api.gui.DialogCloseCause; |
| 13 | +import de.geolykt.starloader.api.gui.WidgetAction; |
| 14 | +import de.geolykt.starloader.api.gui.WidgetActionListener; |
| 15 | +import snoddasmannen.galimulator.ui.Widget$WIDGET_MESSAGE; |
| 16 | + |
| 17 | +public class WidgetActionListenerWrapper implements snoddasmannen.galimulator.hg { |
| 18 | + |
| 19 | + private final List<BasicDialogCloseListener> closeListeners; |
| 20 | + private final List<WidgetActionListener> actionListeners; |
| 21 | + private final AutocloseableDialog parent; |
| 22 | + |
| 23 | + /** |
| 24 | + * Initiates the wrapper with the parent and listeners known. The listeners can be modified later however, provided |
| 25 | + * the list implementation supports that. |
| 26 | + * @param parent The parent that is closable automatically, a null value means that it will never be closed automatically. |
| 27 | + * @param closeListeners The close listeners to have initially |
| 28 | + * @param actionListeners The widget action listeners to have initially |
| 29 | + */ |
| 30 | + WidgetActionListenerWrapper(@Nullable AutocloseableDialog parent, |
| 31 | + @NotNull List<BasicDialogCloseListener> closeListeners, @NotNull List<WidgetActionListener> actionListeners) { |
| 32 | + this.parent = parent; |
| 33 | + this.closeListeners = closeListeners; |
| 34 | + this.actionListeners = actionListeners; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Initiates the wrapper with a parent that is closable automatically. If that parameter is null it should behave the |
| 39 | + * same way as {@link #WidgetActionListenerWrapper()} |
| 40 | + * @param parent The parent dialog |
| 41 | + */ |
| 42 | + public WidgetActionListenerWrapper(@Nullable AutocloseableDialog parent) { |
| 43 | + this(parent, new ArrayList<>(), new ArrayList<>()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Initiates the wrapper with no further information. If the parent Widget is an automatically closing dialog, then |
| 48 | + * {@link #WidgetActionListenerWrapper(AutoCloseable)} should be preferred as |
| 49 | + * otherwise the {@link BasicDialogCloseListener} might get multiple automatic close notifications. |
| 50 | + */ |
| 51 | + public WidgetActionListenerWrapper() { |
| 52 | + this(null, new ArrayList<>(), new ArrayList<>()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void a(Widget$WIDGET_MESSAGE var1) { |
| 57 | + if (var1 == Widget$WIDGET_MESSAGE.a) { |
| 58 | + // Check if it was closed automatically due to timeout, if it was, then ignore this request |
| 59 | + if (parent == null) { |
| 60 | + notifyClose(DialogCloseCause.MANUAL_CLOSE); |
| 61 | + } else { |
| 62 | + long timeout = parent.getAutocloseTime(); |
| 63 | + if (timeout == -1 || timeout < System.currentTimeMillis()) { |
| 64 | + notifyClose(DialogCloseCause.MANUAL_CLOSE); |
| 65 | + } |
| 66 | + } |
| 67 | + for (WidgetActionListener listener : actionListeners) { |
| 68 | + listener.onAction(WidgetAction.CLOSE); |
| 69 | + } |
| 70 | + } else { |
| 71 | + if (var1 == null) { |
| 72 | + DebugNagException.nag("Null widget message!"); |
| 73 | + } |
| 74 | + WidgetAction action = var1 == Widget$WIDGET_MESSAGE.b ? WidgetAction.RESIZE : WidgetAction.REDRAW; |
| 75 | + for (WidgetActionListener listener : actionListeners) { |
| 76 | + listener.onAction(action); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void notifyClose(DialogCloseCause cause) { |
| 82 | + for (BasicDialogCloseListener listener : closeListeners) { |
| 83 | + listener.onClose(cause, null); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void addListener(@NotNull BasicDialogCloseListener listener) { |
| 88 | + closeListeners.add(listener); |
| 89 | + } |
| 90 | + |
| 91 | + public void addListener(@NotNull WidgetActionListener listener) { |
| 92 | + actionListeners.add(listener); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments