001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.acacia.client.ui; 029 030import org.opencms.acacia.client.CmsChoiceMenuEntryBean; 031import org.opencms.acacia.client.css.I_CmsLayoutBundle; 032 033import com.google.gwt.core.client.Scheduler; 034import com.google.gwt.core.client.Scheduler.ScheduledCommand; 035import com.google.gwt.dom.client.Element; 036import com.google.gwt.dom.client.Style; 037import com.google.gwt.dom.client.Style.Unit; 038import com.google.gwt.user.client.Window; 039import com.google.gwt.user.client.ui.Composite; 040import com.google.gwt.user.client.ui.FlowPanel; 041 042/** 043 * A choice submenu widget.<p> 044 */ 045public class CmsChoiceSubmenu extends Composite { 046 047 /** The composite widget. */ 048 private FlowPanel m_root = new FlowPanel(); 049 050 /** 051 * Creates a new submenu.<p> 052 * 053 * @param parentEntry the parent menu entry bean 054 */ 055 public CmsChoiceSubmenu(CmsChoiceMenuEntryBean parentEntry) { 056 057 initWidget(m_root); 058 addStyleName(I_CmsLayoutBundle.INSTANCE.attributeChoice().choices()); 059 addStyleName(I_CmsLayoutBundle.INSTANCE.attributeChoice().submenu()); 060 } 061 062 /** 063 * Adds a new choice widget.<p> 064 * 065 * @param choice the choice widget 066 */ 067 public void addChoice(CmsChoiceMenuEntryWidget choice) { 068 069 m_root.add(choice); 070 } 071 072 /** 073 * Checks whether the submenu should be opened above instead of below.<p> 074 * 075 * @param referenceElement the reference element 076 * @return true if the new submenu should be opened above 077 */ 078 public boolean openAbove(Element referenceElement) { 079 080 int windowTop = Window.getScrollTop(); 081 int windowBottom = Window.getScrollTop() + Window.getClientHeight(); 082 int spaceAbove = referenceElement.getAbsoluteTop() - windowTop; 083 int spaceBelow = windowBottom - referenceElement.getAbsoluteBottom(); 084 return spaceAbove > spaceBelow; 085 } 086 087 /** 088 * Positions a new submenu asynchronously.<p> 089 * 090 * @param widgetEntry the menu entry relative to which the submenu should be positioned 091 */ 092 public void positionDeferred(final CmsChoiceMenuEntryWidget widgetEntry) { 093 094 getElement().getStyle().setPosition(Style.Position.ABSOLUTE); 095 getElement().getStyle().setVisibility(Style.Visibility.HIDDEN); 096 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 097 098 public void execute() { 099 100 positionNextToMenuEntry(widgetEntry); 101 getElement().getStyle().setVisibility(Style.Visibility.VISIBLE); 102 } 103 104 }); 105 } 106 107 /** 108 * Helper method to position a submenu on the left side of a menu entry.<p> 109 * 110 * @param widgetEntry the widget entry relative to which the submenu should be positioned 111 */ 112 protected void positionNextToMenuEntry(final CmsChoiceMenuEntryWidget widgetEntry) { 113 114 Element elem = getElement(); 115 elem.getStyle().setPosition(Style.Position.ABSOLUTE); 116 Element referenceElement = null; 117 int startX = -2000; 118 int startY = -2000; 119 int deltaX = 0; 120 int deltaY = 0; 121 referenceElement = widgetEntry.getElement(); 122 Style style = elem.getStyle(); 123 style.setLeft(startX, Unit.PX); 124 style.setTop(startY, Unit.PX); 125 int myRight = elem.getAbsoluteRight(); 126 int myTop = elem.getAbsoluteTop(); 127 int refLeft = referenceElement.getAbsoluteLeft(); 128 int refTop = referenceElement.getAbsoluteTop(); 129 int newLeft = startX + (refLeft - myRight) + deltaX; 130 int newTop; 131 if (openAbove(referenceElement)) { 132 int myHeight = elem.getOffsetHeight(); 133 int refHeight = referenceElement.getOffsetHeight(); 134 newTop = startY + ((refTop + refHeight) - (myTop + myHeight)) + deltaY; 135 } else { 136 newTop = startY + (refTop - myTop) + deltaY; 137 } 138 style.setLeft(newLeft, Unit.PX); 139 style.setTop(newTop, Unit.PX); 140 } 141 142}