Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 99f0cbfb71da69ff4170cc026f8e435eeb964b70 (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
/*******************************************************************************
 * Copyright (c) 2020 THALES GLOBAL SERVICES.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.edit.internal.part.layoutmanager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.sirius.diagram.DNodeContainer;
import org.eclipse.sirius.ext.gmf.runtime.gef.ui.figures.SiriusWrapLabel;

/**
 * Layout manager specific for compartment.
 * 
 * @author lfasani
 */
public class RegionContainerConstrainedToolbarLayout extends ConstrainedToolbarLayout {
    private DNodeContainer dNodeContainer;

    /**
     * Default constructor.
     * 
     * @param dNodeContainer
     *            The DDiagramElement associated to the editPart using this layoutManager.
     */
    public RegionContainerConstrainedToolbarLayout(DNodeContainer dNodeContainer) {
        this.dNodeContainer = dNodeContainer;
    }

    /**
     * Indicates if the current DNodeContainer contains children.
     */
    private boolean containsRegions() {
        return !dNodeContainer.getOwnedDiagramElements().isEmpty();
    }

    /*
     * (non-Javadoc) Partly copied from super class. The change is about the way the prefSize is computed.
     * @see
     * org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout#calculatePreferredSize(org.eclipse.draw2d.
     * IFigure, int, int)
     */
    @SuppressWarnings({ "deprecation" })
    @Override
    protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHint) {
        Insets insets = container.getInsets();
        if (!container.isVisible())
            return new Dimension(insets.getWidth(), insets.getHeight());
        if (isHorizontal()) {
            // CHECKSTYLE:OFF
            wHint = -1;
            if (hHint >= 0)
                hHint = Math.max(0, hHint - insets.getHeight());
        } else {
            hHint = -1;
            if (wHint >= 0)
                wHint = Math.max(0, wHint - insets.getWidth());
            // CHECKSTYLE:ON
        }

        // The preferred size of the compartment is computed :
        // * width: it is the preferred width of the region(s) except if there is no region (in this case the
        // container's label width is used)
        // * height: with whint=width previously computed, it is the preferred height of (the compartment label +
        // the region)
        List<IFigure> children = getChildren(container);
        List<IFigure> childrenToConsider = children;

        //
        if (containsRegions()) {
            //@formatter:off
            // We remove the label of the region container
            childrenToConsider = getChildren(container).stream()
                    .filter(figure -> !(figure instanceof SiriusWrapLabel))
                    .collect(Collectors.toList());
            //@formatter:on
        }
        Dimension prefSize = calculateChildrenSize(childrenToConsider, wHint, hHint, true);
        prefSize = calculateChildrenSize(children, prefSize.width, hHint, true);
        // Do a second pass, if necessary
        if (wHint >= 0 && prefSize.width > wHint) {
            prefSize = calculateChildrenSize(childrenToConsider, prefSize.width, hHint, true);
        } else if (hHint >= 0 && prefSize.height > hHint) {
            prefSize = calculateChildrenSize(childrenToConsider, wHint, prefSize.height, true);
        }

        prefSize.height += Math.max(0, children.size() - 1) * spacing;
        return transposer.t(prefSize).expand(insets.getWidth(), insets.getHeight()).union(getBorderPreferredSize(container));
    }

    /**
     * Copied from org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout.calculateChildrenSize(List, int,
     * int, boolean)
     */
    private Dimension calculateChildrenSize(List<IFigure> children, int wHint, int hHint, boolean preferred) {
        Dimension childSize;
        IFigure child;
        int height = 0;
        int width = 0;
        for (int i = 0; i < children.size(); i++) {
            child = children.get(i);
            childSize = transposer.t(preferred ? child.getPreferredSize(wHint, hHint) : child.getMinimumSize(wHint, hHint));
            height += childSize.height;
            width = Math.max(width, childSize.width);
        }
        return new Dimension(width, height);
    }

    /**
     * Copied from org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout.getChildren(IFigure).
     */
    private List<IFigure> getChildren(IFigure container) {
        List<IFigure> children = new ArrayList<IFigure>(container.getChildren());
        if (getIgnoreInvisibleChildren()) {
            Iterator<IFigure> iter = children.iterator();
            while (iter.hasNext()) {
                IFigure f = iter.next();
                if (!f.isVisible())
                    iter.remove();
            }
        }
        if (isReversed())
            Collections.reverse(children);
        return children;
    }
}

Back to the top