| 1 | package be.nikiroo.fanfix.reader.tui; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Arrays; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import jexer.TAction; |
| 9 | import jexer.TFileOpenBox.Type; |
| 10 | import jexer.TList; |
| 11 | import jexer.TWindow; |
| 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; |
| 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 | |
| 44 | this.reader = reader; |
| 45 | |
| 46 | listKeys = new ArrayList<MetaData>(); |
| 47 | listItems = new ArrayList<String>(); |
| 48 | |
| 49 | // TODO size + onResize |
| 50 | |
| 51 | |
| 52 | |
| 53 | addLabel("Search: ", 5, 3); |
| 54 | addField(15, 3, 5, true); |
| 55 | |
| 56 | addLabel("Sort by: ", 5, 1); |
| 57 | // -1 = no default index (0 means first,...) 1=height when visible, null |
| 58 | // = action |
| 59 | List<String> data = Arrays.asList("(show all)", "Source", "Name", "Author"); |
| 60 | // must be last so to be able to draw over the rest |
| 61 | // TODO: make it so we cannot add manual entries |
| 62 | // TODO: how to select the item via keyboard? why double-click via mouse? |
| 63 | addComboBox(15, 1, 12, |
| 64 | data, 0, Math.min(data.size()+1,getHeight()-1-1), |
| 65 | null); |
| 66 | |
| 67 | list = addList(listItems, 0, 7, getWidth(), getHeight(), new TAction() { |
| 68 | @Override |
| 69 | public void DO() { |
| 70 | MetaData meta = getSelectedMeta(); |
| 71 | if (meta != null) { |
| 72 | readStory(meta); |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | // TODO: add the current "source/type" or filter |
| 78 | reader.setStatusBar(this, "Library"); |
| 79 | |
| 80 | // TODO: remove when not used anymore |
| 81 | |
| 82 | // addLabel("Label (1,1)", 1, 1); |
| 83 | // addButton("&Button (35,1)", 35, 1, new TAction() { |
| 84 | // public void DO() { |
| 85 | // } |
| 86 | // }); |
| 87 | // addCheckbox(1, 2, "Checky (1,2)", false); |
| 88 | // addProgressBar(1, 3, 30, 42); |
| 89 | // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy"); |
| 90 | // groupy.addRadioButton("Fanfan"); |
| 91 | // groupy.addRadioButton("Tulipe"); |
| 92 | // addField(1, 10, 20, false, "text not fixed."); |
| 93 | // addField(1, 11, 20, true, "text fixed."); |
| 94 | // addText("20x4 Text in (12,20)", 1, 12, 20, 4); |
| 95 | // |
| 96 | // TTreeView tree = addTreeView(30, 5, 20, 5); |
| 97 | // TTreeItem root = new TTreeItem(tree, "expended root", true); |
| 98 | // tree.setSelected(root); // needed to allow arrow navigation without |
| 99 | // // mouse-clicking before |
| 100 | // |
| 101 | // root.addChild("child"); |
| 102 | // root.addChild("child 2").addChild("sub child"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onClose() { |
| 107 | setVisible(false); |
| 108 | super.onClose(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Change the source filter and display all stories matching this source. |
| 113 | * |
| 114 | * @param source |
| 115 | * the new source or NULL for all sources |
| 116 | */ |
| 117 | public void setSource(String source) { |
| 118 | this.source = source; |
| 119 | refreshStories(); |
| 120 | } |
| 121 | |
| 122 | public void refreshStories() { |
| 123 | List<MetaData> metas = reader.getLibrary().getListBySource(source); |
| 124 | setMetas(metas); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Update the list of stories displayed in this {@link TWindow}. |
| 129 | * |
| 130 | * @param meta |
| 131 | * the new (unique) story to display |
| 132 | */ |
| 133 | public void setMeta(MetaData meta) { |
| 134 | List<MetaData> metas = new ArrayList<MetaData>(); |
| 135 | if (meta != null) { |
| 136 | metas.add(meta); |
| 137 | } |
| 138 | |
| 139 | setMetas(metas); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Update the list of stories displayed in this {@link TWindow}. |
| 144 | * |
| 145 | * @param metas |
| 146 | * the new list of stories to display |
| 147 | */ |
| 148 | private void setMetas(List<MetaData> metas) { |
| 149 | listKeys.clear(); |
| 150 | listItems.clear(); |
| 151 | |
| 152 | if (metas != null) { |
| 153 | for (MetaData meta : metas) { |
| 154 | listKeys.add(meta); |
| 155 | listItems.add(desc(meta)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | list.setList(listItems); |
| 160 | } |
| 161 | |
| 162 | public MetaData getSelectedMeta() { |
| 163 | if (list.getSelectedIndex() >= 0) { |
| 164 | return listKeys.get(list.getSelectedIndex()); |
| 165 | } |
| 166 | |
| 167 | return null; |
| 168 | } |
| 169 | |
| 170 | public void readStory(MetaData meta) { |
| 171 | try { |
| 172 | reader.setChapter(-1); |
| 173 | reader.setMeta(meta); |
| 174 | reader.read(); |
| 175 | } catch (IOException e) { |
| 176 | Instance.getTraceHandler().error(e); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private String desc(MetaData meta) { |
| 181 | return String.format("%5s: %s", meta.getLuid(), meta.getTitle()); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onCommand(TCommandEvent command) { |
| 186 | if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) { |
| 187 | TuiReaderApplication.close(this); |
| 188 | } else { |
| 189 | // Handle our own event if needed here |
| 190 | super.onCommand(command); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void onMenu(TMenuEvent menu) { |
| 196 | MetaData meta = getSelectedMeta(); |
| 197 | if (meta != null) { |
| 198 | switch (menu.getId()) { |
| 199 | case TuiReaderApplication.MENU_OPEN: |
| 200 | readStory(meta); |
| 201 | |
| 202 | return; |
| 203 | case TuiReaderApplication.MENU_EXPORT: |
| 204 | |
| 205 | try { |
| 206 | // TODO: choose type, pg, error |
| 207 | OutputType outputType = OutputType.EPUB; |
| 208 | String path = fileOpenBox(".", Type.SAVE); |
| 209 | reader.getLibrary().export(meta.getLuid(), outputType, |
| 210 | path, null); |
| 211 | } catch (IOException e) { |
| 212 | // TODO |
| 213 | e.printStackTrace(); |
| 214 | } |
| 215 | |
| 216 | return; |
| 217 | case -1: |
| 218 | try { |
| 219 | reader.getLibrary().delete(meta.getLuid()); |
| 220 | } catch (IOException e) { |
| 221 | // TODO |
| 222 | } |
| 223 | |
| 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | super.onMenu(menu); |
| 229 | } |
| 230 | } |