Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: 478d3e7b56d55c7f48353c232d5b13b97de70673 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.adt.design;

import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.KeyBoardAccessibilityEditPolicy;

/**
 * This key handler is designed to be re-used by both the WSDL and XSD editor
 */
public class BaseGraphicalViewerKeyHandler extends GraphicalViewerKeyHandler
{
  public BaseGraphicalViewerKeyHandler(GraphicalViewer viewer)
  {
    super(viewer);
  }

  public boolean keyPressed(KeyEvent event)
  {
    int direction = -1;
    boolean isAltDown = (event.stateMask & SWT.ALT) != 0;
    boolean isCtrlDown = (event.stateMask & SWT.CTRL) != 0;
    switch (event.keyCode)
    {
      case SWT.ARROW_LEFT : {
        direction = PositionConstants.WEST;
        break;
      }
      case SWT.ARROW_RIGHT : {
        direction = PositionConstants.EAST;
        break;
      }
      case SWT.ARROW_UP : {
        direction = isAltDown ? KeyBoardAccessibilityEditPolicy.OUT_TO_PARENT : PositionConstants.NORTH;
        break;
      }
      case SWT.ARROW_DOWN : {
    	 direction = isAltDown ? KeyBoardAccessibilityEditPolicy.IN_TO_FIRST_CHILD : PositionConstants.SOUTH;       
        break;
      }
    }
    
    if (direction != -1)
    {
      GraphicalEditPart focusEditPart = getFocusEditPart();
      KeyBoardAccessibilityEditPolicy policy = (KeyBoardAccessibilityEditPolicy)focusEditPart.getEditPolicy(KeyBoardAccessibilityEditPolicy.KEY);
      
      if (policy != null)          
      {
        EditPart target = policy.getRelativeEditPart(focusEditPart, direction);
        if (target != null)
        {
        	if(isCtrlDown) {

        		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        			 Object keyboardDrag = editor.getAdapter(IKeyboardDrag.class);
        			 if (keyboardDrag instanceof IKeyboardDrag) {
        				 ((IKeyboardDrag) keyboardDrag).performKeyboardDrag(focusEditPart, direction);
        				 return true;
        		 }
        	}
        	else {
                navigateTo(target, event);
                return true;
        	}
        }          
      }         
    }
    
    switch (event.keyCode)
    {
      case SWT.PAGE_DOWN :
      {  
        if (scrollPage(event, PositionConstants.SOUTH))
          return true;
      }  
      case SWT.PAGE_UP :
      {  
        if (scrollPage(event, PositionConstants.NORTH))
          return true;
      } 
      /*
      case SWT.F5 :
      {
        IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        if (part != null)
        {
          EditorModeManager manager = (EditorModeManager)part.getAdapter(EditorModeManager.class);
          EditorMode[] modes = manager.getModes();
          EditorMode mode = manager.getCurrentMode();
          List list = Arrays.asList(modes);
          int index = list.indexOf(mode);
          int nextIndex = index + 1;
          if (nextIndex < modes.length)
          {
            mode = (EditorMode)list.get(nextIndex);
          }  
          else
          {
            mode = (EditorMode)list.get(0);            
          }
          if (mode != manager.getCurrentMode())
          {  
            manager.setCurrentMode(mode);
          }  
        }  
        return true;
      }*/      
    }
    return super.keyPressed(event);
  }

  private boolean scrollPage(KeyEvent event, int direction)
  {
    if (!(getViewer().getControl() instanceof FigureCanvas))
      return false;
    FigureCanvas figCanvas = (FigureCanvas) getViewer().getControl();
    Point loc = figCanvas.getViewport().getViewLocation();
    Rectangle area = figCanvas.getViewport().getClientArea(Rectangle.SINGLETON).scale(.8);
    if (direction == PositionConstants.NORTH)
    {
      figCanvas.scrollToY(loc.y - area.height);
    }
    else
    {
      figCanvas.scrollToY(loc.y + area.height);
    }
    return true;
  }
}

Back to the top