TUI: update (most menu functions working)
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderMainWindow.java
CommitLineData
c1873e56
NR
1package be.nikiroo.fanfix.reader;
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;
c1873e56 18
6322ab64
NR
19/**
20 * The library window, that will list all the (filtered) stories available in
bc2ea776 21 * this {@link BasicLibrary}.
6322ab64
NR
22 *
23 * @author niki
24 */
5dd985cf 25class TuiReaderMainWindow extends TWindow {
c1873e56
NR
26 private TList list;
27 private List<MetaData> listKeys;
28 private List<String> listItems;
bc2ea776 29 private Reader reader;
e2d017a3 30 private String source;
6322ab64
NR
31
32 /**
33 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
34 *
35 * @param reader
36 * the reader and main application
37 */
38 public TuiReaderMainWindow(TuiReaderApplication reader) {
c1873e56 39 // Construct a demo window. X and Y don't matter because it will be
bc2ea776 40 // centred on screen.
396e924c 41 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE
c1873e56
NR
42 | UNCLOSABLE);
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
e2d017a3
NR
89 /**
90 * Change the source filter and display all stories matching this source.
91 *
92 * @param source
93 * the new source or NULL for all sources
94 */
95 public void setSource(String source) {
96 this.source = source;
97 refreshStories();
98 }
99
100 public void refreshStories() {
101 List<MetaData> metas = reader.getLibrary().getListBySource(source);
102 setMetas(metas);
103 }
104
bc2ea776
NR
105 /**
106 * Update the list of stories displayed in this {@link TWindow}.
107 *
108 * @param meta
109 * the new (unique) story to display
110 */
111 public void setMeta(MetaData meta) {
112 List<MetaData> metas = new ArrayList<MetaData>();
113 if (meta != null) {
114 metas.add(meta);
c1873e56 115 }
bc2ea776
NR
116
117 setMetas(metas);
c1873e56
NR
118 }
119
6322ab64
NR
120 /**
121 * Update the list of stories displayed in this {@link TWindow}.
122 *
123 * @param metas
124 * the new list of stories to display
125 */
e2d017a3 126 private void setMetas(List<MetaData> metas) {
6322ab64
NR
127 listKeys.clear();
128 listItems.clear();
129
130 if (metas != null) {
131 for (MetaData meta : metas) {
132 listKeys.add(meta);
133 listItems.add(desc(meta));
134 }
135 }
136
137 list.setList(listItems);
138 }
139
e2d017a3
NR
140 public MetaData getSelectedMeta() {
141 if (list.getSelectedIndex() >= 0) {
142 return listKeys.get(list.getSelectedIndex());
143 }
144
145 return null;
146 }
147
148 public void readStory(MetaData meta) {
bc2ea776
NR
149 try {
150 reader.setChapter(-1);
151 reader.setMeta(meta);
152 reader.read();
153 } catch (IOException e) {
154 Instance.syserr(e);
155 }
c1873e56
NR
156 }
157
158 private String desc(MetaData meta) {
159 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
160 }
e2d017a3
NR
161
162 @Override
163 public void onMenu(TMenuEvent menu) {
164 MetaData meta = getSelectedMeta();
165 if (meta != null) {
166 switch (menu.getId()) {
167 case TuiReaderApplication.MENU_OPEN:
168 readStory(meta);
169
170 return;
171 case TuiReaderApplication.MENU_EXPORT:
172
173 try {
174 // TODO: choose type, pg, error
175 OutputType outputType = OutputType.EPUB;
176 String path = fileOpenBox(".", Type.SAVE);
177 reader.getLibrary().export(meta.getLuid(), outputType,
178 path, null);
179 } catch (IOException e) {
180 // TODO
181 e.printStackTrace();
182 }
183
184 return;
185 case -1:
186 try {
187 reader.getLibrary().delete(meta.getLuid());
188 } catch (IOException e) {
189 // TODO
190 }
191
192 return;
193 }
194 }
195
196 super.onMenu(menu);
197 }
198}