1 26 27 package org.objectweb.util.browser.core.popup; 28 29 30 import org.objectweb.util.browser.api.IconProvider; 31 import org.objectweb.util.browser.api.MenuItem; 32 import org.objectweb.util.browser.api.MenuItemTreeView; 33 import org.objectweb.util.browser.api.TreeView; 34 import org.objectweb.util.browser.core.common.ClassResolver; 35 import org.objectweb.util.browser.core.common.DynamicTree; 36 import org.objectweb.util.browser.core.icon.EmptyIconProvider; 37 38 39 import java.awt.Component ; 40 import javax.swing.Action ; 41 import javax.swing.Icon ; 42 import javax.swing.JPopupMenu ; 43 import javax.swing.JMenuItem ; 44 import javax.swing.KeyStroke ; 45 46 public class AdvancedJPopupMenu extends JPopupMenu { 47 48 49 protected DynamicTree tree_; 50 51 52 protected boolean inheritTreeMenu_; 53 54 55 protected MenuItemTreeView treeView_; 56 57 public AdvancedJPopupMenu(DynamicTree tree, MenuItemTreeView treeView, boolean inheritTreeMenu) { 58 tree_ = tree; 59 treeView_ = treeView; 60 inheritTreeMenu_ = inheritTreeMenu; 61 } 62 63 68 public AdvancedJPopupMenu(DynamicTree tree, boolean inheritTreeMenu) { 69 this(tree, new DefaultMenuItemTreeView(tree), inheritTreeMenu); 70 } 71 72 76 public AdvancedJPopupMenu(DynamicTree tree) { 77 this(tree, false); 78 } 79 80 public AdvancedJPopupMenu(DynamicTree tree, MenuItemTreeView treeView) { 81 this(tree, treeView, false); 82 } 83 84 91 protected JMenuItem makeMenuItem(String label, MenuItem action, int itemState, ItemProperty itemProperty) { 92 93 IconProvider iconProvider = itemProperty.getIconProvider(); 94 boolean isUserIcon = iconProvider!=null; 95 if(!isUserIcon) 96 iconProvider = new EmptyIconProvider(); 97 Icon icon = iconProvider.newIcon(treeView_.getSelectedObject()); 98 99 KeyStroke keyStroke = itemProperty.getKeyStroke(); 100 Action theAction = new DefaultAction(tree_, action, treeView_, label, icon, itemProperty.getKeyStroke(), itemProperty.getMnemonic(), isUserIcon, itemState); 101 MyJMenuItem item = new MyJMenuItem(theAction, action.getClass().getName()); 102 106 if (itemState == MenuItem.DISABLED_STATUS) 113 item.setEnabled(false); 114 return item; 115 } 116 117 122 protected boolean contains(MyJMenuItem menuItem) { 123 Component myComponent[] = getComponents(); 124 for (int i = 0; i < myComponent.length; i++) 125 if (myComponent[i] instanceof MyJMenuItem 126 && ((MyJMenuItem) myComponent[i]).getText().equals(menuItem.getText()) 127 ) 128 return true; 129 return false; 130 } 131 132 136 public boolean hasToInheritTreeMenu() { 137 return inheritTreeMenu_; 138 } 139 140 146 public Component add(Component comp) { 147 if (getComponentCount() > 0) { 148 Component c = getComponent(getComponentCount() - 1); 149 if (!(c instanceof javax.swing.JPopupMenu.Separator && comp instanceof javax.swing.JPopupMenu.Separator)) 150 return super.add(comp); 151 } else if (!(getComponentCount() == 0 && comp instanceof javax.swing.JPopupMenu.Separator)) 152 return super.add(comp); 153 return comp; 154 } 155 156 161 public void add(JPopupMenu popupMenu) { 162 if (popupMenu != null) { 163 Component toAdd[] = popupMenu.getComponents(); 164 if (toAdd.length > 0) { 165 addSeparator(); 166 for (int i = 0; i < toAdd.length; i++) { 167 if (((toAdd[i] instanceof MyJMenuItem) && !contains((MyJMenuItem) toAdd[i])) || !(toAdd[i] instanceof MyJMenuItem)) 168 add(toAdd[i]); 169 } 170 Component c = getComponent(getComponentCount() - 1); 172 if (c instanceof javax.swing.JPopupMenu.Separator) 173 remove(c); 174 } 175 } 176 } 177 178 184 public void addMenuItem(String label, ItemProperty itemProperty, DynamicTree tree) { 185 if(label!=null && itemProperty!=null){ 186 String className = itemProperty.getClassName(); 187 if(label.startsWith("-separator-")){ 188 addSeparator(); 189 } else if (className != null && !className.equals("")) { 190 MenuItem action = null; 191 try { 192 action = (MenuItem) ClassResolver.resolve(className).newInstance(); 193 } catch (java.lang.ClassNotFoundException e) { 194 System.out.println("Error : " + className + " : Class Not Found !"); 195 } catch (java.lang.InstantiationException e) { 196 e.printStackTrace(); 197 } catch (java.lang.IllegalAccessException e) { 198 e.printStackTrace(); 199 } catch (java.lang.ClassCastException e) { 200 System.out.println("Error : " + className + " : Not a org.objectweb.util.browser.api.MenuItem implementation"); 201 } 202 if (action != null){ 203 int itemState = action.getStatus(treeView_); 204 if(itemState==MenuItem.DISABLED_STATUS || itemState==MenuItem.ENABLED_STATUS) 205 super.add(makeMenuItem(label, action, itemState, itemProperty)); 206 else if (itemState==MenuItem.NOT_VISIBLE_STATUS){ 207 } 209 } 210 } 211 } 212 } 213 214 218 public void setTreeView(TreeView treeView){ 219 220 } 221 } 222 | Popular Tags |