| 1 | package be.nikiroo.fanfix.reader; |
| 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.TKeypress; |
| 10 | import jexer.TList; |
| 11 | import jexer.TWindow; |
| 12 | import be.nikiroo.fanfix.Instance; |
| 13 | import be.nikiroo.fanfix.data.MetaData; |
| 14 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 15 | |
| 16 | /** |
| 17 | * The library window, that will list all the (filtered) stories available in |
| 18 | * this {@link BasicLibrary}. |
| 19 | * |
| 20 | * @author niki |
| 21 | */ |
| 22 | class TuiReaderMainWindow extends TWindow { |
| 23 | private TList list; |
| 24 | private List<MetaData> listKeys; |
| 25 | private List<String> listItems; |
| 26 | private Reader reader; |
| 27 | |
| 28 | /** |
| 29 | * Create a new {@link TuiReaderMainWindow} with the given story in the |
| 30 | * list. |
| 31 | * |
| 32 | * @param reader |
| 33 | * the reader and main application |
| 34 | * @param meta |
| 35 | * the story to display |
| 36 | */ |
| 37 | public TuiReaderMainWindow(TuiReaderApplication reader, MetaData meta) { |
| 38 | this(reader); |
| 39 | |
| 40 | List<MetaData> metas = new ArrayList<MetaData>(); |
| 41 | metas.add(meta); |
| 42 | setMetas(metas); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create a new {@link TuiReaderMainWindow} without any stories in the list. |
| 47 | * |
| 48 | * @param reader |
| 49 | * the reader and main application |
| 50 | */ |
| 51 | public TuiReaderMainWindow(TuiReaderApplication reader) { |
| 52 | // Construct a demo window. X and Y don't matter because it will be |
| 53 | // centred on screen. |
| 54 | super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE |
| 55 | | UNCLOSABLE); |
| 56 | |
| 57 | this.reader = reader; |
| 58 | |
| 59 | maximize(); |
| 60 | |
| 61 | listKeys = new ArrayList<MetaData>(); |
| 62 | listItems = new ArrayList<String>(); |
| 63 | list = addList(listItems, 0, 0, getWidth(), getHeight(), new TAction() { |
| 64 | @Override |
| 65 | public void DO() { |
| 66 | if (list.getSelectedIndex() >= 0) { |
| 67 | enterOnStory(listKeys.get(list.getSelectedIndex())); |
| 68 | } |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | // TODO: add the current "source/type" or filter |
| 73 | statusBar = newStatusBar("Library"); |
| 74 | statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit"); |
| 75 | |
| 76 | // TODO: remove when not used anymore |
| 77 | |
| 78 | // addLabel("Label (1,1)", 1, 1); |
| 79 | // addButton("&Button (35,1)", 35, 1, new TAction() { |
| 80 | // public void DO() { |
| 81 | // } |
| 82 | // }); |
| 83 | // addCheckbox(1, 2, "Checky (1,2)", false); |
| 84 | // addProgressBar(1, 3, 30, 42); |
| 85 | // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy"); |
| 86 | // groupy.addRadioButton("Fanfan"); |
| 87 | // groupy.addRadioButton("Tulipe"); |
| 88 | // addField(1, 10, 20, false, "text not fixed."); |
| 89 | // addField(1, 11, 20, true, "text fixed."); |
| 90 | // addText("20x4 Text in (12,20)", 1, 12, 20, 4); |
| 91 | // |
| 92 | // TTreeView tree = addTreeView(30, 5, 20, 5); |
| 93 | // TTreeItem root = new TTreeItem(tree, "expended root", true); |
| 94 | // tree.setSelected(root); // needed to allow arrow navigation without |
| 95 | // // mouse-clicking before |
| 96 | // |
| 97 | // root.addChild("child"); |
| 98 | // root.addChild("child 2").addChild("sub child"); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Update the list of stories displayed in this {@link TWindow}. |
| 103 | * |
| 104 | * @param meta |
| 105 | * the new (unique) story to display |
| 106 | */ |
| 107 | public void setMeta(MetaData meta) { |
| 108 | List<MetaData> metas = new ArrayList<MetaData>(); |
| 109 | if (meta != null) { |
| 110 | metas.add(meta); |
| 111 | } |
| 112 | |
| 113 | setMetas(metas); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Update the list of stories displayed in this {@link TWindow}. |
| 118 | * |
| 119 | * @param metas |
| 120 | * the new list of stories to display |
| 121 | */ |
| 122 | public void setMetas(List<MetaData> metas) { |
| 123 | listKeys.clear(); |
| 124 | listItems.clear(); |
| 125 | |
| 126 | if (metas != null) { |
| 127 | for (MetaData meta : metas) { |
| 128 | listKeys.add(meta); |
| 129 | listItems.add(desc(meta)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | list.setList(listItems); |
| 134 | } |
| 135 | |
| 136 | private void enterOnStory(MetaData meta) { |
| 137 | try { |
| 138 | reader.setChapter(-1); |
| 139 | reader.setMeta(meta); |
| 140 | reader.read(); |
| 141 | } catch (IOException e) { |
| 142 | Instance.syserr(e); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private String desc(MetaData meta) { |
| 147 | return String.format("%5s: %s", meta.getLuid(), meta.getTitle()); |
| 148 | } |
| 149 | } |