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