Skip to main content
summaryrefslogtreecommitdiffstats
blob: cbd2f88b0e286c7150a04c381ecd9f121fbf5348 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * 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.wsdl.ui.internal.asd.design.editparts;
import java.util.Iterator;
import java.util.List;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
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.EditPartViewer;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editparts.LayerManager;
import org.eclipse.wst.wsdl.ui.internal.asd.design.figures.ComponentReferenceConnection;


class EditPartNavigationHandlerUtil
{
  static GraphicalEditPart getRelativeEditPart(GraphicalEditPart focusEditPart, int direction)
  {
    // these 'delta' values need to be set to some value that's large enough to cross the gap
    // between adjacent edit parts (since some layouts may space the edit parts out a bit)
    int dx = 5;
    int dy = 5;
        
    IFigure contentPane = focusEditPart.getContentPane();
    
    // we attempt to compute a rectangle that represents our current location
    Rectangle r = focusEditPart.getFigure().getBounds().getCopy();
    focusEditPart.getFigure().translateToAbsolute(r);
    if (contentPane != focusEditPart.getFigure())
    {
      // if the EditPart has a contentPane (that's no the figure) then we assume
      // effective rectangle is only the header portion of the figure
      //
      Rectangle contentPaneBounds = contentPane.getBounds().getCopy();
      contentPane.translateToAbsolute(contentPaneBounds);
      
      // we assume that when the editpart's figure and contentPane are different
      // that portion of the 'hittable' portion of editpart should not include the contentPane
      if (contentPaneBounds.y > r.y)
      {
        // if the contentPane is lower than the figure we only want the rectangle
        // above the contentPane
        r.height = contentPaneBounds.y - r.y;
        // we adjust the rectangle the right slightly since the content is nested a bit     
        r.x = contentPaneBounds.x;
      }
      if (contentPaneBounds.x > r.x)
      {
        // if the contentPane is to the right of the figure we only want the rectangle
        // to the left of the content pane
        r.width = contentPaneBounds.x - r.x;
      }
    }
    Point p = null;
    if (direction == PositionConstants.NORTH)
    {
      p = r.getTopLeft();
      p.y -= dy;
    }
    else if (direction == PositionConstants.SOUTH)
    {
      p = r.getBottomLeft();
      p.x += dx;
      p.y += dy;
    }
    else if (direction == PositionConstants.EAST)
    {
      p = r.getTopRight();
      p.x += dx;
    }
    else if (direction == PositionConstants.WEST)
    {
      p = r.getLeft();
      p.x -= dx;
    }  
    EditPart t = focusEditPart.getViewer().findObjectAt(p);
        
    if (t instanceof ColumnEditPart || t instanceof DefinitionsEditPart)
    {
      t = null;
    }      
    return (GraphicalEditPart)t;
  }
  
  static EditPart getNextSibling(EditPart editPart)
  {    
    EditPart result = null;    
    EditPart parent = editPart.getParent();
    if (parent != null)
    {  
      List children = parent.getChildren();
      int index = children.indexOf(editPart);
      if (index + 1 < children.size())
      {
        result = (EditPart)children.get(index + 1);
      }
    }
    return result;
  }
  
  static EditPart getPrevSibling(EditPart editPart)
  {    
    EditPart result = null;
    EditPart parent = editPart.getParent();
    if (parent != null)
    {  
      List children = parent.getChildren();
      int index = children.indexOf(editPart);
      if (index - 1 >= 0)
      {
        // if this is the first child
        //        
        result = (EditPart)children.get(index - 1);
      } 
    }
    return result;
  } 
  
  static EditPart getNextInterface(EditPart editPart)
  { 
    EditPart result = null;
    for (EditPart e = editPart; e != null; e = e.getParent())
    {
      if (e instanceof InterfaceEditPart)
      {
        InterfaceEditPart ie = (InterfaceEditPart)e;
        result = EditPartNavigationHandlerUtil.getNextSibling(ie);
        break;
      }  
    }  
    return result;
  }          
  
  static EditPart getSourceConnectionEditPart(AbstractGraphicalEditPart editPart)
  {
    // find the first connection that targets this editPart
    // navigate backward along the connection (to the left) to find the sourc edit part
    EditPart result = null;
    EditPartViewer viewer = editPart.getViewer();
    LayerManager manager = (LayerManager)editPart.getViewer().getEditPartRegistry().get(LayerManager.ID);
    IFigure layer = manager.getLayer(LayerConstants.CONNECTION_LAYER);    
    for (Iterator i = layer.getChildren().iterator(); i.hasNext(); )
    {
      Figure figure = (Figure)i.next();
      if (figure instanceof ComponentReferenceConnection)
      {
        ComponentReferenceConnection componentReferenceConnection = (ComponentReferenceConnection)figure;
        ConnectionAnchor targetAnchor = componentReferenceConnection.getTargetAnchor();
        if (targetAnchor.getOwner() == editPart.getFigure())
        {  
          ConnectionAnchor sourceAnchor = componentReferenceConnection.getSourceAnchor();
          IFigure sourceFigure = sourceAnchor.getOwner();          
          EditPart part = null;
          while (part == null && sourceFigure != null) 
          {
            part = (EditPart)viewer.getVisualPartMap().get(sourceFigure);
            sourceFigure = sourceFigure.getParent();
          }          
          result = part;
          break;
        }  
      }                
    }    
    return result;    
  }
}

Back to the top