update to more upstream-y jexer lib, fix library
[fanfix.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderMainWindow.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.tui;
c1873e56 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;
e2d017a3 9import jexer.TFileOpenBox.Type;
5dd985cf
NR
10import jexer.TKeypress;
11import jexer.TList;
5dd985cf 12import jexer.TWindow;
e2d017a3 13import jexer.event.TMenuEvent;
bc2ea776 14import be.nikiroo.fanfix.Instance;
c1873e56 15import be.nikiroo.fanfix.data.MetaData;
bc2ea776 16import be.nikiroo.fanfix.library.BasicLibrary;
e2d017a3 17import be.nikiroo.fanfix.output.BasicOutput.OutputType;
16a81ef7 18import be.nikiroo.fanfix.reader.Reader;
c1873e56 19
6322ab64
NR
20/**
21 * The library window, that will list all the (filtered) stories available in
bc2ea776 22 * this {@link BasicLibrary}.
6322ab64
NR
23 *
24 * @author niki
25 */
5dd985cf 26class TuiReaderMainWindow extends TWindow {
c1873e56
NR
27 private TList list;
28 private List<MetaData> listKeys;
29 private List<String> listItems;
bc2ea776 30 private Reader reader;
e2d017a3 31 private String source;
6322ab64
NR
32
33 /**
34 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
35 *
36 * @param reader
37 * the reader and main application
38 */
39 public TuiReaderMainWindow(TuiReaderApplication reader) {
c1873e56 40 // Construct a demo window. X and Y don't matter because it will be
bc2ea776 41 // centred on screen.
4f66bfa8 42 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE);
c1873e56
NR
43
44 this.reader = reader;
45
46 maximize();
47
48 listKeys = new ArrayList<MetaData>();
49 listItems = new ArrayList<String>();
c1873e56
NR
50 list = addList(listItems, 0, 0, getWidth(), getHeight(), new TAction() {
51 @Override
52 public void DO() {
e2d017a3
NR
53 MetaData meta = getSelectedMeta();
54 if (meta != null) {
55 readStory(meta);
c1873e56
NR
56 }
57 }
58 });
5dd985cf 59
6322ab64 60 // TODO: add the current "source/type" or filter
396e924c
NR
61 statusBar = newStatusBar("Library");
62 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
c1873e56 63
bc2ea776
NR
64 // TODO: remove when not used anymore
65
66 // addLabel("Label (1,1)", 1, 1);
67 // addButton("&Button (35,1)", 35, 1, new TAction() {
68 // public void DO() {
69 // }
70 // });
71 // addCheckbox(1, 2, "Checky (1,2)", false);
72 // addProgressBar(1, 3, 30, 42);
73 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
74 // groupy.addRadioButton("Fanfan");
75 // groupy.addRadioButton("Tulipe");
76 // addField(1, 10, 20, false, "text not fixed.");
77 // addField(1, 11, 20, true, "text fixed.");
78 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
79 //
80 // TTreeView tree = addTreeView(30, 5, 20, 5);
81 // TTreeItem root = new TTreeItem(tree, "expended root", true);
82 // tree.setSelected(root); // needed to allow arrow navigation without
83 // // mouse-clicking before
84 //
85 // root.addChild("child");
86 // root.addChild("child 2").addChild("sub child");
87 }
c1873e56 88
4f66bfa8
NR
89 @Override
90 public void onClose() {
91 setVisible(false);
92 super.onClose();
93 }
94
e2d017a3
NR
95 /**
96 * Change the source filter and display all stories matching this source.
97 *
98 * @param source
99 * the new source or NULL for all sources
100 */
101 public void setSource(String source) {
102 this.source = source;
103 refreshStories();
104 }
105
106 public void refreshStories() {
107 List<MetaData> metas = reader.getLibrary().getListBySource(source);
108 setMetas(metas);
109 }
110
bc2ea776
NR
111 /**
112 * Update the list of stories displayed in this {@link TWindow}.
113 *
114 * @param meta
115 * the new (unique) story to display
116 */
117 public void setMeta(MetaData meta) {
118 List<MetaData> metas = new ArrayList<MetaData>();
119 if (meta != null) {
120 metas.add(meta);
c1873e56 121 }
bc2ea776
NR
122
123 setMetas(metas);
c1873e56
NR
124 }
125
6322ab64
NR
126 /**
127 * Update the list of stories displayed in this {@link TWindow}.
128 *
129 * @param metas
130 * the new list of stories to display
131 */
e2d017a3 132 private void setMetas(List<MetaData> metas) {
6322ab64
NR
133 listKeys.clear();
134 listItems.clear();
135
136 if (metas != null) {
137 for (MetaData meta : metas) {
138 listKeys.add(meta);
139 listItems.add(desc(meta));
140 }
141 }
142
143 list.setList(listItems);
144 }
145
e2d017a3
NR
146 public MetaData getSelectedMeta() {
147 if (list.getSelectedIndex() >= 0) {
148 return listKeys.get(list.getSelectedIndex());
149 }
150
151 return null;
152 }
153
154 public void readStory(MetaData meta) {
bc2ea776
NR
155 try {
156 reader.setChapter(-1);
157 reader.setMeta(meta);
158 reader.read();
159 } catch (IOException e) {
62c63b07 160 Instance.getTraceHandler().error(e);
bc2ea776 161 }
c1873e56
NR
162 }
163
164 private String desc(MetaData meta) {
165 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
166 }
e2d017a3
NR
167
168 @Override
169 public void onMenu(TMenuEvent menu) {
170 MetaData meta = getSelectedMeta();
171 if (meta != null) {
172 switch (menu.getId()) {
173 case TuiReaderApplication.MENU_OPEN:
174 readStory(meta);
175
176 return;
177 case TuiReaderApplication.MENU_EXPORT:
178
179 try {
180 // TODO: choose type, pg, error
181 OutputType outputType = OutputType.EPUB;
182 String path = fileOpenBox(".", Type.SAVE);
183 reader.getLibrary().export(meta.getLuid(), outputType,
184 path, null);
185 } catch (IOException e) {
186 // TODO
187 e.printStackTrace();
188 }
189
190 return;
191 case -1:
192 try {
193 reader.getLibrary().delete(meta.getLuid());
194 } catch (IOException e) {
195 // TODO
196 }
197
198 return;
199 }
200 }
201
202 super.onMenu(menu);
203 }
204}