import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
popup.add(createMenuItemOpenBook());
popup.addSeparator();
popup.add(createMenuItemExport());
- popup.add(createMenuItemMove(true));
+ popup.add(createMenuItemMoveTo(true));
popup.add(createMenuItemSetCover());
popup.add(createMenuItemClearCache());
popup.add(createMenuItemRedownload());
popup.addSeparator();
+ popup.add(createMenuItemRename(true));
+ popup.add(createMenuItemSetAuthor(true));
+ popup.addSeparator();
popup.add(createMenuItemDelete());
popup.addSeparator();
popup.add(createMenuItemProperties());
file.add(createMenuItemOpenBook());
file.add(createMenuItemExport());
- file.add(createMenuItemMove(libOk));
+ file.add(createMenuItemMoveTo(libOk));
file.addSeparator();
file.add(imprt);
file.add(imprtF);
file.addSeparator();
+ file.add(createMenuItemRename(libOk));
+ file.add(createMenuItemSetAuthor(libOk));
+ file.addSeparator();
file.add(exit);
bar.add(file);
JMenu authors = new JMenu("Authors");
authors.setMnemonic(KeyEvent.VK_A);
- List<String> aa = new ArrayList<String>();
- if (libOk) {
- aa.addAll(reader.getLibrary().getAuthors());
- }
- aa.add(0, null);
- for (final String author : aa) {
- JMenuItem item = new JMenuItem(author == null ? "All"
- : author.isEmpty() ? "[unknown]" : author);
- item.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- removeBookPanes();
- addBookPane(author, false);
- refreshBooks();
- }
- });
- authors.add(item);
+ List<Entry<String, List<String>>> authorGroups = reader.getLibrary()
+ .getAuthorsGrouped();
+ if (authorGroups.size() > 1) {
+ // Multiple groups
- if (author == null || author.isEmpty()) {
- authors.addSeparator();
+ // null -> "All" authors special item
+ populateMenuAuthorList(authors, Arrays.asList((String) null));
+
+ for (Entry<String, List<String>> group : authorGroups) {
+ JMenu thisGroup = new JMenu(group.getKey());
+ populateMenuAuthorList(thisGroup, group.getValue());
+ authors.add(thisGroup);
+ }
+ } else {
+ // Only one group
+
+ // null -> "All" authors special item
+ List<String> authorNames = new ArrayList<String>();
+ authorNames.add(null);
+ if (authorGroups.size() > 0) {
+ authorNames.addAll(authorGroups.get(0).getValue());
}
+ populateMenuAuthorList(authors, authorNames);
}
bar.add(authors);
return bar;
}
+ /**
+ * Populate a list of authors as {@link JMenuItem}s into the given
+ * {@link JMenu}.
+ * <p>
+ * Each item will select the author when clicked.
+ *
+ * @param authors
+ * the parent {@link JMenuItem}
+ * @param names
+ * the authors' names
+ */
+ private void populateMenuAuthorList(JMenu authors, List<String> names) {
+ for (final String name : names) {
+ JMenuItem item = new JMenuItem(name == null ? "All"
+ : name.isEmpty() ? "[unknown]" : name);
+ item.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ removeBookPanes();
+ addBookPane(name, false);
+ refreshBooks();
+ }
+ });
+ authors.add(item);
+
+ if (name == null || name.isEmpty()) {
+ authors.addSeparator();
+ }
+ }
+ }
+
/**
* Create the Fanfix Configuration menu item.
*
}
/**
- * Create the delete menu item.
+ * Create the "move to" menu item.
*
* @param libOk
* the library can be queried
*
* @return the item
*/
- private JMenuItem createMenuItemMove(boolean libOk) {
- JMenu moveTo = new JMenu("Move to...");
- moveTo.setMnemonic(KeyEvent.VK_M);
+ private JMenuItem createMenuItemMoveTo(boolean libOk) {
+ JMenuItem changeTo = new JMenu("Move to");
+ changeTo.setMnemonic(KeyEvent.VK_M);
- List<String> types = new ArrayList<String>();
- types.add(null);
+ List<String> values = new ArrayList<String>();
+ values.add(null);
if (libOk) {
- types.addAll(reader.getLibrary().getSources());
+ values.addAll(reader.getLibrary().getSources());
}
- for (String type : types) {
- JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
+ for (String value : values) {
+ JMenuItem item = new JMenuItem(value == null ? "New type..."
+ : value);
- moveTo.add(item);
- if (type == null) {
- moveTo.addSeparator();
- }
+ item.addActionListener(createMoveAction("SOURCE", value));
- final String ftype = type;
- item.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (selectedBook != null) {
- String type = ftype;
- if (type == null) {
- Object rep = JOptionPane.showInputDialog(
- GuiReaderFrame.this, "Move to:",
- "Moving story",
- JOptionPane.QUESTION_MESSAGE, null, null,
- selectedBook.getMeta().getSource());
-
- if (rep == null) {
- return;
- }
+ changeTo.add(item);
+ if (value == null) {
+ ((JMenu) changeTo).addSeparator();
+ }
+ }
- type = rep.toString();
- }
+ return changeTo;
+ }
- final String ftype = type;
- outOfUi(null, new Runnable() {
- @Override
- public void run() {
- reader.changeSource(selectedBook.getMeta()
- .getLuid(), ftype);
+ /**
+ * Create the "set author" menu item.
+ *
+ * @param libOk
+ * the library can be queried
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemSetAuthor(boolean libOk) {
+ JMenu changeTo = new JMenu("Set author");
+ changeTo.setMnemonic(KeyEvent.VK_A);
- selectedBook = null;
+ // New author
+ JMenuItem newItem = new JMenuItem("New author...");
+ changeTo.add(newItem);
+ changeTo.addSeparator();
+ newItem.addActionListener(createMoveAction("AUTHOR", null));
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- setJMenuBar(createMenu(true));
- }
- });
- }
- });
+ // Existing authors
+ if (libOk) {
+ List<Entry<String, List<String>>> authorGroups = reader
+ .getLibrary().getAuthorsGrouped();
+
+ if (authorGroups.size() > 1) {
+ for (Entry<String, List<String>> entry : authorGroups) {
+ JMenu group = new JMenu(entry.getKey());
+ for (String value : entry.getValue()) {
+ JMenuItem item = new JMenuItem(value);
+ item.addActionListener(createMoveAction("AUTHOR", value));
+ group.add(item);
}
+ changeTo.add(group);
}
- });
+ } else if (authorGroups.size() == 1) {
+ for (String value : authorGroups.get(0).getValue()) {
+ JMenuItem item = new JMenuItem(value);
+ item.addActionListener(createMoveAction("AUTHOR", value));
+ changeTo.add(item);
+ }
+ }
}
- return moveTo;
+ return changeTo;
+ }
+
+ /**
+ * Create the "rename" menu item.
+ *
+ * @param libOk
+ * the library can be queried
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemRename(
+ @SuppressWarnings("unused") boolean libOk) {
+ JMenuItem changeTo = new JMenuItem("Rename...");
+ changeTo.setMnemonic(KeyEvent.VK_R);
+ changeTo.addActionListener(createMoveAction("TITLE", null));
+ return changeTo;
+ }
+
+ private ActionListener createMoveAction(final String what, final String type) {
+ return new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ String changeTo = type;
+ if (type == null) {
+ String init = "";
+ if (what.equals("SOURCE")) {
+ init = selectedBook.getMeta().getSource();
+ } else if (what.equals("TITLE")) {
+ init = selectedBook.getMeta().getTitle();
+ } else if (what.equals("AUTHOR")) {
+ init = selectedBook.getMeta().getAuthor();
+ }
+
+ Object rep = JOptionPane.showInputDialog(
+ GuiReaderFrame.this, "Move to:",
+ "Moving story", JOptionPane.QUESTION_MESSAGE,
+ null, null, init);
+
+ if (rep == null) {
+ return;
+ }
+
+ changeTo = rep.toString();
+ }
+
+ final String fChangeTo = changeTo;
+ outOfUi(null, new Runnable() {
+ @Override
+ public void run() {
+ if (what.equals("SOURCE")) {
+ reader.changeSource(selectedBook.getMeta()
+ .getLuid(), fChangeTo);
+ } else if (what.equals("TITLE")) {
+ reader.changeTitle(selectedBook.getMeta()
+ .getLuid(), fChangeTo);
+ } else if (what.equals("AUTHOR")) {
+ reader.changeAuthor(selectedBook.getMeta()
+ .getLuid(), fChangeTo);
+ }
+
+ selectedBook = null;
+
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ setJMenuBar(createMenu(true));
+ }
+ });
+ }
+ });
+ }
+ }
+ };
}
/**