1 26 package org.objectweb.util.explorer.interpreter.lib.swing; 27 28 import java.net.URL ; 29 30 import javax.swing.Action ; 31 import javax.swing.Icon ; 32 import javax.swing.JPopupMenu ; 33 import javax.swing.JSeparator ; 34 35 import org.objectweb.fractal.api.NoSuchInterfaceException; 36 import org.objectweb.util.explorer.api.IconProvider; 37 import org.objectweb.util.explorer.api.MenuItem; 38 import org.objectweb.util.explorer.api.MenuItemTreeView; 39 import org.objectweb.util.explorer.api.TreeView; 40 import org.objectweb.util.explorer.core.code.api.Code; 41 import org.objectweb.util.explorer.core.code.api.CodeProvider; 42 import org.objectweb.util.explorer.core.common.api.Description; 43 import org.objectweb.util.explorer.core.icon.api.IconDescription; 44 import org.objectweb.util.explorer.core.menu.api.ItemDescription; 45 import org.objectweb.util.explorer.core.menu.api.MenuDescription; 46 import org.objectweb.util.explorer.core.menu.api.MenuElement; 47 import org.objectweb.util.explorer.core.menu.api.MenuSeparator; 48 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter; 49 import org.objectweb.util.explorer.interpreter.lib.AbstractDescriptionInterpreter; 50 import org.objectweb.util.explorer.swing.icon.IconFileProvider; 51 import org.objectweb.util.explorer.swing.menu.GenericAction; 52 53 61 public class MenuInterpreter 62 extends AbstractDescriptionInterpreter 63 { 64 65 71 protected final String ICON_INTERPRETER = "icon-interpreter"; 72 73 79 85 89 protected Icon getIcon(IconDescription iconDesc, TreeView treeView){ 90 if(iconDesc!=null){ 91 IconProvider iconProvider = (IconProvider) getIconInterpreter().interprete(iconDesc, treeView); 92 if(iconProvider!=null){ 93 Object object = iconProvider.newIcon(treeView.getSelectedObject()); 94 if(object instanceof Icon ){ 95 return (Icon )object; 96 } else if(object instanceof String ) { 97 return (Icon )(new IconFileProvider((String )object)).newIcon(treeView.getSelectedObject()); 98 } else if (object instanceof URL ) { 99 return (Icon )(new IconFileProvider((URL )object)).newIcon(treeView.getSelectedObject()); 100 } 101 } 102 } 103 return null; 104 } 105 106 109 protected Action getAction(ItemDescription itemDesc, MenuItemTreeView treeView){ 110 if(itemDesc!=null && !itemDesc.isEmpty()){ 111 Code actionCode = getCodeProvider().getCode(itemDesc.getCodeDescription()); 112 if(actionCode!=null && actionCode.isInstanceOf(MenuItem.class)){ 113 MenuItem action = (MenuItem)actionCode.createInstance(); 114 int actionStatus = action.getStatus(treeView); 115 Icon icon = getIcon(itemDesc.getIconDescription(), treeView); 116 if(actionStatus!=MenuItem.NOT_VISIBLE_STATUS){ 117 return new GenericAction(itemDesc, action, treeView, icon, actionStatus==MenuItem.ENABLED_STATUS); 118 } 119 } else { 120 getTrace().warn(itemDesc.getCodeDescription().getCode() + " is not a MenuItem instance!"); 121 } 122 } 123 return null; 124 } 125 126 129 protected JSeparator getSeparator(MenuSeparator menuSeparator){ 130 return new JSeparator (); 131 } 132 133 136 protected JPopupMenu createMenu(MenuDescription menuDesc, MenuItemTreeView treeView){ 137 JPopupMenu menu = null; 138 if(menuDesc!=null && !menuDesc.isEmpty()){ 139 menu = new JPopupMenu (); 140 MenuElement[] menuElement = menuDesc.getAllMenuElements(); 141 for (int i = 0; i < menuElement.length; i++) { 142 MenuElement element = menuElement[i]; 143 if(element instanceof ItemDescription){ 144 Action action = getAction((ItemDescription)element, treeView); 145 if(action != null) { 146 menu.add(action); 147 } 148 } else if(element instanceof MenuSeparator){ 149 menu.add(getSeparator((MenuSeparator)element)); 150 } 151 } 152 } 153 return menu; 154 } 155 156 protected DescriptionInterpreter getIconInterpreter(){ 157 try { 158 return (DescriptionInterpreter)lookupFc(ICON_INTERPRETER); 159 } catch (NoSuchInterfaceException e) { 160 getTrace().warn(ICON_INTERPRETER + ": interface not found!"); 161 return null; 162 } 163 } 164 165 171 174 public String [] clientFc() { 175 return new String []{CodeProvider.CODE_PROVIDER, ICON_INTERPRETER}; 176 } 177 178 184 187 public Object interprete(Description description, Object params) { 188 MenuDescription menuDesc = (MenuDescription)description; 189 return createMenu(menuDesc, (MenuItemTreeView)params); 190 } 191 192 } 193 194 195 196 | Popular Tags |