Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a0bfc7a472840bccb4d3d8dcb8275a423c915af5 (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
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) 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:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: OpenMindmapDBEditor.java,v 1.1 2008/03/16 21:21:14 mtaal Exp $
 */
package org.eclipse.gmf.examples.mindmap.diagram.db;

import java.io.IOException;
import java.util.Collections;

import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.teneo.hibernate.resource.HibernateXMLResource;
import org.eclipse.gmf.examples.mindmap.Map;
import org.eclipse.gmf.examples.mindmap.MindmapFactory;
import org.eclipse.gmf.examples.mindmap.diagram.edit.parts.MapEditPart;
import org.eclipse.gmf.examples.mindmap.diagram.part.MindmapDiagramEditor;
import org.eclipse.gmf.examples.mindmap.diagram.part.MindmapDiagramEditorPlugin;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
 * Rather generic class which opens an editor using the static ID member in the
 * MindmapDiagramDBEditor class.
 * 
 * @author mtaal@elver.org
 */
public class OpenMindmapDBEditor implements IWorkbenchWindowActionDelegate {

	private IWorkbenchWindow window;

	public void dispose() {
	}

	public void init(IWorkbenchWindow window) {
		this.window = window;
		StoreController.getInstance().initializeDataStore();
	}

	public void selectionChanged(IAction action, ISelection selection) {
	}

	public void run(IAction action) {

		try {
			initializeData();
			final IWorkbenchPage page = window.getActivePage();
			page.openEditor(new URIEditorInput(StoreController.DATABASE_URI),
					MindmapDiagramEditor.ID);
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}

	// Ensure that the resource at least contains a Map and a diagram object
	private void initializeData() {
		try {
			final Resource res = new HibernateXMLResource(
					StoreController.DATABASE_URI);
			res.load(Collections.EMPTY_MAP);
			boolean addMap = true;
			boolean addDiagram = true;
			Map map = null;
			for (Object o : res.getContents()) {
				if (o instanceof Map) {
					addMap = false;
					map = (Map) o;
				}
				if (o instanceof Diagram) {
					addDiagram = false;
				}
			}
			if (addMap) {
				map = MindmapFactory.eINSTANCE.createMap();
				res.getContents().add(map);
			}
			if (addDiagram) {
				Diagram diagram = ViewService.createDiagram(map,
						MapEditPart.MODEL_ID,
						MindmapDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
				res.getContents().add(diagram);
				diagram.setName("mindmap");
				diagram.setElement(map);
			}
			if (addMap || addDiagram) {
				res.save(Collections.EMPTY_MAP);
			}
			res.unload();
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}
	}
}

Back to the top