Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
1 | package be.nikiroo.fanfix_swing.gui; |
2 | ||
3cdf3fd8 NR |
3 | import java.awt.event.ActionEvent; |
4 | import java.awt.event.ActionListener; | |
5 | import java.awt.event.KeyEvent; | |
3cdf3fd8 NR |
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; | |
3cdf3fd8 | 13 | import javax.swing.JSplitPane; |
3cdf3fd8 | 14 | |
04eafeea | 15 | import be.nikiroo.fanfix_swing.Actions; |
3cdf3fd8 NR |
16 | import be.nikiroo.utils.Version; |
17 | ||
18 | public class MainFrame extends JFrame { | |
19 | private BooksPanel books; | |
20 | private DetailsPanel details; | |
04eafeea | 21 | private BrowserPanel browser; |
3cdf3fd8 NR |
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 | ||
04eafeea | 31 | browser = new BrowserPanel(); |
3cdf3fd8 NR |
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); | |
2a03ecc0 | 51 | |
3cdf3fd8 NR |
52 | browser.addActionListener(new ActionListener() { |
53 | @Override | |
54 | public void actionPerformed(ActionEvent e) { | |
32ed6089 NR |
55 | books.load(browser.getSelectedSources(), |
56 | browser.getSelectedAuthors(), | |
57 | browser.getSelectedTags()); | |
3cdf3fd8 NR |
58 | details.setBook(browser.getHighlight()); |
59 | } | |
60 | }); | |
2a03ecc0 NR |
61 | books.addActionListener(new ActionListener() { |
62 | @Override | |
63 | public void actionPerformed(ActionEvent e) { | |
64 | if (BooksPanel.INVALIDATE_CACHE.equals(e.getActionCommand())) { | |
65 | browser.reloadData(); | |
66 | } | |
67 | } | |
68 | }); | |
3cdf3fd8 NR |
69 | |
70 | JSplitPane split = split(other, books, orientationH, 0.5, 0); | |
71 | ||
72 | this.add(split); | |
73 | } | |
74 | ||
32ed6089 NR |
75 | private JSplitPane split(JComponent leftTop, JComponent rightBottom, |
76 | boolean horizontal, double ratio, double weight) { | |
77 | JSplitPane split = new JSplitPane( | |
78 | horizontal ? JSplitPane.HORIZONTAL_SPLIT | |
79 | : JSplitPane.VERTICAL_SPLIT, | |
80 | leftTop, rightBottom); | |
3cdf3fd8 NR |
81 | split.setOneTouchExpandable(true); |
82 | split.setResizeWeight(weight); | |
83 | split.setContinuousLayout(true); | |
84 | split.setDividerLocation(ratio); | |
85 | ||
86 | return split; | |
87 | } | |
88 | ||
89 | private JMenuBar createMenuBar() { | |
90 | JMenuBar bar = new JMenuBar(); | |
91 | ||
92 | JMenu file = new JMenu("File"); | |
93 | file.setMnemonic(KeyEvent.VK_F); | |
94 | ||
04eafeea | 95 | JMenuItem item1 = new JMenuItem("Download", KeyEvent.VK_D); |
3cdf3fd8 NR |
96 | item1.addActionListener(new ActionListener() { |
97 | @Override | |
98 | public void actionPerformed(ActionEvent e) { | |
04eafeea NR |
99 | Actions.imprt(MainFrame.this, true, new Runnable() { |
100 | @Override | |
101 | public void run() { | |
102 | browser.reloadData(); | |
32ed6089 NR |
103 | books.load(browser.getSelectedSources(), |
104 | browser.getSelectedAuthors(), | |
04eafeea NR |
105 | browser.getSelectedTags()); |
106 | details.setBook(browser.getHighlight()); | |
107 | } | |
108 | }); | |
109 | } | |
110 | }); | |
111 | ||
112 | JMenuItem item2 = new JMenuItem("Import file", KeyEvent.VK_I); | |
113 | item2.addActionListener(new ActionListener() { | |
114 | @Override | |
115 | public void actionPerformed(ActionEvent e) { | |
116 | Actions.imprt(MainFrame.this, false, new Runnable() { | |
117 | @Override | |
118 | public void run() { | |
119 | browser.reloadData(); | |
32ed6089 NR |
120 | books.load(browser.getSelectedSources(), |
121 | browser.getSelectedAuthors(), | |
04eafeea NR |
122 | browser.getSelectedTags()); |
123 | details.setBook(browser.getHighlight()); | |
124 | } | |
125 | }); | |
3cdf3fd8 NR |
126 | } |
127 | }); | |
128 | ||
129 | file.add(item1); | |
04eafeea | 130 | file.add(item2); |
3cdf3fd8 NR |
131 | |
132 | JMenu edit = new JMenu("Edit"); | |
133 | edit.setMnemonic(KeyEvent.VK_E); | |
134 | ||
135 | JMenu view = new JMenu("View"); | |
136 | view.setMnemonic(KeyEvent.VK_V); | |
137 | ||
138 | JMenuItem listMode = new JMenuItem("List mode", KeyEvent.VK_L); | |
139 | listMode.addActionListener(new ActionListener() { | |
140 | @Override | |
141 | public void actionPerformed(ActionEvent e) { | |
142 | books.setListMode(!books.isListMode()); | |
143 | } | |
144 | }); | |
145 | ||
146 | view.add(listMode); | |
147 | ||
148 | bar.add(file); | |
149 | bar.add(edit); | |
150 | bar.add(view); | |
151 | ||
152 | return bar; | |
153 | } | |
154 | } |