X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Ftui%2FTOptionWindow.java;fp=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Ftui%2FTOptionWindow.java;h=4bb67dece5dd154e4f9079a82b396facc5d02a73;hb=b6d172980d63ca3fa6880f152373de46deaaadcc;hp=0000000000000000000000000000000000000000;hpb=28a82bab4c068af216ac38609a8dd2c0bf2f531a;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/tui/TOptionWindow.java b/src/be/nikiroo/fanfix/reader/tui/TOptionWindow.java new file mode 100644 index 0000000..4bb67de --- /dev/null +++ b/src/be/nikiroo/fanfix/reader/tui/TOptionWindow.java @@ -0,0 +1,120 @@ +package be.nikiroo.fanfix.reader.tui; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import jexer.TAction; +import jexer.TApplication; +import jexer.TPanel; +import jexer.TWidget; +import jexer.event.TCommandEvent; +import be.nikiroo.utils.StringUtils; +import be.nikiroo.utils.resources.Bundle; +import be.nikiroo.utils.resources.MetaInfo; + +public class TOptionWindow> extends TSimpleScrollableWindow { + private List> items; + + public TOptionWindow(TApplication parent, Class type, + final Bundle bundle, String title) { + super(parent, title, 0, 0, CENTERED | RESIZABLE); + + getMainPane().addLabel(title, 0, 0); + + items = new ArrayList>(); + List> groupedItems = MetaInfo.getItems(type, bundle); + int y = 2; + for (MetaInfo item : groupedItems) { + // will populate this.items + y += addItem(getMainPane(), 5, y, item, 0).getHeight(); + } + + y++; + + setRealHeight(y + 1); + + getMainPane().addButton("Reset", 25, y, new TAction() { + @Override + public void DO() { + for (MetaInfo item : items) { + item.reload(); + } + } + }); + + getMainPane().addButton("Default", 15, y, new TAction() { + @Override + public void DO() { + Object snap = bundle.takeSnapshot(); + bundle.reload(true); + for (MetaInfo item : items) { + item.reload(); + } + bundle.reload(false); + bundle.restoreSnapshot(snap); + } + }); + + getMainPane().addButton("Save", 1, y, new TAction() { + @Override + public void DO() { + for (MetaInfo item : items) { + item.save(true); + } + + try { + bundle.updateFile(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + }); + } + + private TWidget addItem(TWidget parent, int x, int y, MetaInfo item, + int nhgap) { + if (item.isGroup()) { + // TODO: width + int w = 80 - x; + + String name = item.getName(); + String info = item.getDescription(); + info = StringUtils.justifyTexts(info, w - 3); // -3 for borders + + final TPanel pane = new TPanel(parent, x, y, w, 1); + pane.addLabel(name, 0, 0); + + int h = 0; + if (!info.isEmpty()) { + h += info.split("\n").length + 1; // +1 for scroll + pane.addText(info + "\n", 0, 1, w, h); + } + + // +1 for the title + h++; + + int paneY = h; // for the info desc + for (MetaInfo subitem : item) { + paneY += addItem(pane, 4, paneY, subitem, nhgap + 11) + .getHeight(); + } + + pane.setHeight(paneY); + return pane; + } + + items.add(item); + return ConfigItem.createItem(parent, x, y, item, nhgap); + } + + @Override + public void onCommand(TCommandEvent command) { + if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) { + TuiReaderApplication.close(this); + } else { + // Handle our own event if needed here + super.onCommand(command); + } + } +}