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