From 2e89f13ddd5b0b1847e5382500d10b09034dda78 Mon Sep 17 00:00:00 2001 From: veronie Date: Sun, 24 May 2020 18:28:03 +0200 Subject: [PATCH 1/2] New Thing class that contains meta data AND details. --- .../src/xyz/veronie/bgg/result/BggApi.java | 28 ++-- .../src/xyz/veronie/bgg/result/Thing.java | 123 ++++++++++-------- .../xyz/veronie/bgg/result/ThingDetails.java | 57 ++++++++ .../xyz/veronie/bgg/result/ThingMetaData.java | 29 +---- .../xyz/veronie/bgg/result/ThingProvider.java | 43 +++--- .../bgg/ui/filters/BggUserSourceFilter.java | 5 +- .../veronie/bgg/ui/parts/BggResultPart.java | 13 +- .../xyz/veronie/bgg/ui/parts/PreparePart.java | 25 ++-- 8 files changed, 185 insertions(+), 138 deletions(-) create mode 100644 xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingDetails.java diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/BggApi.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/BggApi.java index bb898b5..354d428 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/BggApi.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/BggApi.java @@ -48,7 +48,7 @@ public class BggApi { private static int RETRIES = 5; - public ArrayList getThingsForUser(String user) throws IllegalArgumentException { + public ArrayList getThingsForUser(String user) throws IllegalArgumentException { ResultConfig resultConfig = configManager.getResultConfig(); StringBuilder urlStr = new StringBuilder(); @@ -85,7 +85,7 @@ public class BggApi { } - public ArrayList getThingsForGeeklist(int geeklistId) throws IllegalArgumentException { + public ArrayList getThingsForGeeklist(int geeklistId) throws IllegalArgumentException { ResultConfig resultConfig = configManager.getResultConfig(); StringBuilder urlStr = new StringBuilder(); @@ -94,7 +94,7 @@ public class BggApi { return getThings(urlStr.toString()); } - public ArrayList getThingsForFamily(int familyId) throws IllegalArgumentException { + public ArrayList getThingsForFamily(int familyId) throws IllegalArgumentException { ResultConfig resultConfig = configManager.getResultConfig(); StringBuilder urlStr = new StringBuilder(); @@ -108,7 +108,7 @@ public class BggApi { } - private ArrayList getThings(String urlString) throws IllegalArgumentException { + private ArrayList getThings(String urlString) throws IllegalArgumentException { System.out.println("URL: " + urlString); try { @@ -141,7 +141,8 @@ public class BggApi { // do something with the content System.out.println(content.toString()); - ArrayList output = parseThingMetas(content.toString()); + ArrayList output = parseThingMetas(content.toString()); + return output; } @@ -165,8 +166,8 @@ public class BggApi { - private ArrayList parseThingMetas(String content) throws IllegalArgumentException { - ArrayList metas = new ArrayList(); + private ArrayList parseThingMetas(String content) throws IllegalArgumentException { + ArrayList things = new ArrayList(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { @@ -201,7 +202,8 @@ public class BggApi { getValue(eElement, "comment"), Integer.parseInt(getValue(eElement, "numplays")) ); - metas.add(tmd); + Thing thing = new Thing(id, tmd); + things.add(thing); } } // family has "type" @@ -233,7 +235,8 @@ public class BggApi { id, eLink.getAttribute("value"), "", "", "", null); - metas.add(tmd); + Thing thing = new Thing(id, tmd); + things.add(thing); } } } @@ -268,16 +271,17 @@ public class BggApi { Integer id = Integer.parseInt(eElement.getAttribute("objectid")); if(id != 0) { ThingMetaData tmd = new ThingMetaData( - Integer.parseInt(eElement.getAttribute("objectid")), + id, eElement.getAttribute("objectname"), "", "", "", null); - metas.add(tmd); + Thing thing = new Thing(id, tmd); + things.add(thing); } } } } - return metas; + return things; } catch (ParserConfigurationException e) { e.printStackTrace(); diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java index 721df46..c20c304 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java @@ -1,57 +1,66 @@ -package xyz.veronie.bgg.result; - -public class Thing { - Integer id; - String name; - String designer; - String publisher; - String artist; - Integer yearpublished; - Integer minplayers; - Integer maxplayers; - Integer playingtime; - Integer minplaytime; - Integer maxplaytime; - Integer age; - Integer usersrated; - Integer average; - Float bayesaverage; - Integer rank; - Integer rank_wg; - Integer numcomments; - Integer numweights; - Float averageweight; - Float stddev; - Float median; - Boolean owned; - Boolean trading; - Boolean wanting; - Boolean wishing; - Float userrating; - String image; - Integer category; - Integer mechanic; - String comment; - Integer[] players; // 1 to 20 - String description; - // exp, - Integer basegameId; - Integer reimplementId; - String reimplement_name; - Integer reimplementedById; - String reimplementedByName; - Integer containsId; - String containsName; - Integer iscontained; - String iscontained_name; - Integer integration; - String integration_name; - Integer numplays; - Float price; - Float userweight; - Integer wishpriority; - Integer expansions; - String domain; - String family; - Float age_poll; -} +package xyz.veronie.bgg.result; + +public class Thing { + + private int id; + private ThingMetaData metaData; + private ThingDetails details; + + private static String[] titles = { "Id", "Name" }; + + + public Thing(int id, ThingMetaData metaData) { + this.id = id; + this.metaData = metaData; + } + + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public ThingMetaData getMetaData() { + return metaData; + } + public void setMetaData(ThingMetaData metaData) { + this.metaData = metaData; + } + public ThingDetails getDetails() { + return details; + } + public void setDetails(ThingDetails details) { + this.details = details; + } + + + public static String[] getTitles() { + return titles; + } + + /// get field at idx, order of titles + public String getField(int idx) { + if(idx >= titles.length) { + + } + + String returnStr; + + switch(idx) { + case 0: + returnStr = String.valueOf(this.id); + break; + case 1: + returnStr = this.getMetaData().getName(); + break; + default: + throw new ArrayIndexOutOfBoundsException( + "idx " + String.valueOf(idx) + " must be in [0,1]"); + } + + return returnStr; + } + + +} diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingDetails.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingDetails.java new file mode 100644 index 0000000..c064164 --- /dev/null +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingDetails.java @@ -0,0 +1,57 @@ +package xyz.veronie.bgg.result; + +public class ThingDetails { + Integer id; + String name; + String designer; + String publisher; + String artist; + Integer yearpublished; + Integer minplayers; + Integer maxplayers; + Integer playingtime; + Integer minplaytime; + Integer maxplaytime; + Integer age; + Integer usersrated; + Integer average; + Float bayesaverage; + Integer rank; + Integer rank_wg; + Integer numcomments; + Integer numweights; + Float averageweight; + Float stddev; + Float median; + Boolean owned; + Boolean trading; + Boolean wanting; + Boolean wishing; + Float userrating; + String image; + Integer category; + Integer mechanic; + String comment; + Integer[] players; // 1 to 20 + String description; + // exp, + Integer basegameId; + Integer reimplementId; + String reimplement_name; + Integer reimplementedById; + String reimplementedByName; + Integer containsId; + String containsName; + Integer iscontained; + String iscontained_name; + Integer integration; + String integration_name; + Integer numplays; + Float price; + Float userweight; + Integer wishpriority; + Integer expansions; + String domain; + String family; + Float age_poll; +} diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java index 17b3ffc..18f4aa5 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java @@ -14,7 +14,6 @@ public class ThingMetaData implements java.io.Serializable { private String comment; private Integer numPlays; - private static String[] titles = { "Id", "Name" }; private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); @@ -29,9 +28,7 @@ public class ThingMetaData implements java.io.Serializable { this.setNumPlays(numPlays); } - public static String[] getTitles() { - return titles; - } + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { @@ -42,29 +39,7 @@ public class ThingMetaData implements java.io.Serializable { propertyChangeSupport.removePropertyChangeListener(listener); } - /// get field at idx, order of titles - public String getField(int idx) { - if(idx >= titles.length) { - - } - - String returnStr; - - switch(idx) { - case 0: - returnStr = String.valueOf(this.id); - break; - case 1: - returnStr = this.name; - break; - default: - throw new ArrayIndexOutOfBoundsException( - "idx " + String.valueOf(idx) + " must be in [0,1]"); - } - - return returnStr; - } - + public int getId() { return id; diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java index 436d79a..e2991f2 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; -import xyz.veronie.bgg.result.ThingMetaData; import org.eclipse.core.runtime.Path; @@ -16,54 +15,54 @@ public enum ThingProvider { INSTANCE; /// list of things. Each ID is expected to exist exactly once. - private List thingMetas; + private List things; private ThingProvider() { - thingMetas = new ArrayList(); + things = new ArrayList(); } /// replace current list with new list - public void replaceThingMetas(ArrayList metas) { - this.thingMetas = metas; + public void replaceThings(ArrayList things) { + this.things = things; } /// add things from argument to current list iff their ID does not exist yet - public void addThingMetas(ArrayList metas) { - for(ThingMetaData tmd : metas) { + public void addThings(ArrayList things) { + for(Thing tmd : things) { Boolean exists = false; - for(ThingMetaData thisTmd : this.thingMetas) { + for(Thing thisTmd : this.things) { if(tmd.getId() == thisTmd.getId()) { exists = true; } } if(!exists) { - this.thingMetas.add(tmd); + this.things.add(tmd); } } } // helper function for subtractThingMetas - private static Predicate thingEqual(final ThingMetaData other) + private static Predicate thingEqual(final Thing other) { return p -> p.getId() == other.getId(); } /// remove the things in the argument from the current list of things (using their ID) - public void subtractThingMetas(ArrayList metas) { - for(ThingMetaData tmd : metas) { - this.thingMetas.removeIf(thingEqual(tmd)); + public void subtractThings(ArrayList things) { + for(Thing thing : things) { + this.things.removeIf(thingEqual(thing)); } } /// keep only things that exist in both argument list and current list (by ID) - public void intersectThingMetas(ArrayList metas) { - List cpMetas = new ArrayList(this.thingMetas); - this.thingMetas.clear(); - for(ThingMetaData tmd : metas) { - for(ThingMetaData thisTmd : cpMetas) { - if(tmd.getId() == thisTmd.getId()) { - this.thingMetas.add(tmd); + public void intersectThings(ArrayList things) { + List cpThings = new ArrayList(this.things); + this.things.clear(); + for(Thing thing : things) { + for(Thing thisThing : cpThings) { + if(thing.getId() == thisThing.getId()) { + this.things.add(thing); } } } @@ -90,8 +89,8 @@ public enum ThingProvider { // return the current list of ids - public List getThingMetas() { - return thingMetas; + public List getThings() { + return things; } // @Inject diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/filters/BggUserSourceFilter.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/filters/BggUserSourceFilter.java index 66c83b4..692768a 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/filters/BggUserSourceFilter.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/filters/BggUserSourceFilter.java @@ -58,7 +58,6 @@ public class BggUserSourceFilter { Label lblUser = new Label(mainCompo, SWT.LEFT); lblUser.setText("User name: "); lblUser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); - // // Define field assists for the text widget // // use "." and " " activate the content proposals // char[] autoActivationCharacters = new char[] { '.', ' ' }; @@ -73,7 +72,9 @@ public class BggUserSourceFilter { // } cbUserName = new Combo(mainCompo, SWT.DROP_DOWN); - cbUserName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); + GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false); + gd.widthHint = 100; + cbUserName.setLayoutData(gd); cbUserName.setText(configManager.getResultConfig().user); cbUserName.addSelectionListener(new SelectionAdapter() { public void widgetDefaultSelected(SelectionEvent e) { diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java index 4bae887..f791b72 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java @@ -23,6 +23,7 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; +import xyz.veronie.bgg.result.Thing; import xyz.veronie.bgg.result.ThingMetaData; import xyz.veronie.bgg.result.ThingProvider; import xyz.veronie.bgg.types.EventConstants; @@ -62,7 +63,7 @@ public class BggResultPart { viewer.setContentProvider(new ArrayContentProvider()); // Get the content for the viewer, setInput will call getElements in the // contentProvider - viewer.setInput(ThingProvider.INSTANCE.getThingMetas()); + viewer.setInput(ThingProvider.INSTANCE.getThings()); // make the selection available to other views // TODO: getSite().setSelectionProvider(viewer); // Set the sorter for the table @@ -83,7 +84,7 @@ public class BggResultPart { // This will create the columns for the table private void createColumns(final Composite parent, final TableViewer viewer) { - String[] titles = ThingMetaData.getTitles(); + String[] titles = Thing.getTitles(); int[] bounds = { 100, 500 }; // , 100, 100, 100, 100 }; for(int i = 0; i < titles.length; ++i) { @@ -91,7 +92,7 @@ public class BggResultPart { col.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { - ThingMetaData t = (ThingMetaData) element; + Thing t = (Thing) element; return t.getField((int)col.getColumn().getData()); } }); @@ -125,11 +126,11 @@ public class BggResultPart { (@UIEventTopic(EventConstants.TOPIC_RESULT_CHANGED) String empty) { System.out.println("TOPIC_RESULT_CHANGED"); - List thingMetas = ThingProvider.INSTANCE.getThingMetas(); - viewer.setInput(thingMetas); + List things = ThingProvider.INSTANCE.getThings(); + viewer.setInput(things); viewer.refresh(); - statsLabel.setText(Integer.toString(thingMetas.size()) + " items"); + statsLabel.setText(Integer.toString(things.size()) + " items"); statsLabel.redraw(); } diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/PreparePart.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/PreparePart.java index 84bebe4..a1f5313 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/PreparePart.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/PreparePart.java @@ -30,6 +30,7 @@ import org.eclipse.swt.widgets.Label; import xyz.veronie.bgg.result.BggApi; import xyz.veronie.bgg.result.ResultConfig; import xyz.veronie.bgg.result.ResultConfigManager; +import xyz.veronie.bgg.result.Thing; import xyz.veronie.bgg.result.ThingMetaData; import xyz.veronie.bgg.result.ThingProvider; import xyz.veronie.bgg.types.EventConstants; @@ -152,8 +153,8 @@ public class PreparePart { } else { eventBroker.send(EventConstants.TOPIC_STATUS, "Fetching " + selection.getFirstElement().toString() + " '" + user + "'..."); try { - ArrayList thingMetas = bggApi.getThingsForUser(user); - useThingsOnResult(thingMetas); + ArrayList things = bggApi.getThingsForUser(user); + useThingsOnResult(things); } catch(IllegalArgumentException ex) { MessageDialog.openError(mainComposite.getShell(), "", ex.getMessage()); @@ -168,8 +169,8 @@ public class PreparePart { } else { eventBroker.send(EventConstants.TOPIC_STATUS, "Fetching for geeklist id '" + geeklistId + "'"); try { - ArrayList thingMetas = bggApi.getThingsForGeeklist(geeklistId); - useThingsOnResult(thingMetas); + ArrayList things = bggApi.getThingsForGeeklist(geeklistId); + useThingsOnResult(things); } catch(IllegalArgumentException ex) { MessageDialog.openError(mainComposite.getShell(), "", ex.getMessage()); @@ -183,8 +184,8 @@ public class PreparePart { } else { eventBroker.send(EventConstants.TOPIC_STATUS, "Fetching for family id '" + familyId + "'"); try { - ArrayList thingMetas = bggApi.getThingsForFamily(familyId); - useThingsOnResult(thingMetas); + ArrayList things = bggApi.getThingsForFamily(familyId); + useThingsOnResult(things); } catch(IllegalArgumentException ex) { MessageDialog.openError(mainComposite.getShell(), "", ex.getMessage()); @@ -193,23 +194,23 @@ public class PreparePart { } } - private void useThingsOnResult(ArrayList thingMetas) { + private void useThingsOnResult(ArrayList things) { switch(configManager.getResultConfig().action) { case REP: - ThingProvider.INSTANCE.replaceThingMetas(thingMetas); + ThingProvider.INSTANCE.replaceThings(things); break; case ADD: - ThingProvider.INSTANCE.addThingMetas(thingMetas); + ThingProvider.INSTANCE.addThings(things); break; case SUB: - ThingProvider.INSTANCE.subtractThingMetas(thingMetas); + ThingProvider.INSTANCE.subtractThings(things); break; case AND: - ThingProvider.INSTANCE.intersectThingMetas(thingMetas); + ThingProvider.INSTANCE.intersectThings(things); break; } eventBroker.send(EventConstants.TOPIC_RESULT_CHANGED, ""); - eventBroker.send(EventConstants.TOPIC_STATUS, "Fetched " + Integer.toString(thingMetas.size()) + " things."); + eventBroker.send(EventConstants.TOPIC_STATUS, "Fetched " + Integer.toString(things.size()) + " things."); } }); From 2a6cb9fafd7971b31efb331d654e3170b127de76 Mon Sep 17 00:00:00 2001 From: veronie Date: Sun, 24 May 2020 20:40:29 +0200 Subject: [PATCH 2/2] Adding thumbnail images to thing table. --- .../src/xyz/veronie/bgg/result/Thing.java | 76 ++++++++++++++++--- .../xyz/veronie/bgg/result/ThingMetaData.java | 2 +- .../xyz/veronie/bgg/result/ThingProvider.java | 13 +--- .../xyz/veronie/bgg/ui/helpers/Constants.java | 6 ++ .../xyz/veronie/bgg/ui/helpers/Resources.java | 68 +++++++++++++++++ .../veronie/bgg/ui/parts/BggResultPart.java | 55 ++++++++++---- 6 files changed, 184 insertions(+), 36 deletions(-) create mode 100644 xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Constants.java create mode 100644 xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Resources.java diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java index c20c304..5f16a45 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/Thing.java @@ -1,13 +1,36 @@ package xyz.veronie.bgg.result; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageLoader; + +import xyz.veronie.bgg.ui.helpers.Constants; +import xyz.veronie.bgg.ui.helpers.Resources; + public class Thing { private int id; private ThingMetaData metaData; private ThingDetails details; - private static String[] titles = { "Id", "Name" }; - + // cache + private Image thumbImage; + + public static final String IdHeader = "ID"; + public static final String ThumbHeader = ""; + public static final String NameHeader = "Name"; + public static final String DetailsHeader = "Details"; + public static final String ExportFlagHeader = "Export"; public Thing(int id, ThingMetaData metaData) { this.id = id; @@ -34,16 +57,42 @@ public class Thing { this.details = details; } + public Image getThumbnail() { + if(thumbImage == null) { + getThumbImage(id); + } + return thumbImage; + } - public static String[] getTitles() { - return titles; + private void getThumbImage(int thingId) { + Path tmpDir = Resources.INSTANCE.getTmpDir(); + if(tmpDir != null) { + Path imageFile = Paths.get(Resources.INSTANCE.getThumbsDir().toString() + + File.separator + String.valueOf(thingId)); + + if(Files.exists(imageFile, LinkOption.NOFOLLOW_LINKS)) { + thumbImage = Resources.INSTANCE.getResourceManager().createImage( + ImageDescriptor.createFromFile(Thing.class, imageFile.toString())); + } else { + try { + ImageDescriptor descriptor = ImageDescriptor.createFromURL( + new URL(metaData.getThumbURL())); + thumbImage = descriptor.createImage(); + + ImageLoader saver = new ImageLoader(); + saver.data = new ImageData[] { thumbImage.getImageData() }; + saver.save(imageFile.toString(), SWT.IMAGE_PNG); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + } } - /// get field at idx, order of titles + + /// get field at idx, order of titles public String getField(int idx) { - if(idx >= titles.length) { - - } String returnStr; @@ -52,11 +101,20 @@ public class Thing { returnStr = String.valueOf(this.id); break; case 1: + returnStr = ""; + break; + case 2: + returnStr = this.getMetaData().getName(); + break; + case 3: + returnStr = (this.getDetails() != null) ? "yes" : "no"; + break; + case 4: returnStr = this.getMetaData().getName(); break; default: throw new ArrayIndexOutOfBoundsException( - "idx " + String.valueOf(idx) + " must be in [0,1]"); + "idx " + String.valueOf(idx) + " must be in [0,4]"); } return returnStr; diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java index 18f4aa5..578ad1c 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingMetaData.java @@ -77,7 +77,7 @@ public class ThingMetaData implements java.io.Serializable { propertyChangeSupport.firePropertyChange("thumb", this.thumbURL, this.thumbURL = thumbURL); } - + public int getNumPlays() { return numPlays; } diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java index e2991f2..520367b 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/result/ThingProvider.java @@ -30,8 +30,8 @@ public enum ThingProvider { public void addThings(ArrayList things) { for(Thing tmd : things) { Boolean exists = false; - for(Thing thisTmd : this.things) { - if(tmd.getId() == thisTmd.getId()) { + for(Thing thisThing : this.things) { + if(tmd.getId() == thisThing.getId()) { exists = true; } } @@ -93,13 +93,4 @@ public enum ThingProvider { return things; } -// @Inject -// @Optional -// private void subscribeTopicTagResult -// (@UIEventTopic(EventConstants.TOPIC_TAG_RESULT) -// String empty) { -// System.out.println("TOPIC_TAG_RESULT: Tag result now."); -// this.tagResult(); -// } - } diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Constants.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Constants.java new file mode 100644 index 0000000..fbb7f22 --- /dev/null +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Constants.java @@ -0,0 +1,6 @@ +package xyz.veronie.bgg.ui.helpers; + +public class Constants { + public static final String TMP_PREFIX = "BggToolAnother"; + public static final String THUMB_CACHE_DIR = "thumbs"; +} diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Resources.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Resources.java new file mode 100644 index 0000000..1c686a8 --- /dev/null +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/helpers/Resources.java @@ -0,0 +1,68 @@ +package xyz.veronie.bgg.ui.helpers; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; + +public enum Resources { + INSTANCE; + + private Path prefixPath; + private Path tmpDir; + private LocalResourceManager resourceManager; + + private Resources() { + + try { + if(prefixPath == null) { + tmpDir = Paths.get(Constants.TMP_PREFIX); + if(!Files.exists(tmpDir)) { + Files.createDirectory(tmpDir); + } + } else { + tmpDir = Paths.get(prefixPath + File.separator + Constants.TMP_PREFIX); + if(!Files.exists(tmpDir)) { + Files.createDirectory(tmpDir); + } + } + Path thumbPath = getThumbsDir(); + if(!Files.exists(thumbPath)) { + Files.createDirectory(thumbPath); + } + System.out.println("tmp dir: " + tmpDir.toAbsolutePath()); + } + catch(IOException ex) { + ex.printStackTrace(); + } + + resourceManager = new LocalResourceManager(JFaceResources.getResources()); + } + + public Path getThumbsDir() { + return Paths.get(tmpDir + File.separator + Constants.THUMB_CACHE_DIR); + } + + public Path getTmpDir() { + return tmpDir; + } + + public void dispose() { + try { + Files.delete(tmpDir); + } catch (IOException e) { + e.printStackTrace(); + } + getResourceManager().dispose(); + } + + + + public LocalResourceManager getResourceManager() { + return resourceManager; + } +} diff --git a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java index f791b72..6f52e12 100644 --- a/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java +++ b/xyz.veronie.bgg.ui/src/xyz/veronie/bgg/ui/parts/BggResultPart.java @@ -15,6 +15,7 @@ import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -84,19 +85,44 @@ public class BggResultPart { // This will create the columns for the table private void createColumns(final Composite parent, final TableViewer viewer) { - String[] titles = Thing.getTitles(); - int[] bounds = { 100, 500 }; // , 100, 100, 100, 100 }; - - for(int i = 0; i < titles.length; ++i) { - TableViewerColumn col = createTableViewerColumn(titles[i], bounds[i], i); - col.setLabelProvider(new ColumnLabelProvider() { - @Override - public String getText(Object element) { - Thing t = (Thing) element; - return t.getField((int)col.getColumn().getData()); - } - }); - } + + TableViewerColumn col = createTableViewerColumn(Thing.IdHeader, 100, 0); + col.setLabelProvider(new ColumnLabelProvider() { + @Override + public String getText(Object element) { + Thing t = (Thing) element; + return t.getField((int)col.getColumn().getData()); + } + }); + + TableViewerColumn col2 = createTableViewerColumn(Thing.ThumbHeader, 50, 1); + col2.setLabelProvider(new ColumnLabelProvider() { + @Override + public Image getImage(Object element) { + Thing t = (Thing) element; + Image img = t.getThumbnail(); + if(img != null) { + return img; + } else { + return null; + } + } + + @Override + public String getText(Object element) { + return ""; + } + + }); + + TableViewerColumn col3 = createTableViewerColumn(Thing.NameHeader, 400, 2); + col3.setLabelProvider(new ColumnLabelProvider() { + @Override + public String getText(Object element) { + Thing t = (Thing) element; + return t.getField((int)col3.getColumn().getData()); + } + }); } private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) { @@ -128,8 +154,7 @@ public class BggResultPart { System.out.println("TOPIC_RESULT_CHANGED"); List things = ThingProvider.INSTANCE.getThings(); viewer.setInput(things); - viewer.refresh(); - + viewer.refresh(true); statsLabel.setText(Integer.toString(things.size()) + " items"); statsLabel.redraw(); }