HandlerUtils.java 2.09 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
46
47
48
49
50
package de.hftstuttgart.buildingphysics.application.handlers;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

class HandlerUtils {
	static final String CATALOG_PARTDESCRIPTOR_ID = "de.hftstuttgart.buildingphysics.application.partdescriptor.catalog";
	
	public static String openFileDialog(Shell shell) {
		return invokeFileDialog(new FileDialog(shell, SWT.OPEN));
	}

	public static String newFileDialog(Shell shell) {
		return invokeFileDialog(new FileDialog(shell, SWT.SAVE));
	}

	private static String invokeFileDialog(FileDialog dialog) {
		dialog.setFilterPath(System.getProperty("user.home"));
		dialog.setFilterNames(new String [] {"Building Physics Catalog", "All Files (*)"});
		dialog.setFilterExtensions(new String [] {"*.buildphys", "*"});
		return dialog.open();
	}
	
	static void createNewPartWithCatalog(IEclipseContext context, EPartService partService, Resource resource) {		
		context.set(Resource.class, resource);
		MPart catalogPart = partService.createPart(CATALOG_PARTDESCRIPTOR_ID);
		catalogPart.setDirty(true); // must listen to adapter!
		catalogPart.setLabel(resource.getURI().lastSegment());
		partService.showPart(catalogPart, PartState.ACTIVATE);
	}
	
	static AdapterFactoryEditingDomain createEditingDomain() {
		final AdapterFactory composedAdapterFactory = new ComposedAdapterFactory(
				ComposedAdapterFactory.Descriptor.Registry.INSTANCE); // no dispose required?
		final AdapterFactoryEditingDomain domain = new AdapterFactoryEditingDomain(composedAdapterFactory,
				new BasicCommandStack());
		return domain;
	}

}