Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7321629a146cf55d28be282ce21a01f09942413e (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
/**
 * Copyright (c) 2012 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.infra.table.efacet.common.handlers;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.Table;
import org.eclipse.emf.facet.widgets.table.ui.ITableWidget;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.infra.table.efacet.common.editor.AbstractSynchronizedTableEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

//import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidget;

/**
 * 
 * the abstract class for the move line handler
 * 
 */
public abstract class AbstractMoveRowHandler extends AbstractHandler {

	/**
	 * 
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 * 
	 * @param event
	 * @return
	 * @throws ExecutionException
	 */
	public final Object execute(final ExecutionEvent event) throws ExecutionException {
		final Command cmd = getMoveRowCommand();
		if(cmd != null && cmd.canExecute()) {
			//we save the current row selection
			final List<EObject> selectedRows = getSelectedRowEObject();
			getEditingDomain().getCommandStack().execute(cmd);

			//we set the old row selection
			final ITableWidget widget = getTableWidget();
			widget.selectRows(selectedRows, true);
		}
		return null;
	}

	/**
	 * 
	 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
	 * 
	 * @return
	 */
	@Override
	public boolean isEnabled() {
		return (getSelectedRowEObject().size() == 1) && getMoveRowCommand().canExecute();

	}

	/**
	 * 
	 * @return
	 *         a list of the selected rows
	 */
	protected List<EObject> getSelectedRowEObject() {
		final ITableWidget widget = getTableWidget();
		return widget.getSelectedRowEObjects();
	}

	/**
	 * 
	 * @return
	 *         the current table editor
	 */
	protected AbstractSynchronizedTableEditor getCurrentTableEditor() {
		final IWorkbenchPart page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
		if(page instanceof CoreMultiDiagramEditor) {
			final IEditorPart activeEditor = ((CoreMultiDiagramEditor)page).getActiveEditor();
			if(activeEditor instanceof AbstractSynchronizedTableEditor) {
				return (AbstractSynchronizedTableEditor)activeEditor;
			}
		}
		return null;
	}


	/**
	 * 
	 * @return
	 *         the current nattable widget
	 */
	protected ITableWidget getTableWidget() {
		final AbstractSynchronizedTableEditor editor = getCurrentTableEditor();
		if(editor != null) {
			final ITableWidgetProvider provider = (ITableWidgetProvider)editor.getAdapter(ITableWidgetProvider.class);
			if(provider != null) {
				final Object widget = provider.getTableWidget();
				if(widget instanceof ITableWidget) {
					return (ITableWidget)widget;
				}
			}
		}
		return null;
	}

	/**
	 * 
	 * @return the editing domain to use for the command
	 */
	protected TransactionalEditingDomain getEditingDomain() {
		return (TransactionalEditingDomain)getCurrentTableEditor().getEditingDomain();
	}

	/**
	 * 
	 * @return
	 *         the current table
	 */
	protected Table getTable() {
		return (Table)getCurrentTableEditor().getAdapter(Table.class);
	}

	/**
	 * 
	 * @return
	 *         the command to move the lines
	 */
	protected abstract Command getMoveRowCommand();
}

Back to the top