/** * return the resulting merged menubar * * @param master the master menubar * @param servant the servant menubar * @return a Menubar or null if there was nothing to merge */ protected Menubar merge(Menubar master, Menubar servant) { if (logger.isDebugEnabled()) { logger.debug("entering merge(Menubar, Menubar)"); } Menubar result = null; if (master == null) { if (servant != null) { result = servant; } } else { if (servant != null && servant.getMenus() != null && servant.getMenus().getMenu() != null) { /* merge two non null items fill the structure of master with servant elements */ for (int i = 0; i < servant.getMenus().getMenu().size(); i++) { /* if it already contains a menu named like current servant menu, then merge them */ MenuType currentMenu = (MenuType) servant.getMenus().getMenu().get(i); if (currentMenu != null) { Object item = this.getItem( master, currentMenu.getLabel(), currentMenu.getI18Nref(), MenuType.class); if (item != null) { this.merge((MenuType) item, currentMenu); } else { /* add the current servant menu to the list of menu of the master bar */ master.getMenus().getMenu().add(currentMenu); } } } } result = master; } if (logger.isDebugEnabled()) { logger.debug("exiting merge(Menubar, Menubar)"); } return result; }
/** * method that fill the shortcut set with an object ( this object should be instance of * ActionableShortcutedMenuItem but it does nothing if it is an normal Object ). the shortcut of * the element if it exists is set to "" if an existing element already uses it. if the object is * a menu, then * * @param objet an Object * @param recur true if the method should call itself when the object is a container of menuItem */ protected void assignShortcuts(Object object, boolean recur) { if (logger.isDebugEnabled()) { logger.debug("entering assignShortCuts(Object, boolean)"); } if (object != null) { if (recur) { List list = null; if (object instanceof Menubar) { Menubar bar = (Menubar) object; if (bar.getMenus() != null) { list = bar.getMenus().getMenu(); } } else if (object instanceof MenuType) { list = ((MenuType) object).getMenuOrItemOrCheck(); } else if (object instanceof TypeMenu) { TypeMenu menuType = (TypeMenu) object; if (menuType.getItems() != null) { list = menuType.getItems().getMenuOrItemOrCheck(); } } if (list != null) { Iterator it = list.iterator(); while (it.hasNext()) { this.assignShortcuts(it.next(), recur); } } } /* it an ActionableShortcutedMenuItem ? */ if (object instanceof ActionableShortcutedElement) { /* try to add the shortcut and if already presents, remove it */ String shortcut = ((ActionableShortcutedElement) object).getShortcut(); if (shortcut != null) { shortcut = shortcut.trim(); if (shortcut.length() > 0) { if (!this.shortcuts.add(shortcut)) { ((ActionableShortcutedElement) object).setShortcut(""); logger.warn( "the item '" + ((ActionableShortcutedElement) object).getLabel() + "' could not get shortcut '" + shortcut + "' because it's already assignated"); } } } } } if (logger.isDebugEnabled()) { logger.debug("exiting assignShortCuts(Object, boolean)"); } }
/** * return the item named name instance of the given class * * @param parent where to search for the item ( not recursively ) a Menubar or a Menu * @param label the label of an element * @param i18nRef the internationalization reference of an element * @param allowed class * @return a corresponding item or null if nothing was found */ protected Object getItem(Object parent, String label, String i18nRef, Class allowed) { Object o = null; if (parent != null && (parent instanceof Menubar || parent instanceof MenuType || parent instanceof TypeMenu)) { List l = null; if (parent instanceof MenuType) { l = ((MenuType) parent).getMenuOrItemOrCheck(); } else if (parent instanceof Menubar) { Menubar bar = (Menubar) parent; if (bar.getMenus() != null) { l = bar.getMenus().getMenu(); } } else if (parent instanceof TypeMenu) { TypeMenu typeMenu = (TypeMenu) parent; if (typeMenu.getItems() != null) { l = typeMenu.getItems().getMenuOrItemOrCheck(); } } if (l != null) { Iterator it = l.iterator(); while (it.hasNext()) { Object current = it.next(); if (current != null) { if (current instanceof AbstractElement) { AbstractElement elt = (AbstractElement) current; boolean seemsSame = this.equalsAbstractElts(elt, label, i18nRef); if (allowed.isAssignableFrom(current.getClass()) && seemsSame) { o = current; break; } } } } } } return o; }
public void testLoadMenubar() throws Exception { String[] files = new String[] {"menubar_1.xml", "menubar_2.xml"}; JAXBBarLoader jaxb = new JAXBBarLoader(); for (int i = 0; i < files.length; i++) { try { Menubar bar = jaxb.loadMenubar(new FileInputStream(DIR_PATH + File.separator + files[i])); assertTrue(true); if (i == 0) { I18NResources res = bar.getI18NResources(); assertEquals(1, res.getI18NResource().size()); } else if (i == 1) { I18NResources res = bar.getI18NResources(); assertEquals(2, res.getI18NResource().size()); } } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } }