1 26 package org.nightlabs.editor2d; 27 28 import java.awt.Rectangle ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import org.apache.log4j.Logger; 34 import org.eclipse.draw2d.PositionConstants; 35 import org.eclipse.gef.EditPart; 36 import org.eclipse.gef.GraphicalViewer; 37 import org.eclipse.gef.commands.CommandStack; 38 import org.eclipse.gef.commands.CompoundCommand; 39 import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler; 40 import org.eclipse.jface.viewers.ISelection; 41 import org.eclipse.jface.viewers.ISelectionChangedListener; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 import org.eclipse.jface.viewers.SelectionChangedEvent; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.events.DisposeEvent; 46 import org.eclipse.swt.events.DisposeListener; 47 import org.eclipse.swt.events.KeyEvent; 48 import org.nightlabs.config.Config; 49 import org.nightlabs.config.ConfigException; 50 import org.nightlabs.editor2d.command.SetConstraintCommand; 51 import org.nightlabs.editor2d.config.QuickOptionsConfigModule; 52 import org.nightlabs.editor2d.edit.AbstractDrawComponentEditPart; 53 54 57 public class EditorViewerKeyHandler 58 extends GraphicalViewerKeyHandler 59 { 60 public static final Logger LOGGER = Logger.getLogger(EditorViewerKeyHandler.class); 61 62 65 public EditorViewerKeyHandler(GraphicalViewer viewer) 66 { 67 super(viewer); 68 init(); 69 } 70 71 protected void init() 72 { 73 initConfigModule(); 74 getViewer().addSelectionChangedListener(selectionListener); 75 getViewer().getControl().addDisposeListener(disposeListener); 76 } 77 78 protected QuickOptionsConfigModule confMod = null; 79 protected QuickOptionsConfigModule getConfigModule() 80 { 81 if (confMod == null) 82 initConfigModule(); 83 84 return confMod; 85 } 86 87 protected void initConfigModule() 88 { 89 try { 90 confMod = (QuickOptionsConfigModule) 91 Config.sharedInstance().createConfigModule(QuickOptionsConfigModule.class); 92 } catch (ConfigException e) { 93 throw new RuntimeException (e); 94 } 95 } 96 97 protected CommandStack getCommandStack() { 98 return getViewer().getEditDomain().getCommandStack(); 99 } 100 101 public static final List EMPTY_LIST = new ArrayList (0); 102 103 protected List selectedObjects = null; 104 protected List getSelectedObjects() 105 { 106 if (selectedObjects == null) 107 selectedObjects = EMPTY_LIST; 108 109 return selectedObjects; 110 } 111 112 protected ISelectionChangedListener selectionListener = new ISelectionChangedListener() 113 { 114 public void selectionChanged(SelectionChangedEvent event) 115 { 116 ISelection selection = event.getSelection(); 117 if (!selection.isEmpty()) 118 { 119 if (selection instanceof IStructuredSelection) 120 { 121 IStructuredSelection structuredSelection = (IStructuredSelection) selection; 123 selectedObjects = structuredSelection.toList(); 124 return; 125 } 126 } 127 selectedObjects = EMPTY_LIST; 128 } 129 }; 130 131 protected DisposeListener disposeListener = new DisposeListener() 132 { 133 public void widgetDisposed(DisposeEvent e) 134 { 135 getViewer().removeSelectionChangedListener(selectionListener); 136 } 137 }; 138 139 public boolean keyPressed(KeyEvent event) 140 { 141 if (!getSelectedObjects().isEmpty()) 143 { 144 if (acceptLeft(event)) { 145 translate(getSelectedObjects(), LEFT); 147 return true; 148 } 149 if (acceptRight(event)) { 150 translate(getSelectedObjects(), RIGHT); 152 return true; 153 } 154 if (acceptUp(event)) { 155 translate(getSelectedObjects(), UP); 157 return true; 158 } 159 if (acceptDown(event)) { 160 translate(getSelectedObjects(), DOWN); 162 return true; 163 } 164 } 165 return super.keyPressed(event); 167 } 168 169 public static final int LEFT = PositionConstants.LEFT; 170 public static final int RIGHT = PositionConstants.RIGHT; 171 public static final int UP = PositionConstants.TOP; 172 public static final int DOWN = PositionConstants.BOTTOM; 173 174 public int getTranslationX() { 179 return getConfigModule().getMoveTranslationX(); 180 } 181 182 public int getTranslationY() { 187 return getConfigModule().getMoveTranslationY(); 188 } 189 190 195 protected void translate(List editParts, int direction) 196 { 197 CompoundCommand compoundCmd = new CompoundCommand(); 198 for (Iterator it = editParts.iterator(); it.hasNext(); ) 199 { 200 EditPart ep = (EditPart) it.next(); 201 if (ep instanceof AbstractDrawComponentEditPart) 202 { 203 AbstractDrawComponentEditPart dcep = (AbstractDrawComponentEditPart) ep; 204 DrawComponent dc = dcep.getDrawComponent(); 205 SetConstraintCommand cmd = new SetConstraintCommand(); 206 cmd.setPart(dc); 207 Rectangle dcBounds = new Rectangle (dc.getBounds()); 208 switch (direction) 209 { 210 case(DOWN): 211 dcBounds.y += getTranslationY(); 212 break; 213 case(UP): 214 dcBounds.y -= getTranslationY(); 215 break; 216 case(LEFT): 217 dcBounds.x -= getTranslationX(); 218 break; 219 case(RIGHT): 220 dcBounds.x += getTranslationX(); 221 break; 222 } 223 cmd.setBounds(dcBounds); 224 compoundCmd.add(cmd); 225 } 226 } 227 if (!compoundCmd.getCommands().isEmpty()) { 228 getCommandStack().execute(compoundCmd); 229 } 230 231 LOGGER.debug("Translate Command executed"); 232 } 233 234 238 protected boolean acceptLeft(KeyEvent event) { 239 return (event.keyCode == SWT.ARROW_LEFT); 241 } 242 243 247 protected boolean acceptRight(KeyEvent event) { 248 return (event.keyCode == SWT.ARROW_RIGHT); 250 } 251 252 256 protected boolean acceptDown(KeyEvent event) { 257 return (event.keyCode == SWT.ARROW_DOWN); 259 } 260 261 265 protected boolean acceptUp(KeyEvent event) { 266 return (event.keyCode == SWT.ARROW_UP); 268 } 269 270 } 271 | Popular Tags |