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

85 lines
2.8 KiB
Java

package io.gitlab.jfronny.inceptum.gtk.control.settings;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.inceptum.gtk.GtkEnvBackend;
import io.gitlab.jfronny.inceptum.gtk.control.ILabel;
import io.gitlab.jfronny.inceptum.gtk.control.settings.SettingsTab.SectionBuilder.Section;
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
import org.gnome.gtk.*;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;
import java.util.concurrent.atomic.AtomicInteger;
public class SettingsTab extends Box {
protected final Window window;
public SettingsTab(Window window) {
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((String) null);
ListBox listBox = new ListBox();
listBox.selectionMode = SelectionMode.NONE;
listBox.showSeparators = true;
frame.child = listBox;
AtomicInteger count = new AtomicInteger(0);
builder.build(new Section() {
@Override
public IRow row(String title, @Nullable String subtitle, Object... args) {
IRow row = new IRow(title, subtitle, args);
row(row);
return row;
}
@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));
}
}
});
append(frame);
}
public interface SectionBuilder {
void build(Section section);
interface Section {
IRow row(@PropertyKey(resourceBundle = I18n.BUNDLE) String title, @PropertyKey(resourceBundle = I18n.BUNDLE) @Nullable String subtitle, Object... args);
ListBoxRow row(Widget row);
void remove(Widget row);
void clear();
}
}
protected void showError(String message, Throwable t) {
GtkEnvBackend.simpleDialog(
window,
StringFormatter.toString(t),
message,
MessageType.ERROR,
ButtonsType.CLOSE,
null,
null
);
}
}