View Javadoc

1   /**
2    * This file Copyright (c) 2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.editor.client.widget.button;
35  
36  import java.util.Arrays;
37  import java.util.List;
38  
39  import com.google.gwt.event.dom.client.ClickEvent;
40  import com.google.gwt.event.dom.client.ClickHandler;
41  import com.google.gwt.user.client.ui.Button;
42  import com.google.gwt.user.client.ui.MenuBar;
43  import com.google.gwt.user.client.ui.MenuItem;
44  import com.google.gwt.user.client.ui.PopupPanel;
45  
46  /**
47   * A button which, when clicking on it, will display a dropdown menu just beneath. The menu can have different mutually exclusive choices.
48   * Each choice corresponds to a {@link MenuItem}. The order in which these options are added in the menu bar is the same in which
49   * they are in the list or array passed in to the object's constructor. A menu item can also accept a further {@link MenuBar} as a parameter, thus enabling sub menus.
50   * <p>Usage sample:
51   * <pre>
52   *  ...
53   *  // commands are defined elsewhere
54   *  MenuItem one = new MenuItem("Option one", true, optionCommandOne);
55   *  MenuItem two = new MenuItem("Option two", true, optionCommandTwo);
56   *  MenuItem three = new MenuItem("Option three", true, optionCommandThree);
57   *
58   *  List&lt;MenuItem&gt; options = new ArrayList&lt;MenuItem&gt;();
59   *  options.add(one);
60   *  options.add(two);
61   *  options.add(three);
62   *
63   *  DropdownButton dropdown = new DropdownButton("My cool caption", options);
64   *  ...
65   * </pre>
66   *
67   * @version $Id$
68   */
69  public class DropdownButton extends Button {
70  
71      private final PopupPanel dropdownPanel = new PopupPanel(true);
72      private final MenuBar dropdownMenuBar = new MenuBar(true);
73  
74      public DropdownButton(String caption, List<MenuItem> menuItems) {
75          super(caption);
76          if(menuItems == null) {
77              throw new IllegalArgumentException("menuItems cannot be null");
78          }
79          dropdownMenuBar.setStylePrimaryName("mgnlPreviewMenuDropdown");
80          for(MenuItem item: menuItems) {
81              item.setStylePrimaryName("mgnlPreviewMenuItem");
82              dropdownMenuBar.addItem(item);
83          }
84          dropdownPanel.setStylePrimaryName("mgnlPreviewMenuPanel");
85          //TODO add an onMouseOut event to the menu? This however will make it disappear also when hovering back on the button itself.
86          dropdownPanel.add(dropdownMenuBar);
87  
88          addClickHandler(new ClickHandler() {
89              @Override
90              public void onClick(ClickEvent event) {
91                 onClickCallback(event);
92              }
93          });
94      }
95  
96      public DropdownButton(String caption, MenuItem... menuItems) {
97          this(caption, Arrays.asList(menuItems));
98      }
99  
100     /**
101      * Sets the dropdown's position relative to the browser's client area.
102      * @param left in px
103      * @param top in px
104      */
105     protected void setDropdownPosition(int left, int top) {
106         dropdownPanel.setPopupPosition(left, top);
107     }
108 
109     protected void showDropdown() {
110         dropdownMenuBar.setVisible(true);
111         dropdownPanel.show();
112     }
113 
114     protected void hideDropdown() {
115         dropdownMenuBar.setVisible(false);
116         dropdownPanel.hide();
117     }
118 
119     /**
120      * Callback method invoked when clicking on the drop-down button. By default the menu will appear just beneath the button, left aligned with it.
121      */
122     protected void onClickCallback(ClickEvent event) {
123         setDropdownPosition(getAbsoluteLeft(), getAbsoluteTop() + getOffsetHeight());
124         showDropdown();
125     }
126 }