aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/PopupView.java
blob: 73f1e583efb25e811d6f812979bb3aba04e139d7 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.itmill.toolkit.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;

public class PopupView extends AbstractComponentContainer {

    Content itsContent;
    boolean popupVisible;
    ArrayList componentList;

    /* Constructors */

    /**
     * A simple way to create a PopupPanel. Note that the minimal representation
     * may not be dynamically updated, to achieve this create your own Content
     * object.
     * 
     * @param small
     *            the minimal textual representation as HTML
     * @param large
     *            the full, Component-type representation
     */
    public PopupView(final java.lang.String small, final Component large) {
        this(new PopupView.Content() {
            public java.lang.String getMinimizedValueAsHTML() {
                return small;
            }

            public Component getPopupComponent() {
                return large;
            }
        });

    }

    /**
     * Creates a PopupView through the PopupView.Content interface. This allows
     * the creator to dynamically change the contents of the PopupView.
     * 
     * @param content
     *            the PopupView.Content that contains the information for this
     */
    public PopupView(PopupView.Content content) {
        super();
        this.itsContent = content;
        popupVisible = false;
        componentList = new ArrayList(1);
    }

    /**
     * This method will replace the current content of the panel with a new one.
     * Give null to remove current content.
     * 
     * @param newContent
     *            PopupView.Content object containing new information for the
     *            PopupView
     * 
     */
    public void setContent(PopupView.Content content) {
        this.itsContent = content;
        requestRepaint();
    }

    /**
     * Returns the content-package for this PopupView. Returns null if the
     * PopupView has no content.
     * 
     * @return the PopupView.Content for this object or null
     */
    public PopupView.Content getContent() {
        return itsContent;
    }

    /**
     * Return whether the popup is visible.
     * 
     * @return true if the popup is showing
     */
    public boolean getPopupVisibility() {
        return popupVisible;
    }

    /*
     * Methods inherited from AbstractComponentContainer. These are unnecessary
     * (but mandatory). Most of them are not supported in this implementation.
     */

    /**
     * Not supported in this implementation. Will return an empty iterator.
     * 
     * @see com.itmill.toolkit.ui.ComponentContainer#getComponentIterator()
     */
    public Iterator getComponentIterator() {
        return componentList.iterator();

    }

    /**
     * Not supported in this implementation.
     * 
     * @see com.itmill.toolkit.ui.AbstractComponentContainer#removeAllComponents()
     * @throws UnsupportedOperationException
     */
    public void removeAllComponents() {
        throw new UnsupportedOperationException();
    }

    /**
     * In this implementation, moveComponents is not supported. It always throws
     * UnsupportedOperationException.
     * 
     * @see com.itmill.toolkit.ui.AbstractComponentContainer#moveComponentsFrom(com.itmill.toolkit.ui.ComponentContainer)
     * @throws UnsupportedOperationException
     */
    public void moveComponentsFrom(ComponentContainer source)
            throws UnsupportedOperationException {

        throw new UnsupportedOperationException();
    }

    /**
     * Not supported in this implementation.
     * 
     * @see com.itmill.toolkit.ui.AbstractComponentContainer#addComponent(com.itmill.toolkit.ui.Component)
     * @throws UnsupportedOperationException
     */
    public void addComponent(Component c) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();

    }

    /**
     * Not supported in this implementation. Always throws
     * UnsupportedOperationException.
     * 
     * @see com.itmill.toolkit.ui.ComponentContainer#replaceComponent(com.itmill.toolkit.ui.Component,
     *      com.itmill.toolkit.ui.Component)
     * @throws UnsupportedOperationException
     */
    public void replaceComponent(Component oldComponent, Component newComponent)
            throws UnsupportedOperationException {

        throw new UnsupportedOperationException();
    }

    /**
     * Not supported in this implementation
     * 
     * @see com.itmill.toolkit.ui.AbstractComponentContainer#removeComponent(com.itmill.toolkit.ui.Component)
     */
    public void removeComponent(Component c)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();

    }

    /*
     * Methods for server-client communications.
     */

    /**
     * @see com.itmill.toolkit.ui.AbstractComponent#getTag()
     */
    public java.lang.String getTag() {
        return "popupview";
    }

    /**
     * Paint (serialize) the component for the client.
     * 
     * @see com.itmill.toolkit.ui.AbstractComponent#paintContent(com.itmill.toolkit.terminal.PaintTarget)
     */
    public void paintContent(PaintTarget target) throws PaintException {
        // Superclass writes any common attributes in the paint target.
        super.paintContent(target);

        target.addAttribute("html", itsContent.getMinimizedValueAsHTML());
        target.addAttribute("popupVisible", popupVisible);

        if (popupVisible) {
            Component c = itsContent.getPopupComponent();

            target.startTag("popupComponent");
            c.paint(target);
            target.endTag("popupComponent");
        }

    }

    /**
     * Deserialize changes received from client.
     * 
     * @see com.itmill.toolkit.ui.AbstractComponent#changeVariables(java.lang.Object,
     *      java.util.Map)
     */
    public void changeVariables(Object source, Map variables) {
        if (variables.containsKey("popupVisibility")) {

            // TODO we could use boolean allowPopup here to prevent popups from
            // showing

            popupVisible = ((Boolean) variables.get("popupVisibility"))
                    .booleanValue();

            if (popupVisible) {
                Component c = itsContent.getPopupComponent();
                componentList.add(c);
                super.addComponent(c);
            } else if (!componentList.isEmpty()) {
                super.removeComponent((Component) componentList.get(0));
                componentList.clear();
            }
            requestRepaint();
        }
    }

    /**
     * Used to deliver customized content-packages to the PopupView. These are
     * dynamically loaded when they are redrawn.
     */
    public interface Content {

        /**
         * This should return a small view of the full data.
         * 
         * @return value in HTML format
         */
        public String getMinimizedValueAsHTML();

        /**
         * This should return the full Component representing the data
         * 
         * @return a Component for the value
         */
        public Component getPopupComponent();
    }
}