@Test public void testBasicMenuOrder() { JMenu file = (JMenu)tracker.getGravityTracker("File").getMenu(); JMenu first = (JMenu)tracker.getGravityTracker("File.First").getMenu(); JMenu second = (JMenu)tracker.getGravityTracker("File.Second").getMenu(); assertEquals("num sub menus",2, file.getItemCount()); assertEquals("first sub menu", first, file.getItem(0)); assertEquals("second sub menu", second, file.getItem(1)); }
public GravityTracker getGravityTracker(final String menuString) { if (menuString == null) throw new NullPointerException("menu string is null"); if (menuString.isEmpty()) throw new IllegalArgumentException("menu string has zero length"); if (menuString.equals(".") && rootPopupGravityTracker != null) return rootPopupGravityTracker; final List<MenuNameAndGravity> namesAndGravities = parseMenuString(menuString); MenuGravityTracker parentGravityTracker = null; MenuGravityTracker gravityTracker = null; String menu_key = null; for (final MenuNameAndGravity nameAndGravity : namesAndGravities) { final String menu_token = nameAndGravity.getMenuName(); menu_key = menu_key == null ? menu_token : menu_key + "." + menu_token; gravityTracker = menuMap.get(menu_key); if (gravityTracker == null) { final JMenu menu = new JMenu(menu_token); menu.addMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { final int itemCount = menu.getItemCount(); for (int i = 0; i < itemCount; i++) { final JMenuItem item = menu.getItem(i); if (item != null && item.getAction() != null) item.setEnabled(item.getAction().isEnabled()); } } @Override public void menuDeselected(MenuEvent e) { } @Override public void menuCanceled(MenuEvent e) { } }); if (parentGravityTracker != null) parentGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else if (rootMenuBar != null && rootPopupGravityTracker == null) rootMenuBar.add(menu); else if (rootMenuBar == null && rootPopupGravityTracker != null) rootPopupGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else throw new IllegalStateException("we have no root popup menu or menu bar."); gravityTracker = new MenuGravityTracker(menu); menuMap.put(menu_key, gravityTracker); } parentGravityTracker = gravityTracker; } return gravityTracker; }
JMenuTracker { public GravityTracker getGravityTracker(final String menuString) { if (menuString == null) throw new NullPointerException("menu string is null"); if (menuString.isEmpty()) throw new IllegalArgumentException("menu string has zero length"); if (menuString.equals(".") && rootPopupGravityTracker != null) return rootPopupGravityTracker; final List<MenuNameAndGravity> namesAndGravities = parseMenuString(menuString); MenuGravityTracker parentGravityTracker = null; MenuGravityTracker gravityTracker = null; String menu_key = null; for (final MenuNameAndGravity nameAndGravity : namesAndGravities) { final String menu_token = nameAndGravity.getMenuName(); menu_key = menu_key == null ? menu_token : menu_key + "." + menu_token; gravityTracker = menuMap.get(menu_key); if (gravityTracker == null) { final JMenu menu = new JMenu(menu_token); menu.addMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { final int itemCount = menu.getItemCount(); for (int i = 0; i < itemCount; i++) { final JMenuItem item = menu.getItem(i); if (item != null && item.getAction() != null) item.setEnabled(item.getAction().isEnabled()); } } @Override public void menuDeselected(MenuEvent e) { } @Override public void menuCanceled(MenuEvent e) { } }); if (parentGravityTracker != null) parentGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else if (rootMenuBar != null && rootPopupGravityTracker == null) rootMenuBar.add(menu); else if (rootMenuBar == null && rootPopupGravityTracker != null) rootPopupGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else throw new IllegalStateException("we have no root popup menu or menu bar."); gravityTracker = new MenuGravityTracker(menu); menuMap.put(menu_key, gravityTracker); } parentGravityTracker = gravityTracker; } return gravityTracker; } }
JMenuTracker { public GravityTracker getGravityTracker(final String menuString) { if (menuString == null) throw new NullPointerException("menu string is null"); if (menuString.isEmpty()) throw new IllegalArgumentException("menu string has zero length"); if (menuString.equals(".") && rootPopupGravityTracker != null) return rootPopupGravityTracker; final List<MenuNameAndGravity> namesAndGravities = parseMenuString(menuString); MenuGravityTracker parentGravityTracker = null; MenuGravityTracker gravityTracker = null; String menu_key = null; for (final MenuNameAndGravity nameAndGravity : namesAndGravities) { final String menu_token = nameAndGravity.getMenuName(); menu_key = menu_key == null ? menu_token : menu_key + "." + menu_token; gravityTracker = menuMap.get(menu_key); if (gravityTracker == null) { final JMenu menu = new JMenu(menu_token); menu.addMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { final int itemCount = menu.getItemCount(); for (int i = 0; i < itemCount; i++) { final JMenuItem item = menu.getItem(i); if (item != null && item.getAction() != null) item.setEnabled(item.getAction().isEnabled()); } } @Override public void menuDeselected(MenuEvent e) { } @Override public void menuCanceled(MenuEvent e) { } }); if (parentGravityTracker != null) parentGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else if (rootMenuBar != null && rootPopupGravityTracker == null) rootMenuBar.add(menu); else if (rootMenuBar == null && rootPopupGravityTracker != null) rootPopupGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else throw new IllegalStateException("we have no root popup menu or menu bar."); gravityTracker = new MenuGravityTracker(menu); menuMap.put(menu_key, gravityTracker); } parentGravityTracker = gravityTracker; } return gravityTracker; } JMenuTracker(final JPopupMenu rootPopupMenu);  JMenuTracker(final JMenuBar rootMenuBar); }
JMenuTracker { public GravityTracker getGravityTracker(final String menuString) { if (menuString == null) throw new NullPointerException("menu string is null"); if (menuString.isEmpty()) throw new IllegalArgumentException("menu string has zero length"); if (menuString.equals(".") && rootPopupGravityTracker != null) return rootPopupGravityTracker; final List<MenuNameAndGravity> namesAndGravities = parseMenuString(menuString); MenuGravityTracker parentGravityTracker = null; MenuGravityTracker gravityTracker = null; String menu_key = null; for (final MenuNameAndGravity nameAndGravity : namesAndGravities) { final String menu_token = nameAndGravity.getMenuName(); menu_key = menu_key == null ? menu_token : menu_key + "." + menu_token; gravityTracker = menuMap.get(menu_key); if (gravityTracker == null) { final JMenu menu = new JMenu(menu_token); menu.addMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { final int itemCount = menu.getItemCount(); for (int i = 0; i < itemCount; i++) { final JMenuItem item = menu.getItem(i); if (item != null && item.getAction() != null) item.setEnabled(item.getAction().isEnabled()); } } @Override public void menuDeselected(MenuEvent e) { } @Override public void menuCanceled(MenuEvent e) { } }); if (parentGravityTracker != null) parentGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else if (rootMenuBar != null && rootPopupGravityTracker == null) rootMenuBar.add(menu); else if (rootMenuBar == null && rootPopupGravityTracker != null) rootPopupGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else throw new IllegalStateException("we have no root popup menu or menu bar."); gravityTracker = new MenuGravityTracker(menu); menuMap.put(menu_key, gravityTracker); } parentGravityTracker = gravityTracker; } return gravityTracker; } JMenuTracker(final JPopupMenu rootPopupMenu);  JMenuTracker(final JMenuBar rootMenuBar); GravityTracker getGravityTracker(final String menuString); }
JMenuTracker { public GravityTracker getGravityTracker(final String menuString) { if (menuString == null) throw new NullPointerException("menu string is null"); if (menuString.isEmpty()) throw new IllegalArgumentException("menu string has zero length"); if (menuString.equals(".") && rootPopupGravityTracker != null) return rootPopupGravityTracker; final List<MenuNameAndGravity> namesAndGravities = parseMenuString(menuString); MenuGravityTracker parentGravityTracker = null; MenuGravityTracker gravityTracker = null; String menu_key = null; for (final MenuNameAndGravity nameAndGravity : namesAndGravities) { final String menu_token = nameAndGravity.getMenuName(); menu_key = menu_key == null ? menu_token : menu_key + "." + menu_token; gravityTracker = menuMap.get(menu_key); if (gravityTracker == null) { final JMenu menu = new JMenu(menu_token); menu.addMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { final int itemCount = menu.getItemCount(); for (int i = 0; i < itemCount; i++) { final JMenuItem item = menu.getItem(i); if (item != null && item.getAction() != null) item.setEnabled(item.getAction().isEnabled()); } } @Override public void menuDeselected(MenuEvent e) { } @Override public void menuCanceled(MenuEvent e) { } }); if (parentGravityTracker != null) parentGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else if (rootMenuBar != null && rootPopupGravityTracker == null) rootMenuBar.add(menu); else if (rootMenuBar == null && rootPopupGravityTracker != null) rootPopupGravityTracker.addMenu(menu, nameAndGravity.getGravity()); else throw new IllegalStateException("we have no root popup menu or menu bar."); gravityTracker = new MenuGravityTracker(menu); menuMap.put(menu_key, gravityTracker); } parentGravityTracker = gravityTracker; } return gravityTracker; } JMenuTracker(final JPopupMenu rootPopupMenu);  JMenuTracker(final JMenuBar rootMenuBar); GravityTracker getGravityTracker(final String menuString);  }