| 1 | package be.nikiroo.fanfix_swing.gui; |
| 2 | |
| 3 | import java.awt.event.ActionEvent; |
| 4 | import java.awt.event.ActionListener; |
| 5 | import java.awt.event.KeyEvent; |
| 6 | |
| 7 | import javax.swing.JComponent; |
| 8 | import javax.swing.JFrame; |
| 9 | import javax.swing.JLabel; |
| 10 | import javax.swing.JMenu; |
| 11 | import javax.swing.JMenuBar; |
| 12 | import javax.swing.JMenuItem; |
| 13 | import javax.swing.JSplitPane; |
| 14 | |
| 15 | import be.nikiroo.fanfix_swing.Actions; |
| 16 | import be.nikiroo.utils.Version; |
| 17 | |
| 18 | public class MainFrame extends JFrame { |
| 19 | private BooksPanel books; |
| 20 | private DetailsPanel details; |
| 21 | private BrowserPanel browser; |
| 22 | |
| 23 | public MainFrame(boolean sidePanel, boolean detailsPanel) { |
| 24 | super("Fanfix " + Version.getCurrentVersion()); |
| 25 | setSize(800, 600); |
| 26 | setJMenuBar(createMenuBar()); |
| 27 | |
| 28 | sidePanel = true; |
| 29 | detailsPanel = true; |
| 30 | |
| 31 | browser = new BrowserPanel(); |
| 32 | |
| 33 | JComponent other = null; |
| 34 | boolean orientationH = true; |
| 35 | if (sidePanel && !detailsPanel) { |
| 36 | other = browser; |
| 37 | } else if (sidePanel && detailsPanel) { |
| 38 | JComponent side = browser; |
| 39 | details = new DetailsPanel(); |
| 40 | other = split(side, details, false, 0.5, 1); |
| 41 | } else if (!sidePanel && !detailsPanel) { |
| 42 | orientationH = false; |
| 43 | other = new JLabel("<< Go back"); |
| 44 | } else if (!sidePanel && detailsPanel) { |
| 45 | JComponent goBack = new JLabel("<< Go back"); |
| 46 | details = new DetailsPanel(); |
| 47 | other = split(goBack, details, false, 0.5, 1); |
| 48 | } |
| 49 | |
| 50 | books = new BooksPanel(true); |
| 51 | |
| 52 | browser.addActionListener(new ActionListener() { |
| 53 | @Override |
| 54 | public void actionPerformed(ActionEvent e) { |
| 55 | books.load(browser.getSelectedSources(), browser.getSelectedAuthors(), browser.getSelectedTags()); |
| 56 | details.setBook(browser.getHighlight()); |
| 57 | } |
| 58 | }); |
| 59 | books.addActionListener(new ActionListener() { |
| 60 | @Override |
| 61 | public void actionPerformed(ActionEvent e) { |
| 62 | if (BooksPanel.INVALIDATE_CACHE.equals(e.getActionCommand())) { |
| 63 | browser.reloadData(); |
| 64 | } |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | JSplitPane split = split(other, books, orientationH, 0.5, 0); |
| 69 | |
| 70 | this.add(split); |
| 71 | } |
| 72 | |
| 73 | private JSplitPane split(JComponent leftTop, JComponent rightBottom, boolean horizontal, double ratio, |
| 74 | double weight) { |
| 75 | JSplitPane split = new JSplitPane(horizontal ? JSplitPane.HORIZONTAL_SPLIT : JSplitPane.VERTICAL_SPLIT, leftTop, |
| 76 | rightBottom); |
| 77 | split.setOneTouchExpandable(true); |
| 78 | split.setResizeWeight(weight); |
| 79 | split.setContinuousLayout(true); |
| 80 | split.setDividerLocation(ratio); |
| 81 | |
| 82 | return split; |
| 83 | } |
| 84 | |
| 85 | private JMenuBar createMenuBar() { |
| 86 | JMenuBar bar = new JMenuBar(); |
| 87 | |
| 88 | JMenu file = new JMenu("File"); |
| 89 | file.setMnemonic(KeyEvent.VK_F); |
| 90 | |
| 91 | JMenuItem item1 = new JMenuItem("Download", KeyEvent.VK_D); |
| 92 | item1.addActionListener(new ActionListener() { |
| 93 | @Override |
| 94 | public void actionPerformed(ActionEvent e) { |
| 95 | Actions.imprt(MainFrame.this, true, new Runnable() { |
| 96 | @Override |
| 97 | public void run() { |
| 98 | browser.reloadData(); |
| 99 | books.load(browser.getSelectedSources(), browser.getSelectedAuthors(), |
| 100 | browser.getSelectedTags()); |
| 101 | details.setBook(browser.getHighlight()); |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | JMenuItem item2 = new JMenuItem("Import file", KeyEvent.VK_I); |
| 108 | item2.addActionListener(new ActionListener() { |
| 109 | @Override |
| 110 | public void actionPerformed(ActionEvent e) { |
| 111 | Actions.imprt(MainFrame.this, false, new Runnable() { |
| 112 | @Override |
| 113 | public void run() { |
| 114 | browser.reloadData(); |
| 115 | books.load(browser.getSelectedSources(), browser.getSelectedAuthors(), |
| 116 | browser.getSelectedTags()); |
| 117 | details.setBook(browser.getHighlight()); |
| 118 | } |
| 119 | }); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | file.add(item1); |
| 124 | file.add(item2); |
| 125 | |
| 126 | JMenu edit = new JMenu("Edit"); |
| 127 | edit.setMnemonic(KeyEvent.VK_E); |
| 128 | |
| 129 | JMenu view = new JMenu("View"); |
| 130 | view.setMnemonic(KeyEvent.VK_V); |
| 131 | |
| 132 | JMenuItem listMode = new JMenuItem("List mode", KeyEvent.VK_L); |
| 133 | listMode.addActionListener(new ActionListener() { |
| 134 | @Override |
| 135 | public void actionPerformed(ActionEvent e) { |
| 136 | books.setListMode(!books.isListMode()); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | view.add(listMode); |
| 141 | |
| 142 | bar.add(file); |
| 143 | bar.add(edit); |
| 144 | bar.add(view); |
| 145 | |
| 146 | return bar; |
| 147 | } |
| 148 | } |