GUI search, step 3
authorNiki Roo <niki@nikiroo.be>
Tue, 16 Apr 2019 07:04:49 +0000 (09:04 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 16 Apr 2019 07:04:49 +0000 (09:04 +0200)
src/be/nikiroo/fanfix/reader/cli/CliReader.java
src/be/nikiroo/fanfix/reader/ui/GuiReader.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderSearch.java
src/be/nikiroo/fanfix/searchable/BasicSearchable.java
src/be/nikiroo/fanfix/searchable/Fanfiction.java
src/be/nikiroo/fanfix/searchable/MangaLel.java

index 33578be668818e9974d26db122e29cbe1904cafe..a041fbf8aedd8cf6b6aec49ffc79bbdf3d62bea7 100644 (file)
@@ -153,8 +153,7 @@ class CliReader extends BasicReader {
                } else {
                        if (page <= 0) {
                                if (stag.isLeaf()) {
-                                       search.search(stag, 1);
-                                       System.out.println(stag.getPages());
+                                       System.out.println(search.searchPages(stag));
                                } else {
                                        System.out.println(stag.getCount());
                                }
index 62de6ceebde36142656697ac5bdb7bc720927f25..7d9ee81fd7ffd2eae35167c89e310410f342aba6 100644 (file)
@@ -25,6 +25,8 @@ import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.fanfix.library.CacheLibrary;
 import be.nikiroo.fanfix.reader.BasicReader;
 import be.nikiroo.fanfix.reader.Reader;
+import be.nikiroo.fanfix.searchable.BasicSearchable;
+import be.nikiroo.fanfix.searchable.SearchableTag;
 import be.nikiroo.fanfix.supported.SupportType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
@@ -221,17 +223,19 @@ class GuiReader extends BasicReader {
 
        @Override
        public void search(boolean sync) throws IOException {
-               // TODO
+               GuiReaderSearch search = new GuiReaderSearch(this);
                if (sync) {
-                       throw new java.lang.IllegalStateException("Not implemented yet.");
+                       sync(search);
+               } else {
+                       search.setVisible(true);
                }
        }
 
        @Override
        public void search(SupportType searchOn, String keywords, int page,
                        int item, boolean sync) {
-               // TODO: add parameters!
                GuiReaderSearch search = new GuiReaderSearch(this);
+               search.search(searchOn, keywords, page, item);
                if (sync) {
                        sync(search);
                } else {
@@ -240,11 +244,37 @@ class GuiReader extends BasicReader {
        }
 
        @Override
-       public void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) {
-               // TODO
+       public void searchTag(final SupportType searchOn, final int page,
+                       final int item, final boolean sync, final Integer... tags) {
+
+               final GuiReaderSearch search = new GuiReaderSearch(GuiReader.this);
+               final BasicSearchable searchable = BasicSearchable
+                               .getSearchable(searchOn);
+
+               Runnable action = new Runnable() {
+                       @Override
+                       public void run() {
+                               SearchableTag tag = null;
+                               try {
+                                       tag = searchable.getTag(tags);
+                               } catch (IOException e) {
+                                       Instance.getTraceHandler().error(e);
+                               }
+
+                               search.searchTag(searchOn, page, item, tag);
+
+                               if (sync) {
+                                       sync(search);
+                               } else {
+                                       search.setVisible(true);
+                               }
+                       }
+               };
+
                if (sync) {
-                       throw new java.lang.IllegalStateException("Not implemented yet.");
+                       action.run();
+               } else {
+                       new Thread(action).start();
                }
        }
 
index 04c1dad1071b89ef5d1e3a9c794be4c5b187ca3d..59d4f21786724a90a5a0d39c5ea40abdb30f5593 100644 (file)
@@ -36,6 +36,7 @@ public class GuiReaderSearch extends JFrame {
 
        private List<SupportType> supportTypes;
        private SupportType supportType;
+       private boolean searchByTags;
        private List<SearchableTag> tags;
        private String keywords;
        private int page;
@@ -43,6 +44,7 @@ public class GuiReaderSearch extends JFrame {
 
        private JComboBox<SupportType> comboSupportTypes;
        private JTabbedPane searchTabs;
+       private JTextField keywordsField;
 
        private boolean seeWordcount;
        private GuiReaderGroup books;
@@ -56,6 +58,7 @@ public class GuiReaderSearch extends JFrame {
                tags = new ArrayList<SearchableTag>();
                page = 1; // TODO
                maxPage = -1;
+               searchByTags = false;
 
                supportTypes = new ArrayList<SupportType>();
                for (SupportType type : SupportType.values()) {
@@ -71,7 +74,7 @@ public class GuiReaderSearch extends JFrame {
                comboSupportTypes.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               setSupportType((SupportType) comboSupportTypes
+                               updateSupportType((SupportType) comboSupportTypes
                                                .getSelectedItem());
                        }
                });
@@ -108,16 +111,10 @@ public class GuiReaderSearch extends JFrame {
                add(scroll, BorderLayout.CENTER);
        }
 
-       public void setSupportType(SupportType supportType) {
-               this.supportType = supportType;
-               comboSupportTypes.setSelectedItem(supportType);
-               // TODO: reset all
-       }
-
        private JPanel createByNameSearchPanel() {
                JPanel byName = new JPanel(new BorderLayout());
 
-               final JTextField keywordsField = new JTextField();
+               keywordsField = new JTextField();
                byName.add(keywordsField, BorderLayout.CENTER);
 
                // TODO: i18n
@@ -144,12 +141,79 @@ public class GuiReaderSearch extends JFrame {
                return byTag;
        }
 
-       // item 0 = no selction, else = default selection
+       private void updateSupportType(SupportType supportType) {
+               if (supportType != this.supportType) {
+                       this.supportType = supportType;
+                       comboSupportTypes.setSelectedItem(supportType);
+                       // TODO: reset all
+               }
+       }
+
+       private void updateSearchBy(final boolean byTag) {
+               if (byTag != this.searchByTags) {
+                       inUi(new Runnable() {
+                               @Override
+                               public void run() {
+                                       if (!byTag) {
+                                               searchTabs.setSelectedIndex(0);
+                                       } else {
+                                               searchTabs.setSelectedIndex(1);
+                                       }
+                               }
+                       });
+               }
+       }
+
+       private void updatePages(final int page, final Integer maxPage) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               GuiReaderSearch.this.page = page;
+                               GuiReaderSearch.this.maxPage = maxPage;
+                               // TODO: gui
+                               System.out.println("page: " + page);
+                               System.out.println("max page: " + maxPage);
+                       }
+               });
+       }
+
+       private void updateKeywords(final String keywords) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               GuiReaderSearch.this.keywords = keywords;
+                               keywordsField.setText(keywords);
+                       }
+               });
+       }
+
+       // can be NULL
+       private void updateTags(final SearchableTag tag) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               // TODO
+                       }
+               });
+       }
+
+       private void updateBooks(final List<GuiReaderBookInfo> infos) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               books.refreshBooks(infos, seeWordcount);
+                       }
+               });
+       }
+
+       // item 0 = no selection, else = default selection
        public void search(final SupportType searchOn, final String keywords,
                        final int page, final int item) {
-               setSupportType(searchOn);
-               this.keywords = keywords;
-               this.page = page;
+
+               updateSupportType(searchOn);
+               updateSearchBy(false);
+               updateKeywords(keywords);
+               updatePages(page, maxPage);
 
                new Thread(new Runnable() {
                        @Override
@@ -188,60 +252,41 @@ public class GuiReaderSearch extends JFrame {
                }).start();
        }
 
-       private void updatePages(final int page, final Integer maxPage) {
-               inUi(new Runnable() {
-                       @Override
-                       public void run() {
-                               GuiReaderSearch.this.page = page;
-                               GuiReaderSearch.this.maxPage = maxPage;
-                               // TODO: gui
-                               System.out.println("page: " + page);
-                               System.out.println("max page: " + maxPage);
-                       }
-               });
-       }
-
-       private void updateBooks(final List<GuiReaderBookInfo> infos) {
-               inUi(new Runnable() {
-                       @Override
-                       public void run() {
-                               books.refreshBooks(infos, seeWordcount);
-                       }
-               });
-       }
+       public void searchTag(SupportType searchOn, int page, int item,
+                       SearchableTag tag) {
 
-       private void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) throws IOException {
+               updateSupportType(searchOn);
+               updateSearchBy(true);
+               updateTags(tag);
+               updatePages(page, maxPage);
 
                BasicSearchable search = BasicSearchable.getSearchable(searchOn);
-               SearchableTag stag = search.getTag(tags);
-
-               if (stag == null) {
-                       // TODO i18n
-                       System.out.println("Known tags: ");
-                       int i = 1;
-                       for (SearchableTag s : search.getTags()) {
-                               System.out.println(String.format("%d: %s", i, s.getName()));
-                               i++;
+
+               if (tag != null) {
+                       int maxPage = 0;
+                       try {
+                               maxPage = search.searchPages(tag);
+                       } catch (IOException e) {
+                               Instance.getTraceHandler().error(e);
                        }
-               } else {
-                       if (page <= 0) {
-                               if (stag.isLeaf()) {
-                                       search.search(stag, 1);
-                                       System.out.println(stag.getPages());
-                               } else {
-                                       System.out.println(stag.getCount());
-                               }
-                       } else {
+
+                       updatePages(page, maxPage);
+
+                       if (page > 0) {
                                List<MetaData> metas = null;
                                List<SearchableTag> subtags = null;
                                int count;
 
-                               if (stag.isLeaf()) {
-                                       metas = search.search(stag, page);
+                               if (tag.isLeaf()) {
+                                       try {
+                                               metas = search.search(tag, page);
+                                       } catch (IOException e) {
+                                               metas = new ArrayList<MetaData>();
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                        count = metas.size();
                                } else {
-                                       subtags = stag.getChildren();
+                                       subtags = tag.getChildren();
                                        count = subtags.size();
                                }
 
@@ -249,26 +294,11 @@ public class GuiReaderSearch extends JFrame {
                                        if (item <= count) {
                                                if (metas != null) {
                                                        MetaData meta = metas.get(item - 1);
-                                                       // displayStory(meta);
+                                                       // TODO: select story
                                                } else {
                                                        SearchableTag subtag = subtags.get(item - 1);
-                                                       // displayTag(subtag);
+                                                       // TODO: search on tag
                                                }
-                                       } else {
-                                               System.out.println("Invalid item: only " + count
-                                                               + " items found");
-                                       }
-                               } else {
-                                       if (metas != null) {
-                                               // TODO i18n
-                                               System.out.println(String.format("Content of %s: ",
-                                                               stag.getFqName()));
-                                               // displayStories(metas);
-                                       } else {
-                                               // TODO i18n
-                                               System.out.println(String.format("Subtags of %s: ",
-                                                               stag.getFqName()));
-                                               // displayTags(subtags);
                                        }
                                }
                        }
index a67d2f6022e47088824d7f61cbaaa341b6ce8a0c..d38505e7660fb9f0348394ac23390fa3cdb4aa65 100644 (file)
@@ -131,6 +131,20 @@ public abstract class BasicSearchable {
         */
        abstract public int searchPages(String search) throws IOException;
 
+       /**
+        * Search for the given tag and return the number of pages of results of
+        * stories satisfying this tag.
+        * 
+        * @param tag
+        *            the tag to search for
+        * 
+        * @return a number of pages
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       abstract public int searchPages(SearchableTag tag) throws IOException;
+
        /**
         * Search for the given term and return a list of stories satisfying this
         * search term.
index f392bf15cd09a4da7a8877db42dd924b48f3f194..c2dfd5d5714a8f4c5e388aeccf7a8cd51053f303 100644 (file)
@@ -149,15 +149,6 @@ class Fanfiction extends BasicSearchable {
                return getStories(url, null, null);
        }
 
-       @Override
-       public int searchPages(String search) throws IOException {
-               String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
-               String url = BASE_URL + "search/?ready=1&type=story&keywords="
-                               + encoded;
-
-               return getPages(load(url, false));
-       }
-
        @Override
        public List<MetaData> search(SearchableTag tag, int page)
                        throws IOException {
@@ -197,6 +188,25 @@ class Fanfiction extends BasicSearchable {
                return metas;
        }
 
+       @Override
+       public int searchPages(String search) throws IOException {
+               String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
+               String url = BASE_URL + "search/?ready=1&type=story&keywords="
+                               + encoded;
+
+               return getPages(load(url, false));
+       }
+
+       @Override
+       public int searchPages(SearchableTag tag) throws IOException {
+               if (tag.isLeaf()) {
+                       String url = tag.getId();
+                       return getPages(load(url, false));
+               }
+
+               return 0;
+       }
+
        /**
         * Return the number of pages in this stories result listing.
         * 
index 60e25917e889c3d61b0919b903138be5acdbc6ec..3e2924f31d5d84a5082fec20b0058a7091c86f5d 100644 (file)
@@ -50,12 +50,6 @@ class MangaLel extends BasicSearchable {
                // Tags are always complete
        }
 
-       @Override
-       public int searchPages(String search) throws IOException {
-               // No pagination
-               return 1;
-       }
-
        @Override
        public List<MetaData> search(String search, int page) throws IOException {
                String url = BASE_URL + "?nomProjet="
@@ -76,6 +70,22 @@ class MangaLel extends BasicSearchable {
                return getResults(url);
        }
 
+       @Override
+       public int searchPages(String search) throws IOException {
+               // No pagination
+               return 1;
+       }
+
+       @Override
+       public int searchPages(SearchableTag tag) throws IOException {
+               if (tag.isLeaf()) {
+                       // No pagination
+                       return 1;
+               }
+
+               return 0;
+       }
+
        private List<MetaData> getResults(String sourceUrl) throws IOException {
                List<MetaData> metas = new ArrayList<MetaData>();