下面列出了javax.swing.JMenu#getItemCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateMenu (JMenu menu) {
ActionProvider provider = getEngine().getActionProvider();
Map context = getEngine().getContextProvider().getContext();
String containerCtx = (String) menu.getClientProperty(KEY_CONTAINERCONTEXT);
boolean isDynamic = getEngine().getContainerProvider().isDynamicContext(
ContainerProvider.TYPE_MENU, containerCtx);
String[] actions = provider.getActionNames(containerCtx);
// System.err.println("Updating menu " + containerCtx + "actions: " + Arrays.asList(actions));
int count = menu.getItemCount();
// System.err.println("Item count = " + count);
//XXX for dynamic menus, we'll need to compare the contents of the
//menu with the list of strings, and add/prune
for (int i=0; i < count; i++) {
JMenuItem item = menu.getItem(i);
if (item != null) {
String action = (String) item.getClientProperty (KEY_ACTION);
configureMenuItem (item, containerCtx, action, provider, context);
}
}
}
/**
* Removes a menu item with the given name from the menu bar and returns the removed item.
* Returns null if menu item does not exist.
*
* @param menuName String
* @return JMenu
*/
public JMenuItem removeMenuItem(String menuName, String itemName) {
JMenu menu = getMenu(menuName);
if(menu==null) {
return null;
}
itemName = itemName.trim();
JMenuItem item = null;
for(int i = 0; i<menu.getItemCount(); i++) {
JMenuItem next = menu.getItem(i);
if(next.getText().trim().equals(itemName)) {
item = next;
menu.remove(i);
break;
}
}
return item;
}
/**
* Resgier allthe components in the jFrame.
*
* @param c
* @return
*/
public static List<Component> getAllComponents(final Container c) {
Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
compList.add(comp);
if (comp instanceof JMenu) {
JMenu menu = (JMenu) comp;
for (int i = 0; i < menu.getItemCount(); i++) {
if (menu.getItem(i)!=null)
compList.add(menu.getItem(i));
}
}
else if (comp instanceof Container)
compList.addAll(getAllComponents((Container) comp));
}
return compList;
}
public Image getPreImg(Component c, ButtonModel model)
{
JMenu menu = (JMenu) c;
if (menu.getItemCount() > 0)
{
if (model.isSelected())
{
return getRollverImg();
}
else
{
return getNormalImg();
}
}
return null;
}
/**
* Add submenus to a menu.
*
* @param menu Menu.
* @param items Submenus.
*/
public void addSubmenus(JPopupMenu menu, List<JMenuItem> items) {
Configuration config = Configuration.getConfiguration();
final int maxElements = Math.max(config.getInt(null, ConfigurationValueInteger.MENU_SIZE), 10);
if (items.size() <= maxElements) {
for (JMenuItem item : items) {
menu.add(item);
}
return;
}
int i = 0;
while (i < items.size()) {
JMenu currentMenu = new JMenu(items.get(i).getText());
menu.add(currentMenu);
while ((i < items.size()) && (currentMenu.getItemCount() < maxElements)) {
currentMenu.add(items.get(i));
i++;
}
}
}
private void setListener(JMenuItem item, ActionListener listener)
{
if (item instanceof JMenu)
{
JMenu menu = (JMenu) item;
int n = menu.getItemCount();
for (int i = 0; i < n; i++)
{
setListener(menu.getItem(i), listener);
}
}
else if (item != null)
{
item.addActionListener(listener);
}
}
@Override
public void currentIntervalChanged(Interval interval) {
JMenu intervals = getMenu(2);
for (int i = 0; i < intervals.getItemCount(); i++) {
JMenuItem item = intervals.getItem(i);
if ((item != null) && (item.getClass() == IntervalMenuItem.class)) {
IntervalMenuItem intervalItem = (IntervalMenuItem) item;
if (intervalItem.getInterval().equals(interval)) {
intervalItem.setSelected(true);
break;
}
}
}
}
/**
* Construct cascading pull-aside menus using the values of the debug flags
* in the Preferences object.
*
* @param topMenu attach the menus as children of this one.
*/
public static void constructMenu(JMenu topMenu) {
if (debug)
System.out.println("Debug.constructMenu ");
if (topMenu.getItemCount() > 0)
topMenu.removeAll();
try {
addToMenu(topMenu, store); // recursive
} catch (BackingStoreException e) {
}
topMenu.revalidate();
}
static void addRecent( final UiContext context, final File path ) {
final JMenu menu = context.recentMenu;
final int n = menu.getItemCount();
if (n > 9) {
menu.remove(n - 1);
}
final RecentAction ra = new RecentAction(context, path);
final JMenuItem jc = menu.add(ra);
ra.setItem(jc);
if (n > 0) {
menu.remove(menu.getItemCount() - 1);
menu.add(jc, 0);
}
}
private void updateRecent( final UiContext context, final File path ) {
final JMenu menu = context.recentMenu;
final JMenuItem ami = getItem();
if (ami != null) {
for (int i = 0, n = menu.getItemCount(); i < n; ++i) {
final JMenuItem mi = menu.getItem(i);
if (mi == ami) {
menu.remove(i);
menu.add(ami, 0);
break;
}
}
}
}
List<JMenuItem> menuItems() {
List<JMenuItem> menus = new ArrayList();
if (plugins != null) for (ProfilerPlugin plugin : plugins) {
try {
JMenu menu = new JMenu(plugin.getName());
plugin.createMenu(menu);
if (menu.getItemCount() > 0) menus.add(menu);
} catch (Throwable t) {
handleThrowable(plugin, t);
}
}
return menus;
}
protected static void enableMenu (JMenu menu) {
boolean enabled = false;
for (int i = 0; i < menu.getItemCount(); ++i) {
JMenuItem item = menu.getItem(i);
if (item != null && item.isEnabled()) {
enabled = true;
break;
}
}
menu.setEnabled(enabled);
}
protected static void enableMenu (JMenu menu) {
boolean enabled = false;
for (int i = 0; i < menu.getItemCount(); ++i) {
JMenuItem item = menu.getItem(i);
if (item != null && item.isEnabled()) {
enabled = true;
break;
}
}
menu.setEnabled(enabled);
}
private void enableMenu (JMenu menu) {
boolean enabled = false;
for (int i = 0; i < menu.getItemCount(); ++i) {
JMenuItem item = menu.getItem(i);
if (item != null && item.isEnabled()) {
enabled = true;
break;
}
}
menu.setEnabled(enabled);
}
/**
* Sets the listener.
*
* @param item the item
* @param listener the listener
*/
private void setListener(JMenuItem item, ActionListener listener) {
if (item instanceof JMenu) {
JMenu localMenu = (JMenu) item;
int n = localMenu.getItemCount();
for (int i = 0; i < n; i++) {
setListener(localMenu.getItem(i), listener);
}
} else if (item != null) { // null means separator
item.addActionListener(listener);
}
}
public final void addSubMenus(JPopupMenu menu) {
List<T> actionKeys = getActionKeys(existingSubMenus.keySet());
for(T actionKey : actionKeys) {
JMenu actionMenu = existingSubMenus.get(actionKey);
if (actionMenu.getItemCount() > 0) {
menu.add(actionMenu);
}
}
}
private void applyFilter(String filterText)
{
resetMenu();
for (JMenu menuFolder : allMenuFolders.keySet())
{
boolean atLeastOneHit = false;
boolean matchesMenu = menuFolder.getText().toLowerCase().contains(filterText.toLowerCase().trim());
for (int i = 0; i < menuFolder.getItemCount(); i++)
{
GameFontMenuItem gfmi = (GameFontMenuItem) menuFolder.getItem(i);
final boolean matchesGame = gfmi.isMatchingFilterGame(filterText);
final boolean matchesGenre = gfmi.isMatchingFilterGenre(filterText);
final boolean matchesSystem = gfmi.isMatchingFilterSystem(filterText);
boolean hit = matchesMenu || matchesGame || matchesGenre || matchesSystem;
gfmi.setVisible(hit);
if (hit)
{
atLeastOneHit = true;
}
}
allMenuItems.get(menuFolder).setFiltered(!atLeastOneHit);
}
setMenuItemFilterVisibility();
}
public static final void changeMenuItemsState(final JMenu menu, final boolean enableState) {
final int size = menu.getItemCount();
for (int i = 0; i < size; i++) {
final JMenuItem item = menu.getItem(i);
item.setEnabled(enableState);
}
}
/**
* Register all actions as listed in the descriptor files, and organize them
* according to the various domains defined.
* There is one pull-down menu generated for each domain found.
*/
public void registerAllActions ()
{
// Insert an initial separator, to let user easily grab the toolBar
toolBar.addSeparator();
DomainLoop:
for (String domain : Actions.getDomainNames()) {
// Create dedicated menu for this range, if not already existing
JMenu menu = menuMap.get(domain);
if (menu == null) {
logger.debug("Creating menu:{}", domain);
menu = new SeparableMenu(domain);
menuMap.put(domain, menu);
} else {
logger.debug("Augmenting menu:{}", domain);
}
// Proper menu decoration
ResourceMap resource = OmrGui.getApplication().getContext().getResourceMap(
Actions.class);
menu.setText(domain); // As default
menu.setName(domain);
// Register all actions in the given domain
registerDomainActions(domain, menu);
resource.injectComponents(menu); // Localized
SeparableMenu.trimSeparator(menu); // No separator at end of menu
// Smart insertion of the menu into the menu bar, and separators into the toolBar
if (menu.getItemCount() > 0) {
final int toolCount = toolBar.getComponentCount();
if (toolCount > 0) {
Component comp = toolBar.getComponent(toolCount - 1);
if (!(comp instanceof JToolBar.Separator)) {
toolBar.addSeparator();
}
}
menuBar.add(menu);
}
}
}
/**
* Adds the instruction menu for the clicked instruction.
*
* @param model The graph model that provides information about the graph.
* @param node The node whose menu is created.
* @param instruction The instruction that was clicked on.
* @param extensions The list of code node extensions that extend the menu.
*/
public CInstructionMenu(final CGraphModel model, final NaviNode node,
final INaviInstruction instruction, final List<ICodeNodeExtension> extensions) {
super("Instruction" + " " + ZyInstructionBuilder.buildInstructionLine(instruction,
model.getGraph().getSettings(),
new CDefaultModifier(model.getGraph().getSettings(), model.getDebuggerProvider())).first());
final INaviCodeNode codeNode = (INaviCodeNode) node.getRawNode();
final IDebugger debugger = CGraphDebugger.getDebugger(model.getDebuggerProvider(), instruction);
if (debugger != null) {
final INaviModule module = instruction.getModule();
final UnrelocatedAddress instructionAddress =
new UnrelocatedAddress(instruction.getAddress());
add(CActionProxy.proxy(new CActionToggleBreakpoint(
debugger.getBreakpointManager(), module, instructionAddress)));
final BreakpointAddress relocatedAddress =
new BreakpointAddress(module, instructionAddress);
if (debugger.getBreakpointManager().hasBreakpoint(BreakpointType.REGULAR, relocatedAddress)) {
add(CActionProxy.proxy(new CActionToggleBreakpointStatus(
debugger.getBreakpointManager(), module, instructionAddress)));
}
addSeparator();
}
for (final ICodeNodeExtension extension : extensions) {
extension.extendInstruction(this, codeNode, instruction);
}
try {
try {
final JMenu operandsMenu = new COperandsMenu(codeNode, instruction, extensions);
if (operandsMenu.getItemCount() != 0) {
add(operandsMenu);
}
} catch (final MaybeNullException exception) {
// No operands menu to add
}
} catch (final InternalTranslationException e) {
CUtilityFunctions.logException(e);
}
addSeparator();
final JMenu advancedMenu = new JMenu("Advanced");
advancedMenu.add(CActionProxy.proxy(
new CActionDeleteInstruction(model.getParent(), model.getGraph(), node, instruction)));
advancedMenu.add(CActionProxy.proxy(
new CActionSplitAfter(model.getGraph().getRawView(), codeNode, instruction)));
advancedMenu.add(CActionProxy.proxy(new CActionShowReilCode(model.getParent(), instruction)));
add(advancedMenu);
}