Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29651566bf54b7f17ba02ef625b280c9a3972139 (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
/*******************************************************************************
 * Copyright (c) 2009, 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.internal.resource;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument;
import org.eclipse.gmf.runtime.emf.core.resources.GMFResourceFactory;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
import org.eclipse.sirius.diagram.ui.provider.Messages;
import org.eclipse.sirius.diagram.ui.tools.api.util.GMFNotationHelper;
import org.eclipse.ui.IEditorInput;

/**
 * Provide an "error" diagram if initial resource could not be found.
 *
 * @author mchauvin
 */
public class ResourceMissingDocumentProvider {

    private ResourceSet errorResourceSet;

    private Resource errorResource;

    private Diagram errorDiagram;

    /**
     * Set the document content.
     *
     * @param document
     *            the document
     * @param element
     *            the element as editor input
     */
    public void setDocumentContent(final IDocument document, final IEditorInput element) {
        // TODO get diagram name
        document.setContent(getErrorDiagram(Messages.ResourceMissingDocumentProvider_defaultMessage));
    }

    /**
     * Set the document content.
     *
     * @param document
     *            the document
     * @param element
     *            the element as editor input
     * @param noteMessage
     *            the message to write in the note
     */
    public void setDocumentContent(final IDocument document, final IEditorInput element, final String noteMessage) {
        // TODO get diagram name
        document.setContent(getErrorDiagram(noteMessage));
    }

    private Diagram getErrorDiagram(final String noteMessage) {
        if (errorDiagram == null) {
            errorResourceSet = new ResourceSetImpl();
            final TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain(errorResourceSet);
            errorResource = new GMFResourceFactory().createResource(URI.createURI("error://diagram")); //$NON-NLS-1$
            errorResourceSet.getResources().add(errorResource);
            errorDiagram = NotationFactory.eINSTANCE.createDiagram();
            domain.getCommandStack().execute(new AddErrorCommand(domain, errorDiagram, errorResource, noteMessage));
        }
        return errorDiagram;
    }

    /**
     * Dispose the created resources.
     */
    public void dispose() {
        if (errorResourceSet != null) {
            TransactionUtil.getEditingDomain(errorResourceSet).dispose();
            errorResource = null;
            errorDiagram = null;
        }
    }

    /**
     * Specific command to add error diagram in error resource.
     *
     * @author mporhel
     */
    private class AddErrorCommand extends RecordingCommand {

        private final Diagram diagram;

        private final String noteMessage;

        private final Resource resource;

        /**
         * Constructor.
         *
         * @param domain
         *            the editing domain.
         * @param noteMessage
         * @param errorResource
         * @param errorDiagram
         * @param editpart
         *            the source edit part.
         * @param representation
         *            the source representation.
         * @param session
         *            the current session.
         */
        AddErrorCommand(TransactionalEditingDomain domain, Diagram diagram, Resource resource, String noteMessage) {
            super(domain, Messages.AddErrorCommand_label);
            this.diagram = diagram;
            this.resource = resource;
            this.noteMessage = noteMessage;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void doExecute() {
            if (errorResource == null || diagram == null) {
                return;
            }

            resource.getContents().add(diagram);
            GMFNotationHelper.createNote(diagram, noteMessage, DiagramUIPlugin.getPlugin().getPreferenceStore());
        }
    }
}

Back to the top