Search in sources :

Example 1 with WMenu

use of com.github.bordertech.wcomponents.WMenu in project wcomponents by BorderTech.

the class MenuBarExample method buildMenuBar.

/**
 * Builds up a menu bar for inclusion in the example.
 *
 * @param selectedMenuText the WText to display the selected menu item.
 * @return a menu bar for the example.
 */
private WMenu buildMenuBar(final WText selectedMenuText) {
    WMenu menu = new WMenu();
    // The Colours menu just shows simple text
    WSubMenu colourMenu = new WSubMenu("Colours");
    colourMenu.setMode(WSubMenu.MenuMode.LAZY);
    colourMenu.setAccessKey('C');
    addMenuItem(colourMenu, "Red", selectedMenuText);
    addMenuItem(colourMenu, "Green", selectedMenuText);
    addMenuItem(colourMenu, "Blue", selectedMenuText);
    colourMenu.addSeparator();
    colourMenu.add(new WMenuItem("Disable colour menu", new ToggleDisabledAction(colourMenu)));
    menu.add(colourMenu);
    // The Shapes menu shows grouping of items
    WSubMenu shapeMenu = new WSubMenu("Shapes");
    shapeMenu.setAccessKey('S');
    addMenuItem(shapeMenu, "Circle", selectedMenuText);
    WMenuItemGroup triangleGroup = new WMenuItemGroup("Triangles");
    shapeMenu.add(triangleGroup);
    shapeMenu.setMode(WSubMenu.MenuMode.DYNAMIC);
    addMenuItem(triangleGroup, "Equilateral", selectedMenuText);
    addMenuItem(triangleGroup, "Isosceles", selectedMenuText);
    addMenuItem(triangleGroup, "Scalene", selectedMenuText);
    addMenuItem(triangleGroup, "Right-angled", selectedMenuText);
    addMenuItem(triangleGroup, "Obtuse", selectedMenuText);
    WMenuItemGroup quadGroup = new WMenuItemGroup("Quadrilaterals");
    shapeMenu.add(quadGroup);
    addMenuItem(quadGroup, "Square", selectedMenuText);
    addMenuItem(quadGroup, "Rectangle", selectedMenuText);
    addMenuItem(quadGroup, "Rhombus", selectedMenuText);
    addMenuItem(quadGroup, "Trapezoid", selectedMenuText);
    addMenuItem(quadGroup, "Parallelogram", selectedMenuText);
    shapeMenu.addSeparator();
    shapeMenu.add(new WMenuItem("Disable shape menu", new ToggleDisabledAction(shapeMenu)));
    menu.add(shapeMenu);
    // The Image menu shows use of decorated labels and images
    WDecoratedLabel imageLabel = new WDecoratedLabel(new WImage("/image/wrench.png", "spanner"), new WText("Images"), null);
    WSubMenu imageMenu = new WSubMenu(imageLabel);
    imageMenu.add(createImageMenuItem("/image/flag.png", "Flag", "eg-menu-image-1", selectedMenuText));
    imageMenu.add(createImageMenuItem("/image/attachment.png", "Attachment", "eg-menu-image-2", selectedMenuText));
    imageMenu.add(createImageMenuItem("/image/settings.png", "Settings", "eg-menu-image-3", selectedMenuText));
    imageMenu.addSeparator();
    imageMenu.add(new WMenuItem("Disable image menu", new ToggleDisabledAction(imageMenu)));
    menu.add(imageMenu);
    WSubMenu sitesMenu = new WSubMenu("External apps");
    sitesMenu.add(new WMenuItem("External website", "http://www.example.com/"));
    WMenuItem google = new WMenuItem("Example (new window)", "http://www.example.com/");
    google.setTargetWindow("exampleWindow");
    sitesMenu.add(google);
    menu.add(sitesMenu);
    // Add an item to toggle the states of all the menus
    menu.add(new WMenuItem("Toggle top-level menus", new ToggleDisabledAction(colourMenu, shapeMenu, imageMenu, sitesMenu)));
    menu.add(new WMenuItem("Link", "http://www.example.com"));
    menu.add(new WMenuItem("No Action"));
    WMenuItem itemWithIcon = new WMenuItem("Help");
    itemWithIcon.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
        // do something
        }
    });
    itemWithIcon.setHtmlClass(HtmlClassProperties.ICON_HELP_BEFORE);
    menu.add(itemWithIcon);
    WMenuItem itemWithImage = new WMenuItem(new WDecoratedLabel(new WImage("/image/home.png", "home"), new WText("Home"), null));
    menu.add(itemWithImage);
    menu.add(new WMenuItem(new WDecoratedLabel(new WImage("/image/settings.png", "settings"), new WText("Settings Menu"), null)));
    itemWithImage = new WMenuItem(new WDecoratedLabel(new WImage("/image/flag.png", "flag")));
    itemWithImage.setAccessibleText("flag this view");
    menu.add(itemWithImage);
    return menu;
}
Also used : Action(com.github.bordertech.wcomponents.Action) WSubMenu(com.github.bordertech.wcomponents.WSubMenu) WMenuItem(com.github.bordertech.wcomponents.WMenuItem) WMenuItemGroup(com.github.bordertech.wcomponents.WMenuItemGroup) WText(com.github.bordertech.wcomponents.WText) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) WMenu(com.github.bordertech.wcomponents.WMenu) WImage(com.github.bordertech.wcomponents.WImage) WDecoratedLabel(com.github.bordertech.wcomponents.WDecoratedLabel)

