NewHandler.java 1.58 KB
Newer Older
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
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;
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();
			final Resource energyCatalogResource = domain.createResource("file://" + pathToNewCatalog);
			final EObject energyCatalog = EcoreUtil.create(BuildingPhysicsPackage.eINSTANCE.getBuildingPhysicsCatalog());
			energyCatalogResource.getContents().add(energyCatalog);

			// store new (empty) resource at selected path
			try {
				energyCatalogResource.save(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			HandlerUtils.createNewPartWithCatalog(application.getContext(), partService, energyCatalogResource);
		}
	}
}