Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3ec2e5d9a99ab542a68f2e89e997cc79da64659a (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
package org.eclipse.papyrus.infra.table.efacet.nestededitor.editor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.Customization;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.Column;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.FeatureColumn;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.Row;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.TableFactory;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.TablePackage;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidget;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetInternal;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.ITableWidgetProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.validation.internal.modeled.model.validation.Feature;
import org.eclipse.papyrus.infra.core.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.infra.table.efacet.common.listener.AbstractTableTriggerListener;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrusTable;
import org.eclipse.papyrus.infra.table.efacet.nestededitor.internal.copy.TableInstanceCommandFactory;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;


public class HideNewColumnsListener extends AbstractTableTriggerListener {

	/**
	 * features already managed
	 */
	private Set<ETypedElement> featuresAlreadyManaged = new HashSet<ETypedElement>();

	public HideNewColumnsListener(final PapyrusTable table) {
		super(table);
		for(final Column column : table.getTable().getColumns()) {
			if(column instanceof FeatureColumn) {
				final ETypedElement element = ((FeatureColumn)column).getFeature();
				if(element instanceof EStructuralFeature) {
					featuresAlreadyManaged.add((EStructuralFeature)element);
				}
			}
		}
	}

	/**
	 * 
	 * @param notification
	 * a notification
	 * @return
	 * all the features of the object added in the table
	 */
	private Set<ETypedElement> getAllFeaturesObject(final Notification notification) {
		final Set<ETypedElement> newFeatures = new HashSet<ETypedElement>();
		final Object newValue = notification.getNewValue();
		if(newValue instanceof Row) {
			Row row = (Row)newValue;
			EObject element = row.getElement();
			Collection<EStructuralFeature> allFeatures = element.eClass().getEAllStructuralFeatures();
			newFeatures.addAll(allFeatures);
			newFeatures.removeAll(featuresAlreadyManaged);
		}

		return newFeatures;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.table.efacet.common.listener.AbstractTableTriggerListener#isManagedNotification(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 * @return
	 */
	@Override
	protected boolean isManagedNotification(Notification notification) {
		if(notification.getEventType() == Notification.ADD && notification.getNotifier() == this.table.getTable() && notification.getFeature() == TableFactory.eINSTANCE.getTablePackage().getTable_Rows()) {
			Object newValue = notification.getNewValue();
			if(newValue != null && newValue instanceof Row) {
				return true;//there is something to do for each new row 
			}
		}
		return false;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.table.efacet.common.listener.AbstractTableTriggerListener#getCommandName(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 * @return
	 */
	@Override
	protected String getCommandName(Notification notification) {
		return "Hide new columns command";
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.table.efacet.common.listener.AbstractTableTriggerListener#getSynchronizationCommand(org.eclipse.emf.transaction.TransactionalEditingDomain, org.eclipse.emf.common.notify.Notification)
	 *
	 * @param domain
	 * @param notification
	 * @return
	 */
	@Override
	protected Command getSynchronizationCommand(final TransactionalEditingDomain domain, final Notification notification) {
		final Collection<ETypedElement> features = getAllFeaturesObject(notification);
		if(!features.isEmpty()) {
			featuresAlreadyManaged.addAll(features);
			final ITableWidgetInternal widget = getWidget();
			return TableInstanceCommandFactory.createHideColumnCommand(widget, domain, table.getTable(), features);
		}
		return null;
	}

	/**
	 * TODO EMF-Facet should provides a best way to hide columns programmatically
	 * @return
	 */
	private ITableWidgetInternal getWidget() {
		final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		Assert.isTrue(editor instanceof CoreMultiDiagramEditor);
		final IEditorPart activeEditor = ((CoreMultiDiagramEditor)editor).getActiveEditor();
		ITableWidgetProvider provider = (ITableWidgetProvider)activeEditor.getAdapter(ITableWidgetProvider.class);
		ITableWidget widget = provider.getTableWidget();
		if(widget instanceof ITableWidgetInternal) {
			return (ITableWidgetInternal)widget;
		}
		return null;
	}


}

Back to the top