/** * Returns the item at the given, zero-relative index in the receiver. Throws an exception if the * index is out of range. * * @param index the index of the item to return * @return the item at the given index * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the * list minus 1 (inclusive) * </ul> * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public MenuItem getItem(int index) { checkWidget(); if (index < 0) error(SWT.ERROR_INVALID_RANGE); int[] actions = OS.QWidget_actions(handle); int indexOfLastMenuItem = -1; MenuItem itemAtIndex = null; for (int i = 0; i < actions.length; ++i) { Widget widget = Display.getWidget(actions[i]); if (widget != null && widget instanceof MenuItem) { if (++indexOfLastMenuItem == index) { itemAtIndex = (MenuItem) widget; break; } } } if (index > indexOfLastMenuItem) { error(SWT.ERROR_INVALID_RANGE); } return itemAtIndex; }
/** * Returns a (possibly empty) array of <code>MenuItem</code>s which are the items in the receiver. * * <p>Note: This is not the actual structure used by the receiver to maintain its list of items, * so modifying the array will not affect the receiver. * * @return the items in the receiver * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public MenuItem[] getItems() { checkWidget(); int[] handles = OS.QWidget_actions(handle); int count = handles.length; if (count == 0) return new MenuItem[0]; MenuItem[] children = new MenuItem[count]; int items = 0; for (int i = 0; i < count; ++i) { int handle = handles[i]; if (handle != 0) { Widget widget = Display.getWidget(handle); if (widget != null && widget != this) { if (widget instanceof MenuItem) { children[items++] = (MenuItem) widget; } } } } if (items == count) return children; MenuItem[] newChildren = new MenuItem[items]; System.arraycopy(children, 0, newChildren, 0, items); return newChildren; }