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
);
48 listKeys
= new ArrayList
<MetaData
>();
49 listItems
= new ArrayList
<String
>();
51 // TODO size + onResize
53 addLabel("Sort by: ", 5, 1);
54 // -1 = no default index (0 means first,...) 1=height when visible, null
56 addComboBox(15, 1, 12,
57 Arrays
.asList("(show all)", "Source", "Name", "Author"), 0, 1,
60 addLabel("Search: ", 5, 3);
61 addField(15, 3, 12, true);
63 list
= addList(listItems
, 0, 5, getWidth(), getHeight(), new TAction() {
66 MetaData meta
= getSelectedMeta();
73 // TODO: add the current "source/type" or filter
74 reader
.setStatusBar(this, "Library");
76 // TODO: remove when not used anymore
78 // addLabel("Label (1,1)", 1, 1);
79 // addButton("&Button (35,1)", 35, 1, new TAction() {
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);
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
97 // root.addChild("child");
98 // root.addChild("child 2").addChild("sub child");
102 public void onClose() {
108 * Change the source filter and display all stories matching this source.
111 * the new source or NULL for all sources
113 public void setSource(String source
) {
114 this.source
= source
;
118 public void refreshStories() {
119 List
<MetaData
> metas
= reader
.getLibrary().getListBySource(source
);
124 * Update the list of stories displayed in this {@link TWindow}.
127 * the new (unique) story to display
129 public void setMeta(MetaData meta
) {
130 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
139 * Update the list of stories displayed in this {@link TWindow}.
142 * the new list of stories to display
144 private void setMetas(List
<MetaData
> metas
) {
149 for (MetaData meta
: metas
) {
151 listItems
.add(desc(meta
));
155 list
.setList(listItems
);
158 public MetaData
getSelectedMeta() {
159 if (list
.getSelectedIndex() >= 0) {
160 return listKeys
.get(list
.getSelectedIndex());
166 public void readStory(MetaData meta
) {
168 reader
.setChapter(-1);
169 reader
.setMeta(meta
);
171 } catch (IOException e
) {
172 Instance
.getTraceHandler().error(e
);
176 private String
desc(MetaData meta
) {
177 return String
.format("%5s: %s", meta
.getLuid(), meta
.getTitle());
181 public void onCommand(TCommandEvent command
) {
182 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
183 TuiReaderApplication
.close(this);
185 // Handle our own event if needed here
186 super.onCommand(command
);
191 public void onMenu(TMenuEvent menu
) {
192 MetaData meta
= getSelectedMeta();
194 switch (menu
.getId()) {
195 case TuiReaderApplication
.MENU_OPEN
:
199 case TuiReaderApplication
.MENU_EXPORT
:
202 // TODO: choose type, pg, error
203 OutputType outputType
= OutputType
.EPUB
;
204 String path
= fileOpenBox(".", Type
.SAVE
);
205 reader
.getLibrary().export(meta
.getLuid(), outputType
,
207 } catch (IOException e
) {
215 reader
.getLibrary().delete(meta
.getLuid());
216 } catch (IOException e
) {