import be.nikiroo.fanfix.library.BasicLibrary;
import be.nikiroo.fanfix_swing.gui.book.BookBlock;
import be.nikiroo.fanfix_swing.gui.book.BookInfo;
+import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type;
import be.nikiroo.fanfix_swing.gui.book.BookLine;
import be.nikiroo.fanfix_swing.gui.book.BookPopup;
import be.nikiroo.fanfix_swing.gui.book.BookPopup.Informer;
import be.nikiroo.utils.compat.ListCellRenderer6;
import be.nikiroo.utils.ui.DelayWorker;
import be.nikiroo.utils.ui.ListModel;
-import be.nikiroo.utils.ui.ListSnapshot;
import be.nikiroo.utils.ui.ListModel.Predicate;
+import be.nikiroo.utils.ui.ListSnapshot;
import be.nikiroo.utils.ui.ListenerPanel;
-import be.nikiroo.utils.ui.TreeSnapshot;
import be.nikiroo.utils.ui.UIUtils;
public class BooksPanel extends ListenerPanel {
static public final String INVALIDATE_CACHE = "invalidate_cache";
+ private enum ReloadMode {
+ NONE, STA, TYPE_VALUE
+ }
+
+ private class ReloadData {
+ public ReloadMode mode;
+
+ public List<String> sources;
+ public List<String> authors;
+ public List<String> tags;
+
+ public Type type;
+ public String value;
+
+ public ReloadData() {
+ this.mode = ReloadMode.NONE;
+ }
+
+ public ReloadData(List<String> sources, List<String> authors,
+ List<String> tags) {
+ this.mode = ReloadMode.STA;
+ this.sources = sources;
+ this.authors = authors;
+ this.tags = tags;
+ }
+
+ public ReloadData(Type type, String value) {
+ this.mode = ReloadMode.TYPE_VALUE;
+ this.type = type;
+ this.value = value;
+ }
+ }
+
private Map<BookInfo, BookLine> books = new HashMap<BookInfo, BookLine>();
private boolean seeWordCount;
private boolean listMode;
private BooksPanelActions actions;
private BookPopup popup;
- private Object[] lastLoad = new Object[4];
+ private ReloadData lastLoad = new ReloadData();
public BooksPanel(boolean listMode) {
setLayout(new BorderLayout());
public void loadData(final List<String> sources, final List<String> authors,
final List<String> tags) {
synchronized (lastLoad) {
- lastLoad[0] = "sources, authors, tags";
- lastLoad[1] = sources;
- lastLoad[2] = authors;
- lastLoad[3] = tags;
+ lastLoad = new ReloadData(sources, authors, tags);
}
-
+
+ if(sources.size()==0) {
+ new Exception().printStackTrace();
+ }
+
new SwingWorker<List<BookInfo>, Void>() {
@Override
protected List<BookInfo> doInBackground() throws Exception {
}
// TODO
- private void loadData(final BookInfo.Type type, final String value) {
+ private void loadData(final Type type, final String value) {
synchronized (lastLoad) {
- lastLoad[0] = "type";
- lastLoad[1] = type;
- lastLoad[2] = value;
+ lastLoad = new ReloadData(type, value);
}
// TODO todo todo
}
public void reloadData() {
- Object[] lastLoad;
+ ReloadData lastLoad;
synchronized (this.lastLoad) {
- lastLoad = this.lastLoad.clone();
+ lastLoad = this.lastLoad;
}
// Reset the popup menu items for for sources/author
popup.reloadData();
- if (lastLoad[0] == null) {
+ if (lastLoad.mode == ReloadMode.NONE) {
return; // nothing was loaded yet
}
ListSnapshot snapshot = new ListSnapshot(list);
- if (lastLoad[0].toString().equals("sources, authors, tags")) {
- loadData((List<String>) lastLoad[1], (List<String>) lastLoad[2],
- (List<String>) lastLoad[3]);
- } else if (lastLoad[0].toString().equals("type")) {
- loadData((BookInfo.Type) lastLoad[1], (String) lastLoad[2]);
- } else if (lastLoad[0].toString().equals("bookInfos")) {
- loadData((List<BookInfo>) lastLoad[1]);
- } else {
+ switch (lastLoad.mode) {
+ case STA:
+ loadData(lastLoad.sources, lastLoad.authors, lastLoad.tags);
+ break;
+ case TYPE_VALUE:
+ loadData(lastLoad.type, lastLoad.value);
+ break;
+ default:
Instance.getInstance().getTraceHandler()
- .error("Unknown last load type: " + lastLoad[0]);
+ .error("Unknown last load type: " + lastLoad.mode);
+ break;
}
snapshot.apply();
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
private int index;
private JTree tree;
+ private TreeSelectionListener treeListener;
private DefaultMutableTreeNode root;
private DataTree<DataNodeBook> data;
private SearchBar searchBar;
root = new DefaultMutableTreeNode();
tree = new JTree(root);
- tree.setUI(new BasicTreeUI());
- TreeCellSpanner spanner = new TreeCellSpanner(tree,
- generateCellRenderer());
- tree.setCellRenderer(spanner);
- tree.setRootVisible(false);
- tree.setShowsRootHandles(false);
-
- tree.addTreeSelectionListener(new TreeSelectionListener() {
+ treeListener = new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
List<String> elements = new ArrayList<String>();
}
}
- BrowserTab.this.selectedElements = elements;
+ Collections.sort(elements);
+
+ boolean same = false;
+ if (BrowserTab.this.selectedElements.size() == elements
+ .size()) {
+ same = true;
+ for (int i = 0; i < elements.size(); i++) {
+ String newEl = elements.get(i);
+ String oldEl = BrowserTab.this.selectedElements.get(i);
+ if (!newEl.equals(oldEl)) {
+ same = false;
+ break;
+ }
+ }
+ }
- fireActionPerformed(BrowserTab.this.listenerCommand);
+ if (!same) {
+ BrowserTab.this.selectedElements = elements;
+ fireActionPerformed(BrowserTab.this.listenerCommand);
+ }
}
- });
+ };
+
+ tree.setUI(new BasicTreeUI());
+ TreeCellSpanner spanner = new TreeCellSpanner(tree,
+ generateCellRenderer());
+ tree.setCellRenderer(spanner);
+ tree.setRootVisible(false);
+ tree.setShowsRootHandles(false);
+ tree.addTreeSelectionListener(treeListener);
add(UIUtils.scroll(tree, false), BorderLayout.CENTER);
node2node(root, filtered);
totalCount = filtered.count() - 1; // root is counted
+ tree.removeTreeSelectionListener(treeListener);
((DefaultTreeModel) tree.getModel()).reload();
-
snapshot.apply();
+ tree.addTreeSelectionListener(treeListener);
+
+ // Try to fire it (it will not do anything if no selection changed)
+ treeListener.valueChanged(
+ new TreeSelectionEvent(this, null, false, null, null));
if (fireActionPerformed) {
fireActionPerformed(listenerCommand);