1 package org.columba.core.gui.menu; 19 20 import java.awt.Component ; 21 import java.util.Enumeration ; 22 import java.util.Hashtable ; 23 import java.util.logging.Logger ; 24 25 import javax.swing.Action ; 26 import javax.swing.JMenuItem ; 27 28 import org.columba.core.logging.Logging; 29 30 public class MenuModel { 31 32 private static final Logger LOG = Logger 33 .getLogger("org.columba.core.gui.menu"); 35 private IMenuElement rootElement; 36 37 private Hashtable placeholders = new Hashtable (); 38 39 private Hashtable submenues = new Hashtable (); 40 41 private String id; 42 43 public MenuModel(String id) { 44 this.id = id; 45 46 rootElement = MenuElementFactory.createMenuElement(id, "rootLabel"); 47 submenues.put(id, this); 48 } 49 50 public void insert(Action action, int position) { 51 insert(position, MenuElementFactory.createActionElement(action)); 52 } 53 54 public void insert(Component component, int position) { 55 insert(position, MenuElementFactory.createComponentElement(component)); 56 } 57 58 public void insert(JMenuItem menuItem, int position) { 59 insert(position, MenuElementFactory.createMenuItemElement(menuItem)); 60 } 61 62 public void insertSeparator(int position) { 63 insert(position, MenuElementFactory.createSeparatorElement()); 64 } 65 66 public void insertPlaceholder(String placeholderId, int position) { 67 insert(position, MenuElementFactory 68 .createPlaceholderElement(placeholderId)); 69 } 70 71 protected void insert(int position, IMenuElement element) { 72 rootElement.insert(element, position); 73 74 if (element.isPlaceholder()) { 75 String id = ((MenuElement) element).getPlaceholderId(); 76 if (placeholders.containsKey(id)) 77 throw new IllegalArgumentException ("placeholder id <" + id 78 + "> already used."); 79 80 placeholders.put(id, element); 81 } 82 } 83 84 protected void append(IMenuElement element) { 85 86 rootElement.add(element); 87 88 if (element.isPlaceholder()) { 89 String id = ((MenuElement) element).getPlaceholderId(); 90 if (placeholders.containsKey(id)) 91 throw new IllegalArgumentException ("placeholder id <" + id 92 + "> already used."); 93 94 placeholders.put(id, element); 95 } 96 } 97 98 protected int insert(IMenuElement element, String placeholderId) { 99 if (element == null) 100 throw new IllegalArgumentException ("element == null"); 101 if (placeholderId == null) 102 throw new IllegalArgumentException ( 103 "placeholderId == null, for element " + element.toString()); 104 105 if (placeholders.containsKey(placeholderId) == false) { 106 if (Logging.DEBUG) 107 printDebugPlaceholders(); 108 109 LOG.severe("no matching placeholder with id <" + placeholderId 110 + "> in menu <" + getId() + "> found."); 111 112 } 113 114 IMenuElement placeholderElement = (IMenuElement) placeholders 115 .get(placeholderId); 116 117 int index = rootElement.indexOf(placeholderElement); 118 119 if (index != -1) 120 insert(index, element); 121 122 return index; 123 } 124 125 128 private void printDebugPlaceholders() { 129 Enumeration e = placeholders.elements(); 130 while (e.hasMoreElements()) { 131 LOG.info(((IMenuElement) e.nextElement()).toString()); 132 } 133 } 134 135 138 public String getId() { 139 return id; 140 } 141 142 143 144 public void add(Component component) { 145 append(MenuElementFactory.createComponentElement(component)); 146 } 147 148 public void add(Action action) { 149 append(MenuElementFactory.createActionElement(action)); 150 } 151 152 public void add(JMenuItem menuItem) { 153 append(MenuElementFactory.createMenuItemElement(menuItem)); 154 } 155 156 public void addSeparator() { 157 append(MenuElementFactory.createSeparatorElement()); 158 } 159 160 public int insertSeparator(String placeholderId) { 161 int index = insert(MenuElementFactory.createSeparatorElement(), 162 placeholderId); 163 return index; 164 } 165 166 public void appendPlaceholder(String placeholderId) { 167 append(MenuElementFactory.createPlaceholderElement(placeholderId)); 168 } 169 170 public MenuModel getSubmenu(String submenuId) { 171 if (submenues.containsKey(submenuId) == false) 172 throw new IllegalArgumentException ("no matching menu with id " 173 + submenuId + " found."); 174 return (MenuModel) submenues.get(submenuId); 175 } 176 177 public void addSubmenu(MenuModel submenu) { 178 rootElement.add(submenu.getRootElement()); 179 submenues.put(submenu.getId(), submenu); 180 181 } 182 183 protected IMenuElement getRootElement() { 184 return rootElement; 185 } 186 187 public int insert(Action action, String placeholderId) { 188 int index = insert(MenuElementFactory.createActionElement(action), 189 placeholderId); 190 191 return index; 192 } 193 194 public int insert(JMenuItem menuItem, String placeholderId) { 195 int index = insert(MenuElementFactory.createMenuItemElement(menuItem), 196 placeholderId); 197 return index; 198 } 199 200 public Enumeration getSubmenuEnumeration() { 201 return submenues.elements(); 202 } 203 204 public void insertPlaceholder(String placeholderId, 205 String insertionPlaceholder) { 206 insert(MenuElementFactory.createPlaceholderElement(placeholderId), 207 insertionPlaceholder); 208 } 209 210 public int insert(Component component, String placeholderId) { 211 int index = insert( 212 MenuElementFactory.createComponentElement(component), 213 placeholderId); 214 return index; 215 } 216 } 217 | Popular Tags |