From 2a6cb9fafd7971b31efb331d654e3170b127de76 Mon Sep 17 00:00:00 2001 From: veronie Date: Sun, 24 May 2020 20:40:29 +0200 Subject: [PATCH] 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(); }