Inceptum/launcher-gtk/src/main/java/io/gitlab/jfronny/inceptum/gtk/control/settings/SettingsTab.java

85 lines
2.8 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.inceptum.gtk.control.settings;
2023-01-28 19:25:52 +01:00
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.inceptum.gtk.GtkEnvBackend;
import io.gitlab.jfronny.inceptum.gtk.control.ILabel;
2023-01-29 16:33:59 +01:00
import io.gitlab.jfronny.inceptum.gtk.control.settings.SettingsTab.SectionBuilder.Section;
2023-01-28 19:25:52 +01:00
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
import org.gtk.gtk.*;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;
2023-01-29 16:33:59 +01:00
import java.util.concurrent.atomic.AtomicInteger;
2023-01-28 19:25:52 +01:00
public class SettingsTab extends Box {
protected final Window window;
2023-01-28 19:25:52 +01:00
public SettingsTab(Window window) {
2023-01-28 19:25:52 +01:00
super(Orientation.VERTICAL, 8);
this.marginHorizontal = 24;
this.marginTop = 12;
this.window = window;
}
protected void section(@Nullable @PropertyKey(resourceBundle = I18n.BUNDLE) String title, SectionBuilder builder) {
if (title != null) append(new ILabel(title, ILabel.Mode.HEADING));
Frame frame = new Frame(null);
ListBox listBox = new ListBox();
listBox.selectionMode = SelectionMode.NONE;
2023-01-29 16:33:59 +01:00
listBox.showSeparators = true;
2023-01-28 19:25:52 +01:00
frame.child = listBox;
2023-01-29 16:33:59 +01:00
AtomicInteger count = new AtomicInteger(0);
builder.build(new Section() {
2023-01-28 19:25:52 +01:00
@Override
public IRow row(String title, @Nullable String subtitle, Object... args) {
IRow row = new IRow(title, subtitle, args);
2023-01-29 16:33:59 +01:00
row(row);
2023-01-28 19:25:52 +01:00
return row;
}
2023-01-29 16:33:59 +01:00
@Override
public ListBoxRow row(Widget row) {
listBox.append(row);
return listBox.getRowAtIndex(count.getAndIncrement());
}
@Override
public void remove(Widget row) {
listBox.remove(row);
count.decrementAndGet();
}
@Override
public void clear() {
for (int i = 0, len = count.getAndSet(0); i < len; i++) {
listBox.remove(listBox.getRowAtIndex(0));
}
}
2023-01-28 19:25:52 +01:00
});
append(frame);
}
2023-01-29 16:33:59 +01:00
public interface SectionBuilder {
2023-01-28 19:25:52 +01:00
void build(Section section);
interface Section {
IRow row(@PropertyKey(resourceBundle = I18n.BUNDLE) String title, @PropertyKey(resourceBundle = I18n.BUNDLE) @Nullable String subtitle, Object... args);
2023-01-29 16:33:59 +01:00
ListBoxRow row(Widget row);
void remove(Widget row);
void clear();
2023-01-28 19:25:52 +01:00
}
}
protected void showError(String message, Throwable t) {
GtkEnvBackend.simpleDialog(
window,
StringFormatter.toString(t),
message,
MessageType.ERROR,
ButtonsType.CLOSE,
null,
null
);
}
}