Example 2 with WMenu

use of com.github.bordertech.wcomponents.WMenu in project wcomponents by BorderTech.

the class TreeMenuExample method buildTreeMenu.

/**
 * Builds up a tree menu for inclusion in the example.
 *
 * @param selectedMenuText the WText to display the selected menu item.
 * @return a tree menu for the example.
 */
private WMenu buildTreeMenu(final WText selectedMenuText) {
    WMenu menu = new WMenu(WMenu.MenuType.TREE);
    menu.setSelectMode(SelectMode.SINGLE);
    mapTreeHierarchy(menu, createExampleHierarchy(), selectedMenuText);
    return menu;
}
Also used : WMenu(com.github.bordertech.wcomponents.WMenu)

Example 3 with WMenu

use of com.github.bordertech.wcomponents.WMenu in project wcomponents by BorderTech.

the class TreeMenuExample method mapTreeHierarchy.

/**
 * Recursively maps a tree hierarchy to a hierarchical menu.
 *
 * @param currentComponent the current component in the menu.
 * @param currentNode the current node in the tree.
 * @param selectedMenuText the WText to display the selected menu item.
 */
private void mapTreeHierarchy(final WComponent currentComponent, final StringTreeNode currentNode, final WText selectedMenuText) {
    if (currentNode.isLeaf()) {
        WMenuItem menuItem = new WMenuItem(currentNode.getData(), new ExampleMenuAction(selectedMenuText));
        menuItem.setActionObject(currentNode.getData());
        if (currentComponent instanceof WMenu) {
            ((WMenu) currentComponent).add(menuItem);
        } else {
            ((WSubMenu) currentComponent).add(menuItem);
        }
    } else {
        WSubMenu subMenu = new WSubMenu(currentNode.getData());
        subMenu.setSelectMode(SelectMode.SINGLE);
        if (currentComponent instanceof WMenu) {
            ((WMenu) currentComponent).add(subMenu);
        } else {
            ((WSubMenu) currentComponent).add(subMenu);
        }
        // Expand the first couple of levels in the tree by default.
        if (currentNode.getLevel() < 2) {
            subMenu.setOpen(true);
        }
        for (Iterator<TreeNode> i = currentNode.children(); i.hasNext(); ) {
            mapTreeHierarchy(subMenu, (StringTreeNode) i.next(), selectedMenuText);
        }
    }
}
Also used : WMenuItem(com.github.bordertech.wcomponents.WMenuItem) WSubMenu(com.github.bordertech.wcomponents.WSubMenu) TreeNode(com.github.bordertech.wcomponents.util.TreeNode) WMenu(com.github.bordertech.wcomponents.WMenu)

Example 4 with WMenu

use of com.github.bordertech.wcomponents.WMenu in project wcomponents by BorderTech.

the class WContentExample method addContentRow.

/**
 * Adds components to the given container which demonstrate various ways of acessing the given content.
 *
 * @param contentDesc the description of the content, used to label the controls.
 * @param contentAccess the content which will be displayed.
 * @param target the container to add the UI controls to.
 */
