Skip to main content
summaryrefslogtreecommitdiffstats
blob: 47f83c3dc0533386b5f0e172e7b7225751f7715f (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
 * Copyright (c) 2011 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.table.menu.actions;


import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.facet.widgets.nattable.INatTableWidget;
import org.eclipse.emf.facet.widgets.nattable.INatTableWidgetProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.table.common.editor.AbstractNattableEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * This action allows to add selected elements in the model explorer in the last opened table
 * 
 * 
 * @deprecated : the drag&drop replaces this action
 */
@Deprecated
public class AddToCurrentTableAction {

	/**
	 * The selected elements
	 */
	private List<EObject> selectedElements;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param selectedElements
	 *        the selected elements
	 */
	public AddToCurrentTableAction(List<EObject> selectedElements) {
		this.selectedElements = selectedElements;
	}

	/**
	 * 
	 * @return
	 *         the builded command
	 */
	protected Command getBuildedCommand() {
		return new ICommandProxy(new MyAddCommand(getEditingDomain()));
	}

	/**
	 * The command to add selected elements into the table
	 * 
	 * 
	 * 
	 */
	public class MyAddCommand extends AbstractTransactionalCommand {

		/**
		 * 
		 * Constructor.
		 * 
		 * @param domain
		 *        the domain for this command
		 */
		public MyAddCommand(TransactionalEditingDomain domain) {
			super(domain, "", null); //$NON-NLS-1$
		}

		/**
		 * 
		 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor,
		 *      org.eclipse.core.runtime.IAdaptable)
		 * 
		 * @param monitor
		 * @param info
		 * @return
		 * @throws ExecutionException
		 */
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			//we add the rows to the widget, which owns the Table!
			AbstractNattableEditor editor = getCurrentTableEditor();
			INatTableWidgetProvider provider = (INatTableWidgetProvider)editor.getAdapter(INatTableWidgetProvider.class);
			if(provider != null) {
				INatTableWidget natTableWidget = provider.getNatTableWidget();

				natTableWidget.addRows(getSelection());
			}
			return CommandResult.newOKCommandResult();
		}

	}

	/**
	 * 
	 * @return
	 */
	public Command getCommand() {
		if(isEnabled()) {
			return getBuildedCommand();
		}
		return UnexecutableCommand.INSTANCE;
	}

	protected boolean isEnabled() {
		if(getSelection().isEmpty() || getCurrentTableEditor() == null) {
			return false;
		}
		return true;
	}

	private List<EObject> getSelection() {
		//		for(EObject current : selectedElements) {
		//			AbstractNattableEditor editor = getCurrentTableEditor();
		//			NatTableWidget natTableWidget = editor.getNatTableWidget();
		//			//TODO : managed with the query!
		//			boolean accept = natTableWidget.acceptsElement(current);
		//			int dummy = 0;
		//			dummy++;
		//		}
		return this.selectedElements;
	}

	/**
	 * 
	 * @return
	 *         the current table editor
	 */
	protected AbstractNattableEditor getCurrentTableEditor() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		if(workbench != null) {
			IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
			IWorkbenchPage activePage = workbenchWindow.getActivePage();
			if(activePage != null) {
				IEditorPart activeEditor = activePage.getActiveEditor();
				ISashWindowsContainer container = (ISashWindowsContainer)activeEditor.getAdapter(ISashWindowsContainer.class);
				if(container != null) {
					IEditorPart currentEditor = container.getActiveEditor();
					if(currentEditor instanceof AbstractNattableEditor) {
						return (AbstractNattableEditor)currentEditor;
					}
				}
			}
		}


		return null;
	}

	/**
	 * 
	 * @return
	 */
	protected ISashWindowsContainer getISashWindowsContainer() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		if(workbench != null) {
			IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
			IWorkbenchPage activePage = workbenchWindow.getActivePage();
			if(activePage != null) {
				IEditorPart activeEditor = activePage.getActiveEditor();
				return (ISashWindowsContainer)activeEditor.getAdapter(ISashWindowsContainer.class);
			}
		}
		return null;
	}

	/**
	 * Retrieves the TransactionalEditingDomain
	 * 
	 * @return the editing domain (can be null)
	 */
	protected TransactionalEditingDomain getEditingDomain() {
		TransactionalEditingDomain editingDomain = null;
		editingDomain = (TransactionalEditingDomain)getCurrentTableEditor().getEditingDomain();
		return editingDomain;
	}
}

Back to the top