summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/ui/renderer/ClickableRenderer.java
blob: 851bbf8510a06cd8d2b1268e408ffa96921b69d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright 2000-2014 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.ui.renderer;

import java.lang.reflect.Method;

import com.vaadin.event.ConnectorEventListener;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.AbstractRenderer;
import com.vaadin.ui.Grid.Column;
import com.vaadin.util.ReflectTools;

/**
 * An abstract superclass for Renderers that render clickable items. Click
 * listeners can be added to a renderer to be notified when any of the rendered
 * items is clicked.
 * 
 * @param <T>
 *            the type presented by the renderer
 * 
 * @since 7.4
 * @author Vaadin Ltd
 */
public class ClickableRenderer<T> extends AbstractRenderer<T> {

    /**
     * An interface for listening to {@link RendererClickEvent renderer click
     * events}.
     * 
     * @see {@link ButtonRenderer#addClickListener(RendererClickListener)}
     */
    public interface RendererClickListener extends ConnectorEventListener {

        static final Method CLICK_METHOD = ReflectTools.findMethod(
                RendererClickListener.class, "click", RendererClickEvent.class);

        /**
         * Called when a rendered button is clicked.
         * 
         * @param event
         *            the event representing the click
         */
        void click(RendererClickEvent event);
    }

    /**
     * An event fired when a button rendered by a ButtonRenderer is clicked.
     */
    public static class RendererClickEvent extends ClickEvent {

        private Object itemId;
        private Column column;

        protected RendererClickEvent(Grid source, Object itemId, Column column,
                MouseEventDetails mouseEventDetails) {
            super(source, mouseEventDetails);
            this.itemId = itemId;
            this.column = column;
        }

        /**
         * Returns the item ID of the row where the click event originated.
         * 
         * @return the item ID of the clicked row
         */
        public Object getItemId() {
            return itemId;
        }

        /**
         * Returns the {@link Column} where the click event originated.
         * 
         * @return the column of the click event
         */
        public Column getColumn() {
            return column;
        }
    }

    protected ClickableRenderer(Class<T> presentationType) {
        super(presentationType);
        registerRpc(new RendererClickRpc() {
            @Override
            public void click(String rowKey, String columnId,
                    MouseEventDetails mouseDetails) {
                fireEvent(new RendererClickEvent(getParentGrid(),
                        getItemId(rowKey), getColumn(columnId), mouseDetails));
            }
        });
    }

    /**
     * Adds a click listener to this button renderer. The listener is invoked
     * every time one of the buttons rendered by this renderer is clicked.
     * 
     * @param listener
     *            the click listener to be added
     */
    public void addClickListener(RendererClickListener listener) {
        addListener(RendererClickEvent.class, listener,
                RendererClickListener.CLICK_METHOD);
    }

    /**
     * Removes the given click listener from this renderer.
     * 
     * @param listener
     *            the click listener to be removed
     */
    public void removeClickListener(RendererClickListener listener) {
        removeListener(RendererClickEvent.class, listener);
    }
}