Add a new TUI system based upon Jexer (WIP)
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderMainWindow.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import jexer.TAction;
7 import jexer.TList;
8 import jexer.TRadioGroup;
9 import jexer.TTreeItem;
10 import jexer.TTreeView;
11 import jexer.TWindow;
12 import be.nikiroo.fanfix.data.MetaData;
13
14 public class TuiReaderMainWindow extends TWindow {
15 private TList list;
16 private List<MetaData> listKeys;
17 private List<String> listItems;
18 private TuiReaderApplication reader;
19
20 /**
21 * Constructor.
22 *
23 * @param parent
24 * the main application
25 * @param flags
26 * bitmask of MODAL, CENTERED, or RESIZABLE
27 */
28 public TuiReaderMainWindow(TuiReaderApplication reader,
29 List<MetaData> stories) {
30 // Construct a demo window. X and Y don't matter because it will be
31 // centered on screen.
32 super(reader, "Demo Window", 0, 0, 60, 18, CENTERED | RESIZABLE
33 | UNCLOSABLE);
34
35 this.reader = reader;
36
37 maximize();
38
39 listKeys = new ArrayList<MetaData>();
40 listItems = new ArrayList<String>();
41
42 if (stories != null) {
43 for (MetaData meta : stories) {
44 listKeys.add(meta);
45 listItems.add(desc(meta));
46 }
47 }
48
49 list = addList(listItems, 0, 0, getWidth(), getHeight(), new TAction() {
50 @Override
51 public void DO() {
52 if (list.getSelectedIndex() >= 0) {
53 enterOnStory(listKeys.get(list.getSelectedIndex()));
54 }
55 }
56 });
57
58 if (false) {
59 addLabel("Label (1,1)", 1, 1);
60 addButton("&Button (35,1)", 35, 1, new TAction() {
61 public void DO() {
62 }
63 });
64 addCheckbox(1, 2, "Checky (1,2)", false);
65 addProgressBar(1, 3, 30, 42);
66 TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
67 groupy.addRadioButton("Fanfan");
68 groupy.addRadioButton("Tulipe");
69 addField(1, 10, 20, false, "text not fixed.");
70 addField(1, 11, 20, true, "text fixed.");
71 addText("20x4 Text in (12,20)", 1, 12, 20, 4);
72
73 TTreeView tree = addTreeView(30, 5, 20, 5);
74 TTreeItem root = new TTreeItem(tree, "expended root", true);
75 tree.setSelected(root); // needed to allow arrow navigation without
76 // mouse-clicking before
77
78 root.addChild("child");
79 root.addChild("child 2").addChild("sub child");
80
81 }
82 }
83
84 private void enterOnStory(MetaData meta) {
85 reader.open(meta);
86 }
87
88 private String desc(MetaData meta) {
89 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
90 }
91 }