Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1425cf69fcc9305fab50b1c5cc5d0d9fdd6b3771 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*******************************************************************************
 * Copyright (c) 2007, 2015 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.api.policy;

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

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.sirius.diagram.ui.provider.Messages;

/**
 * This edit policy is composed of many edit policies.
 *
 * @author ymortier
 */
public class CompoundEditPolicy implements EditPolicy {

    /** The delegated edit policies. */
    private List<EditPolicy> delegatedEditPolicies = new LinkedList<EditPolicy>();

    /** The host. */
    private EditPart host;

    /**
     * if <code>true</code> then null command are added to the resulted compound
     * command (so the command is unexecutable).
     */
    private boolean allowNullCommand;

    /**
     * <code>true</code> if the resulted command can contain null value.
     *
     * @param allowNullCommand
     *            <code>true</code> if the resulted command can contain null
     *            value.
     */
    public void setAllowNullCommand(final boolean allowNullCommand) {
        this.allowNullCommand = allowNullCommand;
    }

    /**
     * Returns <code>true</code> if the resulted command can contain null value.
     *
     * @return <code>true</code> if the resulted command can contain null value.
     */
    public boolean isAllowNullCommand() {
        return allowNullCommand;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#activate()
     */
    @Override
    public void activate() {
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().activate();
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#deactivate()
     */
    @Override
    public void deactivate() {
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().deactivate();
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#eraseSourceFeedback(org.eclipse.gef.Request)
     */
    @Override
    public void eraseSourceFeedback(final Request request) {
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().eraseSourceFeedback(request);
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#eraseTargetFeedback(org.eclipse.gef.Request)
     */
    @Override
    public void eraseTargetFeedback(final Request request) {
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().eraseTargetFeedback(request);
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#getCommand(org.eclipse.gef.Request)
     */
    @Override
    public Command getCommand(final Request request) {
        CompoundCommand ret = null;
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.listIterator();
        while (iterEditPolicies.hasNext()) {
            final EditPolicy editPolicy = iterEditPolicies.next();
            final Command command = editPolicy.getCommand(request);
            if (command != null || isAllowNullCommand()) {
                if (ret == null) {
                    ret = new CompoundCommand();
                }
                ret.add(command);
            }
        }
        /*
         * If there are only null commands in the result, we should return null.
         */
        boolean containsOnlyNullCommands = true;
        if (ret != null) {
            for (final Object c : ret.getCommands()) {
                if (c != null) {
                    containsOnlyNullCommands = false;
                    break;
                }
            }
        }
        return containsOnlyNullCommands ? null : ret;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#getHost()
     */
    @Override
    public EditPart getHost() {
        return this.host;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#getTargetEditPart(org.eclipse.gef.Request)
     */
    @Override
    public EditPart getTargetEditPart(final Request request) {
        EditPart res = null;
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext() && res == null) {
            final EditPolicy next = iterEditPolicies.next();
            res = next.getTargetEditPart(request);
        }
        return res;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#setHost(org.eclipse.gef.EditPart)
     */
    @Override
    public void setHost(final EditPart editpart) {
        this.host = editpart;
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().setHost(editpart);
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#showSourceFeedback(org.eclipse.gef.Request)
     */
    @Override
    public void showSourceFeedback(final Request request) {
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().showSourceFeedback(request);
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#showTargetFeedback(org.eclipse.gef.Request)
     */
    @Override
    public void showTargetFeedback(final Request request) {
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext()) {
            iterEditPolicies.next().showTargetFeedback(request);
        }
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.gef.Request)
     */
    @Override
    public boolean understandsRequest(final Request request) {
        boolean res = false;
        final Iterator<EditPolicy> iterEditPolicies = this.delegatedEditPolicies.iterator();
        while (iterEditPolicies.hasNext() && !res) {
            res = iterEditPolicies.next().understandsRequest(request);
        }
        return res;
    }

    /**
     * Adds an edit policy.
     *
     * @param editPolicy
     *            the edit policy to add.
     */
    public void addEditPolicy(final EditPolicy editPolicy) {
        if (editPolicy == null) {
            throw new IllegalArgumentException(Messages.CompoundEditPolicy_nullEditPolicyMsg);
        }
        this.delegatedEditPolicies.add(editPolicy);
    }

    /**
     * Removes an edit policy.
     *
     * @param policy
     *            the policy to remove.
     */
    public void removeEditPolicy(final EditPolicy policy) {
        this.delegatedEditPolicies.remove(policy);
    }

    /**
     * Return all edit policies.
     *
     * @return all edit policies.
     */
    public List<EditPolicy> getEditPolicies() {
        return this.delegatedEditPolicies;
    }

}

Back to the top