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