GridActionRenderer Add-on
Renderer for adding an action column to a Grid.
GridActionRenderer is an add-on for Vaadin 7. It's a renderer for adding an action column to Grid with separate tooltips for each action.
There are two extended Grid versions included for ease of use: ActionGrid for regular actions and DownloadActionGrid for when one or more of the actions should trigger a download. The download functionality uses GridFileDownloader add-on.
It's possible to use the renderer with a vanilla Grid or some other extended Grid version, but in those cases it's still necessary to copy over the functionality from the aforementioned classes, as well as the ActionGridConnector (or tooltips won't work). Neither action handling nor download triggering are trivial additions to Grid, so take care and test extensively if you make modifications to the default behaviour.
I won't be available to make any changes to this add-on during the next four weeks at the very least, but you can find link to the sources in the side bar, and pull requests are always welcome (although I won't be available to resolve those either for the same period).
Probably works on earlier versions as well, but I haven't had time to test yet.
Note: for the DownloadActionGrid to work properly, push or polling needs to be in use.
Sample code
package org.vaadin.anna.gridactionrenderer.demo; import java.util.ArrayList; import java.util.List; import org.vaadin.anna.gridactionrenderer.ActionGrid; import org.vaadin.anna.gridactionrenderer.GridAction; import org.vaadin.anna.gridactionrenderer.GridActionRenderer.GridActionClickEvent; import org.vaadin.anna.gridactionrenderer.GridDownloadAction; import com.vaadin.data.Item; import com.vaadin.server.FontAwesome; import com.vaadin.ui.Notification; import com.vaadin.ui.Notification.Type; public class MyActionGrid extends ActionGrid { public MyActionGrid() { super(createActions()); addStyleName("demogrid"); addColumn("name"); setCaption("Action Grid"); Column column = addColumn("actions"); column.setRenderer(getGridActionRenderer()); for (int i = 0; i < 25; ++i) { Item item = getContainerDataSource().addItem(i); item.getItemProperty("name").setValue("Item" + i); item.getItemProperty("actions").setValue(i % 3 == 0 ? "1,2" : "-1"); } } private static List<GridAction> createActions() { List<GridAction> actions = new ArrayList<GridAction>(); actions.add(new GridAction(FontAwesome.USER, "user")); actions.add(new GridAction(FontAwesome.GEAR, "settings")); actions.add(new GridDownloadAction(FontAwesome.TICKET, "ticket")); return actions; } @Override public void click(GridActionClickEvent event) { Item item = getContainerDataSource().getItem(event.getItemId()); Notification.show(item.getItemProperty("name").getValue() + " - " + event.getAction().getDescription(), Type.ERROR_MESSAGE); } }
package org.vaadin.anna.gridactionrenderer.demo; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.vaadin.anna.gridactionrenderer.DownloadActionGrid; import org.vaadin.anna.gridactionrenderer.GridAction; import org.vaadin.anna.gridactionrenderer.GridActionRenderer.GridActionClickEvent; import org.vaadin.anna.gridactionrenderer.GridDownloadAction; import org.vaadin.gridfiledownloader.GridFileDownloader.GridStreamResource; import com.vaadin.data.Item; import com.vaadin.server.FontAwesome; import com.vaadin.ui.Notification; import com.vaadin.ui.Notification.Type; public class MyDownloadActionGrid extends DownloadActionGrid { @SuppressWarnings("unchecked") public MyDownloadActionGrid() { super(createActions()); addStyleName("demogrid"); addColumn("name"); setCaption("Download Action Grid"); Column column = addColumn("actions"); column.setRenderer(getGridActionRenderer()); for (int i = 0; i < 25; ++i) { Item item = getContainerDataSource().addItem(i); item.getItemProperty("name").setValue("Item" + i); item.getItemProperty("actions").setValue(i % 3 == 0 ? "1,2" : "-1"); } } private static List<GridAction> createActions() { List<GridAction> actions = new ArrayList<GridAction>(); actions.add(new GridAction(FontAwesome.USER, "user")); actions.add(new GridAction(FontAwesome.GEAR, "settings")); actions.add(new GridDownloadAction(FontAwesome.DOWNLOAD, "download")); return actions; } @Override public void click(GridActionClickEvent event) { super.click(event); // must be called if (event.getAction().isDownloadAction()) { return; // already handled in extended method } Item item = getContainerDataSource().getItem(event.getItemId()); Notification.show(item.getItemProperty("name").getValue() + " - " + event.getAction().getDescription(), Type.ERROR_MESSAGE); } @Override public GridStreamResource createStreamResource() { return new GridStreamResource() { @Override public InputStream getStream() { // in real use-case you would find or create the file here based // on the downloadItemId and action (if there are more than one) int writeAtOnce = 1024 * 1024 * 1024; byte[] b = new byte[writeAtOnce]; return new ByteArrayInputStream(b); } @Override public String getFilename() { // in real use-case you would find or create the file here based // on the downloadItemId and action (if there are more than one) GridDownloadAction downloadAction = getDownloadAction(); Item item = getContainerDataSource().getItem( getDownloadItemId()); return "file_" + downloadAction.getDescription() + "_" + item.getItemProperty("name").getValue() + ".txt"; } }; } }
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
- Released
- 2016-08-19
- Maturity
- EXPERIMENTAL
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 7.6+
- Browser
- N/A
GridActionRenderer Add-on - Vaadin Add-on Directory
Renderer for adding an action column to a Grid.Source Code
Discussion Forum