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