Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5fa285e5a3cac6f825a267846a33939f09c9119d (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) 2008 Conselleria de Infraestructuras y Transporte, Generalitat 
 * de la Comunitat Valenciana . 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: Francisco Javier Cano Muñoz (Prodevelop) - Initial implementation
 *
 ******************************************************************************/
package org.eclipse.papyrus.navigator.actions;

import java.util.Collection;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.ui.action.CommandActionHandler;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.navigator.internal.utils.NavigatorUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.uml2.uml.NamedElement;

/**
 * Action to rename a {@link NamedElement} in the Model Explorer. This action is binded to the "F2"
 * key.
 * 
 * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a>
 * @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=290514">Bug #290514</a>
 */
public class RenameNamedElementAction extends CommandActionHandler {

	/**
	 * Constructor binds this action to the "F2" key.
	 * 
	 * @param editingDomain
	 */
	public RenameNamedElementAction(EditingDomain editingDomain) {
		super(editingDomain, "Rename...");
		setAccelerator(SWT.F2);
	}

	/**
	 * The selected {@link NamedElement}.
	 */
	private NamedElement selectedNamedElement = null;

	/**
	 * Get the selected {@link NamedElement}.
	 * 
	 * @return
	 */
	protected NamedElement getSelectedNamedElement() {
		return selectedNamedElement;
	}

	/**
	 * Set the selected {@link NamedElement}.
	 * 
	 * @param selectedNamedElement
	 */
	// @notused
	protected void setSelectedNamedElement(NamedElement selectedNamedElement) {
		this.selectedNamedElement = selectedNamedElement;
	}

	/**
	 * Update the selection.
	 * 
	 * @return true if this {@link Action} is still enabled after the new selection.
	 */
	@Override
	public boolean updateSelection(IStructuredSelection selection) {
		ISelection unwrappedselection = NavigatorUtils.unwrapSelection(selection);
		if(unwrappedselection instanceof StructuredSelection) {
			StructuredSelection structuredSelection = (StructuredSelection)unwrappedselection;
			if(structuredSelection.size() == 1) {
				Object selectedObject = structuredSelection.getFirstElement();
				if(selectedObject instanceof NamedElement) {
					setSelectedNamedElement((NamedElement)selectedObject);
					return super.updateSelection(selection);
				}
			}
		}
		setSelectedNamedElement(null);
		return super.updateSelection(selection);
	}

	/**
	 * Create the {@link Command} to execute when this {@link Action} is run.
	 */
	@Override
	public Command createCommand(Collection<?> selection) {
		TransactionalEditingDomain domain = getTransactionalEditingDomain();
		if(domain == null) {
			return null;
		}
		return new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Rename an existing diagram", "New name:", getSelectedNamedElement().getName(), null);
				if(dialog.open() == Window.OK) {
					final String name = dialog.getValue();
					getSelectedNamedElement().setName(name);
				}
			}
		};
	}

	/**
	 * Get the {@link EditingDomain} as a {@link TransactionalEditingDomain}.
	 * 
	 * @return
	 */
	protected TransactionalEditingDomain getTransactionalEditingDomain() {
		if(getEditingDomain() instanceof TransactionalEditingDomain) {
			return (TransactionalEditingDomain)getEditingDomain();
		}
		return null;
	}

	/**
	 * Update the active {@link IWorkbenchPart} and the active {@link EditingDomain}.
	 * 
	 * @param workbenchPart
	 */
	public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) {
		if(workbenchPart instanceof IEditingDomainProvider) {
			domain = ((IEditingDomainProvider)workbenchPart).getEditingDomain();
		}
	}
}

Back to the top