summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/sampler/ActiveLink.java
blob: 252a7b12e0daaf11291a3d0152ecafc7b3df7d3a (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.itmill.toolkit.demo.sampler;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;

import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.terminal.Resource;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.Link;
import com.itmill.toolkit.ui.Button.ClickEvent;
import com.itmill.toolkit.ui.Button.ClickListener;

@SuppressWarnings("serial")
public class ActiveLink extends Link {

    private static final String TAG = "activelink";

    private static final Method LINK_FOLLOWED_METHOD;

    private HashSet listeners = new HashSet();

    public ActiveLink() {
        super();
    }

    public ActiveLink(String caption, Resource resource, String targetName,
            int width, int height, int border) {
        super(caption, resource, targetName, width, height, border);
    }

    public ActiveLink(String caption, Resource resource) {
        super(caption, resource);
    }

    @Override
    public String getTag() {
        return TAG;
    }

    static {
        try {
            LINK_FOLLOWED_METHOD = LinkActivatedListener.class
                    .getDeclaredMethod("linkActivated",
                            new Class[] { LinkActivatedEvent.class });
        } catch (final java.lang.NoSuchMethodException e) {
            // This should never happen
            throw new java.lang.RuntimeException(
                    "Internal error finding methods in ActiveLink");
        }
    }

    /**
     * Adds the link activated listener.
     * 
     * @param listener
     *            the Listener to be added.
     */
    public void addListener(LinkActivatedListener listener) {
        listeners.add(listener);
        addListener(LinkActivatedEvent.class, listener, LINK_FOLLOWED_METHOD);
        if (listeners.size() == 1) {
            requestRepaint();
        }
    }

    /**
     * Removes the link activated listener.
     * 
     * @param listener
     *            the Listener to be removed.
     */
    public void removeListener(ClickListener listener) {
        listeners.remove(listener);
        removeListener(ClickEvent.class, listener, LINK_FOLLOWED_METHOD);
        if (listeners.size() == 0) {
            requestRepaint();
        }
    }

    /**
     * Emits the options change event.
     */
    protected void fireClick(boolean linkOpened) {
        fireEvent(new LinkActivatedEvent(this, linkOpened));
    }

    @Override
    public void paintContent(PaintTarget target) throws PaintException {
        super.paintContent(target);

        if (listeners.size() > 0) {
            target.addVariable(this, "activated", false);
            target.addVariable(this, "opened", false);
        }
    }

    @Override
    public void changeVariables(Object source, Map variables) {
        super.changeVariables(source, variables);
        if (!isReadOnly() && variables.containsKey("activated")) {
            final Boolean activated = (Boolean) variables.get("activated");
            final Boolean opened = (Boolean) variables.get("opened");
            if (activated != null && activated.booleanValue() && !isReadOnly()) {
                fireClick((opened != null && opened.booleanValue() ? true
                        : false));
            }
        }
    }

    public class LinkActivatedEvent extends Component.Event {

        private boolean linkOpened;

        /**
         * New instance of text change event.
         * 
         * @param source
         *            the Source of the event.
         */
        public LinkActivatedEvent(Component source, boolean linkOpened) {
            super(source);
            this.linkOpened = linkOpened;
        }

        /**
         * Gets the ActiveLink where the event occurred.
         * 
         * @return the Source of the event.
         */
        public ActiveLink getActiveLink() {
            return (ActiveLink) getSource();
        }

        /**
         * Indicates whether or not the link was opened on the client, i.e in a
         * new window/tab. If the link was not opened, the listener should react
         * to the event and "do something", otherwise the link does nothing.
         * 
         * @return true if the link was opened on the client
         */
        public boolean isLinkOpened() {
            return linkOpened;
        }
    }

    /**
     * ActiveLink click listener
     */
    public interface LinkActivatedListener extends Serializable {

        /**
         * ActiveLink has been activated.
         * 
         * @param event
         *            ActiveLink click event.
         */
        public void linkActivated(LinkActivatedEvent event);

    }

}