X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Ftui%2FTuiReaderMainWindow.java;h=6523cfa0eab33e244cd39d54bfbfbcc350b9e9a1;hb=81a6120de7dac9853bc9321a204b607bbf458a2e;hp=f2167a6d3a1371026d96002640c99f90db0acdc5;hpb=bce88a008c85e24da6c0398eedd41f9e492b8d39;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java b/src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java index f2167a6..6523cfa 100644 --- a/src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java @@ -2,20 +2,22 @@ package be.nikiroo.fanfix.reader.tui; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import jexer.TAction; +import jexer.TComboBox; import jexer.TCommand; import jexer.TField; import jexer.TFileOpenBox.Type; import jexer.TKeypress; +import jexer.TLabel; import jexer.TList; import jexer.TStatusBar; import jexer.TWindow; import jexer.event.TCommandEvent; import jexer.event.TKeypressEvent; import jexer.event.TMenuEvent; +import jexer.event.TResizeEvent; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.library.BasicLibrary; @@ -33,13 +35,25 @@ class TuiReaderMainWindow extends TWindow { public static final TCommand CMD_SEARCH = new TCommand(MENU_SEARCH) { }; + public enum Mode { + SOURCE, AUTHOR, + } + private TList list; private List listKeys; private List listItems; private Reader reader; - private String source; + + private Mode mode = Mode.SOURCE; + private String target = null; private String filter = ""; + private List sizeConstraints = new ArrayList(); + + // The 2 comboboxes used to select by source/author + private TComboBox selectTargetBox; + private TComboBox selectBox; + /** * Create a new {@link TuiReaderMainWindow} without any stories in the list. * @@ -56,47 +70,15 @@ class TuiReaderMainWindow extends TWindow { listKeys = new ArrayList(); listItems = new ArrayList(); - // TODO size + onResize - - addLabel("Search: ", 5, 3); - @SuppressWarnings("unused") - TField field = new TField(this, 15, 3, 5, true) { - @Override - public void onKeypress(TKeypressEvent keypress) { - super.onKeypress(keypress); - TKeypress key = keypress.getKey(); - if (key.isFnKey() && key.getKeyCode() == TKeypress.ENTER) { - TuiReaderMainWindow.this.filter = getText(); - TuiReaderMainWindow.this.refreshStories(); - } - } - }; - - addLabel("Sort by: ", 5, 1); - // -1 = no default index (0 means first,...) 1=height when visible, null - // = action - List data = Arrays.asList("(show all)", "Source", "Name", - "Author"); - // must be last so to be able to draw over the rest - // TODO: make it so we cannot add manual entries - // TODO: how to select the item via keyboard? why double-click via - // mouse? - addComboBox(15, 1, 12, data, 0, - Math.min(data.size() + 1, getHeight() - 1 - 1), null); - - list = addList(listItems, 0, 7, getWidth(), getHeight(), new TAction() { - @Override - public void DO() { - MetaData meta = getSelectedMeta(); - if (meta != null) { - readStory(meta); - } - } - }); + addList(); + addSearch(); + addSelect(); TStatusBar statusBar = reader.setStatusBar(this, "Library"); statusBar.addShortcutKeypress(TKeypress.kbCtrlF, CMD_SEARCH, "Search"); + TSizeConstraint.resize(sizeConstraints); + // TODO: remove when not used anymore // addLabel("Label (1,1)", 1, 1); @@ -122,6 +104,120 @@ class TuiReaderMainWindow extends TWindow { // root.addChild("child 2").addChild("sub child"); } + private void addSearch() { + TLabel lblSearch = addLabel("Search: ", 0, 0); + + TField search = new TField(this, 0, 0, 1, true) { + @Override + public void onKeypress(TKeypressEvent keypress) { + super.onKeypress(keypress); + TKeypress key = keypress.getKey(); + if (key.isFnKey() && key.getKeyCode() == TKeypress.ENTER) { + TuiReaderMainWindow.this.filter = getText(); + TuiReaderMainWindow.this.refreshStories(); + } + } + }; + + TSizeConstraint.setSize(sizeConstraints, lblSearch, 5, 1, null, null); + TSizeConstraint.setSize(sizeConstraints, search, 15, 1, -5, null); + } + + private void addList() { + list = addList(listItems, 0, 0, 10, 10, new TAction() { + @Override + public void DO() { + MetaData meta = getSelectedMeta(); + if (meta != null) { + readStory(meta); + } + } + }); + + TSizeConstraint.setSize(sizeConstraints, list, 0, 7, 0, 0); + } + + private void addSelect() { + // TODO: i18n + final List selects = new ArrayList(); + selects.add("(show all)"); + selects.add("Sources"); + selects.add("Author"); + + final List selectTargets = new ArrayList(); + selectTargets.add(""); + + TLabel lblSelect = addLabel("Select: ", 0, 0); + + TAction onSelect = new TAction() { + @Override + public void DO() { + String smode = selectBox.getText(); + boolean showTarget; + if (smode == null || smode.equals("(show all)")) { + showTarget = false; + } else if (smode.equals("Sources")) { + selectTargets.clear(); + selectTargets.add("(show all)"); + for (String source : reader.getLibrary().getSources()) { + selectTargets.add(source); + } + showTarget = true; + } else { + selectTargets.clear(); + selectTargets.add("(show all)"); + for (String author : reader.getLibrary().getAuthors()) { + selectTargets.add(author); + } + + showTarget = true; + } + + selectTargetBox.setVisible(showTarget); + selectTargetBox.setEnabled(showTarget); + if (showTarget) { + selectTargetBox.reflowData(); + } + + selectTargetBox.setText(selectTargets.get(0)); + if (showTarget) { + TuiReaderMainWindow.this.activate(selectTargetBox); + } else { + TuiReaderMainWindow.this.activate(list); + } + } + }; + + selectBox = addComboBox(0, 0, 10, selects, 0, -1, onSelect); + + selectTargetBox = addComboBox(0, 0, 0, selectTargets, 0, -1, + new TAction() { + @Override + public void DO() { + if (selectTargetBox.getText().equals( + selectTargets.get(0))) { + setMode(mode, null); + } else { + setMode(mode, selectTargetBox.getText()); + } + } + }); + + // Set defaults + onSelect.DO(); + + TSizeConstraint.setSize(sizeConstraints, lblSelect, 5, 3, null, null); + TSizeConstraint.setSize(sizeConstraints, selectBox, 15, 3, -5, null); + TSizeConstraint.setSize(sizeConstraints, selectTargetBox, 15, 4, -5, + null); + } + + @Override + public void onResize(TResizeEvent resize) { + super.onResize(resize); + TSizeConstraint.resize(sizeConstraints); + } + @Override public void onClose() { setVisible(false); @@ -134,18 +230,30 @@ class TuiReaderMainWindow extends TWindow { * Will take the current settings into account (filter, source...). */ public void refreshStories() { - List metas = reader.getLibrary().getListBySource(source); + List metas; + if (mode == Mode.SOURCE) { + metas = reader.getLibrary().getListBySource(target); + } else if (mode == Mode.AUTHOR) { + metas = reader.getLibrary().getListByAuthor(target); + } else { + metas = reader.getLibrary().getList(); + } + setMetas(metas); } /** - * Change the source filter and display all stories matching this source. + * Change the author/source filter and display all stories matching this + * target. * - * @param source - * the new source or NULL for all sources + * @param mode + * the new mode or NULL for no sorting + * @param target + * the actual target for the given mode, or NULL for all of them */ - public void setSource(String source) { - this.source = source; + public void setMode(Mode mode, String target) { + this.mode = mode; + this.target = target; refreshStories(); } @@ -174,6 +282,9 @@ class TuiReaderMainWindow extends TWindow { } list.setList(listItems); + if (listItems.size() > 0) { + list.setSelectedIndex(0); + } } public MetaData getSelectedMeta() {