Code cleanup 3 and update jexer-niki :
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderMainWindow.java
CommitLineData
c1873e56
NR
1package be.nikiroo.fanfix.reader;
2
bc2ea776 3import java.io.IOException;
c1873e56
NR
4import java.util.ArrayList;
5import java.util.List;
6
5dd985cf
NR
7import jexer.TAction;
8import jexer.TCommand;
9import jexer.TKeypress;
10import jexer.TList;
5dd985cf 11import jexer.TWindow;
bc2ea776 12import be.nikiroo.fanfix.Instance;
c1873e56 13import be.nikiroo.fanfix.data.MetaData;
bc2ea776 14import be.nikiroo.fanfix.library.BasicLibrary;
c1873e56 15
6322ab64
NR
16/**
17 * The library window, that will list all the (filtered) stories available in
bc2ea776 18 * this {@link BasicLibrary}.
6322ab64
NR
19 *
20 * @author niki
21 */
5dd985cf 22class TuiReaderMainWindow extends TWindow {
c1873e56
NR
23 private TList list;
24 private List<MetaData> listKeys;
25 private List<String> listItems;
bc2ea776 26 private Reader reader;
c1873e56
NR
27
28 /**
bc2ea776 29 * Create a new {@link TuiReaderMainWindow} with the given story in the
6322ab64 30 * list.
c1873e56 31 *
5dd985cf
NR
32 * @param reader
33 * the reader and main application
6322ab64
NR
34 * @param meta
35 * the story to display
6322ab64 36 */
bc2ea776 37 public TuiReaderMainWindow(TuiReaderApplication reader, MetaData meta) {
6322ab64
NR
38 this(reader);
39
40 List<MetaData> metas = new ArrayList<MetaData>();
41 metas.add(meta);
42 setMetas(metas);
6322ab64
NR
43 }
44
45 /**
46 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
47 *
48 * @param reader
49 * the reader and main application
50 */
51 public TuiReaderMainWindow(TuiReaderApplication reader) {
c1873e56 52 // Construct a demo window. X and Y don't matter because it will be
bc2ea776 53 // centred on screen.
396e924c 54 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE
c1873e56
NR
55 | UNCLOSABLE);
56
57 this.reader = reader;
58
59 maximize();
60
61 listKeys = new ArrayList<MetaData>();
62 listItems = new ArrayList<String>();
c1873e56
NR
63 list = addList(listItems, 0, 0, getWidth(), getHeight(), new TAction() {
64 @Override
65 public void DO() {
66 if (list.getSelectedIndex() >= 0) {
67 enterOnStory(listKeys.get(list.getSelectedIndex()));
68 }
69 }
70 });
5dd985cf 71
6322ab64 72 // TODO: add the current "source/type" or filter
396e924c
NR
73 statusBar = newStatusBar("Library");
74 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
c1873e56 75
bc2ea776
NR
76 // TODO: remove when not used anymore
77
78 // addLabel("Label (1,1)", 1, 1);
79 // addButton("&Button (35,1)", 35, 1, new TAction() {
80 // public void DO() {
81 // }
82 // });
83 // addCheckbox(1, 2, "Checky (1,2)", false);
84 // addProgressBar(1, 3, 30, 42);
85 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
86 // groupy.addRadioButton("Fanfan");
87 // groupy.addRadioButton("Tulipe");
88 // addField(1, 10, 20, false, "text not fixed.");
89 // addField(1, 11, 20, true, "text fixed.");
90 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
91 //
92 // TTreeView tree = addTreeView(30, 5, 20, 5);
93 // TTreeItem root = new TTreeItem(tree, "expended root", true);
94 // tree.setSelected(root); // needed to allow arrow navigation without
95 // // mouse-clicking before
96 //
97 // root.addChild("child");
98 // root.addChild("child 2").addChild("sub child");
99 }
c1873e56 100
bc2ea776
NR
101 /**
102 * Update the list of stories displayed in this {@link TWindow}.
103 *
104 * @param meta
105 * the new (unique) story to display
106 */
107 public void setMeta(MetaData meta) {
108 List<MetaData> metas = new ArrayList<MetaData>();
109 if (meta != null) {
110 metas.add(meta);
c1873e56 111 }
bc2ea776
NR
112
113 setMetas(metas);
c1873e56
NR
114 }
115
6322ab64
NR
116 /**
117 * Update the list of stories displayed in this {@link TWindow}.
118 *
119 * @param metas
120 * the new list of stories to display
121 */
122 public void setMetas(List<MetaData> metas) {
123 listKeys.clear();
124 listItems.clear();
125
126 if (metas != null) {
127 for (MetaData meta : metas) {
128 listKeys.add(meta);
129 listItems.add(desc(meta));
130 }
131 }
132
133 list.setList(listItems);
134 }
135
c1873e56 136 private void enterOnStory(MetaData meta) {
bc2ea776
NR
137 try {
138 reader.setChapter(-1);
139 reader.setMeta(meta);
140 reader.read();
141 } catch (IOException e) {
142 Instance.syserr(e);
143 }
c1873e56
NR
144 }
145
146 private String desc(MetaData meta) {
147 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
148 }
149}