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.JMenu;
19:
20: public class RaplaMenu extends JMenu implements IdentifiableMenuEntry,
21: MenuInterface {
22: private static final long serialVersionUID = 1L;
23:
24: String id;
25:
26: public RaplaMenu(String id) {
27: super ();
28: this .id = id;
29: }
30:
31: public String getId() {
32: return id;
33: }
34:
35: private int getIndexOfEntryWithId(String id) {
36: int size = getMenuComponentCount();
37: for (int i = 0; i < size; i++) {
38: Component component = getMenuComponent(i);
39: if (component instanceof IdentifiableMenuEntry) {
40: IdentifiableMenuEntry comp = (IdentifiableMenuEntry) component;
41: if (id != null && id.equals(comp.getId())) {
42: return i;
43: }
44: }
45: }
46: return -1;
47: }
48:
49: public void removeAllBetween(String startId, String endId) {
50: int startIndex = getIndexOfEntryWithId(startId);
51: int endIndex = getIndexOfEntryWithId(endId);
52: if (startIndex < 0 || endIndex < 0)
53: return;
54:
55: for (int i = startIndex + 1; i < endIndex; i++) {
56: remove(startIndex + 1);
57: }
58:
59: }
60:
61: public boolean hasId(String id) {
62: return getIndexOfEntryWithId(id) >= 0;
63: }
64:
65: public void insertAfterId(Component component, String id) {
66: if (id == null) {
67: getPopupMenu().add(component);
68: } else {
69: int index = getIndexOfEntryWithId(id);
70: getPopupMenu().insert(component, index + 1);
71: }
72: }
73:
74: public void insertBeforeId(JComponent component, String id) {
75: int index = getIndexOfEntryWithId(id);
76: getPopupMenu().insert(component, index);
77: }
78:
79: }
|