Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4e5fad84b880c79acf8b37f117546a38dbb0a6ca (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST, Christian W. Damus, and others.
 *
 * 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:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905
 *
 *****************************************************************************/
package org.eclipse.papyrus.editor.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.editor.Activator;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.internationalization.utils.utils.LabelInternationalization;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;

/**
 * A command to be used with the Eclipse Commands Framework.
 * This command allows to rename a nested editor.
 *
 * This command use a Transaction.
 * TODO Move to gmf adapter.
 *
 * @author cedric dumoulin
 *
 */
public class RenameNestedEditorCommand extends AbstractHandler {

	/**
	 * Check if the Command is enabled.
	 */
	@Override
	public void setEnabled(Object evaluationContext) {
	}

	/**
	 * Execute the command. This method is called when the action is triggered.
	 *
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		// try {
		// IEditorPart part = HandlerUtil.getActiveEditor(event);
		// IPageMngr pageMngr = (IPageMngr)part.getAdapter(IPageMngr.class);
		// ISashWindowsContainer container = (ISashWindowsContainer)part.getAdapter(ISashWindowsContainer.class);
		// IPage sashPage = container.getActiveSashWindowsPage();
		//
		// if(sashPage instanceof IEditorPage )
		// {
		// IEditorPage editorPage = (IEditorPage)sashPage;
		// execute(editorPage.getRawModel(), editorPage.getIEditorPart());
		// }
		// // Bug from sash Di to be corrected
		// if(pageIdentifier instanceof PageRef)
		// {
		// pageIdentifier = ((PageRef)pageIdentifier).getPageIdentifier();
		// }
		// execute(sashPage.getRawModel(), sashPage.);
		//
		// } catch (NullPointerException e) {
		// // PageMngr can't be found
		// return null;
		// }



		return null;
	}

	/**
	 * Close selected page.
	 *
	 * @param pageMngr
	 */
	public void execute(final Diagram diagram, final IEditorPart editorPart) {
		TransactionalEditingDomain editingDomain = null;

		if (editorPart instanceof IAdaptable) {
			ServicesRegistry registry = ((IAdaptable) editorPart).getAdapter(ServicesRegistry.class);

			if (registry == null) {
				editingDomain = ((IAdaptable) editorPart).getAdapter(TransactionalEditingDomain.class);
			} else {
				try {
					editingDomain = ServiceUtils.getInstance().getTransactionalEditingDomain(registry);
				} catch (ServiceException ex) {
					Activator.log.error(ex);
				}
			}
		}

		if (editingDomain != null) {
			// If the diagram label is available, modify this one.
			final String diagramLabel = LabelInternationalization.getInstance().getDiagramLabelWithoutName(diagram);
			if (null != diagramLabel) {
				InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Rename diagram label", "New label:", diagramLabel, null); //$NON-NLS-1$ //$NON-NLS-2$
				if (Window.OK == dialog.open()) {
					final String label = dialog.getValue();
					editingDomain.getCommandStack().execute(LabelInternationalization.getInstance().getSetDiagramLabelCommand(editingDomain, diagram, label, null));
				}
			} else {
				InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Rename an existing diagram", "New name:", diagram.getName(), null); //$NON-NLS-1$ //$NON-NLS-2$
				if (dialog.open() == Window.OK) {
					final String name = dialog.getValue();
					if (name != null && name.length() > 0) {

						Command command = new RecordingCommand(editingDomain) {


							@Override
							protected void doExecute() {
								diagram.setName(name);
							}
						};

						editingDomain.getCommandStack().execute(command);
					}
				}
			}
		}
	}

}

Back to the top