| 1 | package be.nikiroo.fanfix.reader.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Component; |
| 5 | import java.awt.EventQueue; |
| 6 | import java.awt.event.ActionEvent; |
| 7 | import java.awt.event.ActionListener; |
| 8 | import java.lang.reflect.InvocationTargetException; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import javax.swing.JComboBox; |
| 13 | import javax.swing.JFrame; |
| 14 | import javax.swing.JLabel; |
| 15 | import javax.swing.JPanel; |
| 16 | import javax.swing.JScrollPane; |
| 17 | |
| 18 | import be.nikiroo.fanfix.Instance; |
| 19 | import be.nikiroo.fanfix.data.MetaData; |
| 20 | import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; |
| 21 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
| 22 | import be.nikiroo.fanfix.searchable.SearchableTag; |
| 23 | import be.nikiroo.fanfix.supported.SupportType; |
| 24 | |
| 25 | /** |
| 26 | * This frame will allow you to search through the supported websites for new |
| 27 | * stories/comics. |
| 28 | * |
| 29 | * @author niki |
| 30 | */ |
| 31 | // JCombobox<E> not 1.6 compatible |
| 32 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 33 | public class GuiReaderSearchFrame extends JFrame { |
| 34 | private static final long serialVersionUID = 1L; |
| 35 | |
| 36 | private List<SupportType> supportTypes; |
| 37 | private SupportType supportType; |
| 38 | private int page; |
| 39 | private int maxPage; |
| 40 | |
| 41 | private JComboBox comboSupportTypes; |
| 42 | private GuiReaderSearchByNamePanel searchPanel; |
| 43 | |
| 44 | private boolean seeWordcount; |
| 45 | private GuiReaderGroup books; |
| 46 | |
| 47 | public GuiReaderSearchFrame(final GuiReader reader) { |
| 48 | super("Browse stories"); |
| 49 | setLayout(new BorderLayout()); |
| 50 | setSize(800, 600); |
| 51 | |
| 52 | page = 1; // TODO |
| 53 | maxPage = -1; |
| 54 | |
| 55 | supportTypes = new ArrayList<SupportType>(); |
| 56 | for (SupportType type : SupportType.values()) { |
| 57 | if (BasicSearchable.getSearchable(type) != null) { |
| 58 | supportTypes.add(type); |
| 59 | } |
| 60 | } |
| 61 | supportType = supportTypes.isEmpty() ? null : supportTypes.get(0); |
| 62 | |
| 63 | comboSupportTypes = new JComboBox( |
| 64 | supportTypes.toArray(new SupportType[] {})); |
| 65 | comboSupportTypes.addActionListener(new ActionListener() { |
| 66 | @Override |
| 67 | public void actionPerformed(ActionEvent e) { |
| 68 | updateSupportType((SupportType) comboSupportTypes |
| 69 | .getSelectedItem()); |
| 70 | } |
| 71 | }); |
| 72 | JPanel searchSites = new JPanel(new BorderLayout()); |
| 73 | searchSites.add(comboSupportTypes, BorderLayout.CENTER); |
| 74 | searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST); |
| 75 | |
| 76 | searchPanel = new GuiReaderSearchByNamePanel(supportType); |
| 77 | searchPanel.addActionListener(new ActionListener() { |
| 78 | @Override |
| 79 | public void actionPerformed(ActionEvent e) { |
| 80 | updatePages(0, maxPage); |
| 81 | List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>(); |
| 82 | for (MetaData meta : searchPanel.getStories()) { |
| 83 | infos.add(GuiReaderBookInfo.fromMeta(meta)); |
| 84 | } |
| 85 | |
| 86 | updateBooks(infos); |
| 87 | |
| 88 | // ! 1-based index ! |
| 89 | int item = searchPanel.getStoryItem(); |
| 90 | if (item > 0 && item <= books.getBooksCount()) { |
| 91 | // TODO: "click" on item ITEM |
| 92 | } |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | JPanel top = new JPanel(new BorderLayout()); |
| 97 | top.add(searchSites, BorderLayout.NORTH); |
| 98 | top.add(searchPanel, BorderLayout.CENTER); |
| 99 | |
| 100 | add(top, BorderLayout.NORTH); |
| 101 | |
| 102 | books = new GuiReaderGroup(reader, null, null); |
| 103 | books.setActionListener(new BookActionListener() { |
| 104 | @Override |
| 105 | public void select(GuiReaderBook book) { |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void popupRequested(GuiReaderBook book, Component target, |
| 110 | int x, int y) { |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void action(GuiReaderBook book) { |
| 115 | new GuiReaderSearchAction(reader.getLibrary(), book.getInfo()) |
| 116 | .setVisible(true); |
| 117 | } |
| 118 | }); |
| 119 | JScrollPane scroll = new JScrollPane(books); |
| 120 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
| 121 | add(scroll, BorderLayout.CENTER); |
| 122 | } |
| 123 | |
| 124 | private void updateSupportType(SupportType supportType) { |
| 125 | if (supportType != this.supportType) { |
| 126 | this.supportType = supportType; |
| 127 | comboSupportTypes.setSelectedItem(supportType); |
| 128 | books.clear(); |
| 129 | searchPanel.setSupportType(supportType); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void updatePages(final int page, final Integer maxPage) { |
| 134 | inUi(new Runnable() { |
| 135 | @Override |
| 136 | public void run() { |
| 137 | GuiReaderSearchFrame.this.page = page; |
| 138 | GuiReaderSearchFrame.this.maxPage = maxPage; |
| 139 | |
| 140 | searchPanel.setPage(page); |
| 141 | |
| 142 | // TODO: gui |
| 143 | System.out.println("page: " + page); |
| 144 | System.out.println("max page: " + maxPage); |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | private void updateBooks(final List<GuiReaderBookInfo> infos) { |
| 150 | setWaitingScreen(true); |
| 151 | inUi(new Runnable() { |
| 152 | @Override |
| 153 | public void run() { |
| 154 | books.refreshBooks(infos, seeWordcount); |
| 155 | setWaitingScreen(false); |
| 156 | } |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | // item 0 = no selection, else = default selection |
| 161 | public void search(final SupportType searchOn, final String keywords, |
| 162 | final int page, final int item) { |
| 163 | |
| 164 | updatePages(page, maxPage); |
| 165 | searchPanel.setSupportType(searchOn); |
| 166 | |
| 167 | if (keywords != null) { |
| 168 | setWaitingScreen(true); |
| 169 | searchPanel.search(keywords, item, new Runnable() { |
| 170 | @Override |
| 171 | public void run() { |
| 172 | setWaitingScreen(false); |
| 173 | } |
| 174 | }); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // tag: null = base tags |
| 179 | public void searchTag(final SupportType searchOn, final int page, |
| 180 | final int item, final SearchableTag tag) { |
| 181 | |
| 182 | setWaitingScreen(true); |
| 183 | updatePages(page, maxPage); |
| 184 | searchPanel.setSupportType(searchOn); |
| 185 | searchPanel.searchTag(tag, item, new Runnable() { |
| 186 | @Override |
| 187 | public void run() { |
| 188 | setWaitingScreen(false); |
| 189 | } |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Process the given action in the main Swing UI thread. |
| 195 | * <p> |
| 196 | * The code will make sure the current thread is the main UI thread and, if |
| 197 | * not, will switch to it before executing the runnable. |
| 198 | * <p> |
| 199 | * Synchronous operation. |
| 200 | * |
| 201 | * @param run |
| 202 | * the action to run |
| 203 | */ |
| 204 | static void inUi(final Runnable run) { |
| 205 | if (EventQueue.isDispatchThread()) { |
| 206 | run.run(); |
| 207 | } else { |
| 208 | try { |
| 209 | EventQueue.invokeAndWait(run); |
| 210 | } catch (InterruptedException e) { |
| 211 | error(e); |
| 212 | } catch (InvocationTargetException e) { |
| 213 | error(e); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void error(Exception e) { |
| 219 | Instance.getTraceHandler().error(e); |
| 220 | } |
| 221 | |
| 222 | static void error(String e) { |
| 223 | Instance.getTraceHandler().error(e); |
| 224 | } |
| 225 | |
| 226 | private void setWaitingScreen(final boolean waiting) { |
| 227 | inUi(new Runnable() { |
| 228 | @Override |
| 229 | public void run() { |
| 230 | GuiReaderSearchFrame.this.setEnabled(!waiting); |
| 231 | books.setEnabled(!waiting); |
| 232 | searchPanel.setEnabled(!waiting); |
| 233 | } |
| 234 | }); |
| 235 | } |
| 236 | } |