1 26 package org.objectweb.util.explorer.interpreter.lib.swt; 27 28 import java.net.URL ; 29 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.MenuItem; 34 import org.objectweb.fractal.api.NoSuchInterfaceException; 35 import org.objectweb.util.explorer.api.IconProvider; 36 import org.objectweb.util.explorer.api.MenuItemTreeView; 37 import org.objectweb.util.explorer.api.TreeView; 38 import org.objectweb.util.explorer.core.code.api.Code; 39 import org.objectweb.util.explorer.core.code.api.CodeProvider; 40 import org.objectweb.util.explorer.core.common.api.Description; 41 import org.objectweb.util.explorer.core.icon.api.IconDescription; 42 import org.objectweb.util.explorer.core.menu.api.AcceleratorDescription; 43 import org.objectweb.util.explorer.core.menu.api.ItemDescription; 44 import org.objectweb.util.explorer.core.menu.api.MenuDescription; 45 import org.objectweb.util.explorer.core.menu.api.MenuElement; 46 import org.objectweb.util.explorer.core.menu.api.MenuSeparator; 47 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter; 48 import org.objectweb.util.explorer.interpreter.lib.AbstractDescriptionInterpreter; 49 50 58 public abstract class AbstractMenuInterpreter 59 extends AbstractDescriptionInterpreter 60 { 61 62 68 protected final String ICON_INTERPRETER = "icon-interpreter"; 69 70 76 82 86 protected Image getIcon(IconDescription iconDesc, TreeView treeView){ 87 if(iconDesc!=null){ 88 IconProvider iconProvider = (IconProvider) getIconInterpreter().interprete(iconDesc, treeView); 89 if(iconProvider!=null){ 90 Object object = iconProvider.newIcon(treeView.getSelectedObject()); 91 if(object instanceof Image){ 92 return (Image)object; 93 } else if(object instanceof String ) { 94 return (Image)(new IconFileProvider((String )object)).newIcon(treeView.getSelectedObject()); 95 } else if (object instanceof URL ) { 96 return (Image)(new IconFileProvider((URL )object)).newIcon(treeView.getSelectedObject()); 97 } 98 } 99 } 100 return null; 101 } 102 103 protected int getAccelerator(AcceleratorDescription accDesc){ 104 int acc = 0; 105 if(accDesc!=null && !accDesc.isEmpty()){ 106 if (accDesc.getCtrlKey()) acc+=SWT.CTRL; 107 if (accDesc.getAltKey()) acc+=SWT.ALT; 108 if (accDesc.getShiftKey()) acc+=SWT.SHIFT; 109 acc += accDesc.getAcceleratorCharacter(); 110 } 111 return acc; 112 } 113 114 protected String getAcceleratorName(AcceleratorDescription accDesc){ 115 if(accDesc!=null && !accDesc.isEmpty()){ 116 return accDesc.getName(); 117 } 118 return null; 119 } 120 121 124 protected void addMenuItem(Menu menu, ItemDescription itemDesc, MenuItemTreeView treeView){ 125 if(itemDesc!=null && !itemDesc.isEmpty()){ 126 Code actionCode = getCodeProvider().getCode(itemDesc.getCodeDescription()); 127 if(actionCode!=null && actionCode.isInstanceOf(org.objectweb.util.explorer.api.MenuItem.class)){ 128 org.objectweb.util.explorer.api.MenuItem action = (org.objectweb.util.explorer.api.MenuItem)actionCode.createInstance(); 129 int actionStatus = action.getStatus(treeView); 130 if(actionStatus!=org.objectweb.util.explorer.api.MenuItem.NOT_VISIBLE_STATUS){ 131 MenuItem menuItem = new MenuItem(menu, SWT.PUSH); 132 String acceleratorName = getAcceleratorName(itemDesc.getAcceleratorDescription()); 133 if(acceleratorName!=null && !acceleratorName.equals("")){ 134 menuItem.setText(itemDesc.getLabel() + "\t" + acceleratorName); 135 menuItem.setAccelerator(getAccelerator(itemDesc.getAcceleratorDescription())); 136 } else { 137 menuItem.setText(itemDesc.getLabel()); 138 } 139 menuItem.addListener(SWT.Selection, new GenericAction(action, treeView)); 140 menuItem.setEnabled(actionStatus!=org.objectweb.util.explorer.api.MenuItem.DISABLED_STATUS); 141 menuItem.setImage(getIcon(itemDesc.getIconDescription(), treeView)); 142 } 143 } else { 144 getTrace().warn(itemDesc.getCodeDescription().getCode() + " is not a MenuItem instance!"); 145 } 146 } 147 } 148 149 152 protected void addMenuSeparator(Menu menu){ 153 MenuItem separator = new MenuItem(menu, SWT.SEPARATOR); 154 } 155 156 protected Menu createMenu(MenuDescription menuDesc, MenuItemTreeView treeView){ 157 Menu menu = null; 158 if(menuDesc!=null && !menuDesc.isEmpty()){ 159 menu = getMenu(); 160 MenuElement[] menuElement = menuDesc.getAllMenuElements(); 161 for (int i = 0; i < menuElement.length; i++) { 162 MenuElement element = menuElement[i]; 163 if(element instanceof ItemDescription){ 164 addMenuItem(menu, (ItemDescription)element, treeView); 165 } else if(element instanceof MenuSeparator){ 166 addMenuSeparator(menu); 167 } 168 } 169 } 170 return menu; 171 } 172 173 protected DescriptionInterpreter getIconInterpreter(){ 174 try { 175 return (DescriptionInterpreter)lookupFc(ICON_INTERPRETER); 176 } catch (NoSuchInterfaceException e) { 177 getTrace().warn(ICON_INTERPRETER + ": interface not found!"); 178 return null; 179 } 180 } 181 182 188 191 public String [] clientFc() { 192 return new String []{CodeProvider.CODE_PROVIDER, ICON_INTERPRETER}; 193 } 194 195 201 204 public Object interprete(Description description, TreeView treeView) { 205 MenuDescription menuDesc = (MenuDescription)description; 206 return createMenu(menuDesc, (MenuItemTreeView)treeView); 207 } 208 209 215 public abstract Menu getMenu(); 216 } 217 | Popular Tags |