Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 660033656118282b8999b9fda596c8d32e0bc6a7 (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
/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.properties.modelelement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.infra.nattable.model.nattable.NattablePackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.AbstractHeaderAxisConfiguration;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.NattableaxisconfigurationPackage;
import org.eclipse.papyrus.infra.nattable.properties.observable.AbstractAxisHeaderConfigurationElementObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.ColumnDisplayFilterHeaderObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.ColumnDisplayIndexHeaderObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.ColumnDisplayLabelHeaderObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.ColumnIndexHeaderStyleObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.RowDisplayIndexHeaderObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.RowDisplayLabelHeaderObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.observable.RowIndexHeaderStyleObservableValue;
import org.eclipse.papyrus.infra.nattable.properties.utils.Constants;
import org.eclipse.papyrus.infra.nattable.utils.HeaderAxisConfigurationManagementUtils;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.views.properties.modelelement.EMFModelElement;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;


public class NatTableModelElement extends EMFModelElement {

	/**
	 * this listener used to update the value of the property view
	 */
	private Adapter tableListener;

	/**
	 * the map linking the propertypath and the observable values
	 */
	private Map<String, AbstractAxisHeaderConfigurationElementObservableValue> observableValues;

	/**
	 * the collection of the interesting features to update the property view
	 */
	private Collection<EStructuralFeature> interestingFeatures;

	/**
	 * the row header axis configuration
	 */
	private AbstractHeaderAxisConfiguration rowHeaderAxisConfiguration;

	/**
	 * the column header axis configuration
	 */
	private AbstractHeaderAxisConfiguration columnHeaderAxisConfiguration;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param source
	 *        the table
	 * @param domain
	 *        the editing domain
	 */
	public NatTableModelElement(final Table source, final EditingDomain domain) {
		super(source, domain);
		init();
	}

	/**
	 * Add the listener
	 */
	private void init() {
		this.observableValues = new HashMap<String, AbstractAxisHeaderConfigurationElementObservableValue>();
		this.interestingFeatures = new ArrayList<EStructuralFeature>();
		interestingFeatures.add(NattablePackage.eINSTANCE.getTable_InvertAxis());
		interestingFeatures.add(NattablePackage.eINSTANCE.getTable_LocalColumnHeaderAxisConfiguration());
		interestingFeatures.add(NattablePackage.eINSTANCE.getTable_LocalRowHeaderAxisConfiguration());
		interestingFeatures.add(NattableaxisconfigurationPackage.eINSTANCE.getAbstractHeaderAxisConfiguration_DisplayFilter());
		interestingFeatures.add(NattableaxisconfigurationPackage.eINSTANCE.getAbstractHeaderAxisConfiguration_DisplayIndex());
		interestingFeatures.add(NattableaxisconfigurationPackage.eINSTANCE.getAbstractHeaderAxisConfiguration_DisplayLabel());
		interestingFeatures.add(NattableaxisconfigurationPackage.eINSTANCE.getAbstractHeaderAxisConfiguration_IndexStyle());

		this.tableListener = new AdapterImpl() {

			/**
			 * 
			 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
			 * 
			 * @param msg
			 */
			@Override
			public void notifyChanged(Notification msg) {
				if(interestingFeatures.contains(msg.getFeature())) {
					updateValues();
				}
			}
		};
		columnHeaderAxisConfiguration = HeaderAxisConfigurationManagementUtils.getColumnAbstractHeaderAxisInTable(getEditedTable());
		rowHeaderAxisConfiguration = HeaderAxisConfigurationManagementUtils.getRowAbstractHeaderAxisInTable(getEditedTable());
		if(columnHeaderAxisConfiguration != null) {
			columnHeaderAxisConfiguration.eAdapters().add(tableListener);
		}
		if(rowHeaderAxisConfiguration != null) {
			rowHeaderAxisConfiguration.eAdapters().add(tableListener);
		}

		source.eAdapters().add(tableListener);
	}

	/**
	 * update all values in the property view
	 */
	private void updateValues() {
		//we do the update with asyncExec to be sure that the new value will be set before to update it
		Display.getDefault().asyncExec(new Runnable() {

			/**
			 * 
			 * @see java.lang.Runnable#run()
			 * 
			 */
			@Override
			public void run() {
				if(columnHeaderAxisConfiguration != null) {
					columnHeaderAxisConfiguration.eAdapters().remove(tableListener);
				}
				if(rowHeaderAxisConfiguration != null) {
					rowHeaderAxisConfiguration.eAdapters().remove(tableListener);
				}
				columnHeaderAxisConfiguration = HeaderAxisConfigurationManagementUtils.getColumnAbstractHeaderAxisInTable(getEditedTable());
				rowHeaderAxisConfiguration = HeaderAxisConfigurationManagementUtils.getRowAbstractHeaderAxisInTable(getEditedTable());
				if(columnHeaderAxisConfiguration != null) {
					columnHeaderAxisConfiguration.eAdapters().add(tableListener);
				}
				if(rowHeaderAxisConfiguration != null) {
					rowHeaderAxisConfiguration.eAdapters().add(tableListener);
				}

				Event event = new Event();
				for(final AbstractAxisHeaderConfigurationElementObservableValue current : observableValues.values()) {
					current.handleEvent(event);
				}
			}
		});
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.views.properties.modelelement.AbstractModelElement#dispose()
	 * 
	 */
	@Override
	public void dispose() {
		super.dispose();
		source.eAdapters().remove(tableListener);
		if(columnHeaderAxisConfiguration != null) {
			columnHeaderAxisConfiguration.eAdapters().remove(tableListener);
		}
		if(rowHeaderAxisConfiguration != null) {
			rowHeaderAxisConfiguration.eAdapters().remove(tableListener);
		}
		observableValues.clear();
	}


	/**
	 * 
	 * @return
	 */
	private Table getEditedTable() {
		return (Table)source;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.views.properties.modelelement.EMFModelElement#doGetObservable(java.lang.String)
	 * 
	 * @param propertyPath
	 * @return
	 */
	@Override
	protected IObservable doGetObservable(String propertyPath) {
		AbstractAxisHeaderConfigurationElementObservableValue value = this.observableValues.get(propertyPath);
		if(value == null) {
			Table table = getEditedTable();
			//column properties
			if(Constants.LOCAL_COLUMN_HEADER_AXIS_CONFIGURATION_DISPLAY_FILTER.equals(propertyPath)) {
				value = new ColumnDisplayFilterHeaderObservableValue(table);
			} else if(Constants.LOCAL_COLUMN_HEADER_AXIS_CONFIGURATION_DISPLAY_INDEX.equals(propertyPath)) {
				value = new ColumnDisplayIndexHeaderObservableValue(table);
			} else if(Constants.LOCAL_COLUMN_HEADER_AXIS_CONFIGURATION_DISPLAY_LABEL.equals(propertyPath)) {
				value = new ColumnDisplayLabelHeaderObservableValue(table);
			} else if(Constants.LOCAL_COLUMN_HEADER_AXIS_CONFIGURATION_INDEX_STYLE.equals(propertyPath)) {
				value = new ColumnIndexHeaderStyleObservableValue(table);
				//row property
			} else if(Constants.LOCAL_ROW_HEADER_AXIS_CONFIGURATION_DISPLAY_INDEX.equals(propertyPath)) {
				value = new RowDisplayIndexHeaderObservableValue(table);
			} else if(Constants.LOCAL_ROW_HEADER_AXIS_CONFIGURATION_DISPLAY_LABEL.equals(propertyPath)) {
				value = new RowDisplayLabelHeaderObservableValue(table);
			} else if(Constants.LOCAL_ROW_HEADER_AXIS_CONFIGURATION_INDEX_STYLE.equals(propertyPath)) {
				value = new RowIndexHeaderStyleObservableValue(table);
			}

			if(value != null) {
				this.observableValues.put(propertyPath, (AbstractAxisHeaderConfigurationElementObservableValue)value);
			}
		}
		if(value != null) {
			return value;
		}
		return super.doGetObservable(propertyPath);
	}
}

Back to the top