Remove or move java.awt dependencies
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderMainWindow.java
1 package be.nikiroo.fanfix.reader.tui;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import jexer.TAction;
8 import jexer.TCommand;
9 import jexer.TFileOpenBox.Type;
10 import jexer.TKeypress;
11 import jexer.TList;
12 import jexer.TWindow;
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;
19
20 /**
21 * The library window, that will list all the (filtered) stories available in
22 * this {@link BasicLibrary}.
23 *
24 * @author niki
25 */
26 class TuiReaderMainWindow extends TWindow {
27 private TList list;
28 private List<MetaData> listKeys;
29 private List<String> listItems;
30 private Reader reader;
31 private String source;
32
33 /**
34 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
35 *
36 * @param reader
37 * the reader and main application
38 */
39 public TuiReaderMainWindow(TuiReaderApplication reader) {
40 // Construct a demo window. X and Y don't matter because it will be
41 // centred on screen.
42 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE
43 | UNCLOSABLE);
44
45 this.reader = reader;
46
47 maximize();
48
49 listKeys = new ArrayList<MetaData>();
50 listItems = new ArrayList<String>();
51 list = addList(listItems, 0, 0, getWidth(), getHeight(), new TAction() {
52 @Override
53 public void DO() {
54 MetaData meta = getSelectedMeta();
55 if (meta != null) {
56 readStory(meta);
57 }
58 }
59 });
60
61 // TODO: add the current "source/type" or filter
62 statusBar = newStatusBar("Library");
63 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
64
65 // TODO: remove when not used anymore
66
67 // addLabel("Label (1,1)", 1, 1);
68 // addButton("&Button (35,1)", 35, 1, new TAction() {
69 // public void DO() {
70 // }
71 // });
72 // addCheckbox(1, 2, "Checky (1,2)", false);
73 // addProgressBar(1, 3, 30, 42);
74 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
75 // groupy.addRadioButton("Fanfan");
76 // groupy.addRadioButton("Tulipe");
77 // addField(1, 10, 20, false, "text not fixed.");
78 // addField(1, 11, 20, true, "text fixed.");
79 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
80 //
81 // TTreeView tree = addTreeView(30, 5, 20, 5);
82 // TTreeItem root = new TTreeItem(tree, "expended root", true);
83 // tree.setSelected(root); // needed to allow arrow navigation without
84 // // mouse-clicking before
85 //
86 // root.addChild("child");
87 // root.addChild("child 2").addChild("sub child");
88 }
89
90 /**
91 * Change the source filter and display all stories matching this source.
92 *
93 * @param source
94 * the new source or NULL for all sources
95 */
96 public void setSource(String source) {
97 this.source = source;
98 refreshStories();
99 }
100
101 public void refreshStories() {
102 List<MetaData> metas = reader.getLibrary().getListBySource(source);
103 setMetas(metas);
104 }
105
106 /**
107 * Update the list of stories displayed in this {@link TWindow}.
108 *
109 * @param meta
110 * the new (unique) story to display
111 */
112 public void setMeta(MetaData meta) {
113 List<MetaData> metas = new ArrayList<MetaData>();
114 if (meta != null) {
115 metas.add(meta);
116 }
117
118 setMetas(metas);
119 }
120
121 /**
122 * Update the list of stories displayed in this {@link TWindow}.
123 *
124 * @param metas
125 * the new list of stories to display
126 */
127 private void setMetas(List<MetaData> metas) {
128 listKeys.clear();
129 listItems.clear();
130
131 if (metas != null) {
132 for (MetaData meta : metas) {
133 listKeys.add(meta);
134 listItems.add(desc(meta));
135 }
136 }
137
138 list.setList(listItems);
139 }
140
141 public MetaData getSelectedMeta() {
142 if (list.getSelectedIndex() >= 0) {
143 return listKeys.get(list.getSelectedIndex());
144 }
145
146 return null;
147 }
148
149 public void readStory(MetaData meta) {
150 try {
151 reader.setChapter(-1);
152 reader.setMeta(meta);
153 reader.read();
154 } catch (IOException e) {
155 Instance.getTraceHandler().error(e);
156 }
157 }
158
159 private String desc(MetaData meta) {
160 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
161 }
162
163 @Override
164 public void onMenu(TMenuEvent menu) {
165 MetaData meta = getSelectedMeta();
166 if (meta != null) {
167 switch (menu.getId()) {
168 case TuiReaderApplication.MENU_OPEN:
169 readStory(meta);
170
171 return;
172 case TuiReaderApplication.MENU_EXPORT:
173
174 try {
175 // TODO: choose type, pg, error
176 OutputType outputType = OutputType.EPUB;
177 String path = fileOpenBox(".", Type.SAVE);
178 reader.getLibrary().export(meta.getLuid(), outputType,
179 path, null);
180 } catch (IOException e) {
181 // TODO
182 e.printStackTrace();
183 }
184
185 return;
186 case -1:
187 try {
188 reader.getLibrary().delete(meta.getLuid());
189 } catch (IOException e) {
190 // TODO
191 }
192
193 return;
194 }
195 }
196
197 super.onMenu(menu);
198 }
199 }