prevent selecting a row when clicking on a link that is inside that row

Having a popup window action bundled with the row click window, I need to put a link into one of the cell in the row.  As a result the left click would trigger the download link as well as the pop up window meaning row is also selected.

 

To prevent this from happening, we need to sink the click event, also prevent the event from propagating.

 

Source code:

 

public class AttachmentCellRenderer implements GridCellRenderer<ModelData>
{
    private UrlHelper.AttachmentType attachmentService = UrlHelper.AttachmentType.DYNO;

    @Override
    public Object render(ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<ModelData> store, Grid<ModelData> grid)
    {
        VerticalPanel content = new VerticalPanel();
        String text = model.get(config.id);
        StringBuilder displayString = new StringBuilder();

        if (text != null)
        {
            Document doc = XMLParser.parse(text);
            Element element = doc.getDocumentElement();
            NodeList nodeList = element.getChildNodes();
            int nodeCount = nodeList.getLength();

            for (int j = 0; j < nodeCount; j++)
            {
                Element attachmentElement = (Element) nodeList.item(j);
                final int attachmentId = Integer.parseInt(String.valueOf(attachmentElement.getAttribute(EventDataAttribute.ATTACHMENT_ID)));
                String fileName = attachmentElement.getAttribute(EventDataAttribute.FILE_NAME);
                if (fileName != null)
                {
                    // Download link name
                    String label = SafeHtmlUtils.htmlEscape(fileName);
                    Anchor downloadLink = new Anchor(label, UrlHelper.getDownloadAttachmentUrl(attachmentService, attachmentId))
                    {
                        @Override
                        public void onBrowserEvent(Event event)
                        {
                            if (event.getTypeInt() == Event.ONMOUSEDOWN || event.getTypeInt() == Event.ONCLICK)
                            {
                                event.stopPropagation();
                            }
                            super.onBrowserEvent(event);
                        }
                    };
                    downloadLink.sinkEvents(Event.ONMOUSEDOWN);
                    downloadLink.setStyleName("Attachment");
                    downloadLink.setTitle(label);
                    downloadLink.setTarget("_blank");

                    content.add(downloadLink);
                }
            }
        }
        return content;
    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s