| @@ -1,13 +1,36 @@ | |||||
| package xyz.veronie.bgg.result; | 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 { | public class Thing { | ||||
| private int id; | private int id; | ||||
| private ThingMetaData metaData; | private ThingMetaData metaData; | ||||
| private ThingDetails details; | 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) { | public Thing(int id, ThingMetaData metaData) { | ||||
| this.id = id; | this.id = id; | ||||
| @@ -34,16 +57,42 @@ public class Thing { | |||||
| this.details = details; | 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) { | public String getField(int idx) { | ||||
| if(idx >= titles.length) { | |||||
| } | |||||
| String returnStr; | String returnStr; | ||||
| @@ -52,11 +101,20 @@ public class Thing { | |||||
| returnStr = String.valueOf(this.id); | returnStr = String.valueOf(this.id); | ||||
| break; | break; | ||||
| case 1: | 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(); | returnStr = this.getMetaData().getName(); | ||||
| break; | break; | ||||
| default: | default: | ||||
| throw new ArrayIndexOutOfBoundsException( | throw new ArrayIndexOutOfBoundsException( | ||||
| "idx " + String.valueOf(idx) + " must be in [0,1]"); | |||||
| "idx " + String.valueOf(idx) + " must be in [0,4]"); | |||||
| } | } | ||||
| return returnStr; | return returnStr; | ||||
| @@ -77,7 +77,7 @@ public class ThingMetaData implements java.io.Serializable { | |||||
| propertyChangeSupport.firePropertyChange("thumb", this.thumbURL, | propertyChangeSupport.firePropertyChange("thumb", this.thumbURL, | ||||
| this.thumbURL = thumbURL); | this.thumbURL = thumbURL); | ||||
| } | } | ||||
| public int getNumPlays() { | public int getNumPlays() { | ||||
| return numPlays; | return numPlays; | ||||
| } | } | ||||
| @@ -30,8 +30,8 @@ public enum ThingProvider { | |||||
| public void addThings(ArrayList<Thing> things) { | public void addThings(ArrayList<Thing> things) { | ||||
| for(Thing tmd : things) { | for(Thing tmd : things) { | ||||
| Boolean exists = false; | 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; | exists = true; | ||||
| } | } | ||||
| } | } | ||||
| @@ -93,13 +93,4 @@ public enum ThingProvider { | |||||
| return things; | 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(); | |||||
| // } | |||||
| } | } | ||||
| @@ -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"; | |||||
| } | |||||
| @@ -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; | |||||
| } | |||||
| } | |||||
| @@ -15,6 +15,7 @@ import org.eclipse.jface.viewers.TableViewerColumn; | |||||
| import org.eclipse.swt.SWT; | import org.eclipse.swt.SWT; | ||||
| import org.eclipse.swt.events.SelectionAdapter; | import org.eclipse.swt.events.SelectionAdapter; | ||||
| import org.eclipse.swt.events.SelectionEvent; | import org.eclipse.swt.events.SelectionEvent; | ||||
| import org.eclipse.swt.graphics.Image; | |||||
| import org.eclipse.swt.layout.GridData; | import org.eclipse.swt.layout.GridData; | ||||
| import org.eclipse.swt.layout.GridLayout; | import org.eclipse.swt.layout.GridLayout; | ||||
| import org.eclipse.swt.widgets.Button; | import org.eclipse.swt.widgets.Button; | ||||
| @@ -84,19 +85,44 @@ public class BggResultPart { | |||||
| // This will create the columns for the table | // This will create the columns for the table | ||||
| private void createColumns(final Composite parent, final TableViewer viewer) { | 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) { | private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) { | ||||
| @@ -128,8 +154,7 @@ public class BggResultPart { | |||||
| System.out.println("TOPIC_RESULT_CHANGED"); | System.out.println("TOPIC_RESULT_CHANGED"); | ||||
| List<Thing> things = ThingProvider.INSTANCE.getThings(); | List<Thing> things = ThingProvider.INSTANCE.getThings(); | ||||
| viewer.setInput(things); | viewer.setInput(things); | ||||
| viewer.refresh(); | |||||
| viewer.refresh(true); | |||||
| statsLabel.setText(Integer.toString(things.size()) + " items"); | statsLabel.setText(Integer.toString(things.size()) + " items"); | ||||
| statsLabel.redraw(); | statsLabel.redraw(); | ||||
| } | } | ||||