default search/filter to realtime
[nikiroo-utils.git] / src / be / nikiroo / fanfix_swing / gui / MainFrame.java
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 books = new BooksPanel(true);
33
34 JComponent other = null;
35 boolean orientationH = true;
36 if (sidePanel && !detailsPanel) {
37 other = browser;
38 } else if (sidePanel && detailsPanel) {
39 JComponent side = browser;
40 details = new DetailsPanel();
41 other = split(side, details, false, 0.5, 1);
42 } else if (!sidePanel && !detailsPanel) {
43 orientationH = false;
44 other = new JLabel("<< Go back");
45 } else if (!sidePanel && detailsPanel) {
46 JComponent goBack = new JLabel("<< Go back");
47 details = new DetailsPanel();
48 other = split(goBack, details, false, 0.5, 1);
49 }
50
51 browser.addActionListener(new ActionListener() {
52 @Override
53 public void actionPerformed(ActionEvent e) {
54 books.load(browser.getSelectedSources(),
55 browser.getSelectedAuthors(),
56 browser.getSelectedTags());
57 details.setBook(browser.getHighlight());
58 }
59 });
60 books.addActionListener(new ActionListener() {
61 @Override
62 public void actionPerformed(ActionEvent e) {
63 if (BooksPanel.INVALIDATE_CACHE.equals(e.getActionCommand())) {
64 browser.reloadData();
65 }
66 }
67 });
68
69 JSplitPane split = split(other, books, orientationH, 0.5, 0);
70
71 this.add(split);
72 }
73
74 private JSplitPane split(JComponent leftTop, JComponent rightBottom,
75 boolean horizontal, double ratio, double weight) {
76 JSplitPane split = new JSplitPane(
77 horizontal ? JSplitPane.HORIZONTAL_SPLIT
78 : JSplitPane.VERTICAL_SPLIT,
79 leftTop, rightBottom);
80 split.setOneTouchExpandable(true);
81 split.setResizeWeight(weight);
82 split.setContinuousLayout(true);
83 split.setDividerLocation(ratio);
84
85 return split;
86 }
87
88 private JMenuBar createMenuBar() {
89 JMenuBar bar = new JMenuBar();
90
91 JMenu file = new JMenu("File");
92 file.setMnemonic(KeyEvent.VK_F);
93
94 JMenuItem item1 = new JMenuItem("Download", KeyEvent.VK_D);
95 item1.addActionListener(new ActionListener() {
96 @Override
97 public void actionPerformed(ActionEvent e) {
98 Actions.imprt(MainFrame.this, true, new Runnable() {
99 @Override
100 public void run() {
101 browser.reloadData();
102 books.load(browser.getSelectedSources(),
103 browser.getSelectedAuthors(),
104 browser.getSelectedTags());
105 details.setBook(browser.getHighlight());
106 }
107 });
108 }
109 });
110
111 JMenuItem item2 = new JMenuItem("Import file", KeyEvent.VK_I);
112 item2.addActionListener(new ActionListener() {
113 @Override
114 public void actionPerformed(ActionEvent e) {
115 Actions.imprt(MainFrame.this, false, new Runnable() {
116 @Override
117 public void run() {
118 browser.reloadData();
119 books.load(browser.getSelectedSources(),
120 browser.getSelectedAuthors(),
121 browser.getSelectedTags());
122 details.setBook(browser.getHighlight());
123 }
124 });
125 }
126 });
127
128 file.add(item1);
129 file.add(item2);
130
131 JMenu edit = new JMenu("Edit");
132 edit.setMnemonic(KeyEvent.VK_E);
133
134 JMenu view = new JMenu("View");
135 view.setMnemonic(KeyEvent.VK_V);
136
137 JMenuItem listMode = new JMenuItem("List mode", KeyEvent.VK_L);
138 listMode.addActionListener(new ActionListener() {
139 @Override
140 public void actionPerformed(ActionEvent e) {
141 books.setListMode(!books.isListMode());
142 }
143 });
144
145 view.add(listMode);
146
147 bar.add(file);
148 bar.add(edit);
149 bar.add(view);
150
151 return bar;
152 }
153 }