001
002package org.opencms.ui.shared;
003
004import java.io.Serializable;
005import java.util.ArrayList;
006import java.util.HashSet;
007import java.util.List;
008import java.util.Set;
009
010import com.vaadin.shared.AbstractComponentState;
011
012/**
013 * The context menu state.<p>
014 */
015public class CmsContextMenuState extends AbstractComponentState {
016
017    /**
018     * The menu item state.<p>
019     */
020    public static class ContextMenuItemState implements Serializable {
021
022        /** The serial version id. */
023        private static final long serialVersionUID = 3836772122928080543L;
024
025        /** The caption. */
026        private String m_caption;
027
028        /** The item children. */
029        private List<ContextMenuItemState> m_children;
030
031        /** The description, used as tooltip. */
032        private String m_description;
033
034        /** The enabled flag. */
035        private boolean m_enabled = true;
036
037        /** The item id. */
038        private String m_id;
039
040        /** The separator flag. */
041        private boolean m_separator;
042
043        /** The styles. */
044        private Set<String> m_styles;
045
046        /**
047         * Constructor.<p>
048         */
049        public ContextMenuItemState() {
050
051            m_children = new ArrayList<CmsContextMenuState.ContextMenuItemState>();
052            m_styles = new HashSet<String>();
053        }
054
055        /**
056         * Adds a child item.<p>
057         *
058         * @param caption the caption
059         * @param id the id
060         *
061         * @return the child item state
062         */
063        public ContextMenuItemState addChild(String caption, String id) {
064
065            ContextMenuItemState child = new ContextMenuItemState();
066            child.setCaption(caption);
067            child.m_id = id;
068
069            m_children.add(child);
070
071            return child;
072        }
073
074        /**
075         * @see java.lang.Object#equals(java.lang.Object)
076         */
077        @Override
078        public boolean equals(Object obj) {
079
080            if (this == obj) {
081                return true;
082            }
083
084            if (obj instanceof ContextMenuItemState) {
085                return m_id.equals(((ContextMenuItemState)obj).m_id);
086            }
087
088            return false;
089        }
090
091        /**
092         * Returns the caption.<p>
093         *
094         * @return the caption
095         */
096        public String getCaption() {
097
098            return m_caption;
099        }
100
101        /**
102         * Returns the child items.<p>
103         *
104         * @return the child items
105         */
106        public List<ContextMenuItemState> getChildren() {
107
108            return m_children;
109        }
110
111        /**
112         * Returns the description.<p>
113         *
114         * @return the description
115         */
116        public String getDescription() {
117
118            return m_description;
119        }
120
121        /**
122         * Returns the id.<p>
123         *
124         * @return the id
125         */
126        public String getId() {
127
128            return m_id;
129        }
130
131        /**
132         * Returns the styles.<p>
133         *
134         * @return the styles
135         */
136        public Set<String> getStyles() {
137
138            return m_styles;
139        }
140
141        /**
142         * @see java.lang.Object#hashCode()
143         */
144        @Override
145        public int hashCode() {
146
147            return m_id.hashCode();
148        }
149
150        /**
151         * Returns whether the item is enabled.<p>
152         *
153         * @return <code>true</code> if the item is enabled
154         */
155        public boolean isEnabled() {
156
157            return m_enabled;
158        }
159
160        /**
161         * Returns whether a separator should be displayed.<p>
162         *
163         * @return <code>true</code> if a separator should be displayed
164         */
165        public boolean isSeparator() {
166
167            return m_separator;
168        }
169
170        /**
171         * Removes the given child.<p>
172         *
173         * @param child the child to remove
174         */
175        public void removeChild(ContextMenuItemState child) {
176
177            m_children.remove(child);
178        }
179
180        /**
181         * Sets the caption.<p>
182         *
183         * @param caption the caption
184         */
185        public void setCaption(String caption) {
186
187            m_caption = caption;
188        }
189
190        /**
191         * Sets the child items.<p>
192         *
193         * @param children the children
194         */
195        public void setChildren(List<ContextMenuItemState> children) {
196
197            m_children = children;
198        }
199
200        /**
201         * Sets the description.<p>
202         *
203         * @param description the description to set
204         */
205        public void setDescription(String description) {
206
207            m_description = description;
208        }
209
210        /**
211         * Sets the item enabled.<p>
212         *
213         * @param enabled <code>true</code> to enable the item
214         */
215        public void setEnabled(boolean enabled) {
216
217            m_enabled = enabled;
218        }
219
220        /**
221         * Sets the id.<p>
222         *
223         * @param id the id to set
224         */
225        public void setId(String id) {
226
227            m_id = id;
228        }
229
230        /**
231         * Sets whether a separator should be displayed.<p>
232         *
233         * @param separator <code>true</code> if a separator should be displayed
234         */
235        public void setSeparator(boolean separator) {
236
237            m_separator = separator;
238        }
239
240        /**
241         * Sets the styles.<p>
242         *
243         * @param styleNames the styles
244         */
245        public void setStyles(Set<String> styleNames) {
246
247            m_styles = styleNames;
248        }
249    }
250
251    /** The serial version id. */
252    private static final long serialVersionUID = -247856391284942254L;
253
254    /** The hides automatically flag. */
255    private boolean m_hideAutomatically;
256
257    /** The opens automatically flag. */
258    private boolean m_openAutomatically;
259
260    /** The root items. */
261    private List<ContextMenuItemState> m_rootItems;
262
263    /**
264     * Constructor.<p>
265     */
266    public CmsContextMenuState() {
267
268        m_rootItems = new ArrayList<CmsContextMenuState.ContextMenuItemState>();
269    }
270
271    /**
272     * Adds a child item.<p>
273     *
274     * @param itemCaption the caption
275     * @param itemId the id
276     *
277     * @return the item state
278     */
279    public ContextMenuItemState addChild(String itemCaption, String itemId) {
280
281        ContextMenuItemState rootItem = new ContextMenuItemState();
282        rootItem.setCaption(itemCaption);
283        rootItem.setId(itemId);
284
285        m_rootItems.add(rootItem);
286
287        return rootItem;
288    }
289
290    /**
291     * Returns the root items.<p>
292     *
293     * @return the root items
294     */
295    public List<ContextMenuItemState> getRootItems() {
296
297        return m_rootItems;
298    }
299
300    /**
301     * Returns whether the menu is set to hide automatically.<p>
302     *
303     * @return <code>true</code> if context menu is hidden automatically
304     */
305    public boolean isHideAutomatically() {
306
307        return m_hideAutomatically;
308    }
309
310    /**
311     * Returns whether the menu is set to open automatically.<p>
312     *
313     * @return <code>true</code> if open automatically is on. If open automatically is on, it
314     *         means that context menu will always be opened when it's host
315     *         component is right clicked. If automatic opening is turned off,
316     *         context menu will only open when server side open(x, y) is
317     *         called.
318     */
319    public boolean isOpenAutomatically() {
320
321        return m_openAutomatically;
322    }
323
324    /**
325     * Enables or disables automatic hiding of context menu.<p>
326     *
327     * @param hideAutomatically the hide automatically flag
328     */
329    public void setHideAutomatically(boolean hideAutomatically) {
330
331        this.m_hideAutomatically = hideAutomatically;
332    }
333
334    /**
335     * Enables or disables open automatically feature. If open automatically is
336     * on, it means that context menu will always be opened when it's host
337     * component is right clicked. If automatic opening is turned off, context
338     * menu will only open when server side open(x, y) is called.<p>
339     *
340     * @param openAutomatically the open automatically flag
341     */
342    public void setOpenAutomatically(boolean openAutomatically) {
343
344        this.m_openAutomatically = openAutomatically;
345    }
346
347    /**
348     * Sets the root items.<p>
349     *
350     * @param rootItems the root items
351     */
352    public void setRootItems(List<ContextMenuItemState> rootItems) {
353
354        this.m_rootItems = rootItems;
355    }
356}