Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 564f7f1db02ff2b9692de50b9f038aaa3a5c1288 (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
/*******************************************************************************
 * Copyright (c) 2007, 2015 THALES GLOBAL SERVICES and others.
 * 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.tools.internal.views.providers.outline;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.DDiagramElementContainer;
import org.eclipse.sirius.diagram.DEdge;
import org.eclipse.sirius.diagram.business.api.query.DDiagramElementQuery;
import org.eclipse.sirius.diagram.ui.business.api.provider.AbstractDDiagramElementLabelItemProvider;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
import org.eclipse.sirius.diagram.ui.tools.api.image.DiagramImagesPath;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

/**
 * Label provider for the diagram outline tree.
 * 
 * @author Mariot Chauvin (mchauvin)
 */
public class OutlineLabelProvider extends LabelProvider implements IFontProvider {

    /** */
    private AdapterFactory factory;

    /**
     * Constructor.
     * 
     * @param adapterFactory
     */
    public OutlineLabelProvider() {
        this.factory = DiagramUIPlugin.getPlugin().getItemProvidersAdapterFactory();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
     */
    @Override
    public Image getImage(final Object element) {
        Image result = null;
        if (element instanceof DSemanticDecorator) {
            result = getImage((DSemanticDecorator) element);
        } else if (element instanceof AbstractDDiagramElementLabelItemProvider) {
            result = getImage((AbstractDDiagramElementLabelItemProvider) element);
        }
        return result;
    }

    /**
     * Returns the image for the label of the given DNodeLabelItemProvider.
     * 
     * @param element
     *            the element for which to provide the label image
     * @return the image used to label the element, or <code>null</code> if
     *         there is no image for the given object
     */
    private Image getImage(final AbstractDDiagramElementLabelItemProvider element) {
        Image result;
        ImageDescriptor descriptor = ExtendedImageRegistry.getInstance().getImageDescriptor(element.getImage(element.getTarget()));
        if (descriptor == null) {
            descriptor = ImageDescriptor.getMissingImageDescriptor();
        }
        result = DiagramUIPlugin.getPlugin().getImage(descriptor);

        Option<DDiagramElement> optionTarget = element.getDiagramElementTarget();
        if (optionTarget.some() && new DDiagramElementQuery(optionTarget.get()).isLabelHidden()) {
            result = getDecoratedImage(result, DiagramImagesPath.HIDDEN_DECORATOR, IDecoration.TOP_LEFT);
        }

        return result;
    }

    private Image getDecoratedImage(Image baseImage, String decoratorPath, int decoratorPosition) {
        ImageDescriptor decoratorDescriptor = ExtendedImageRegistry.getInstance().getImageDescriptor(DiagramUIPlugin.INSTANCE.getImage(decoratorPath));
        DecorationOverlayIcon finalDescriptor = new DecorationOverlayIcon(baseImage, decoratorDescriptor, decoratorPosition);
        return DiagramUIPlugin.getPlugin().getImage(finalDescriptor);
    }

    /**
     * Returns the image for the label of the given DSemanticDecorator.
     * 
     * @param element
     *            the element for which to provide the label image
     * @return the image used to label the element, or <code>null</code> if
     *         there is no image for the given object
     */
    private Image getImage(DSemanticDecorator element) {
        Image result = null;
        final EObject target = element.getTarget();
        if (target != null) {
            final IItemLabelProvider labelProvider = (IItemLabelProvider) this.factory.adapt(target, IItemLabelProvider.class);
            if (labelProvider != null) {
                ImageDescriptor descriptor = ExtendedImageRegistry.getInstance().getImageDescriptor(labelProvider.getImage(target));
                if (descriptor == null) {
                    descriptor = ImageDescriptor.getMissingImageDescriptor();
                }
                result = DiagramUIPlugin.getPlugin().getImage(descriptor);

                if (element instanceof DEdge) {
                    result = getDecoratedImage(result, DiagramImagesPath.VIEW_EDGE_DECORATOR, IDecoration.BOTTOM_LEFT);
                    result = computeFoldDecorator(result, (DEdge) element);
                }

                if (element instanceof DDiagramElement) {
                    DDiagramElementQuery query = new DDiagramElementQuery((DDiagramElement) element);
                    if (query.isHidden()) {
                        result = getDecoratedImage(result, DiagramImagesPath.HIDDEN_DECORATOR, IDecoration.TOP_LEFT);
                    }
                    if (element instanceof DDiagramElementContainer && query.isLabelHidden()) {
                        result = getDecoratedImage(result, DiagramImagesPath.HIDDEN_LABEL_DECORATOR, IDecoration.BOTTOM_RIGHT);
                    }
                }
            }
        }
        return result;
    }

    private Image computeFoldDecorator(final Image baseImage, final DEdge edge) {
        if (new DDiagramElementQuery(edge).isFolded()) {
            return getDecoratedImage(baseImage, DiagramImagesPath.FOLD_DECORATOR, IDecoration.TOP_RIGHT);
        }
        return baseImage;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(final Object element) {
        String result = null;
        if (element instanceof DSemanticDecorator) {
            final EObject target = ((DSemanticDecorator) element).getTarget();
            if (target != null) {
                final IItemLabelProvider labelProvider = (IItemLabelProvider) this.factory.adapt(target, IItemLabelProvider.class);
                if (labelProvider != null) {
                    result = labelProvider.getText(target);
                }
            }
        } else if (element instanceof AbstractDDiagramElementLabelItemProvider) {
            result = ((AbstractDDiagramElementLabelItemProvider) element).getText(((AbstractDDiagramElementLabelItemProvider) element).getTarget());
        }
        return result;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
     */
    public Font getFont(final Object element) {
        Font result = JFaceResources.getDefaultFont();
        if (element instanceof DDiagramElement) {
            final DDiagramElement vpe = (DDiagramElement) element;
            if (!vpe.isVisible() || (vpe instanceof DDiagramElementContainer && new DDiagramElementQuery(vpe).isLabelHidden())) {
                result = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
            }
        } else if (element instanceof AbstractDDiagramElementLabelItemProvider) {
            Option<DDiagramElement> optionTarget = ((AbstractDDiagramElementLabelItemProvider) element).getDiagramElementTarget();
            if (optionTarget.some() && new DDiagramElementQuery(optionTarget.get()).isLabelHidden()) {
                result = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
            }
        }
        return result;
    }
}

Back to the top