private void addRecentUse(String item, boolean front, boolean store) { int i, n; for (i = 0, n = this.getItemCount() - 2; i < n && !this.getItem(i).getText().equals(item); i++) ; if (i == n) { while (n >= _provider.getMaxCount()) { if (front) { this.remove(n - 1); } else { this.remove(0); } n -= 1; } JMenuItem mitem = new JMenuItem(item); mitem.setActionCommand("recent:" + item); mitem.addActionListener(_listener); if (front) { this.insert(mitem, 0); } else { this.insert(mitem, this.getItemCount() - 2); } } else { JMenuItem mitem = this.getItem(i); this.remove(i); if (front) { this.insert(mitem, 0); } else { this.insert(mitem, this.getItemCount() - 2); } } if (store) { storeList(); } }
public JRecentlyUsedMenu(String label, RecentlyUsedProvider provider, ActionListener l) { super(); int index = label.indexOf("_"); KeyStroke ks = null; if (index >= 0) { String c = label.substring(index + 1, index + 2); ks = KeyStroke.getKeyStroke(c); label = label.replaceFirst("[_]", ""); } super.setText(label); if (ks != null) { super.setMnemonic(ks.getKeyCode()); } _provider = provider; _listener = l; this.add(new JSeparator()); String txt = _provider.clearListText(); if (txt == null) { txt = "Clear recently used list"; } JMenuItem m = new JMenuItem(txt); m.setActionCommand("clear-recently-used"); m.addActionListener(this); this.add(m); Vector<String> items = provider.getList(); Iterator<String> it = items.iterator(); int n; n = provider.getMaxCount(); while (it.hasNext() && (getItemCount() - 2) < n) { String item = it.next(); if (item != null) { item = item.trim(); if (!item.equals("")) { addRecentUse(item, false, false); } } } }