Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad8b695186690de12a7bac599f0bab150820ddae (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*******************************************************************************
 * Copyright (c) 2010, 2011 THALES GLOBAL SERVICES.
 * 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.ui.tools.api.command;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.google.common.collect.Iterables;

import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync.ResourceStatus;
import org.eclipse.sirius.common.tools.api.util.TreeItemWrapper;
import org.eclipse.sirius.common.ui.SiriusTransPlugin;
import org.eclipse.sirius.common.ui.tools.api.selection.EMFMessageDialog;
import org.eclipse.sirius.common.ui.tools.api.selection.EObjectSelectionWizard;
import org.eclipse.sirius.DAnalysisSessionEObject;
import org.eclipse.sirius.DRepresentation;
import org.eclipse.sirius.DRepresentationElement;
import org.eclipse.sirius.SiriusPlugin;
import org.eclipse.sirius.business.api.helper.SelectionDescriptionHelper;
import org.eclipse.sirius.business.api.helper.SiriusUtil;
import org.eclipse.sirius.business.api.query.URIQuery;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.description.tool.SelectModelElementVariable;
import org.eclipse.sirius.provider.SiriusEditPlugin;
import org.eclipse.sirius.tools.api.command.ui.UICallBack;
import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;
import org.eclipse.sirius.ui.business.api.resource.LoadEMFResourceRunnableWithProgress;
import org.eclipse.sirius.ui.tools.api.Messages;
import org.eclipse.sirius.ui.tools.api.views.ViewHelper;

/**
 * Implementation of the {@link UICallBack} interface using SWT.
 * 
 * @author mchauvin
 * @since 2.6
 */
public abstract class AbstractSWTCallback implements UICallBack {

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#askForVariableValues(org.eclipse.emf.ecore.EObject,
     *      org.eclipse.sirius.description.tool.SelectModelElementVariable)
     */
    public Collection<EObject> askForVariableValues(final EObject model, final SelectModelElementVariable variable) throws InterruptedException {
        Collection<EObject> variableValues = new ArrayList<EObject>();
        final TreeItemWrapper input = new TreeItemWrapper(null, null);
        computeInput(model, variable, input);
        final EObjectSelectionWizard wizard = new EObjectSelectionWizard(EObjectSelectionWizard.WIZARD_GENERIC_DIALOG_TITLE, variable.getMessage(), null, input,
                ViewHelper.INSTANCE.createAdapterFactory());
        wizard.setMany(variable.isMultiple());
        final WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
        final int result = dialog.open();
        if (result == Window.OK) {
            if (!variable.isMultiple() && wizard.getSelectedEObject() != null) {
                variableValues.add(wizard.getSelectedEObject());
            } else {
                final EList<EObject> value = new BasicEList<EObject>();
                value.addAll(wizard.getSelectedEObjects());
                variableValues = value;
            }
        } else {
            throw new InterruptedException();
        }
        return variableValues;
    }

    private void computeInput(final EObject model, final SelectModelElementVariable variable, final TreeItemWrapper input) {
        if (model != null) {
            final IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(model);
            if (variable.getCandidatesExpression() != null) {
                final DRepresentation representation = model instanceof DRepresentationElement ? SiriusUtil.findRepresentation((DRepresentationElement) model) : null;
                interpreter.setVariable(this.getVariableNameForRepresentation(), representation);
                SelectionDescriptionHelper.computeInput(variable, model, interpreter, input);
                interpreter.unSetVariable(this.getVariableNameForRepresentation());
            }
        }
    }

    /**
     * Get the interpreter variable name for representation.
     * 
     * @return the variable interpreter name for representation
     */
    protected abstract String getVariableNameForRepresentation();

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#askForDetailName(java.lang.String)
     */
    public String askForDetailName(final String defaultName) throws InterruptedException {
        return askForDetailName(defaultName, null);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#askForDetailName(java.lang.String)
     */
    public String askForDetailName(final String defaultName, final String representationDescription) throws InterruptedException {
        String description = null;
        if (representationDescription != null && representationDescription.trim().length() > 0) {
            description = Messages.createRepresentationInputDialog_RepresentationDescriptionLabel + representationDescription;
        }
        if (description == null) {
            description = "";
        } else {
            description += "\n\n";
        }
        description += Messages.createRepresentationInputDialog_NewRepresentationNameLabel;
        final InputDialog askSiriusName = new InputDialog(Display.getDefault().getActiveShell(), Messages.createRepresentationInputDialog_Title, description, defaultName, new IInputValidator() {

            public String isValid(final String newText) {
                return null;
            }
        });
        if (askSiriusName.open() == Window.OK) {
            return askSiriusName.getValue();
        }
        throw new InterruptedException("Cancel");
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#openEObjectsDialogMessage(java.util.Collection,
     *      java.lang.String, java.lang.String)
     */
    public boolean openEObjectsDialogMessage(final Collection<EObject> objects, final String title, final String message) {
        return EMFMessageDialog.openQuestionWithEObjects(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), ViewHelper.INSTANCE.createAdapterFactory(), objects, title, message);
    }

    /**
     * 
     * {@inheritDoc}
     */
    public void openRepresentation(final Session openedSession, final DRepresentation representation) {
        try {
            new ProgressMonitorDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell()).run(false, false, new IRunnableWithProgress() {

                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    DialectUIManager.INSTANCE.openEditor(openedSession, representation, monitor);
                }
            });
        } catch (InvocationTargetException e) {
            SiriusEditPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SiriusEditPlugin.ID, e.getLocalizedMessage(), e));
        } catch (InterruptedException e) {
            SiriusEditPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SiriusEditPlugin.ID, e.getLocalizedMessage(), e));
        }
    }

    /**
     * {@inheritDoc}
     */
    public Resource loadResource(final EditingDomain domain, final IFile file) {
        final LoadEMFResourceRunnableWithProgress operation = new LoadEMFResourceRunnableWithProgress(domain.getResourceSet(), file);
        try {
            operation.run(new NullProgressMonitor());
        } catch (final InvocationTargetException e) {
            SiriusTransPlugin.INSTANCE.error("error loading EMF resource", e);
        } catch (final InterruptedException e) {
            SiriusTransPlugin.INSTANCE.error("error loading EMF resource", e);
        }
        return operation.getLoadedResource();
    }

    /**
     * {@inheritDoc}
     * 
     * @throws InterruptedException
     */
    public Collection<EObject> askForEObjects(String message, TreeItemWrapper input, AdapterFactory factory) throws InterruptedException {
        final EObjectSelectionWizard wizard = new EObjectSelectionWizard(EObjectSelectionWizard.WIZARD_GENERIC_DIALOG_TITLE, message, null, input, factory);
        wizard.setMany(true);
        final WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
        final int result = dialog.open();
        if (result == Window.OK) {
            return wizard.getSelectedEObjects();
        }
        throw new InterruptedException();
    }

    /**
     * {@inheritDoc}
     * 
     * @throws InterruptedException
     */
    public EObject askForEObject(String message, TreeItemWrapper input, AdapterFactory factory) throws InterruptedException {
        final EObjectSelectionWizard wizard = new EObjectSelectionWizard(EObjectSelectionWizard.WIZARD_GENERIC_DIALOG_TITLE, message, null, input, factory);
        wizard.setMany(false);
        final WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
        final int result = dialog.open();
        if (result == Window.OK) {
            return wizard.getSelectedEObject();
        }
        throw new InterruptedException();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#shouldReload(Resource)
     */
    public boolean shouldReload(final Resource resource) {
        return openQuestion("Reload the resource?", getCommonMessage(resource) + "externally changed, should we reload it?");
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#shouldRemove(Resource)
     */
    public boolean shouldRemove(final Resource resource) {
        return openQuestion("Remove the resource?", getCommonMessage(resource) + "externally deleted, should we remove it and lose changes?");
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#shouldClose(Session,
     *      Resource)
     */
    public boolean shouldClose(final Session session, final Resource resource) {
        return openQuestion("Close the representations file?", getCommonMessage(resource) + "deleted and contains critical data, should we close it?");
    }

    /**
     * Convenience method to open a simple Yes/No question dialog.
     * 
     * @param title
     *            the dialog's title, or <code>null</code> if none
     * @param message
     *            the message
     * @return <code>true</code> if the user presses the Yes button,
     *         <code>false</code> otherwise
     */
    private boolean openQuestion(final String title, final String message) {
        if (inUIThread()) {
            return MessageDialog.openQuestion(getActiveShell(), title, message);
        } else {
            RunnableWithResult<Boolean> reload = new RunnableWithResult.Impl<Boolean>() {
                public void run() {
                    setResult(MessageDialog.openQuestion(getActiveShell(), title, message));
                }
            };
            PlatformUI.getWorkbench().getDisplay().syncExec(reload);
            return reload.getResult();
        }
    }

    /**
     * Return an expression describing what is saving :
     * <UL>
     * <LI>"Models" if only semantic files have been modified,</LI>
     * <LI>"Representations" if only representations files have been modified,</LI>
     * <LI>"Models and Representations" if both.</LI>
     * </UL>
     * suffixed with :
     * <UL>
     * <LI>"project's session name" if the session has a second segment (that is
     * the project name).</LI>
     * <LI>"toPlatformString URI" if the session uses a InMemoryQuery</LI>
     * <LI>"the toString URI" otherwise.</LI>
     * </UL>
     * <BR>
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#getSessionDisplayed(org.eclipse.sirius.business.api.session.Session)
     */
    public String getSessionNameToDisplayWhileSaving(Session session) {
        String name = "";
        if (session != null) {
            String projectName = null;
            Resource representationsFileResource = session.getSessionResource();
            URI representationsFileURI = representationsFileResource.getURI();
            if (representationsFileURI.segments().length > 1) {
                projectName = representationsFileURI.segment(1);
            }
            if (ResourceSetSync.getStatus(representationsFileResource).equals(ResourceStatus.SYNC)) {
                name = "Models";
            } else {
                if (semanticResourcesDirty(session)) {
                    name = "Models and Representations";
                } else {
                    name = "Representations";
                }
            }
            URIQuery uriQuery = new URIQuery(representationsFileURI);
            if (projectName != null) {
                name += " for project " + projectName;
            } else if (uriQuery.isInMemoryURI()) {
                name += " for \"" + uriQuery.toPlatformString() + "\"";
            } else {
                name += " for \"" + representationsFileURI.toString() + "\"";
            }
        }

        return name;
    }

    private boolean semanticResourcesDirty(Session session) {
        for (final Resource semanticResource : getAllSemanticResources(session)) {
            if (!ResourceSetSync.getStatus(semanticResource).equals(ResourceStatus.SYNC)) {
                return true;
            }
        }
        return false;
    }

    private Iterable<Resource> getAllSemanticResources(Session session) {
        if (session instanceof DAnalysisSessionEObject) {
            return Iterables.concat(session.getSemanticResources(), ((DAnalysisSessionEObject) session).getControlledResources());
        }
        return session.getSemanticResources();
    }

    private String getCommonMessage(final Resource resource) {
        return "The " + resource.getURI() + " resource has been ";
    }

    private boolean inUIThread() {
        return Display.getCurrent() != null;
    }

    private Shell getActiveShell() {
        return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.sirius.tools.api.command.ui.UICallBack#openError(java.lang.String,
     *      java.lang.String)
     */
    public void openError(String title, String message) {
        if (inUIThread()) {
            MessageDialog.openError(getActiveShell(), title, message);
        }
    }

}

Back to the top