Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7126044b88c6783caa1394949263487a36f53c98 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.eclipse.papyrus.infra.table.efacet.common.editor;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TriggerListener;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.table.efacet.common.Activator;
import org.eclipse.papyrus.infra.table.efacet.common.input.PapyrusTableEditorInput;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrusTable;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrustablePackage;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;

public abstract class AbstractTableEditor extends NatTableEditor2 {

	/** the service registry */
	protected ServicesRegistry servicesRegistry;

	/** the table instance */
	protected PapyrusTable rawModel;

	/** listener on the model for synchronized table */
	private TriggerListener modelTriggerListener;

	/** listener on the table for synchronized table : listen the properties "isSynchronized" and "fillingQueries" */
	private TriggerListener tableTriggerListener;

	/**
	 * the part name synchronizer
	 */
	private final PartNameSynchronizer synchronizer;


	public AbstractTableEditor(final ServicesRegistry servicesRegistry, final PapyrusTable rawModel) {
		this.servicesRegistry = servicesRegistry;
		this.rawModel = rawModel;
		this.synchronizer = new PartNameSynchronizer(rawModel);
	}



	/**
	 * 
	 * @see org.eclipse.emf.facet.widgets.nattable.workbench.editor.NatTableEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
	 * 
	 * @param site
	 * @param input
	 * @throws PartInitException
	 */
	@Override
	public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
		final PapyrusTableEditorInput tableEditorInput = new PapyrusTableEditorInput(this.rawModel, getEditingDomain());
		setSite(site);
		setInput(tableEditorInput);
		setPartName(this.rawModel.getName());
		super.init(site, tableEditorInput);
	}

	//	/**
	//	 * add listeners on the context of the table and on the table itself
	//	 */
	//	protected void addListeners() {
	//		EditingDomain editingDomain = getEditingDomain();
	//		Assert.isTrue(editingDomain instanceof TransactionalEditingDomain);
	//		this.modelTriggerListener = new ModelTriggerListener(this.rawModel, (INatTableWidgetProvider)getAdapter(INatTableWidgetProvider.class));
	//		((TransactionalEditingDomain)editingDomain).addResourceSetListener(this.modelTriggerListener);
	//		this.tableTriggerListener = new TableTriggerListener(this.rawModel, (INatTableWidgetProvider)getAdapter(INatTableWidgetProvider.class));
	//		((TransactionalEditingDomain)editingDomain).addResourceSetListener(this.tableTriggerListener);
	//	}

	//	/**
	//	 * 
	//	 * @see org.eclipse.papyrus.infra.table.common.internal.NatTableEditor#dispose()
	//	 *
	//	 *  {@inheritDoc}
	//	 */
	//	@Override
	//	public void dispose() {
	//		((TransactionalEditingDomain)getEditingDomain()).removeResourceSetListener(this.modelTriggerListener);
	//		((TransactionalEditingDomain)getEditingDomain()).removeResourceSetListener(this.tableTriggerListener);
	//		super.dispose();
	//	}

	//	/**
	//	 * 
	//	 * @param rawModel2
	//	 */
	//	private void initHiddenColumn(final PapyrusTableInstance rawModel2) {
	//		for(Column current : rawModel2.getTable().getColumns()) {
	//			if(current instanceof DefaultLabelColumn || current instanceof MetaClassColumn || current instanceof EContainerColumn){
	//				String name = NatTableWidgetInternalUtils.getColumnName(current);
	//				if(getInitialHiddenColumns().contains(name)) {
	//					current.setIsHidden(true);
	//				}
	//			}
	//		}
	//	}

	//	/**
	//	 * 
	//	 * @return
	//	 * a list of the names of the columns to hide by default
	//	 */
	//	protected List<String> getInitialHiddenColumns() {
	//		return Collections.emptyList();
	//	}

	/**
	 * 
	 * @see org.eclipse.emf.facet.widgets.nattable.workbench.editor.NatTableEditor#getEditingDomain()
	 * 
	 * @return
	 */
	@Override
	public EditingDomain getEditingDomain() {
		try {
			return ServiceUtils.getInstance().getTransactionalEditingDomain(this.servicesRegistry);
		} catch (final ServiceException e) {
			Activator.log.error(e);
		}
		return null;
	}


	//	/**
	//	 * This method execute the filling queries
	//	 */
	//	@Deprecated
	//	protected void executeQueries() {
	//		if(this.rawModel.isIsSynchronized()) {
	//			TableInstance table = this.rawModel.getTable();
	//			EObject context = table.getContext();
	//			List<EObject> elementsToAdd = new ArrayList<EObject>();
	//			for(ModelQuery query : this.rawModel.getFillingQueries()) {
	//				ModelQuerySetCatalog catalog = ModelQuerySetCatalog.getSingleton();
	//				AbstractModelQuery impl = null;
	//				try {
	//					impl = catalog.getModelQueryImpl(query);
	//				} catch (ModelQueryException e) {
	//					e.printStackTrace();
	//				}
	//				if(impl != null) {
	//					ModelQueryResult result = impl.evaluate(context);
	//					Object value = result.getValue();
	//					if(value instanceof Collection<?>) {
	//						for(Object currentObject : (Collection<?>)value) {
	//							if(currentObject instanceof EObject && !table.getElements().contains(currentObject)) {
	//								elementsToAdd.add((EObject)currentObject);
	//							}
	//						}
	//
	//					} else {
	//						//nothing to do for the moment
	//					}
	//				}
	//			}
	//			if(!elementsToAdd.isEmpty()) {
	//				//				this.natTableWidget.addRows(elementsToAdd);
	//				if(this.natTableWidget instanceof IPapyrusNatTableWidget) {
	//					((IPapyrusNatTableWidget)this.natTableWidget).addRowsOutOfCommandStack(elementsToAdd);
	//				}
	//			}
	//		}
	//
	//	}

	/**
	 * A class taking in charge the synchronization of the partName and the diagram name.
	 * When diagram name change, the other is automatically updated.
	 * 
	 * @author vincent lorenzo
	 *         adapted class from UmlGmfDiagramEditor
	 */
	public class PartNameSynchronizer {

		/** the papyrus table */
		private PapyrusTable papyrusTable;

		/**
		 * Listener on diagram name change.
		 */
		private final Adapter tableNameListener = new Adapter() {

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
			 * 
			 * @param notification
			 */
			public void notifyChanged(final Notification notification) {
				if(notification.getFeatureID(PapyrusTable.class) == PapyrustablePackage.PAPYRUS_TABLE__NAME && notification.getNotifier() == PartNameSynchronizer.this.papyrusTable) {
					setPartName(PartNameSynchronizer.this.papyrusTable.getName());
				}
			}

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
			 * 
			 * @return
			 */
			public Notifier getTarget() {
				return null;
			}

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
			 * 
			 * @param newTarget
			 */
			public void setTarget(final Notifier newTarget) {
			}

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
			 * 
			 * @param type
			 * @return
			 */
			public boolean isAdapterForType(final Object type) {
				return false;
			}

		};

		/**
		 * 
		 * Constructor.
		 * 
		 * @param diagram
		 */
		public PartNameSynchronizer(final PapyrusTable papyrusTable) {
			setTable(papyrusTable);
		}

		/**
		 * Change the associated diagram.
		 * 
		 * @param papyrusTable
		 */
		public void setTable(final PapyrusTable papyrusTable) {
			// Remove from old diagram, if any
			if(this.papyrusTable != null) {
				papyrusTable.eAdapters().remove(this.tableNameListener);
			}
			// Set new table
			this.papyrusTable = papyrusTable;
			// Set editor name
			setPartName(papyrusTable.getName());
			// Listen to name change
			papyrusTable.eAdapters().add(this.tableNameListener);
		}
	}
}

Back to the top