| 1 | package be.nikiroo.fanfix.reader.tui; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import jexer.TAction; |
| 8 | import jexer.TApplication; |
| 9 | import jexer.TPanel; |
| 10 | import jexer.TWidget; |
| 11 | import jexer.event.TCommandEvent; |
| 12 | import be.nikiroo.utils.StringUtils; |
| 13 | import be.nikiroo.utils.resources.Bundle; |
| 14 | import be.nikiroo.utils.resources.MetaInfo; |
| 15 | |
| 16 | public class TOptionWindow<E extends Enum<E>> extends TSimpleScrollableWindow { |
| 17 | private List<MetaInfo<E>> items; |
| 18 | |
| 19 | public TOptionWindow(TApplication parent, Class<E> type, |
| 20 | final Bundle<E> bundle, String title) { |
| 21 | super(parent, title, 0, 0, CENTERED | RESIZABLE); |
| 22 | |
| 23 | getMainPane().addLabel(title, 0, 0); |
| 24 | |
| 25 | items = new ArrayList<MetaInfo<E>>(); |
| 26 | List<MetaInfo<E>> groupedItems = MetaInfo.getItems(type, bundle); |
| 27 | int y = 2; |
| 28 | for (MetaInfo<E> item : groupedItems) { |
| 29 | // will populate this.items |
| 30 | y += addItem(getMainPane(), 5, y, item, 0).getHeight(); |
| 31 | } |
| 32 | |
| 33 | y++; |
| 34 | |
| 35 | setRealHeight(y + 1); |
| 36 | |
| 37 | getMainPane().addButton("Reset", 25, y, new TAction() { |
| 38 | @Override |
| 39 | public void DO() { |
| 40 | for (MetaInfo<E> item : items) { |
| 41 | item.reload(); |
| 42 | } |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | getMainPane().addButton("Default", 15, y, new TAction() { |
| 47 | @Override |
| 48 | public void DO() { |
| 49 | Object snap = bundle.takeSnapshot(); |
| 50 | bundle.reload(true); |
| 51 | for (MetaInfo<E> item : items) { |
| 52 | item.reload(); |
| 53 | } |
| 54 | bundle.reload(false); |
| 55 | bundle.restoreSnapshot(snap); |
| 56 | } |
| 57 | }); |
| 58 | |
| 59 | getMainPane().addButton("Save", 1, y, new TAction() { |
| 60 | @Override |
| 61 | public void DO() { |
| 62 | for (MetaInfo<E> item : items) { |
| 63 | item.save(true); |
| 64 | } |
| 65 | |
| 66 | try { |
| 67 | bundle.updateFile(); |
| 68 | } catch (IOException e1) { |
| 69 | e1.printStackTrace(); |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | private TWidget addItem(TWidget parent, int x, int y, MetaInfo<E> item, |
| 76 | int nhgap) { |
| 77 | if (item.isGroup()) { |
| 78 | // TODO: width |
| 79 | int w = 80 - x; |
| 80 | |
| 81 | String name = item.getName(); |
| 82 | String info = item.getDescription(); |
| 83 | info = StringUtils.justifyTexts(info, w - 3); // -3 for borders |
| 84 | |
| 85 | final TPanel pane = new TPanel(parent, x, y, w, 1); |
| 86 | pane.addLabel(name, 0, 0); |
| 87 | |
| 88 | int h = 0; |
| 89 | if (!info.isEmpty()) { |
| 90 | h += info.split("\n").length + 1; // +1 for scroll |
| 91 | pane.addText(info + "\n", 0, 1, w, h); |
| 92 | } |
| 93 | |
| 94 | // +1 for the title |
| 95 | h++; |
| 96 | |
| 97 | int paneY = h; // for the info desc |
| 98 | for (MetaInfo<E> subitem : item) { |
| 99 | paneY += addItem(pane, 4, paneY, subitem, nhgap + 11) |
| 100 | .getHeight(); |
| 101 | } |
| 102 | |
| 103 | pane.setHeight(paneY); |
| 104 | return pane; |
| 105 | } |
| 106 | |
| 107 | items.add(item); |
| 108 | return ConfigItem.createItem(parent, x, y, item, nhgap); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onCommand(TCommandEvent command) { |
| 113 | if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) { |
| 114 | TuiReaderApplication.close(this); |
| 115 | } else { |
| 116 | // Handle our own event if needed here |
| 117 | super.onCommand(command); |
| 118 | } |
| 119 | } |
| 120 | } |