Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d971c2b68d189408c2277abdc530555a47265886 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.common.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForIEvaluationContext;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;

/**
 * This handler allows to rename a Papyrus Table. The handler is activated when
 * the active editor is a Papyrus NatTableEditor.
 * 
 * @author Camille Letavernier
 * 
 */
public class RenameTableHandler extends AbstractHandler {

	public static String RenameTableHandler_NewName = "New name:";

	public static String RenameTableHandler_RenameAnExistingTable = "Rename an existing table";

	/**
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 * @param event
	 * @return
	 * @throws ExecutionException
	 * 
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		executeTransaction(event);

		return null;
	}

	/**
	 * Execute as transaction
	 * 
	 * @param event
	 */
	private void executeTransaction(ExecutionEvent event) {

		// Get requested objects
		final INattableModelManager tableManager;
		try {
			IEvaluationContext context = getIEvaluationContext(event);
			tableManager = lookupTableManager(context);
		} catch (ServiceException e) {
			// silently fails
			return;
		}

		if(tableManager == null) {
			return;
		}

		// Open the dialog to ask the new name
		String currentName = tableManager.getTableName();
		String newName = null;
		InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), RenameTableHandler_RenameAnExistingTable, RenameTableHandler_NewName, currentName, null);
		if(dialog.open() == Window.OK) {
			newName = dialog.getValue();
			if(newName == null || newName.length() <= 0) {
				return;
			}
		} else {
			// cancelled
			return;
		}

		tableManager.setTableName(newName);
	}

	/**
	 * Get the name used in the {@link RecordingCommand}. This name will be
	 * visible in undo/redo.
	 * 
	 * @return The command name to show.
	 */
	public String getCommandName() {
		return "Rename Table";
	}

	protected IEvaluationContext getIEvaluationContext(ExecutionEvent event) {
		if(event.getApplicationContext() instanceof IEvaluationContext) {
			return (IEvaluationContext)event.getApplicationContext();
		}
		return null;

	}

	/**
	 * Get the Table model element. This method can be used from {@link #execute(ExecutionEvent)} or {@link #setEnabled(Object)}.
	 * 
	 * @return The current table
	 * @throws ServiceException
	 */
	protected INattableModelManager lookupTableManager(IEvaluationContext context) throws ServiceException {
		IEditorPart editor = ServiceUtilsForIEvaluationContext.getInstance().getService(IMultiDiagramEditor.class, context);

		INattableModelManager tableManager = (INattableModelManager)editor.getAdapter(INattableModelManager.class);
		return tableManager;
	}

	/**
	 * Try to lookup the TransactionalEditingDomain.
	 * 
	 * @return
	 * @throws ServiceException
	 *         If the Editing domain can't be found.
	 */
	protected TransactionalEditingDomain lookupTransactionalEditingDomain(IEvaluationContext context) throws ServiceException {

		// Get page from the event !
		// IWorkbenchPage page =
		// HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();

		return ServiceUtilsForIEvaluationContext.getInstance().getTransactionalEditingDomain(context);
	}

	/**
	 * Called by framework. Need to set the enabled flag.
	 * 
	 * @see org.eclipse.core.commands.AbstractHandler#setEnabled(java.lang.Object)
	 * 
	 * @param evaluationContext
	 */
	@Override
	public void setEnabled(Object evaluationContext) {
		if(!(evaluationContext instanceof IEvaluationContext)) {
			setBaseEnabled(false);
			return;
		}

		IEvaluationContext context = (IEvaluationContext)evaluationContext;

		try {
			// Try to get the Table
			setBaseEnabled(lookupTableManager(context) != null);
			return;
		} catch (ServiceException e) {
			// Can't find ServiceRegistry: disable
		}

		//In all other cases
		setBaseEnabled(false);
	}
}

Back to the top