blob: c0fc8e32907fc183724c745a0085d47bfe202222 [file] [log] [blame]
rmah80671f22006-08-21 19:53:19 +00001/*******************************************************************************
kchongc7560532008-04-14 18:08:00 +00002 * Copyright (c) 2001, 2008 IBM Corporation and others.
rmah80671f22006-08-21 19:53:19 +00003 * 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 *******************************************************************************/
11package org.eclipse.wst.xsd.ui.internal.editor;
12
kchongc7560532008-04-14 18:08:00 +000013import org.eclipse.gef.EditPart;
rmah80671f22006-08-21 19:53:19 +000014import org.eclipse.gef.GraphicalEditPart;
kchongc7560532008-04-14 18:08:00 +000015import org.eclipse.jface.viewers.ISelectionProvider;
16import org.eclipse.jface.viewers.StructuredSelection;
17import org.eclipse.ui.IEditorPart;
18import org.eclipse.ui.PlatformUI;
19import org.eclipse.wst.xsd.ui.internal.adapters.XSDAttributeDeclarationAdapter;
20import org.eclipse.wst.xsd.ui.internal.adapters.XSDElementDeclarationAdapter;
rmah80671f22006-08-21 19:53:19 +000021import org.eclipse.wst.xsd.ui.internal.adt.design.IKeyboardDrag;
kchongc7560532008-04-14 18:08:00 +000022import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.KeyBoardAccessibilityEditPolicy;
23import org.eclipse.wst.xsd.ui.internal.commands.BaseDragAndDropCommand;
24import org.eclipse.wst.xsd.ui.internal.commands.XSDAttributeDragAndDropCommand;
25import org.eclipse.wst.xsd.ui.internal.commands.XSDElementDragAndDropCommand;
26import org.eclipse.wst.xsd.ui.internal.design.editparts.XSDBaseFieldEditPart;
27import org.eclipse.xsd.XSDWildcard;
rmah80671f22006-08-21 19:53:19 +000028
kchongc7560532008-04-14 18:08:00 +000029public class KeyboardDragImpl implements IKeyboardDrag
30{
31 public void performKeyboardDrag(GraphicalEditPart movingElement, int direction)
32 {
33 KeyBoardAccessibilityEditPolicy policy = (KeyBoardAccessibilityEditPolicy) movingElement.getEditPolicy(KeyBoardAccessibilityEditPolicy.KEY);
rmah80671f22006-08-21 19:53:19 +000034
kchongc7560532008-04-14 18:08:00 +000035 EditPart rightElement = policy.getRelativeEditPart(movingElement, direction);
36 policy = (KeyBoardAccessibilityEditPolicy) rightElement.getEditPolicy(KeyBoardAccessibilityEditPolicy.KEY);
37 EditPart leftElement = (policy != null) ? policy.getRelativeEditPart(rightElement, direction) : null;
rmah80671f22006-08-21 19:53:19 +000038
kchong2f62ce52008-04-29 16:36:35 +000039 // TODO: add support for extenders
40 if (!(movingElement instanceof XSDBaseFieldEditPart)) return;
41
kchongc7560532008-04-14 18:08:00 +000042 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 }
rmah80671f22006-08-21 19:53:19 +000080}