Allow null tabs in TabGUI

This commit is contained in:
NichtStudioCode 2021-11-07 17:33:33 +01:00
parent 4b9486eef7
commit 3c57947f3a
2 changed files with 24 additions and 2 deletions

@ -8,6 +8,7 @@ import de.studiocode.invui.gui.structure.Structure;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -38,6 +39,8 @@ public class SimpleTabGUI extends TabGUI {
} }
private List<SlotElement> getLinkingElements(GUI gui) { private List<SlotElement> getLinkingElements(GUI gui) {
if (gui == null) return null;
List<SlotElement> elements = new ArrayList<>(); List<SlotElement> elements = new ArrayList<>();
for (int slot = 0; slot < gui.getSize(); slot++) { for (int slot = 0; slot < gui.getSize(); slot++) {
SlotElement link = new LinkedSlotElement(gui, slot); SlotElement link = new LinkedSlotElement(gui, slot);
@ -48,7 +51,7 @@ public class SimpleTabGUI extends TabGUI {
} }
public List<GUI> getTabs() { public List<GUI> getTabs() {
return tabs; return Collections.unmodifiableList(tabs);
} }
@Override @Override
@ -56,4 +59,9 @@ public class SimpleTabGUI extends TabGUI {
return linkingElements.get(tab); return linkingElements.get(tab);
} }
@Override
public boolean isTabAvailable(int tab) {
return tabs.get(tab) != null;
}
} }

@ -12,7 +12,7 @@ public abstract class TabGUI extends BaseGUI implements Controllable {
private final int tabAmount; private final int tabAmount;
private final int[] listSlots; private final int[] listSlots;
private int currentTab; private int currentTab = -1;
public TabGUI(int width, int height, int tabAmount, int... listSlots) { public TabGUI(int width, int height, int tabAmount, int... listSlots) {
super(width, height); super(width, height);
@ -28,12 +28,16 @@ public abstract class TabGUI extends BaseGUI implements Controllable {
public void showTab(int tab) { public void showTab(int tab) {
if (tab < 0 || tab >= tabAmount) if (tab < 0 || tab >= tabAmount)
throw new IllegalArgumentException("Tab out of bounds"); throw new IllegalArgumentException("Tab out of bounds");
if (!isTabAvailable(tab))
return;
currentTab = tab; currentTab = tab;
update(); update();
} }
protected void update() { protected void update() {
if (currentTab == -1) currentTab = getFirstAvailableTab();
updateControlItems(); updateControlItems();
updateContent(); updateContent();
} }
@ -47,10 +51,20 @@ public abstract class TabGUI extends BaseGUI implements Controllable {
} }
} }
public int getFirstAvailableTab() {
for (int tab = 0; tab < tabAmount; tab++) {
if (isTabAvailable(tab)) return tab;
}
throw new UnsupportedOperationException("At least one tab needs to be available");
}
public int getCurrentTab() { public int getCurrentTab() {
return currentTab; return currentTab;
} }
public abstract List<SlotElement> getSlotElements(int tab); public abstract List<SlotElement> getSlotElements(int tab);
public abstract boolean isTabAvailable(int tab);
} }