Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 841a5894513e6cd9f1b625e2587fd9eb9c151c94 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * Copyright (c) 2004 - 2010 Eike Stepper (Berlin, Germany) 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:
 *    Andre Dietisheim - initial API and implementation
 */
package org.eclipse.emf.cdo.ui.internal.branch.item;

import org.eclipse.zest.layouts.dataStructures.DisplayIndependentRectangle;
import org.eclipse.zest.layouts.dataStructures.InternalNode;

/**
 * Various utility methods that help to deal with the manipulations needed to build a BranchTree.
 * 
 * @author Andre Dietisheim
 */
public class BranchPointNodeUtils
{

  /**
   * returns a BranchGraphNode for a given internal Node.
   * 
   * @param internalNode
   *          the internal node
   * @return the branch graph node
   * @see InternalNode
   * @see AbstractBranchPointNode
   */
  public static AbstractBranchPointNode getBranchPointNode(InternalNode internalNode)
  {
    AbstractBranchPointNode branchGraphNode = null;
    Object graphData = internalNode.getLayoutEntity().getGraphData();
    if (graphData != null && graphData instanceof AbstractBranchPointNode)
    {
      branchGraphNode = (AbstractBranchPointNode)graphData;
    }
    return branchGraphNode;
  }

  /**
   * Returns an internal node for a given branch graph node.
   * 
   * @param branchPointNode
   *          the branch graph node
   * @return the internal node
   * @see AbstractBranchPointNode
   * @see InternalNode
   */
  public static InternalNode getInternalNode(AbstractBranchPointNode branchPointNode)
  {
    InternalNode internalNode = null;
    Object layoutInformation = branchPointNode.getLayoutEntity().getLayoutInformation();
    if (layoutInformation instanceof InternalNode)
    {
      internalNode = (InternalNode)layoutInformation;
    }
    return internalNode;
  }

  /**
   * Returns the center of the given node on the x-axis.
   * 
   * @param node
   *          the node to get the x-center from
   * @return the center of the given node on the x-axis
   */
  public static double getCenterX(AbstractBranchPointNode node)
  {
    InternalNode internalNode = getInternalNode(node);
    return internalNode.getInternalX() + internalNode.getInternalWidth() / 2;
  }

  /**
   * Centers the x coordinate of the given target node compared to the given source node and returns the result. If
   * source node is <tt>null</tt>, the target node is translated to the right by the half of its own width (centered on
   * its own location).
   * 
   * @param nodeToCenter
   *          the node to center
   * @param nodeToCenterOn
   *          the node to center on
   * @return the centered x
   */
  public static double getCenteredX(InternalNode nodeToCenter, InternalNode nodeToCenterOn)
  {
    if (nodeToCenterOn == null)
    {
      return nodeToCenter.getInternalX() - nodeToCenter.getInternalWidth() / 2;
    }

    return nodeToCenterOn.getInternalX() + (nodeToCenterOn.getInternalWidth() - nodeToCenter.getInternalWidth()) / 2;
  }

  /**
   * Centers the x coordinate of the given target node.
   * 
   * @param sourceInternalNode
   *          the internal node to center on the x-axis
   * @return the centered x
   */
  public static double getCenteredX(InternalNode sourceInternalNode)
  {
    return getCenteredX(sourceInternalNode, null);
  }

  /**
   * Sets the internal size of the internal node that displays the given branch tree node.
   * 
   * @param node
   *          the branch tree nodes whose internal node shall be set in (internal) size
   * @see InternalNode
   * @see InternalNode#setInternalSize(double, double)
   * @see AbstractBranchPointNode#getSize()
   */
  public static void setInternalSize(AbstractBranchPointNode node)
  {
    InternalNode internalNode = getInternalNode(node);
    internalNode.setInternalSize(node.getSize().preciseWidth(), node.getSize().preciseHeight());
  }

  /**
   * Translates the internal location of the internal node that displays the given branch tree node.
   * 
   * @param node
   *          the node whose internal node shall be translated
   * @param deltaX
   *          the delta x
   * @param deltaY
   *          the delta y
   * @see InternalNode
   * @see InternalNode#getInternalX()
   * @see InternalNode#getInternalY()
   * @see InternalNode#setInternalLocation(double, double)
   */
  public static void translateInternalLocation(AbstractBranchPointNode node, double deltaX, double deltaY)
  {
    InternalNode internalNode = getInternalNode(node);
    internalNode.setInternalLocation(internalNode.getInternalX() + deltaX, internalNode.getInternalY() + deltaY);
  }

  /**
   * Sets the internal location of the internal node which displays the given branch tree node.
   * 
   * @param node
   *          the node whose internal node's internal location shall be set
   * @param x
   *          the x
   * @param y
   *          the y
   */
  public static void setInternalLocation(AbstractBranchPointNode node, double x, double y)
  {
    InternalNode internalNode = getInternalNode(node);
    internalNode.setInternalLocation(x, y);
  }

  /**
   * Centers the internal node (which displays the given branch tree node) horizontally relatively to the (internal node
   * that displays) the given source node.
   * 
   * @param nodeToBeCentered
   *          the node to be centered
   * @param sourcePositionNode
   *          the source node
   * @param y
   *          the y coordinate to apply
   */
  public static void centerHorizontally(AbstractBranchPointNode nodeToBeCentered,
      AbstractBranchPointNode sourcePositionNode, double y)
  {

    InternalNode internalNode = getInternalNode(nodeToBeCentered);
    double x = getCenteredX(internalNode, getInternalNode(sourcePositionNode));
    internalNode.setInternalLocation(x, y);
  }

  /**
   * Centers the internal node (which displays the given branch tree node) horizontally. It gets shifted to the right by
   * the half of its own width.
   * 
   * @param nodeToBeCentered
   *          the node to be centered
   * @param y
   *          the y coordinate to apply
   */
  public static void centerHorizontally(AbstractBranchPointNode nodeToBeCentered, double y)
  {
    InternalNode internalNode = getInternalNode(nodeToBeCentered);
    double x = getCenteredX(internalNode);
    internalNode.setInternalLocation(x, y);
  }

  /**
   * Returns the bounds of the given node.
   * 
   * @param node
   *          the node to get the bounds of
   * @return the bounds of the given node
   */
  public static DisplayIndependentRectangle getBounds(AbstractBranchPointNode node)
  {
    return getBounds(getInternalNode(node));
  }

  /**
   * Returns the bounds of the given internal node.
   * 
   * @param node
   *          the node to retrieve the bounds of
   * @return the bounds
   */
  private static DisplayIndependentRectangle getBounds(InternalNode node)
  {
    return new DisplayIndependentRectangle(node.getInternalX(), node.getInternalY(), node.getInternalWidth(), node
        .getInternalHeight());
  }
}

Back to the top