Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fecf78745b757172068f73546a9da8df6fbe9e7b (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
/**
 * 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.layout;

import org.eclipse.emf.cdo.ui.internal.branch.geometry.GeometryUtils;
import org.eclipse.emf.cdo.ui.internal.branch.item.AbstractBranchPointNode;
import org.eclipse.emf.cdo.ui.internal.branch.item.BranchPointNode;
import org.eclipse.emf.cdo.ui.internal.branch.item.BranchPointNodeUtils;

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

/**
 * A strategy that layouts a branch. A branch centers its (sibling) nodes below each other while using their time stamp
 * to set the y coordinate. Sub-Branches are displaced to the right or to the left (left and right sub branches are
 * distributed equally).
 * <p>
 * The current implementation may only layout vertically.
 * 
 * @author Andre Dietisheim
 */
public abstract class AbstractBranchViewLayoutStrategy implements BranchViewLayoutStrategy
{
  protected AbstractBranchViewLayoutStrategy()
  {
  }

  public void layoutBaselineNode(BranchView branchView, AbstractBranchPointNode node)
  {
    BranchPointNodeUtils.setInternalSize(node);
    setBaselineNodeLocation(node);
    initBranchBounds(branchView, node);
  }

  protected void initBranchBounds(BranchView branchView, AbstractBranchPointNode node)
  {
    InternalNode baselineInternalNode = BranchPointNodeUtils.getInternalNode(node);
    DisplayIndependentRectangle bounds = new DisplayIndependentRectangle(baselineInternalNode.getInternalX(),
        baselineInternalNode.getInternalY(), baselineInternalNode.getInternalWidth(), baselineInternalNode
            .getInternalHeight());
    branchView.setBounds(bounds);
  }

  /**
   * Layout the given node as sibling node to the baseline node (and its siblings).
   * 
   * @param branchView
   *          the branch view to layout the node to
   * @param node
   *          the node to layout
   */
  public void layoutNode(BranchView branchView, AbstractBranchPointNode node, AbstractBranchPointNode previousNode)
  {
    BranchPointNodeUtils.setInternalSize(node);
    setSameBranchNodeLocation(node, previousNode);
    setBranchBounds(branchView, node);
  }

  /**
   * Sets the bounds of the given node. The node is centered horizontally to the given previous node.
   */
  protected void setBaselineNodeLocation(AbstractBranchPointNode node)
  {
    double y = node.getTimeStamp();
    BranchPointNodeUtils.centerHorizontally(node, y);
  }

  /**
   * Sets the location of the given node. The node is centered horizontally to the given previous (sibling) node.
   */
  protected void setSameBranchNodeLocation(AbstractBranchPointNode node, AbstractBranchPointNode previousNode)
  {
    double y = node.getTimeStamp();
    BranchPointNodeUtils.centerHorizontally(node, previousNode, y);
  }

  /**
   * Sets the bounds of the current branch for the given additional node. The bounds are expanded if the size of the
   * node requires it.
   * 
   * @param branchView
   *          the branch view to set the bounds
   * @param node
   *          the node that was added and shall be included in the given branch view
   */
  protected void setBranchBounds(BranchView branchView, AbstractBranchPointNode node)
  {
    InternalNode internalNode = BranchPointNodeUtils.getInternalNode(node);

    DisplayIndependentRectangle bounds = GeometryUtils.union(branchView.getBounds(), //
        internalNode.getInternalX() //
        , internalNode.getInternalY() //
        , internalNode.getInternalWidth() //
        , internalNode.getInternalHeight());
    branchView.setBounds(bounds);
  }

  /**
   * Adds the given sub branch to this branch. This strategy distributes the sub-branches equally to the left and to the
   * right of this branch. It starts by putting the last sub branch to the right of the current branch and puts the
   * previous one to the left etc. .
   * 
   * @param branchPointNode
   *          the node on the current branch view that the sub-branch view shall be attached to
   * @param subBranchView
   *          the sub branch view to add
   * @param branchView
   *          the branch view
   */
  public void layoutSubBranchView(BranchView branchView, BranchView subBranchView, BranchPointNode branchPointNode)
  {
    if (subBranchView != null)
    {
      setBranchViewLocation(branchView, subBranchView, branchPointNode);
      branchView.setBounds(GeometryUtils.union(branchView.getBounds(), subBranchView.getBounds()));
    }
  }

  protected abstract void setBranchViewLocation(BranchView branchView, BranchView subBranchView,
      BranchPointNode branchPointNode);

  public void translateBy(BranchView branchView, DisplayIndependentDimension offsets)
  {
    translateBranchNodesBy(branchView, offsets);
    translateSubBranchesBy(branchView, offsets);
    GeometryUtils.translateRectangle(offsets.width, offsets.height, branchView.getBounds());
  }

  /**
   * Translates all sub branches of the given branch.
   * 
   * @param dimension
   *          the dimension to translate this branch by
   */
  protected void translateSubBranchesBy(BranchView branchView, DisplayIndependentDimension dimension)
  {
    for (BranchView subBranch : branchView.getSubBranchViews())
    {
      translateBy(subBranch, dimension);
    }
  }

  /**
   * Translates all the sibling nodes in this branch view. Applies the given horizontal and vertical offset.
   * 
   * @param branchView
   *          the branch view to translate the nodes of
   * @param dimension
   *          the dimension to translate by
   */
  protected void translateBranchNodesBy(BranchView branchView, DisplayIndependentDimension dimension)
  {
    for (AbstractBranchPointNode node : branchView.getNodes())
    {
      BranchPointNodeUtils.translateInternalLocation(node, dimension.width, dimension.height);
    }
  }

  public void scale(BranchView branchView, DisplayIndependentDimension scaling)
  {
    scaleSameBranchNodes(branchView, scaling);
    scaleSubBranches(branchView, scaling);
  }

  public void scale(BranchView branchView, DisplayIndependentRectangle targetBounds)
  {
    DisplayIndependentRectangle bounds = branchView.getBounds();
    DisplayIndependentDimension scaling = new DisplayIndependentDimension(bounds.width / targetBounds.width,
        bounds.height / targetBounds.height);
    scale(branchView, scaling);
    translateBy(branchView, GeometryUtils.getTranslation(branchView.getBounds(), targetBounds.x, targetBounds.y));
  }

  protected void scaleSameBranchNodes(BranchView branchView, DisplayIndependentDimension scaling)
  {
    double centerX = BranchPointNodeUtils.getCenterX(branchView.getBaselineNode());
    branchView.resetBounds();
    for (AbstractBranchPointNode node : branchView.getNodes())
    {
      InternalNode internalNode = BranchPointNodeUtils.getInternalNode(node);
      double newX = centerX / scaling.width - internalNode.getInternalWidth() / 2;
      double newY = internalNode.getInternalY() / scaling.height;
      internalNode.setInternalLocation(newX, newY);
      if (!branchView.areBoundsSet())
      {
        initBranchBounds(branchView, node);
      }
      else
      {
        setBranchBounds(branchView, node);
      }
    }
  }

  protected void scaleSubBranches(BranchView branchView, DisplayIndependentDimension targetDimension)
  {
    for (BranchView subBranch : branchView.getSubBranchViews())
    {
      scale(subBranch, targetDimension);
    }
  }
}

Back to the top