Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7ee41c009ccde415613f3d56b5f5c9ba9dfef0e5 (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
/*******************************************************************************
 * 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.actions.repair.commands;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.command.IdentityCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmf.runtime.diagram.ui.internal.editparts.DefaultNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.sirius.business.api.helper.SiriusUtil;
import org.eclipse.sirius.common.tools.DslCommonPlugin;
import org.eclipse.sirius.diagram.ui.provider.Messages;
import org.eclipse.sirius.ui.tools.api.profiler.SiriusTasks;

/**
 * Specific command to remove invalid views.
 * 
 * @author mporhel
 */
@SuppressWarnings("restriction")
public class RemoveInvalidViewsCommand extends IdentityCommand {

    private Resource airdResource;

    /**
     * Default constructor.
     * 
     * @param airdResource
     *            Aird {@link Resource}
     */
    public RemoveInvalidViewsCommand(Resource airdResource) {
        super(Messages.RemoveInvalidViewsCommand_label);
        this.airdResource = airdResource;
    }

    /**
     * Overridden.
     * 
     * {@inheritDoc}
     */
    @Override
    public void execute() {
        final Iterator<EObject> iterContents = airdResource.getAllContents();
        final List<View> viewsToRemove = new LinkedList<View>();
        while (iterContents.hasNext()) {
            final EObject next = iterContents.next();
            if (next instanceof View) {
                final View currentView = (View) next;
                if (!isValid(currentView)) {
                    viewsToRemove.add(currentView);
                }
            }
        }

        for (final View view : viewsToRemove) {
            SiriusUtil.delete(view);
        }
        airdResource = null;
    }

    /**
     * Check if a view is valid.
     * 
     * @param view
     *            the view to check
     * @return <code>true</code>if the view is valid, <code>false</code>
     *         otherwise
     */
    public boolean isValid(final View view) {
        return isValid(view, false);
    }

    /**
     * Check if a view is valid.
     * 
     * @param view
     *            the view to check
     * @param checkElement
     *            true if we must check that the element of this view is not
     *            null and is in a resource
     * @return <code>true</code>if the view is valid, <code>false</code>
     *         otherwise
     */
    public boolean isValid(final View view, boolean checkElement) {
        DslCommonPlugin.PROFILER.startWork(SiriusTasks.IS_VIEW_VALID);
        // Otherwise some providers could be removed from GMF Services
        boolean result = true;
        try {
            if (checkElement) {
                result = view.getElement() != null && view.getElement().eResource() != null;
            }
            result = result && !(EditPartService.getInstance().createGraphicEditPart(view) instanceof DefaultNodeEditPart);
            // CHECKSTYLE:OFF
        } catch (final Exception e) {
            // CHECKSTYLE:ON
            // silent
        }
        DslCommonPlugin.PROFILER.stopWork(SiriusTasks.IS_VIEW_VALID);
        return result;
    }

    /**
     * Overridden to avoid the CommandStack to keep a reference to this command.
     * 
     * {@inheritDoc}
     */
    @Override
    public boolean canUndo() {
        return false;
    }
}

Back to the top