NewHandler.java 1.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package de.hftstuttgart.buildingphysics.application.handlers;

import de.hftstuttgart.buildingphysics.BuildingPhysicsPackage;
import java.io.IOException;

import javax.inject.Named;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
15
import org.eclipse.emf.ecore.xmi.XMLResource;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.swt.widgets.Shell;


public class NewHandler {

	@Execute
	public void execute(MApplication application, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
			EPartService partService) {

		final String pathToNewCatalog = HandlerUtils.newFileDialog(shell);

		if (pathToNewCatalog != null) {
			// create new catalog with resource at above path
			final AdapterFactoryEditingDomain domain = HandlerUtils.createEditingDomain();
31
32
33
34
35
36
37
38
39
			final Resource catalogResource = domain.createResource("file://" + pathToNewCatalog);

			// assume default XMI factory was used to create an XML resource
			if (catalogResource instanceof XMLResource) {
				((XMLResource)catalogResource).setEncoding("UTF-8");
			}

			final EObject catalog = EcoreUtil.create(BuildingPhysicsPackage.eINSTANCE.getBuildingPhysicsCatalog());
			catalogResource.getContents().add(catalog);
40
41
42

			// store new (empty) resource at selected path
			try {
43
				catalogResource.save(null);
44
45
46
47
48
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

49
			HandlerUtils.createNewPartWithCatalog(application.getContext(), partService, catalogResource);
50
51
52
		}
	}
}