Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d082bac9710559d678b236d6558ff520f9d3b6f (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
110
111
112
113
114
115
116
117
118
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * 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:
 *  Tristan Faure (Atos Origin) tristan.faure@atosorigin.com - Initial API and implementation
 *
  *****************************************************************************/
package org.eclipse.papyrus.resource.migration.popup.actions;

import java.io.IOException;
import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.resource.migration.controller.GenModelUpdater;
import org.eclipse.papyrus.resource.migration.controller.exceptions.URINotValidException;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class UpdateGenModelAction implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public UpdateGenModelAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		if (PlatformUI.getWorkbench() != null) {
			IWorkbench workbench = PlatformUI.getWorkbench();
			if (workbench.getActiveWorkbenchWindow() != null) {
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				if (window.getSelectionService() != null) {
					ISelectionService service = window.getSelectionService();
					ISelection selection = service.getSelection();
					if (selection instanceof IStructuredSelection) {
						final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
						BusyIndicator.showWhile(shell.getDisplay(), new Runnable() {

							public void run() {
								runProcess(structuredSelection);
							}
						});
					}
				}
			}
		}
	}

	/**
	 * perform the process
	 * 
	 * @param structuredSelection
	 */
	private void runProcess(final IStructuredSelection structuredSelection) {
		try {
			Iterator<?> iterator = structuredSelection.iterator();
			GenModelUpdater updater = new GenModelUpdater();
			while (iterator.hasNext()) {
				Object next = iterator.next();
				if (next instanceof IFile) {
					IFile ifile = (IFile) next;
					updater.transform(URI.createURI(ifile.getLocationURI().toString()));
					ifile.getParent().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
				}
			}
			MessageDialog.openInformation(shell, "Finish", "The genmodel has been updated");
		} catch (URINotValidException e) {
			MessageDialog.openError(shell, "Error", e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			MessageDialog.openError(shell, "Error", "An error occurs during save process");
			e.printStackTrace();
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}

Back to the top