1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.io
.IOException
;
4 import java
.util
.ArrayList
;
8 import jexer
.TFileOpenBox
.Type
;
11 import jexer
.event
.TCommandEvent
;
12 import jexer
.event
.TMenuEvent
;
13 import be
.nikiroo
.fanfix
.Instance
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
16 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
17 import be
.nikiroo
.fanfix
.reader
.Reader
;
20 * The library window, that will list all the (filtered) stories available in
21 * this {@link BasicLibrary}.
25 class TuiReaderMainWindow
extends TWindow
{
27 private List
<MetaData
> listKeys
;
28 private List
<String
> listItems
;
29 private Reader reader
;
30 private String source
;
33 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
36 * the reader and main application
38 public TuiReaderMainWindow(TuiReaderApplication reader
) {
39 // Construct a demo window. X and Y don't matter because it will be
41 super(reader
, "Library", 0, 0, 60, 18, CENTERED
| RESIZABLE
);
47 listKeys
= new ArrayList
<MetaData
>();
48 listItems
= new ArrayList
<String
>();
49 list
= addList(listItems
, 0, 0, getWidth(), getHeight(), new TAction() {
52 MetaData meta
= getSelectedMeta();
59 // TODO: add the current "source/type" or filter
60 reader
.setStatusBar(this, "Library");
62 // TODO: remove when not used anymore
64 // addLabel("Label (1,1)", 1, 1);
65 // addButton("&Button (35,1)", 35, 1, new TAction() {
69 // addCheckbox(1, 2, "Checky (1,2)", false);
70 // addProgressBar(1, 3, 30, 42);
71 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
72 // groupy.addRadioButton("Fanfan");
73 // groupy.addRadioButton("Tulipe");
74 // addField(1, 10, 20, false, "text not fixed.");
75 // addField(1, 11, 20, true, "text fixed.");
76 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
78 // TTreeView tree = addTreeView(30, 5, 20, 5);
79 // TTreeItem root = new TTreeItem(tree, "expended root", true);
80 // tree.setSelected(root); // needed to allow arrow navigation without
81 // // mouse-clicking before
83 // root.addChild("child");
84 // root.addChild("child 2").addChild("sub child");
88 public void onClose() {
94 * Change the source filter and display all stories matching this source.
97 * the new source or NULL for all sources
99 public void setSource(String source
) {
100 this.source
= source
;
104 public void refreshStories() {
105 List
<MetaData
> metas
= reader
.getLibrary().getListBySource(source
);
110 * Update the list of stories displayed in this {@link TWindow}.
113 * the new (unique) story to display
115 public void setMeta(MetaData meta
) {
116 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
125 * Update the list of stories displayed in this {@link TWindow}.
128 * the new list of stories to display
130 private void setMetas(List
<MetaData
> metas
) {
135 for (MetaData meta
: metas
) {
137 listItems
.add(desc(meta
));
141 list
.setList(listItems
);
144 public MetaData
getSelectedMeta() {
145 if (list
.getSelectedIndex() >= 0) {
146 return listKeys
.get(list
.getSelectedIndex());
152 public void readStory(MetaData meta
) {
154 reader
.setChapter(-1);
155 reader
.setMeta(meta
);
157 } catch (IOException e
) {
158 Instance
.getTraceHandler().error(e
);
162 private String
desc(MetaData meta
) {
163 return String
.format("%5s: %s", meta
.getLuid(), meta
.getTitle());
167 public void onCommand(TCommandEvent command
) {
168 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
169 TuiReaderApplication
.close(this);
171 // Handle our own event if needed here
172 super.onCommand(command
);
177 public void onMenu(TMenuEvent menu
) {
178 MetaData meta
= getSelectedMeta();
180 switch (menu
.getId()) {
181 case TuiReaderApplication
.MENU_OPEN
:
185 case TuiReaderApplication
.MENU_EXPORT
:
188 // TODO: choose type, pg, error
189 OutputType outputType
= OutputType
.EPUB
;
190 String path
= fileOpenBox(".", Type
.SAVE
);
191 reader
.getLibrary().export(meta
.getLuid(), outputType
,
193 } catch (IOException e
) {
201 reader
.getLibrary().delete(meta
.getLuid());
202 } catch (IOException e
) {