Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f98fc9b238037bd962a8e7309da3dc28d1817ed0 (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
/*******************************************************************************
 * Copyright (c) 2004 Peter Nehrer and Composent, Inc.
 * 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:
 *     Peter Nehrer - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.example.sdo.editor;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.sdo.ISharedDataGraph;
import org.eclipse.ecf.sdo.IUpdateConsumer;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.sdo.EDataGraph;
import org.eclipse.emf.ecore.sdo.presentation.SDOEditor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;

import commonj.sdo.ChangeSummary;
import commonj.sdo.DataGraph;

/**
 * @author pnehrer
 */
public class SharedSDOEditor extends SDOEditor {

    private class UpdateConsumer implements IUpdateConsumer {
        public boolean consumeUpdate(ISharedDataGraph graph, ID containerID) {
            ChangeSummary changeSummary = graph.getDataGraph()
                    .getChangeSummary();
            changeSummary.endLogging();
            SharedSDOEditor.super.doSave(null);
            changeSummary.beginLogging();
            return true;
        }

        public void updateFailed(ISharedDataGraph graph, ID containerID,
                Throwable cause) {
            EditorPlugin.getDefault().log(
                    new CoreException(new Status(Status.ERROR, EditorPlugin
                            .getDefault().getBundle().getSymbolicName(), 0,
                            "Data graph upate failed.", cause)));
        }
    }

    private ISharedDataGraph sharedDataGraph;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.emf.ecore.sdo.presentation.SDOEditor#init(org.eclipse.ui.IEditorSite,
     *      org.eclipse.ui.IEditorInput)
     */
    public void init(IEditorSite site, IEditorInput editorInput)
            throws PartInitException {
        if (editorInput instanceof IFileEditorInput)
            try {
                EditorPlugin.getDefault()
                        .checkConnected(
                                ((IFileEditorInput) editorInput).getFile()
                                        .getProject());
            } catch (ECFException e) {
                EditorPlugin.getDefault().log(e);
            	sharedDataGraph = null;
            }

        super.init(site, editorInput);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.emf.ecore.sdo.presentation.SDOEditor#createModel()
     */
    public void createModel() {
        try {
            IFileEditorInput modelFile = (IFileEditorInput) getEditorInput();
            String path = modelFile.getFile().getFullPath().toString();
            URI uri = URI.createPlatformResourceURI(modelFile.getFile()
                    .getFullPath().toString());
            if (EditorPlugin.getDefault().isPublished(path)) {
                sharedDataGraph = EditorPlugin.getDefault().subscribe(path,
                        new UpdateConsumer());
                EDataGraph dataGraph = (EDataGraph) sharedDataGraph
                        .getDataGraph();
                dataGraph.getDataGraphResource().setURI(uri);
                editingDomain.getResourceSet().getResources().addAll(
                        dataGraph.getResourceSet().getResources());
                dataGraph.setResourceSet(editingDomain.getResourceSet());
            } else {
                Resource resource = editingDomain.loadResource(uri.toString());
                DataGraph dataGraph = (DataGraph) resource.getContents().get(0);
                sharedDataGraph = EditorPlugin.getDefault().publish(path,
                        dataGraph, new UpdateConsumer());
            }
        } catch (ECFException e) {
            EditorPlugin.getDefault().log(e);
            if (sharedDataGraph != null) {
                sharedDataGraph.dispose();
                sharedDataGraph = null;
            }
            super.createModel();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.emf.ecore.sdo.presentation.SDOEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
     */
    public void doSave(IProgressMonitor progressMonitor) {
        super.doSave(progressMonitor);
        if (sharedDataGraph != null)
            try {
                sharedDataGraph.commit();
            } catch (ECFException e) {
                EditorPlugin.getDefault().log(e);
            }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.emf.ecore.sdo.presentation.SDOEditor#dispose()
     */
    public void dispose() {
        if (sharedDataGraph != null)
            sharedDataGraph.dispose();

        super.dispose();
    }
}

Back to the top