Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 628ab998129d9f6ea1cc65eb98c826942d017248 (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
/*******************************************************************************
 * Copyright (c) 2017, 2018 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.tools.internal.decoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.diagram.ui.edit.api.part.IDiagramElementEditPart;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
import org.eclipse.sirius.diagram.ui.tools.api.decoration.AbstractSiriusDecorationDescriptorProvider;
import org.eclipse.sirius.diagram.ui.tools.api.decoration.DecorationDescriptor;
import org.eclipse.sirius.diagram.ui.tools.api.decoration.DecorationDescriptor.DisplayPriority;
import org.eclipse.sirius.diagram.ui.tools.api.decoration.SiriusDecorationDescriptorProvider;
import org.eclipse.sirius.diagram.ui.tools.api.image.DiagramImagesPath;
import org.eclipse.sirius.ecore.extender.business.api.permission.IPermissionAuthority;
import org.eclipse.sirius.ecore.extender.business.api.permission.LockStatus;
import org.eclipse.sirius.ecore.extender.business.api.permission.PermissionAuthorityRegistry;
import org.eclipse.sirius.ext.jface.viewers.IToolTipProvider;
import org.eclipse.sirius.viewpoint.description.DecorationDistributionDirection;
import org.eclipse.sirius.viewpoint.description.Position;
import org.eclipse.sirius.viewpoint.provider.SiriusEditPlugin;
import org.eclipse.swt.graphics.Image;

/**
 * This {@link SiriusDecorationDescriptorProvider} provides a decoration on the bottom left corner when the element is
 * in disableEditMode (and another where it is also invalid).
 * 
 * @author <a href="mailto:laurent.fasani@obeo.fr">Laurent Fasani</a>
 */
public class EditModeDecorationDescriptorProvider extends AbstractSiriusDecorationDescriptorProvider {

    private static final String NAME = "editModeDecorator"; //$NON-NLS-1$

    @Override
    public boolean provides(IDiagramElementEditPart editPart) {
        return true;
    }

    @Override
    public List<DecorationDescriptor> createDecorationDescriptors(IDiagramElementEditPart editPart, Session session) {
        Image decorationImage = getDecorationImage(editPart);
        if (decorationImage != null) {
            DecorationDescriptor decoDesc = new DecorationDescriptor();
            decoDesc.setName(NAME);
            decoDesc.setPosition(Position.SOUTH_WEST_LITERAL);
            decoDesc.setDistributionDirection(DecorationDistributionDirection.HORIZONTAL);
            decoDesc.setDisplayPriority(DisplayPriority.HIGH_PRIORITY.getValue());
            decoDesc.setDecorationAsImage(decorationImage);

            // add tooltip
            decoDesc.setTooltipAsString(getToolTip(editPart));

            return Arrays.asList(decoDesc);
        }

        return new ArrayList<>();
    }

    private String getToolTip(IDiagramElementEditPart editPart) {
        EObject representedObject = editPart.resolveTargetSemanticElement();

        if (representedObject != null) {
            IToolTipProvider tooltipProvider = Platform.getAdapterManager().getAdapter(representedObject, IToolTipProvider.class);
            if (tooltipProvider != null) {
                return tooltipProvider.getToolTipText(representedObject);
            }
        }
        return null;
    }

    /**
     * Check if an edit part is broken for decoration.
     * 
     * @param editPart
     *            the edit part to check
     * @return <code>true</code> if the editPart is not broken to be decorated, <code>false</code> otherwise
     */
    protected boolean isBroken(IDiagramElementEditPart editPart) {
        final EObject target = editPart.resolveTargetSemanticElement();
        if (isBroken(target)) {
            return true;
        }
        return false;
    }

    /**
     * Check if an EObject is broken (ie null or without eResource).
     * 
     * @param eObject
     *            the eObject to check
     * @return <code>true</code> if the eObject is broken, <code>false</code> otherwise
     */
    protected boolean isBroken(EObject eObject) {
        return eObject == null || eObject.eResource() == null;
    }

    /**
     * Check if the editPart respect conditions to be decorate.
     * 
     * @param editPart
     *            the editPart to check
     * @return true if the editPart respect conditions to be decorate, false otherwise
     */
    @Override
    protected boolean shouldBeDecorated(final EditPart editPart) {
        return editPart instanceof IDiagramElementEditPart && super.shouldBeDecorated(editPart);
    }

    /**
     * Get the decoration image.<br>
     * 
     * @param editPart
     *            the edit part to get the decoration image from
     * @return <code>null</code> if no image found.
     */
    protected Image getDecorationImage(EditPart editPart) {
        Image decorationImage = null;
        if (editPart instanceof IDiagramElementEditPart) {
            IDiagramElementEditPart part = (IDiagramElementEditPart) editPart;

            Boolean isBroken = null;
            // Case 1 : permission authority forbids the edition of the semantic
            // element associated to this edit part
            if (isDecorableEditPart(part)) {
                IPermissionAuthority auth = PermissionAuthorityRegistry.getDefault().getPermissionAuthority(part.getEditingDomain().getResourceSet());
                if (auth != null) {
                    EObject representedObject = part.resolveTargetSemanticElement();
                    isBroken = isBroken(representedObject);
                    if (!isBroken) {
                        decorationImage = getLockStatusDecorationImage(auth.getLockStatus(representedObject));
                    }
                }
            }

            // Case 2 : edit part is broken
            if (decorationImage == null) {
                if ((isBroken != null && isBroken.booleanValue()) || (isBroken == null && isBroken(part))) {
                    // If the edit part is broken, we return a "deleted" image (red cross)
                    decorationImage = DiagramUIPlugin.getPlugin().getImage(DiagramUIPlugin.Implementation.getBundledImageDescriptor(DiagramImagesPath.DELETED_DIAG_ELEM_DECORATOR_ICON));
                }
            }
        }
        return decorationImage;
    }

    /**
     * Return the image corresponding to the given {@link LockStatus}.
     * 
     * @param lockStatus
     *            the lock status of the element to decorate.
     * @return the image corresponding to the given {@link LockStatus}
     */
    protected Image getLockStatusDecorationImage(LockStatus lockStatus) {
        if (LockStatus.LOCKED_BY_OTHER.equals(lockStatus)) {
            // It means that the semantic element referenced by this edit part
            // is not editable, we return a "locked" image (red padlock)
            return DiagramUIPlugin.getPlugin().getImage(SiriusEditPlugin.Implementation.getBundledImageDescriptor("icons/full/decorator/permission_denied.gif")); //$NON-NLS-1$
        }
        return null;
    }
}

Back to the top