extensions/net.sf.basedb.thumbnails/trunk/src/net/sf/basedb/thumbnails/ThumbnailsView.java

Code
Comments
Other
Rev Date Author Line
3403 16 Jun 15 nicklas 1 package net.sf.basedb.thumbnails;
3403 16 Jun 15 nicklas 2
3409 18 Jun 15 nicklas 3 import java.util.Arrays;
3409 18 Jun 15 nicklas 4 import java.util.Iterator;
3409 18 Jun 15 nicklas 5 import java.util.List;
3409 18 Jun 15 nicklas 6
3409 18 Jun 15 nicklas 7 import net.sf.basedb.clients.web.extensions.DynamicActionAttribute;
3403 16 Jun 15 nicklas 8 import net.sf.basedb.clients.web.extensions.JspContext;
3403 16 Jun 15 nicklas 9 import net.sf.basedb.clients.web.extensions.toolbar.ButtonAction;
3409 18 Jun 15 nicklas 10 import net.sf.basedb.clients.web.extensions.toolbar.ButtonBean;
3403 16 Jun 15 nicklas 11 import net.sf.basedb.core.File;
3403 16 Jun 15 nicklas 12 import net.sf.basedb.util.extensions.ActionFactory;
3403 16 Jun 15 nicklas 13 import net.sf.basedb.util.extensions.InvokationContext;
3403 16 Jun 15 nicklas 14
3403 16 Jun 15 nicklas 15 /**
3403 16 Jun 15 nicklas 16   Action factory for the thumbnails view on the single-item file
3409 18 Jun 15 nicklas 17   view. We hook into the 'toolbar' extension point and create a
3409 18 Jun 15 nicklas 18   "fake" button acting as a proxy for the file information. 
3409 18 Jun 15 nicklas 19   The 'thumbnail.js' script will take care of the rest and
3409 18 Jun 15 nicklas 20   dynamically inject an image onto the file page and hide
3409 18 Jun 15 nicklas 21   the fake button.
3403 16 Jun 15 nicklas 22
3403 16 Jun 15 nicklas 23   @author nicklas
3403 16 Jun 15 nicklas 24   @since 1.0
3403 16 Jun 15 nicklas 25 */
3403 16 Jun 15 nicklas 26 public class ThumbnailsView 
3403 16 Jun 15 nicklas 27   implements ActionFactory<ButtonAction>
3403 16 Jun 15 nicklas 28 {
3403 16 Jun 15 nicklas 29
3403 16 Jun 15 nicklas 30   public ThumbnailsView() 
3403 16 Jun 15 nicklas 31   {}
3403 16 Jun 15 nicklas 32   
3403 16 Jun 15 nicklas 33   @Override
3403 16 Jun 15 nicklas 34   public boolean prepareContext(InvokationContext<? super ButtonAction> context) 
3403 16 Jun 15 nicklas 35   {
3403 16 Jun 15 nicklas 36     JspContext jspContext = (JspContext)context.getClientContext();
3403 16 Jun 15 nicklas 37     Object item = jspContext.getCurrentItem();
3403 16 Jun 15 nicklas 38     if (item instanceof File)
3403 16 Jun 15 nicklas 39     {
3403 16 Jun 15 nicklas 40       File file = (File)item;
3417 24 Jun 15 nicklas 41       if (Thumbnails.getImageLoader(file) != null)
3403 16 Jun 15 nicklas 42       {
3403 16 Jun 15 nicklas 43         String home = jspContext.getHome(context.getExtension());
3403 16 Jun 15 nicklas 44         jspContext.addScript(home + "/thumbnails.js");
3403 16 Jun 15 nicklas 45         jspContext.addStylesheet(home + "/thumbnails.css");
3409 18 Jun 15 nicklas 46         return true;
3403 16 Jun 15 nicklas 47       }
3403 16 Jun 15 nicklas 48     }
3403 16 Jun 15 nicklas 49     return false;
3403 16 Jun 15 nicklas 50   }
3403 16 Jun 15 nicklas 51
3403 16 Jun 15 nicklas 52   @Override
3403 16 Jun 15 nicklas 53   public ButtonAction[] getActions(InvokationContext<? super ButtonAction> context) 
3403 16 Jun 15 nicklas 54   {
3409 18 Jun 15 nicklas 55     File file = (File)context.getClientContext().getCurrentItem();
3409 18 Jun 15 nicklas 56     return new ButtonAction[] { new ThumbnailProxy(file.getId()) } ;
3403 16 Jun 15 nicklas 57   }
3403 16 Jun 15 nicklas 58
3409 18 Jun 15 nicklas 59   /**
3409 18 Jun 15 nicklas 60     Inserts a toolbar button acting as a proxy for the thumbnail.
3409 18 Jun 15 nicklas 61     The thumbnail.js script will inject an image with the information
3409 18 Jun 15 nicklas 62     from this button and then hide the button.
3409 18 Jun 15 nicklas 63     
3409 18 Jun 15 nicklas 64     For this to work, the button must have 'auto-init' in the class
3409 18 Jun 15 nicklas 65     name and 'data-auto-init=thumbnail-proxy'. 'data-file-id' is the
3409 18 Jun 15 nicklas 66     id to the file. Thumbnail size can be set with 'data-size' but
3409 18 Jun 15 nicklas 67     defaults to 'XLARGE'. The target element that should receive the 
3409 18 Jun 15 nicklas 68     image can be set with 'data-target' and defaults to 'main.properties.content'.
3409 18 Jun 15 nicklas 69   */
3409 18 Jun 15 nicklas 70   static class ThumbnailProxy
3409 18 Jun 15 nicklas 71     extends ButtonBean
3409 18 Jun 15 nicklas 72   {
3409 18 Jun 15 nicklas 73
3409 18 Jun 15 nicklas 74     private List<DynamicActionAttribute> attributes;
3409 18 Jun 15 nicklas 75     
3409 18 Jun 15 nicklas 76     ThumbnailProxy(int fileId) 
3409 18 Jun 15 nicklas 77     {
3409 18 Jun 15 nicklas 78       setId("thumbnail-proxy");
3409 18 Jun 15 nicklas 79       setTitle("Thumbnail proxy");
3409 18 Jun 15 nicklas 80       setClazz("auto-init thumbnail-proxy");
3409 18 Jun 15 nicklas 81       setVisible(true);
3409 18 Jun 15 nicklas 82       setEnabled(true);
3409 18 Jun 15 nicklas 83       attributes = Arrays.asList(
3409 18 Jun 15 nicklas 84         new DynamicActionAttribute("data-file-id", Integer.toString(fileId)),
3409 18 Jun 15 nicklas 85         new DynamicActionAttribute("data-auto-init", "thumbnail-proxy")
3409 18 Jun 15 nicklas 86       );
3409 18 Jun 15 nicklas 87     }
3409 18 Jun 15 nicklas 88
3409 18 Jun 15 nicklas 89     @Override
3409 18 Jun 15 nicklas 90     public Iterator<DynamicActionAttribute> getDynamicActionAttributes() 
3409 18 Jun 15 nicklas 91     {
3409 18 Jun 15 nicklas 92       return attributes.iterator();
3409 18 Jun 15 nicklas 93     }
3409 18 Jun 15 nicklas 94     
3409 18 Jun 15 nicklas 95   }  
3409 18 Jun 15 nicklas 96   
3403 16 Jun 15 nicklas 97 }