if (getShell() == null || getShell().isDisposed())
{
Display.getDefault().removeFilter(SWT.KeyDown, this);
return;
}
KeyEvent ke = new KeyEvent(e);
if (ke.keyCode == SWT.ALT || ke.keyCode == SWT.F10)
{
if (ke.keyCode == SWT.ALT) altKeyDown = true;
nonAltKeyDown = false;
}
else
{
int ch = ke.keyCode;
if (ch == 0) ch = ke.character;
String pattern = "&" + (char) ch;
if ((altKeyDown == true || trackItemIndex > -1) && popupMenu == null)
{
boolean flag = false;
for (int i = 0; i < menu.getItemCount(); i++)
{
if (menu.getItem(i).getText().toLowerCase().indexOf(
pattern.toLowerCase()) > -1)
{
setSelection(i, true);
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
if (popupMenu != null) popupMenu.setSelection(0);
}
});
flag = true;
break;
}
}
if (!flag && ch < 256)
{
trackItemIndex = -1;
refresh();
altKeyDown = false;
nonAltKeyDown = true;
return;
}
}
else if (popupMenu != null)
{
if (popupMenu.getCurrentMenu() != null) popupMenu.getCurrentMenu()
.dealAltKeyEvent(ke);
else
popupMenu.dealAltKeyEvent(ke);
}
nonAltKeyDown = true;
}
if (ke.keyCode == KEY_RIGHT && trackItemIndex > -1)
{
if (popupMenu == null || popupMenu.getCurrentMenu() == null
|| popupMenu.getCurrentMenu().isSubMenuEnd())
{
refresh();
int index = drawCommands.size();
if (chevronStartItem != null) index = menu.indexOf(chevronStartItem) + 1;
if (++trackItemIndex == index) trackItemIndex = 0;
if (!selected)
{
refresh();
drawCommand(trackItemIndex, true);
}
else
{
if (popupMenu != null)
{
popupMenu.hideMenu();
popupMenu = null;
}
refresh();
drawCommand(trackItemIndex, true);
if (!isShowMenu() && selected)
{
final MenuDrawCommand command = (MenuDrawCommand) drawCommands
.get(trackItemIndex);
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
showMenu(command);
}
});
}
return;
}
}
else
{
popupMenu.getCurrentMenu().subMenuSelected();
}
}
if (ke.keyCode == KEY_LEFT && trackItemIndex > -1)
{
if (popupMenu == null || popupMenu.getCurrentMenu() == null
|| popupMenu.getCurrentMenu() == popupMenu)
{
refresh();
if (--trackItemIndex == -1)
{
if (chevronStartItem != null) trackItemIndex = menu
.indexOf(chevronStartItem);
else
trackItemIndex = drawCommands.size() - 1;
}
if (!selected)
{
refresh();
drawCommand(trackItemIndex, true);
}
else
{
if (popupMenu != null)
{
popupMenu.hideMenu();
popupMenu = null;
}
refresh();
drawCommand(trackItemIndex, true);
if (!isShowMenu() && selected)
{
final MenuDrawCommand command = (MenuDrawCommand) drawCommands
.get(trackItemIndex);
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
showMenu(command);
}
});
}
return;
}
}
else
{
popupMenu.getCurrentMenu().parentMenuSelected();
}
}
if ((ke.keyCode == KEY_DOWN || ke.keyCode == KEY_UP) && trackItemIndex > -1)
{
if (!selected)
{
selected = true;
refresh();
if (!isShowMenu() && selected)
{
final MenuDrawCommand command = (MenuDrawCommand) drawCommands
.get(trackItemIndex);
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
showMenu(command);
}
});
}
}
else
{
if (popupMenu != null)
{
if (popupMenu.getCurrentMenu() == null)
{
popupMenu.setSelection(0);
}
else
{
if (ke.keyCode == KEY_DOWN) popupMenu.getCurrentMenu()
.downSelected();
else
popupMenu.getCurrentMenu().upSelected();
}
}
}
}
if (ke.keyCode == SWT.ESC)
{
if (selected == false)
{
refresh();
trackItemIndex = -1;
}
else
{
if (popupMenu == null || popupMenu.getCurrentMenu() == null
|| popupMenu.getCurrentMenu() == popupMenu)
{
selected = false;
refresh();
drawCommand(trackItemIndex, false);
if (popupMenu != null)
{
popupMenu.hideMenu();
popupMenu = null;
}
}
else
{
popupMenu.getCurrentMenu().parentMenuSelected();
}
}
}
if (ke.keyCode == SWT.CR)
{
if (popupMenu != null && popupMenu.getCurrentMenu() != null)
{
popupMenu.getCurrentMenu().handleSelectedEvent();
}
else
{
if (popupMenu == null && trackItemIndex != -1 && selected == false)
{
selected = true;
refresh();
drawCommand(trackItemIndex, true);
if (!isShowMenu() && selected)
{
showMenu((MenuDrawCommand) drawCommands.get(trackItemIndex));
}
}
}
}
}
};
Display.getDefault().addFilter(SWT.KeyDown, keyDownListener);
final Listener keyUpListener = new Listener()
{
public void handleEvent(Event e)
{
MenuHolder holder = MenuHolderManager.getActiveHolder();
if (holder != null && holder != MenuBar.this) return;
if (getShell() == null || getShell().isDisposed())
{
Display.getDefault().removeFilter(SWT.KeyUp, this);
return;
}
KeyEvent ke = new KeyEvent(e);
if ((ke.keyCode == SWT.ALT || ke.keyCode == SWT.F10) && !nonAltKeyDown)
{
if (drawCommands.size() > 0)
{
// If no item is currently tracked then...