1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.io
.IOException
;
4 import java
.util
.ArrayList
;
8 import jexer
.TComboBox
;
11 import jexer
.TFileOpenBox
.Type
;
12 import jexer
.TKeypress
;
15 import jexer
.TStatusBar
;
17 import jexer
.event
.TCommandEvent
;
18 import jexer
.event
.TKeypressEvent
;
19 import jexer
.event
.TMenuEvent
;
20 import jexer
.event
.TResizeEvent
;
21 import be
.nikiroo
.fanfix
.Instance
;
22 import be
.nikiroo
.fanfix
.data
.MetaData
;
23 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
24 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
25 import be
.nikiroo
.fanfix
.reader
.Reader
;
28 * The library window, that will list all the (filtered) stories available in
29 * this {@link BasicLibrary}.
33 class TuiReaderMainWindow
extends TWindow
{
34 public static final int MENU_SEARCH
= 1100;
35 public static final TCommand CMD_SEARCH
= new TCommand(MENU_SEARCH
) {
43 private List
<MetaData
> listKeys
;
44 private List
<String
> listItems
;
45 private Reader reader
;
47 private Mode mode
= Mode
.SOURCE
;
48 private String target
= null;
49 private String filter
= "";
51 private List
<TSizeConstraint
> sizeConstraints
= new ArrayList
<TSizeConstraint
>();
53 // TODO: because no way to find out the current index!!
54 private TComboBox select
;
55 private TComboBox option
;
58 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
61 * the reader and main application
63 public TuiReaderMainWindow(TuiReaderApplication reader
) {
64 // Construct a demo window. X and Y don't matter because it will be
66 super(reader
, "Library", 0, 0, 60, 18, CENTERED
| RESIZABLE
);
70 listKeys
= new ArrayList
<MetaData
>();
71 listItems
= new ArrayList
<String
>();
75 addSelect(); // TODO: last so it can draw over the rest
77 TStatusBar statusBar
= reader
.setStatusBar(this, "Library");
78 statusBar
.addShortcutKeypress(TKeypress
.kbCtrlF
, CMD_SEARCH
, "Search");
80 TSizeConstraint
.resize(sizeConstraints
);
82 // TODO: remove when not used anymore
84 // addLabel("Label (1,1)", 1, 1);
85 // addButton("&Button (35,1)", 35, 1, new TAction() {
89 // addCheckbox(1, 2, "Checky (1,2)", false);
90 // addProgressBar(1, 3, 30, 42);
91 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
92 // groupy.addRadioButton("Fanfan");
93 // groupy.addRadioButton("Tulipe");
94 // addField(1, 10, 20, false, "text not fixed.");
95 // addField(1, 11, 20, true, "text fixed.");
96 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
98 // TTreeView tree = addTreeView(30, 5, 20, 5);
99 // TTreeItem root = new TTreeItem(tree, "expended root", true);
100 // tree.setSelected(root); // needed to allow arrow navigation without
101 // // mouse-clicking before
103 // root.addChild("child");
104 // root.addChild("child 2").addChild("sub child");
107 private void addSearch() {
108 TLabel lblSearch
= addLabel("Search: ", 0, 0);
110 TField search
= new TField(this, 0, 0, 1, true) {
112 public void onKeypress(TKeypressEvent keypress
) {
113 super.onKeypress(keypress
);
114 TKeypress key
= keypress
.getKey();
115 if (key
.isFnKey() && key
.getKeyCode() == TKeypress
.ENTER
) {
116 TuiReaderMainWindow
.this.filter
= getText();
117 TuiReaderMainWindow
.this.refreshStories();
122 TSizeConstraint
.setSize(sizeConstraints
, lblSearch
, 5, 1, null, null);
123 TSizeConstraint
.setSize(sizeConstraints
, search
, 15, 1, -5, null);
126 private void addList() {
127 list
= addList(listItems
, 0, 0, 10, 10, new TAction() {
130 MetaData meta
= getSelectedMeta();
137 TSizeConstraint
.setSize(sizeConstraints
, list
, 0, 7, 0, 0);
140 private void addSelect() {
141 // TODO: make a full list
142 final List
<String
> options
= new ArrayList
<String
>();
143 options
.add("(show all)");
144 options
.add("Sources");
145 options
.add("Author");
148 final List
<String
> selects
= new ArrayList
<String
>();
149 selects
.add("(show all)");
150 for (String source
: reader
.getLibrary().getSources()) {
154 TLabel lblSelect
= addLabel("Select: ", 0, 0);
156 // TODO: why must be last so to be able to draw over the rest
157 // TODO: make it so we cannot add manual entries
158 // TODO: how to select the item via keyboard? why double-click via
160 // TODO: how to change the values size on resize?
161 // TODO: setWidth() does not impact the display width, only the control
162 // and the down arrow on the right
163 // TODO: width 1 +resize + click on down arrow = bad format exception
164 select
= addComboBox(0, 0, 10, selects
, 0,
165 Math
.min(selects
.size() + 1, getHeight() - 1 - 1),
169 // TODO: detect (show all)
170 if (select
.getText().equals("(show all)")) {
173 setMode(mode
, select
.getText());
178 option
= addComboBox(0, 0, 10, options
, 0,
179 Math
.min(selects
.size() + 1, getHeight() - 1 - 1),
183 // TODO clear not working!!
185 String smode
= option
.getText();
187 if (smode
== null || smode
.equals("(show all)")) {
189 select
.setVisible(false);
190 select
.setEnabled(false);
191 } else if (smode
.equals("Sources")) {
193 select
.setVisible(true);
194 select
.setEnabled(true);
196 selects
.add("(show all)");
197 for (String source
: reader
.getLibrary()
203 select
.setVisible(true);
204 select
.setEnabled(true);
206 selects
.add("(show all)");
207 for (String author
: reader
.getLibrary()
213 // TODO: detect (show all)
214 if (select
.getText().equals("(show all)")) {
217 setMode(mode
, select
.getText());
222 TSizeConstraint
.setSize(sizeConstraints
, lblSelect
, 5, 3, null, null);
223 TSizeConstraint
.setSize(sizeConstraints
, option
, 15, 3, -5, null);
224 TSizeConstraint
.setSize(sizeConstraints
, select
, 15, 4, -5, null);
228 public void onResize(TResizeEvent resize
) {
229 super.onResize(resize
);
230 TSizeConstraint
.resize(sizeConstraints
);
234 public void onClose() {
240 * Refresh the list of stories displayed in this library.
242 * Will take the current settings into account (filter, source...).
244 public void refreshStories() {
245 List
<MetaData
> metas
;
246 if (mode
== Mode
.SOURCE
) {
247 metas
= reader
.getLibrary().getListBySource(target
);
248 } else if (mode
== Mode
.AUTHOR
) {
249 metas
= reader
.getLibrary().getListByAuthor(target
);
251 metas
= reader
.getLibrary().getList();
258 * Change the author/source filter and display all stories matching this
262 * the new mode or NULL for no sorting
264 * the actual target for the given mode, or NULL for all of them
266 public void setMode(Mode mode
, String target
) {
268 this.target
= target
;
273 * Update the list of stories displayed in this {@link TWindow}.
275 * If a filter is set, only the stories which pass the filter will be
279 * the new list of stories to display
281 private void setMetas(List
<MetaData
> metas
) {
286 for (MetaData meta
: metas
) {
287 String desc
= desc(meta
);
289 || desc
.toLowerCase().contains(filter
.toLowerCase())) {
296 list
.setList(listItems
);
299 public MetaData
getSelectedMeta() {
300 if (list
.getSelectedIndex() >= 0) {
301 return listKeys
.get(list
.getSelectedIndex());
307 public void readStory(MetaData meta
) {
309 reader
.setChapter(-1);
310 reader
.setMeta(meta
);
312 } catch (IOException e
) {
313 Instance
.getTraceHandler().error(e
);
317 private String
desc(MetaData meta
) {
318 return String
.format("%5s: %s", meta
.getLuid(), meta
.getTitle());
322 public void onCommand(TCommandEvent command
) {
323 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
324 TuiReaderApplication
.close(this);
326 // Handle our own event if needed here
327 super.onCommand(command
);
332 public void onMenu(TMenuEvent menu
) {
333 MetaData meta
= getSelectedMeta();
335 switch (menu
.getId()) {
336 case TuiReaderApplication
.MENU_OPEN
:
340 case TuiReaderApplication
.MENU_EXPORT
:
343 // TODO: choose type, pg, error
344 OutputType outputType
= OutputType
.EPUB
;
345 String path
= fileOpenBox(".", Type
.SAVE
);
346 reader
.getLibrary().export(meta
.getLuid(), outputType
,
348 } catch (IOException e
) {
357 reader
.getLibrary().delete(meta
.getLuid());
358 } catch (IOException e
) {