1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.io
.IOException
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Arrays
;
9 import jexer
.TFileOpenBox
.Type
;
12 import jexer
.event
.TCommandEvent
;
13 import jexer
.event
.TMenuEvent
;
14 import be
.nikiroo
.fanfix
.Instance
;
15 import be
.nikiroo
.fanfix
.data
.MetaData
;
16 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
17 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
18 import be
.nikiroo
.fanfix
.reader
.Reader
;
21 * The library window, that will list all the (filtered) stories available in
22 * this {@link BasicLibrary}.
26 class TuiReaderMainWindow
extends TWindow
{
28 private List
<MetaData
> listKeys
;
29 private List
<String
> listItems
;
30 private Reader reader
;
31 private String source
;
34 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
37 * the reader and main application
39 public TuiReaderMainWindow(TuiReaderApplication reader
) {
40 // Construct a demo window. X and Y don't matter because it will be
42 super(reader
, "Library", 0, 0, 60, 18, CENTERED
| RESIZABLE
);
46 listKeys
= new ArrayList
<MetaData
>();
47 listItems
= new ArrayList
<String
>();
49 // TODO size + onResize
53 addLabel("Search: ", 5, 3);
54 addField(15, 3, 5, true);
56 addLabel("Sort by: ", 5, 1);
57 // -1 = no default index (0 means first,...) 1=height when visible, null
59 List
<String
> data
= Arrays
.asList("(show all)", "Source", "Name", "Author");
60 // must be last so to be able to draw over the rest
61 // TODO: make it so we cannot add manual entries
62 // TODO: how to select the item via keyboard? why double-click via mouse?
63 addComboBox(15, 1, 12,
64 data
, 0, Math
.min(data
.size()+1,getHeight()-1-1),
67 list
= addList(listItems
, 0, 7, getWidth(), getHeight(), new TAction() {
70 MetaData meta
= getSelectedMeta();
77 // TODO: add the current "source/type" or filter
78 reader
.setStatusBar(this, "Library");
80 // TODO: remove when not used anymore
82 // addLabel("Label (1,1)", 1, 1);
83 // addButton("&Button (35,1)", 35, 1, new TAction() {
87 // addCheckbox(1, 2, "Checky (1,2)", false);
88 // addProgressBar(1, 3, 30, 42);
89 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
90 // groupy.addRadioButton("Fanfan");
91 // groupy.addRadioButton("Tulipe");
92 // addField(1, 10, 20, false, "text not fixed.");
93 // addField(1, 11, 20, true, "text fixed.");
94 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
96 // TTreeView tree = addTreeView(30, 5, 20, 5);
97 // TTreeItem root = new TTreeItem(tree, "expended root", true);
98 // tree.setSelected(root); // needed to allow arrow navigation without
99 // // mouse-clicking before
101 // root.addChild("child");
102 // root.addChild("child 2").addChild("sub child");
106 public void onClose() {
112 * Change the source filter and display all stories matching this source.
115 * the new source or NULL for all sources
117 public void setSource(String source
) {
118 this.source
= source
;
122 public void refreshStories() {
123 List
<MetaData
> metas
= reader
.getLibrary().getListBySource(source
);
128 * Update the list of stories displayed in this {@link TWindow}.
131 * the new (unique) story to display
133 public void setMeta(MetaData meta
) {
134 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
143 * Update the list of stories displayed in this {@link TWindow}.
146 * the new list of stories to display
148 private void setMetas(List
<MetaData
> metas
) {
153 for (MetaData meta
: metas
) {
155 listItems
.add(desc(meta
));
159 list
.setList(listItems
);
162 public MetaData
getSelectedMeta() {
163 if (list
.getSelectedIndex() >= 0) {
164 return listKeys
.get(list
.getSelectedIndex());
170 public void readStory(MetaData meta
) {
172 reader
.setChapter(-1);
173 reader
.setMeta(meta
);
175 } catch (IOException e
) {
176 Instance
.getTraceHandler().error(e
);
180 private String
desc(MetaData meta
) {
181 return String
.format("%5s: %s", meta
.getLuid(), meta
.getTitle());
185 public void onCommand(TCommandEvent command
) {
186 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
187 TuiReaderApplication
.close(this);
189 // Handle our own event if needed here
190 super.onCommand(command
);
195 public void onMenu(TMenuEvent menu
) {
196 MetaData meta
= getSelectedMeta();
198 switch (menu
.getId()) {
199 case TuiReaderApplication
.MENU_OPEN
:
203 case TuiReaderApplication
.MENU_EXPORT
:
206 // TODO: choose type, pg, error
207 OutputType outputType
= OutputType
.EPUB
;
208 String path
= fileOpenBox(".", Type
.SAVE
);
209 reader
.getLibrary().export(meta
.getLuid(), outputType
,
211 } catch (IOException e
) {
219 reader
.getLibrary().delete(meta
.getLuid());
220 } catch (IOException e
) {