Skip to main content
summaryrefslogtreecommitdiffstats
blob: fc0a1685fba6b8852f1f8665d8098f433fe2beb4 (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
/*******************************************************************************
 * Copyright (c) 2010 BestSolution.at 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:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 ******************************************************************************/
package org.eclipse.e4.tools.emf.editor;

import java.util.Collection;

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.model.application.ui.basic.MBasicFactory;
import org.eclipse.e4.ui.model.application.ui.basic.MInputPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class OpenModelFileHandler {
	@Execute
	public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MApplication application, EModelService modelService, EPartService partService) {
		System.err.println("Execute!");
		FileDialog dialog = new FileDialog(shell);
		String file = dialog.open();
		if( file != null ) {
			String name = file.substring(file.lastIndexOf("/") + 1);
			String filePath = "file://" + file;
			Collection<MInputPart> parts = partService.getInputParts(filePath);
			if( parts.size() == 0 ) {
				MPartStack stack = (MPartStack) modelService.find("org.eclipse.e4.tools.emf.editor.mainwindow.editorstack", application);
				
				try {
					MInputPart part = MBasicFactory.INSTANCE.createInputPart();
					part.setLabel(name);
					part.setTooltip(file);
					part.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.ui/org.eclipse.e4.tools.emf.ui.internal.wbm.ApplicationModelEditor");
					part.setIconURI("platform:/plugin/org.eclipse.e4.tools.emf.editor/icons/full/application_view_tile.png");
					part.setInputURI(filePath);
					
					part.setCloseable(true);
					stack.getChildren().add(part);
					stack.setSelectedElement(part);
					System.err.println("Done");
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				partService.showPart(parts.iterator().next(), PartState.ACTIVATE);
			}
		}
	}
}

Back to the top