From ed2551a6b1eda640da2ee87377d30c55bd11659d Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 5 Aug 2008 15:25:22 +0000 Subject: Client: Added the Item Buffer back This was removed in [68] because it was thought to be the cause of a bug. For more info, see [68] and [76]. --- .../com/fourisland/instadisc/Database/Item.java | 85 ++++++++++++++++++++ .../com/fourisland/instadisc/Database/Wrapper.java | 54 +++++++++++-- .../instadisc/IDItemListCellRenderer.java | 2 +- .../com/fourisland/instadisc/InstaDiscView.java | 20 ++--- .../src/com/fourisland/instadisc/Item/Item.java | 90 ++++------------------ .../instadisc/ManageSubscriptionsForm.java | 3 - 6 files changed, 155 insertions(+), 99 deletions(-) create mode 100644 client/trunk/src/com/fourisland/instadisc/Database/Item.java (limited to 'client') diff --git a/client/trunk/src/com/fourisland/instadisc/Database/Item.java b/client/trunk/src/com/fourisland/instadisc/Database/Item.java new file mode 100644 index 0000000..7dbc89b --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/Database/Item.java @@ -0,0 +1,85 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.instadisc.Database; + +import com.sleepycat.persist.model.Entity; +import com.sleepycat.persist.model.PrimaryKey; +import java.util.HashMap; + +/** + * + * @author hatkirby + */ +@Entity +public class Item { + + @PrimaryKey + private Integer id; + private String subscription; + private String title; + private String author; + private String url; + private HashMap semantics; + + public Item() { + semantics = new HashMap(); + } + + public Integer getID() { + return id; + } + + public String getSubscription() { + return subscription; + } + + public String getTitle() { + return title; + } + + public String getAuthor() { + return author; + } + + public String getURL() { + return url; + } + + public HashMap getSemantics() { + return semantics; + } + + public void setID(Integer id) { + this.id = id; + } + + public void setSubscription(String subscription) { + this.subscription = subscription; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setAuthor(String author) { + this.author = author; + } + + public void setURL(String url) { + this.url = url; + } + + public void setSemantics(HashMap semantics) { + this.semantics = semantics; + } + + public String getSemantics(String key) { + return semantics.get(key); + } + + public void putSemantics(String key, String value) { + semantics.put(key, value); + } +} diff --git a/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java b/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java index 6ab0c72..3423e97 100644 --- a/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java +++ b/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java @@ -14,6 +14,7 @@ import com.sleepycat.persist.StoreConfig; import java.io.File; import java.util.Collection; import java.util.Iterator; +import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; @@ -29,6 +30,7 @@ public class Wrapper { public static PrimaryIndex idConfig; public static PrimaryIndex subscription; public static PrimaryIndex filter; + public static PrimaryIndex item; public static void init(String loc) { @@ -51,6 +53,7 @@ public class Wrapper { idConfig = es.getPrimaryIndex(String.class, IDConfig.class); subscription = es.getPrimaryIndex(String.class, Subscription.class); filter = es.getPrimaryIndex(Integer.class, Filter.class); + item = es.getPrimaryIndex(Integer.class, Item.class); } catch (DatabaseException ex) { Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); } @@ -173,9 +176,8 @@ public class Wrapper { Collection vals = subscription.map().values(); Subscription subs[] = new Subscription[vals.size()]; Iterator i = vals.iterator(); - int j=0; - while (i.hasNext()) - { + int j = 0; + while (i.hasNext()) { subs[j] = i.next(); j++; } @@ -234,13 +236,53 @@ public class Wrapper { Collection vals = filter.map().values(); Filter fils[] = new Filter[vals.size()]; Iterator i = vals.iterator(); - int j=0; - while (i.hasNext()) - { + int j = 0; + while (i.hasNext()) { fils[j] = i.next(); j++; } return fils; } } + + public static Integer countItem() { + synchronized (item) { + return item.map().size(); + } + } + + public static void dropFromTopItem() { + synchronized (item) { + try { + Iterator> i = item.map().entrySet().iterator(); + item.delete(i.next().getKey()); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + public static Item[] getAllItem() { + synchronized (item) { + Collection vals = item.map().values(); + Item items[] = new Item[vals.size()]; + Iterator i = vals.iterator(); + int j = 0; + while (i.hasNext()) { + items[j] = i.next(); + j++; + } + return items; + } + } + + public static void addItem(Item i) { + synchronized (item) { + try { + item.put(i); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + } + } + } } diff --git a/client/trunk/src/com/fourisland/instadisc/IDItemListCellRenderer.java b/client/trunk/src/com/fourisland/instadisc/IDItemListCellRenderer.java index cdbc686..21924e8 100644 --- a/client/trunk/src/com/fourisland/instadisc/IDItemListCellRenderer.java +++ b/client/trunk/src/com/fourisland/instadisc/IDItemListCellRenderer.java @@ -4,7 +4,7 @@ */ package com.fourisland.instadisc; -import com.fourisland.instadisc.Item.Item; +import com.fourisland.instadisc.Database.Item; import com.fourisland.instadisc.Database.Wrapper; import com.fourisland.instadisc.Item.Categories.Category; import java.awt.Component; diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java index f5f767f..be96e8a 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java @@ -3,7 +3,7 @@ */ package com.fourisland.instadisc; -import com.fourisland.instadisc.Item.Item; +import com.fourisland.instadisc.Database.Item; import com.fourisland.instadisc.Database.Wrapper; import com.fourisland.instadisc.Item.Categories.InstaDiscIcon; import java.awt.AWTException; @@ -21,7 +21,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.logging.Level; import java.util.logging.Logger; -import javax.swing.DefaultListModel; import javax.swing.Timer; import javax.swing.Icon; import javax.swing.ImageIcon; @@ -107,10 +106,8 @@ public class InstaDiscView extends FrameView { } } - lm.ensureCapacity(10); - lm.clear(); - jList1.setModel(lm); jList1.setCellRenderer(new IDItemListCellRenderer()); + refreshItemPane(); InstaDiscThread idt = new InstaDiscThread(); Thread idtt = new Thread(idt); @@ -370,17 +367,10 @@ public class InstaDiscView extends FrameView { private final Icon[] busyIcons = new Icon[15]; private int busyIconIndex = 0; private JDialog aboutBox; - private DefaultListModel lm = new DefaultListModel(); - public void addItemPane(Item item) { - if (lm.size() >= Integer.decode(Wrapper.getConfig("itemBufferSize"))) { - while (lm.size() >= Integer.decode(Wrapper.getConfig("itemBufferSize"))) { - lm.remove(0); - } - } - - lm.addElement(item); - jList1.setModel(lm); + public void refreshItemPane() { + Item items[] = Wrapper.getAllItem(); + jList1.setListData(items); jList1.repaint(); } } diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Item.java b/client/trunk/src/com/fourisland/instadisc/Item/Item.java index 143e06f..927101b 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/Item.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/Item.java @@ -19,75 +19,8 @@ import java.util.HashMap; public class Item { HashMap headerMap; - private Integer id; - private String subscription; - private String title; - private String author; - private String url; - private HashMap semantics; - - public Item() { - semantics = new HashMap(); - } - - public Integer getID() { - return id; - } - - public String getSubscription() { - return subscription; - } - - public String getTitle() { - return title; - } - - public String getAuthor() { - return author; - } - - public String getURL() { - return url; - } - - public HashMap getSemantics() { - return semantics; - } - - public void setID(Integer id) { - this.id = id; - } - - public void setSubscription(String subscription) { - this.subscription = subscription; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setAuthor(String author) { - this.author = author; - } - - public void setURL(String url) { - this.url = url; - } - - public void setSemantics(HashMap semantics) { - this.semantics = semantics; - } - - public String getSemantics(String key) { - return semantics.get(key); - } - - public void putSemantics(String key, String value) { - semantics.put(key, value); - } public Item(HashMap headerMap) { - this(); this.headerMap = headerMap; } @@ -97,12 +30,19 @@ public class Item { XmlRpc xmlrpc = new XmlRpc("deleteItem"); xmlrpc.addParam(Integer.decode(headerMap.get("ID"))); xmlrpc.execute(); + + if (Wrapper.countItem() >= Integer.decode(Wrapper.getConfig("itemBufferSize"))) { + while (Wrapper.countItem() >= Integer.decode(Wrapper.getConfig("itemBufferSize"))) { + Wrapper.dropFromTopItem(); + } + } - setID(Integer.decode(headerMap.get("ID"))); - setSubscription(headerMap.get("Subscription")); - setTitle(headerMap.get("Title")); - setAuthor(headerMap.get("Author")); - setURL(headerMap.get("URL")); + com.fourisland.instadisc.Database.Item item = new com.fourisland.instadisc.Database.Item(); + item.setID(Integer.decode(headerMap.get("ID"))); + item.setSubscription(headerMap.get("Subscription")); + item.setTitle(headerMap.get("Title")); + item.setAuthor(headerMap.get("Author")); + item.setURL(headerMap.get("URL")); HashMap temp = new HashMap(headerMap); temp.remove("ID"); @@ -112,9 +52,11 @@ public class Item { temp.remove("Title"); temp.remove("Author"); temp.remove("URL"); - setSemantics(temp); + item.setSemantics(temp); + + Wrapper.addItem(item); - ((InstaDiscView) InstaDiscApp.getApplication().getMainView()).addItemPane(this); + ((InstaDiscView) InstaDiscApp.getApplication().getMainView()).refreshItemPane(); if (SystemTray.isSupported()) { InstaDiscApp.ti.displayMessage("New item recieved!", Wrapper.getSubscription(headerMap.get("Subscription")).getTitle() + ", " + headerMap.get("Title") + " by " + headerMap.get("Author"), MessageType.INFO); diff --git a/client/trunk/src/com/fourisland/instadisc/ManageSubscriptionsForm.java b/client/trunk/src/com/fourisland/instadisc/ManageSubscriptionsForm.java index b1190f4..e6d2a6f 100644 --- a/client/trunk/src/com/fourisland/instadisc/ManageSubscriptionsForm.java +++ b/client/trunk/src/com/fourisland/instadisc/ManageSubscriptionsForm.java @@ -153,11 +153,8 @@ public class ManageSubscriptionsForm extends javax.swing.JDialog { private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables - DefaultListModel lm = new DefaultListModel(); - public void refreshSubscriptionPane() { Subscription[] subscriptions = Wrapper.getAllSubscription(); - System.out.println(Arrays.deepToString(subscriptions)); jList1.setListData(subscriptions); jList1.repaint(); -- cgit 1.4.1