rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2008 IBM Corporation and others. |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.wst.xsd.ui.internal.editor; |
| 12 | |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 13 | import org.eclipse.gef.EditPart; |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 14 | import org.eclipse.gef.GraphicalEditPart; |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 15 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 16 | import org.eclipse.jface.viewers.StructuredSelection; |
| 17 | import org.eclipse.ui.IEditorPart; |
| 18 | import org.eclipse.ui.PlatformUI; |
| 19 | import org.eclipse.wst.xsd.ui.internal.adapters.XSDAttributeDeclarationAdapter; |
| 20 | import org.eclipse.wst.xsd.ui.internal.adapters.XSDElementDeclarationAdapter; |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 21 | import org.eclipse.wst.xsd.ui.internal.adt.design.IKeyboardDrag; |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.KeyBoardAccessibilityEditPolicy; |
| 23 | import org.eclipse.wst.xsd.ui.internal.commands.BaseDragAndDropCommand; |
| 24 | import org.eclipse.wst.xsd.ui.internal.commands.XSDAttributeDragAndDropCommand; |
| 25 | import org.eclipse.wst.xsd.ui.internal.commands.XSDElementDragAndDropCommand; |
| 26 | import org.eclipse.wst.xsd.ui.internal.design.editparts.XSDBaseFieldEditPart; |
| 27 | import org.eclipse.xsd.XSDWildcard; |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 28 | |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 29 | public class KeyboardDragImpl implements IKeyboardDrag |
| 30 | { |
| 31 | public void performKeyboardDrag(GraphicalEditPart movingElement, int direction) |
| 32 | { |
| 33 | KeyBoardAccessibilityEditPolicy policy = (KeyBoardAccessibilityEditPolicy) movingElement.getEditPolicy(KeyBoardAccessibilityEditPolicy.KEY); |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 34 | |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 35 | EditPart rightElement = policy.getRelativeEditPart(movingElement, direction); |
| 36 | policy = (KeyBoardAccessibilityEditPolicy) rightElement.getEditPolicy(KeyBoardAccessibilityEditPolicy.KEY); |
| 37 | EditPart leftElement = (policy != null) ? policy.getRelativeEditPart(rightElement, direction) : null; |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 38 | |
kchong | 2f62ce5 | 2008-04-29 16:36:35 +0000 | [diff] [blame] | 39 | // TODO: add support for extenders |
| 40 | if (!(movingElement instanceof XSDBaseFieldEditPart)) return; |
| 41 | |
kchong | c756053 | 2008-04-14 18:08:00 +0000 | [diff] [blame] | 42 | XSDBaseFieldEditPart movingField = (XSDBaseFieldEditPart) movingElement; |
| 43 | XSDBaseFieldEditPart leftField = (XSDBaseFieldEditPart) leftElement; |
| 44 | XSDBaseFieldEditPart rightField = (XSDBaseFieldEditPart) rightElement; |
| 45 | |
| 46 | Object movingObject = movingField.getModel(); |
| 47 | |
| 48 | BaseDragAndDropCommand command = null; |
| 49 | if (movingObject instanceof XSDElementDeclarationAdapter || movingObject instanceof XSDWildcard) |
| 50 | { |
| 51 | command = new XSDElementDragAndDropCommand(movingField, leftField, rightField, direction); |
| 52 | } |
| 53 | else if (movingObject instanceof XSDAttributeDeclarationAdapter) |
| 54 | { |
| 55 | command = new XSDAttributeDragAndDropCommand(movingField, leftField, rightField, direction); |
| 56 | } |
| 57 | |
| 58 | if (command != null && command.canExecute()) |
| 59 | { |
| 60 | command.execute(); |
| 61 | // This is to reselect the moved item |
| 62 | try |
| 63 | { |
| 64 | IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); |
| 65 | if (editor != null && editor.getAdapter(ISelectionProvider.class) != null) |
| 66 | { |
| 67 | ISelectionProvider provider = (ISelectionProvider) editor.getAdapter(ISelectionProvider.class); |
| 68 | if (provider != null) |
| 69 | { |
| 70 | provider.setSelection(new StructuredSelection(movingElement.getModel())); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | catch (Exception e) |
| 75 | { |
| 76 | |
| 77 | } |
| 78 | } |
| 79 | } |
rmah | 80671f2 | 2006-08-21 19:53:19 +0000 | [diff] [blame] | 80 | } |