org.wings.plaf.css.MenuCG Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package org.wings.plaf.css;
import org.wings.*;
import org.wings.io.Device;
import org.wings.plaf.Update;
import org.wings.util.KeystrokeUtil;
import java.awt.event.ActionListener;
import java.io.IOException;
public final class MenuCG extends org.wings.plaf.css.MenuItemCG implements
org.wings.plaf.MenuCG {
private static final long serialVersionUID = 1L;
private SResourceIcon arrowIcon = new SResourceIcon("org/wings/icons/MenuArrowRight.gif");
{
arrowIcon.getId();
}
private SResourceIcon arrowIconDisabled = new SResourceIcon("org/wings/icons/MenuArrowRight_Disabled.gif");
{
arrowIconDisabled.getId();
}
@Override
public void installCG(final SComponent comp) {
super.installCG(comp);
}
@Override
public void uninstallCG(final SComponent comp) {
}
@Override
public void writePopup(final Device device, SMenu menu)
throws IOException {
if (menu.isEnabled()) {
device.print("");
for (int i = 0; i < menu.getMenuComponentCount(); i++) {
SComponent menuItem = menu.getMenuComponent(i);
if (menuItem.isVisible()) {
device.print("\n - ");
if (menuItem instanceof SMenuItem) {
device.print("");
}
menuItem.write(device);
if (menuItem instanceof SMenuItem) {
device.print("");
}
if (menuItem.isEnabled() && menuItem instanceof SMenu) {
menuItem.putClientProperty("popup", Boolean.TRUE);
menuItem.write(device);
menuItem.putClientProperty("popup", null);
}
device.print("
");
}
}
device.print("
");
}
device.print("\n");
}
/* (non-Javadoc)
* @see org.wings.plaf.css.MenuCG#printScriptHandlers(org.wings.io.Device, org.wings.SComponent)
*/
protected static void printScriptHandlers(Device device, SComponent menuItem, String handler) throws IOException {
// Print the script handlers if this is a SMenu OR if the parent has both, items and menus as childs.
// In the latter case a menu item might need to close an open submenu from a menu on the same level.
SMenuItem tMenuItem = (SMenuItem) menuItem;
if (!(tMenuItem instanceof SMenu)) {
if (tMenuItem.getParentMenu() != null && tMenuItem.getParentMenu() instanceof SMenu) {
SMenu tParentMenu = (SMenu) tMenuItem.getParentMenu();
boolean tHasMenuChild = false;
boolean tHasMenuItemChild = false;
for (int tChildIndex = 0; tChildIndex < tParentMenu.getMenuComponentCount(); tChildIndex++) {
SComponent tChild = tParentMenu.getChild(tChildIndex);
if (tChild instanceof SMenu) {
tHasMenuChild = true;
} else {
tHasMenuItemChild = true;
}
}
// No handler if not both types are present
if (!(tHasMenuChild && tHasMenuItemChild)) {
return;
}
}
}
device.print(" ").print(handler).print("=\"wpm_openMenu(event, '");
device.print(tMenuItem.getName());
device.print("_pop','");
device.print(tMenuItem.getParentMenu().getName());
device.print("_pop');\"");
}
/* (non-Javadoc)
* @see org.wings.plaf.css.MenuCG#writeListAttributes(org.wings.io.Device, org.wings.SMenu)
*/
protected static void writeListAttributes(Device device, SMenu menu) throws IOException {
// calculate max length of children texts for sizing of layer
int maxLength = 0;
for (int i = 0; i < menu.getMenuComponentCount(); i++) {
if (!(menu.getMenuComponent(i) instanceof SMenuItem))
continue;
int textLength = 0;
String text = ((SMenuItem) menu.getMenuComponent(i)).getText();
if ( text != null )
textLength = Double.valueOf(text.length()*menu.getWidthScaleFactor()).intValue();
if ( ((SMenuItem) menu.getMenuComponent(i)).getAccelerator() != null )
textLength = textLength + Double.valueOf(KeystrokeUtil.keyStroke2String(((SMenuItem) menu.getMenuComponent(i)).getAccelerator()).length() * ((SMenuItem) menu.getMenuComponent(i)).getAcceleratorWidthScaleFactor() ).intValue();
if (textLength > maxLength) {
maxLength = textLength;
if (menu.getMenuComponent(i) instanceof SMenu) {
maxLength = maxLength + 2; //graphics
}
}
}
device.print(" style=\"width:");
device.print(maxLength);
device.print("em;\"");
device.print(" id=\"");
device.print(menu.getName());
device.print("_pop\"");
}
@Override
public void writeInternal(final Device device, final SComponent _c)
throws IOException {
SMenu menu = (SMenu) _c;
if (menu.getClientProperty("popup") == null)
writeItemContent(device, menu);
else
writePopup(device, menu);
}
@Override
public Update getComponentUpdate(SComponent component) {
SComponent parentMenu = ((SMenu) component).getParentMenu();
if (parentMenu != null)
return parentMenu.getCG().getComponentUpdate(parentMenu);
else
return new ComponentUpdate(this, component);
}
protected static class ComponentUpdate extends AbstractComponentCG.ComponentUpdate {
public ComponentUpdate(AbstractComponentCG cg, SComponent component) {
super(cg, component);
}
@Override
public Handler getHandler() {
component.putClientProperty("popup", Boolean.TRUE);
UpdateHandler handler = (UpdateHandler) super.getHandler();
component.putClientProperty("popup", null);
handler.setName("componentMenu");
handler.setParameter(0, component.getName() + "_pop");
return handler;
}
}
}