/* * Smart GWT (GWT for SmartClient) * Copyright 2008 and beyond, Isomorphic Software, Inc. * * Smart GWT is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version 3 * as published by the Free Software Foundation. Smart GWT is also * available under typical commercial license terms - see * http://smartclient.com/license * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.util.Arrays; import com.smartgwt.client.util.SC; import com.smartgwt.client.types.Overflow; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.Visibility; import com.smartgwt.client.types.LayoutPolicy; import com.smartgwt.client.data.RecordList; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.Button; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.AdaptWidthByCustomizer; import com.smartgwt.client.widgets.menu.Menu; import com.smartgwt.client.widgets.menu.MenuItem; import com.smartgwt.client.widgets.menu.MenuButton; import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.SpinnerItem; import com.smartgwt.client.widgets.events.ClickEvent; import com.smartgwt.client.widgets.events.ClickHandler; import com.smartgwt.client.widgets.events.DrawHandler; import com.smartgwt.client.widgets.events.DrawEvent; import com.smartgwt.client.widgets.toolbar.ToolStrip; import com.smartgwt.client.widgets.toolbar.ToolStripButton; import com.smartgwt.client.widgets.toolbar.ToolStripSeparator; import com.google.gwt.core.client.EntryPoint; public class InlinedMenuSample implements EntryPoint { public class InlinedMenu extends HLayout { Menu menu; MenuButton menuButton; Integer minimalWidth; RecordList inlinedItems = new RecordList(); int inlinedCount, inlinedMax; private ToolStripButton createMenuItem(final String title) { ToolStripButton item = new ToolStripButton(title); item.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { SC.say(title); } }); item.setWidth(1); item.setWrap(false); item.setVisibility(Visibility.HIDDEN); return item; } private void initialize() { // add buttons to represent inlined menu items MenuItem[] items = menu.getItems(); for (int i = 0; i < items.length; i++) { addMember(createMenuItem(items[i].getTitle())); } inlinedMax = getMembers().length; // add a menu button to show non-inlined items menuButton = new MenuButton(null, menu); menuButton.setWidth(1); menuButton.setOverflow(Overflow.VISIBLE); addMember(menuButton); } // get width of the next item to be inlined, by drawing it if needed private int getNextInlinedItemWidth() { Canvas item = getMembers()[inlinedCount]; if (!item.isDrawn()) item.draw(); boolean isLast = inlinedCount == inlinedMax - 1; return item.getVisibleWidth() + (isLast ? -minimalWidth : 0); } // add an inlined item - hide menu button if appropriate private void addInlinedItem() { inlinedItems.addAt(menu.getDataAsRecordList().removeAt(0), 0); if (menu.getTotalRows() == 0) menuButton.hide(); getMembers()[inlinedCount++].show(); } // remove an inlined item - show menu button if appropriate private void removeInlinedItem() { if (menu.getTotalRows() == 0) menuButton.show(); menu.getDataAsRecordList().addAt(inlinedItems.removeAt(0), 0); getMembers()[--inlinedCount].hide(); } public InlinedMenu(Menu menu) { setCanAdaptWidth(true); setOverflow(Overflow.HIDDEN); setDefaultLayoutAlign(Alignment.CENTER); setAdaptWidthByCustomizer(new AdaptWidthByCustomizer() { @Override public int adaptWidthBy(int pixelDifference, int unadaptedWidth) { // set the minimal width if (minimalWidth == null) { minimalWidth = menuButton.getVisibleWidth(); } // all non-hidden children are drawn; expected width is sum of their widths int expectedWidth = 0; for (Canvas member : getMembers()) { if (member.getVisibility() == Visibility.HIDDEN) continue; expectedWidth += member.getVisibleWidth(); } // calculate desired width based on overflow/surplus and unadapted width; // if desired width differs from the expected, add/remove inlined items int desiredWidth = unadaptedWidth + pixelDifference; if (desiredWidth < expectedWidth) { // remove inlined items if we have an overflow while (inlinedCount > 0 && expectedWidth > desiredWidth) { removeInlinedItem(); expectedWidth -= getNextInlinedItemWidth(); } } else if (desiredWidth > expectedWidth) { int deltaX; // add inlined items if we have surplus space while (inlinedCount < inlinedMax && expectedWidth + (deltaX = getNextInlinedItemWidth()) <= desiredWidth) { addInlinedItem(); expectedWidth += deltaX; } } return expectedWidth - unadaptedWidth; } }); this.menu = menu; initialize(); } } private MenuItem createMenuItem(final String title) { MenuItem item = new MenuItem(title); item.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() { @Override public void onClick(MenuItemClickEvent event) { SC.say(title); } }); return item; } public void onModuleLoad() { final Label variableName = new Label(); variableName.setWidth(1); variableName.setWrap(false); Menu menu = new Menu(); menu.setItems(new MenuItem[] { createMenuItem("Contact"), createMenuItem("Hire Now"), createMenuItem("View Résumé") }); ToolStrip toolStrip = new ToolStrip(); toolStrip.setWidth(375); toolStrip.setMembersMargin(5); toolStrip.setLayoutLeftMargin(10); toolStrip.setShowResizeBar(true); toolStrip.setMembers(variableName, new ToolStripSeparator(), new InlinedMenu(menu)); HLayout outerLayout = new HLayout(); outerLayout.setHeight(30); outerLayout.setWidth100(); outerLayout.setHPolicy(LayoutPolicy.NONE); outerLayout.setMembers(toolStrip); final Button toggleName; toggleName = new Button(); toggleName.addClickHandler(new ClickHandler() { boolean longName = true; @Override public void onClick(ClickEvent event) { longName = !longName; String name = longName ? "Alejandro O'Reilly" : "Lucy Lu"; variableName.setContents("
Candidate: " + name + "
"); toggleName.setTitle(longName ? "Shorter Name" : "Longer Name"); } }); toggleName.setTop(50); toggleName.fireEvent(new ClickEvent(null){}); // viewpanel canvas Canvas canvas = new Canvas(); canvas.addChild(outerLayout); canvas.addChild(toggleName); canvas.draw(); } }