Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0cd6837d3a99076f73f3e50d47b586b3300a26d4 (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
/*******************************************************************************
 * Copyright (c) 2017, 2018 THALES GLOBAL SERVICES 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.editor.tabbar;

import java.util.Collections;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.TransactionImpl;
import org.eclipse.gmf.runtime.diagram.ui.actions.CheckedPropertyAction;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.sirius.common.ui.tools.api.util.EclipseUIUtil;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DiagramPlugin;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
import org.eclipse.sirius.diagram.ui.provider.Messages;
import org.eclipse.sirius.diagram.ui.tools.api.editor.DDiagramEditor;
import org.eclipse.sirius.diagram.ui.tools.api.image.DiagramImagesPath;
import org.eclipse.sirius.diagram.ui.tools.api.ui.actions.ActionIds;
import org.eclipse.sirius.diagram.ui.tools.internal.editor.tabbar.contributions.ModesMenuManager;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.EditorActionBarContributor;

/**
 * An action that switches the showing mode of the given {@link DDiagram}.
 * 
 * @author <a href="mailto:pierre.guilet@obeo.fr">Pierre Guilet</a>
 *
 */
public class VisibilityModeSwitchingAction extends CheckedPropertyAction {

    /**
     * Icon used in the tabbar to allow end-user to activate the showing mode. It is also used in the editor's status
     * line to indicate that showing mode is active.
     */
    private static final ImageDescriptor ACTIVATE_SHOW_HIDE_MODE_IMAGE_DESCRIPTOR = DiagramUIPlugin.Implementation.getBundledImageDescriptor(DiagramImagesPath.SHOWING_MODE_ACTIVE_ICON);

    /**
     * The {@link DDiagram} on witch the showing mode should be switched.
     */
    private DDiagram ddiagram;

    /**
     * The {@link DDiagramEditor} containing the tabbar containing this action.
     */
    private DDiagramEditor editor;

    /**
     * The {@link ModesMenuManager} handling this action.
     */
    private ModesMenuManager modesMenuManager;

    /**
     * Default constructor.
     * 
     * @param page
     *            The workbench part associated with this action
     *
     * @param editor
     *            The {@link DDiagramEditor} containing the tabbar containing this action.
     * @param editorDiagram
     *            the {@link DDiagram} on witch the showing mode should be switched
     * @param modesMenuManager
     *            The {@link ModesMenuManager} handling this action.
     */
    public VisibilityModeSwitchingAction(IWorkbenchPage page, DDiagramEditor editor, DDiagram editorDiagram, ModesMenuManager modesMenuManager) {
        super(page, ActionIds.SWITCH_SHOWING_MODE, Messages.ChangeEditModeAction_ChangePropertyValueRequest_label, ActionIds.SWITCH_SHOWING_MODE);
        setWorkbenchPart(page.getActivePart());
        setId(ActionIds.SWITCH_SHOWING_MODE);
        setToolTipText(Messages.EditModeAction_Label);
        this.ddiagram = editorDiagram;
        if (ddiagram != null) {
            setImageDescriptor(ACTIVATE_SHOW_HIDE_MODE_IMAGE_DESCRIPTOR);
            setTextAndStatusAccordingToShowingMode();
        }
        this.editor = editor;
        this.modesMenuManager = modesMenuManager;
    }

    /**
     * Switches the text associated to this Action according to the current ShowingMode status (activated or not) and
     * updates this editor's statusLine.
     */
    private void setTextAndStatusAccordingToShowingMode() {
        // Step 1 : updating action's text and image
        setText(Messages.ShowingModeSwitchingAction_label);

        // Step 2 : updating the editor's status bar
        IEditorPart activeEditor = EclipseUIUtil.getActiveEditor();
        if (activeEditor != null) {
            // Step 2.1 : trying to get the status line manager
            IEditorSite site = (IEditorSite) activeEditor.getSite();
            EditorActionBarContributor actionBarContributor = null;
            IStatusLineManager statusLineManager = null;
            if (site != null && site.getActionBarContributor() instanceof EditorActionBarContributor) {
                actionBarContributor = (EditorActionBarContributor) site.getActionBarContributor();
            }
            if (actionBarContributor != null && actionBarContributor.getActionBars() != null) {
                statusLineManager = actionBarContributor.getActionBars().getStatusLineManager();
            }

            // Step 2.2 : if statusLineManager can be found
            if (statusLineManager != null) {
                // we update it according to the DDiagram showing mode status
                if (this.ddiagram != null && this.ddiagram.isIsInShowingMode()) {
                    String statusMessage = Messages.ShowingModeSwitchingAction_statusOn;
                    statusLineManager.setMessage(DiagramUIPlugin.getPlugin().getImage(ACTIVATE_SHOW_HIDE_MODE_IMAGE_DESCRIPTOR), statusMessage);
                } else {
                    statusLineManager.setMessage(""); //$NON-NLS-1$
                }
            }
        }
    }

    @Override
    public int getStyle() {
        return AS_CHECK_BOX;
    }

    @Override
    protected boolean isSelectionListener() {
        return false;
    }

    @Override
    protected boolean calculateEnabled() {
        return ddiagram != null && editor != null;
    }

    @Override
    protected void doRun(IProgressMonitor progressMonitor) {
        TransactionalEditingDomain editingDomain = (TransactionalEditingDomain) editor.getEditingDomain();
        if (editingDomain != null) {

            // We don't use a command stack because we don't want the mode update to be undone
            TransactionImpl t = new TransactionImpl(editingDomain, false, Collections.EMPTY_MAP);
            try {
                t.start();
                this.ddiagram.setIsInLayoutingMode(false);
                this.ddiagram.setIsInShowingMode(true);
                t.commit();
            } catch (RollbackException | InterruptedException e) {
                DiagramPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, DiagramPlugin.ID, Messages.ChangeEditModeAction_ChangeFailure, e));
            }

        }
        modesMenuManager.refresh();
        setTextAndStatusAccordingToShowingMode();
    }

    @Override
    protected boolean calculateChecked() {
        return ddiagram != null && ddiagram.isIsInShowingMode();
    }

    @Override
    public void dispose() {
        ddiagram = null;
        modesMenuManager = null;
        editor = null;
        super.dispose();
    }

}

Back to the top