Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5b90b9116274e166269f552cd518b77762556a06 (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
/*****************************************************************************
 * Copyright (c) 2010, 2013 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - Factor out workspace storage for pluggable storage providers (CDO)
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.toolsmiths.ui;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.papyrus.infra.properties.contexts.Context;
import org.eclipse.papyrus.infra.properties.internal.ui.runtime.IInternalConfigurationManager;
import org.eclipse.papyrus.infra.properties.ui.runtime.PropertiesRuntime;
import org.eclipse.papyrus.views.properties.toolsmiths.Activator;
import org.eclipse.papyrus.views.properties.toolsmiths.storage.actions.IContextCopyAction;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * An action to build a new Property view context from an existing one.
 *
 * @author Camille Letavernier
 */
public class CopyContextAction {

	private IContextCopyAction delegate;

	public CopyContextAction(IContextCopyAction delegate) {
		super();

		this.delegate = delegate;
	}

	/**
	 * Copy an existing context to a new one with the given name.
	 * The new context is registered to the {@link ConfigurationManager}.
	 * To enable the edition of the context, an invisible project is created
	 * in the workspace. The files are stored in the runtime plugin's preference
	 * folder.
	 *
	 * @param source
	 *            The source Context to copy
	 * @param targetName
	 *            The name of the new context
	 * @param activate
	 *            If true, the new context will be activated and available immediately,
	 *            while the previous one will be disabled to avoid conflicts
	 * @return
	 * 		The new Context or {@code null} if it was not created (because of error or user cancellation
	 */
	public Context copy(final Context source, final String targetName, final boolean activate) {
		final Context[] result = { null };

		ProgressMonitorDialog dialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
		try {
			dialog.run(true, true, new IRunnableWithProgress() {

				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					try {
						result[0] = delegate.copy(source, targetName, monitor);

						if (result[0] != null) {
							IInternalConfigurationManager mgr = (IInternalConfigurationManager) PropertiesRuntime.getConfigurationManager();
							mgr.addContext(result[0], activate);
							if (activate) {
								mgr.disableContext(source, true);
							}
						}
					} catch (CoreException ex) {
						throw new InvocationTargetException(ex);
					}
				}

			});
		} catch (InvocationTargetException ex) {
			if (ex.getTargetException() instanceof CoreException) {
				CoreException ce = (CoreException) ex.getTargetException();
				Activator.log.error(ce);
				StatusManager.getManager().handle(ce.getStatus(), StatusManager.SHOW);
			} else {
				Activator.log.error(ex);
			}
		} catch (InterruptedException ex) {
			Activator.log.error(ex);
		}

		return result[0];
	}
}

Back to the top