a7c743cf82858e6af475f25cda6e4b1f244880c2
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderFrame.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Desktop;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.KeyEvent;
9 import java.awt.event.WindowEvent;
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import javax.swing.JFrame;
16 import javax.swing.JMenu;
17 import javax.swing.JMenuBar;
18 import javax.swing.JMenuItem;
19 import javax.swing.JOptionPane;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22
23 import be.nikiroo.fanfix.Instance;
24 import be.nikiroo.fanfix.Main;
25 import be.nikiroo.fanfix.bundles.UiConfig;
26 import be.nikiroo.fanfix.data.MetaData;
27 import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListner;
28 import be.nikiroo.utils.WrapLayout;
29
30 class LocalReaderFrame extends JFrame {
31 private static final long serialVersionUID = 1L;
32 private LocalReader reader;
33 private List<MetaData> stories;
34 private List<LocalReaderBook> books;
35 private JPanel bookPane;
36 private String type;
37 private Color color;
38
39 public LocalReaderFrame(LocalReader reader, String type) {
40 super("Fanfix Library");
41
42 this.reader = reader;
43
44 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45 setSize(800, 600);
46 setLayout(new BorderLayout());
47
48 books = new ArrayList<LocalReaderBook>();
49 bookPane = new JPanel(new WrapLayout(WrapLayout.LEADING, 5, 5));
50
51 color = null;
52 String bg = Instance.getUiConfig().getString(UiConfig.BACKGROUND_COLOR);
53 if (bg.startsWith("#") && bg.length() == 7) {
54 try {
55 color = new Color(Integer.parseInt(bg.substring(1, 3), 16),
56 Integer.parseInt(bg.substring(3, 5), 16),
57 Integer.parseInt(bg.substring(5, 7), 16));
58 } catch (NumberFormatException e) {
59 color = null; // no changes
60 e.printStackTrace();
61 }
62 }
63
64 if (color != null) {
65 setBackground(color);
66 bookPane.setBackground(color);
67 }
68
69 JScrollPane scroll = new JScrollPane(bookPane);
70 scroll.getVerticalScrollBar().setUnitIncrement(16);
71 add(scroll, BorderLayout.CENTER);
72
73 refreshBooks(type);
74 setJMenuBar(createMenu());
75
76 setVisible(true);
77 }
78
79 private void refreshBooks(String type) {
80 this.type = type;
81 stories = Instance.getLibrary().getList(type);
82 books.clear();
83 bookPane.removeAll();
84 for (MetaData meta : stories) {
85 LocalReaderBook book = new LocalReaderBook(meta);
86 if (color != null) {
87 book.setBackground(color);
88 }
89
90 books.add(book);
91 final String luid = meta.getLuid();
92 book.addActionListener(new BookActionListner() {
93 public void select(LocalReaderBook book) {
94 for (LocalReaderBook abook : books) {
95 abook.setSelected(abook == book);
96 }
97 }
98
99 public void action(LocalReaderBook book) {
100 try {
101 File target = LocalReaderFrame.this.reader
102 .getTarget(luid);
103 Desktop.getDesktop().browse(target.toURI());
104 } catch (IOException e) {
105 Instance.syserr(e);
106 }
107 }
108 });
109
110 bookPane.add(book);
111 }
112
113 bookPane.validate();
114 bookPane.repaint();
115 }
116
117 private JMenuBar createMenu() {
118 JMenuBar bar = new JMenuBar();
119
120 JMenu file = new JMenu("File");
121
122 JMenuItem imprt = new JMenuItem("Import", KeyEvent.VK_I);
123 imprt.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) {
125 String url = JOptionPane.showInputDialog(LocalReaderFrame.this,
126 "url of the story to import?\n" + "\n"
127 + "Note: it will currently make the UI \n"
128 + "unresponsive until it is downloaded...",
129 "Importing from URL", JOptionPane.QUESTION_MESSAGE);
130 if (url != null && !url.isEmpty()) {
131 if (Main.imprt(url) != 0) {
132 JOptionPane.showMessageDialog(LocalReaderFrame.this,
133 "Cannot import: " + url, "Imort error",
134 JOptionPane.ERROR_MESSAGE);
135 } else {
136 refreshBooks(type);
137 }
138 }
139 }
140 });
141 JMenu types = new JMenu("Type");
142 List<String> tt = Instance.getLibrary().getTypes();
143 tt.add(0, null);
144 for (final String type : tt) {
145 JMenuItem item = new JMenuItem(type == null ? "[all]" : type);
146 item.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 refreshBooks(type);
149 }
150 });
151 types.add(item);
152 }
153 JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X);
154 exit.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent e) {
156 LocalReaderFrame.this.dispatchEvent(new WindowEvent(
157 LocalReaderFrame.this, WindowEvent.WINDOW_CLOSING));
158 }
159 });
160
161 file.add(imprt);
162 file.add(types);
163 file.addSeparator();
164 file.add(exit);
165
166 bar.add(file);
167
168 return bar;
169 }
170 }