private void addContentRow(final String contentDesc, final ContentAccess contentAccess, final MutableContainer target) {
    // Demonstrate WButton + WContent, round trip
    WButton button = new WButton(contentDesc);
    final WContent buttonContent = new WContent();
    button.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            buttonContent.setContentAccess(contentAccess);
            buttonContent.display();
        }
    });
    WContainer buttonCell = new WContainer();
    buttonCell.add(buttonContent);
    buttonCell.add(button);
    target.add(buttonCell);
    // Demonstrate WButton + WContent, using AJAX
    WButton ajaxButton = new WButton(contentDesc);
    final WContent ajaxContent = new WContent();
    ajaxButton.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            ajaxContent.setContentAccess(contentAccess);
            ajaxContent.display();
        }
    });
    WContainer ajaxCell = new WContainer();
    // The WContent must be wrapped in an AJAX targetable container
    WPanel ajaxContentPanel = new WPanel();
    ajaxContentPanel.add(ajaxContent);
    ajaxCell.add(ajaxButton);
    ajaxCell.add(ajaxContentPanel);
    ajaxButton.setAjaxTarget(ajaxContentPanel);
    target.add(ajaxCell);
    // Demonstrate WContentLink - new window
    WContentLink contentLinkNewWindow = new WContentLink(contentDesc) {

        @Override
        protected void preparePaintComponent(final Request request) {
            super.preparePaintComponent(request);
            setContentAccess(contentAccess);
        }
    };
    target.add(contentLinkNewWindow);
    // Demonstrate WContentLink - prompt to save
    WContentLink contentLinkPromptToSave = new WContentLink(contentDesc) {

        @Override
        protected void preparePaintComponent(final Request request) {
            super.preparePaintComponent(request);
            setContentAccess(contentAccess);
        }
    };
    contentLinkPromptToSave.setDisplayMode(DisplayMode.PROMPT_TO_SAVE);
    target.add(contentLinkPromptToSave);
    // Demonstrate WContentLink - inline
    WContentLink contentLinkInline = new WContentLink(contentDesc) {

        @Override
        protected void preparePaintComponent(final Request request) {
            super.preparePaintComponent(request);
            setContentAccess(contentAccess);
        }
    };
    contentLinkInline.setDisplayMode(DisplayMode.DISPLAY_INLINE);
    target.add(contentLinkInline);
    // Demonstrate targeting of content via a URL
    WMenu menu = new WMenu(WMenu.MenuType.FLYOUT);
    final WContent menuContent = new WContent();
    menuContent.setDisplayMode(DisplayMode.PROMPT_TO_SAVE);
    WMenuItem menuItem = new WMenuItem(contentDesc) {

        @Override
        protected void preparePaintComponent(final Request request) {
            super.preparePaintComponent(request);
            menuContent.setContentAccess(contentAccess);
            setUrl(menuContent.getUrl());
        }
    };
    menu.add(menuItem);
    WContainer menuCell = new WContainer();
    menuCell.add(menuContent);
    menuCell.add(menu);
    target.add(menuCell);
}
Also used : Action(com.github.bordertech.wcomponents.Action) WContainer(com.github.bordertech.wcomponents.WContainer) WMenuItem(com.github.bordertech.wcomponents.WMenuItem) WContent(com.github.bordertech.wcomponents.WContent) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) WPanel(com.github.bordertech.wcomponents.WPanel) Request(com.github.bordertech.wcomponents.Request) WMenu(com.github.bordertech.wcomponents.WMenu) WButton(com.github.bordertech.wcomponents.WButton) WContentLink(com.github.bordertech.wcomponents.WContentLink)

Example 5 with WMenu

use of com.github.bordertech.wcomponents.WMenu in project wcomponents by BorderTech.

the class WMenuRenderer method doRender.

/**
 * Paints the given WMenu.
 *
 * @param component the WMenu to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WMenu menu = (WMenu) component;
    XmlStringBuilder xml = renderContext.getWriter();
    int rows = menu.getRows();
    xml.appendTagOpen("ui:menu");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    switch(menu.getType()) {
        case BAR:
            xml.appendAttribute("type", "bar");
            break;
        case FLYOUT:
            xml.appendAttribute("type", "flyout");
            break;
        case TREE:
            xml.appendAttribute("type", "tree");
            break;
        case COLUMN:
            xml.appendAttribute("type", "column");
            break;
        default:
            throw new IllegalStateException("Invalid menu type: " + menu.getType());
    }
    xml.appendOptionalAttribute("disabled", menu.isDisabled(), "true");
    xml.appendOptionalAttribute("hidden", menu.isHidden(), "true");
    xml.appendOptionalAttribute("rows", rows > 0, rows);
    switch(menu.getSelectionMode()) {
        case NONE:
            break;
        case SINGLE:
            xml.appendAttribute("selectMode", "single");
            break;
        case MULTIPLE:
            xml.appendAttribute("selectMode", "multiple");
            break;
        default:
            throw new IllegalStateException("Invalid select mode: " + menu.getSelectMode());
    }
    xml.appendClose();
    // Render margin
    MarginRendererUtil.renderMargin(menu, renderContext);
    paintChildren(menu, renderContext);
    xml.appendEndTag("ui:menu");
}
Also used : WMenu(com.github.bordertech.wcomponents.WMenu) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Aggregations

WMenu (com.github.bordertech.wcomponents.WMenu)28 WMenuItem (com.github.bordertech.wcomponents.WMenuItem)23 Test (org.junit.Test)16 WSubMenu (com.github.bordertech.wcomponents.WSubMenu)8 Action (com.github.bordertech.wcomponents.Action)4 ActionEvent (com.github.bordertech.wcomponents.ActionEvent)4 WDecoratedLabel (com.github.bordertech.wcomponents.WDecoratedLabel)2 WImage (com.github.bordertech.wcomponents.WImage)2 WMenuItemGroup (com.github.bordertech.wcomponents.WMenuItemGroup)2 WText (com.github.bordertech.wcomponents.WText)2 TreeNode (com.github.bordertech.wcomponents.util.TreeNode)2 Margin (com.github.bordertech.wcomponents.Margin)1 Request (com.github.bordertech.wcomponents.Request)1 TestAction (com.github.bordertech.wcomponents.TestAction)1 WButton (com.github.bordertech.wcomponents.WButton)1 WContainer (com.github.bordertech.wcomponents.WContainer)1 WContent (com.github.bordertech.wcomponents.WContent)1 WContentLink (com.github.bordertech.wcomponents.WContentLink)1 WPanel (com.github.bordertech.wcomponents.WPanel)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1