01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13: package org.rapla.gui.toolkit;
14:
15: import java.awt.Component;
16:
17: import javax.swing.JComponent;
18: import javax.swing.JMenuItem;
19: import javax.swing.JPopupMenu;
20:
21: public class RaplaPopupMenu extends JPopupMenu implements MenuInterface {
22: private static final long serialVersionUID = 1L;
23:
24: public RaplaPopupMenu() {
25: super ();
26: }
27:
28: private int getIndexOfEntryWithId(String id) {
29: int size = getComponentCount();
30: for (int i = 0; i < size; i++) {
31: Component component = getComponent(i);
32: if (component instanceof IdentifiableMenuEntry) {
33: IdentifiableMenuEntry comp = (IdentifiableMenuEntry) component;
34: if (id != null && id.equals(comp.getId())) {
35: return i;
36: }
37: }
38: }
39: return -1;
40: }
41:
42: public void removeAllBetween(String startId, String endId) {
43: int startIndex = getIndexOfEntryWithId(startId);
44: int endIndex = getIndexOfEntryWithId(endId);
45: if (startIndex < 0 || endIndex < 0)
46: return;
47:
48: for (int i = startIndex + 1; i < endIndex; i++) {
49: remove(startIndex);
50: }
51:
52: }
53:
54: public void insertAfterId(Component component, String id) {
55: if (id == null) {
56: add(component);
57: } else {
58: int index = getIndexOfEntryWithId(id);
59: insert(component, index + 1);
60: }
61: }
62:
63: public void insertBeforeId(JComponent component, String id) {
64: int index = getIndexOfEntryWithId(id);
65: insert(component, index);
66: }
67:
68: public void remove(JMenuItem item) {
69: remove(item);
70: }
71:
72: }
|