Skip to main content
summaryrefslogtreecommitdiffstats
blob: e7bcc8dc29e9026c06fa14dc56b743712187f33a (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
262
263
264
265
266
267
/*****************************************************************************
 * 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.utils;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.NattableFactory;
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.nattableaxisprovider.AbstractAxisProvider;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableconfiguration.TableConfiguration;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.DisplayStyle;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.IntListValueStyle;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.NattablestylePackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.TableDisplayStyle;

/**
 *
 * @author Vincent Lorenzo
 *
 */
public class TableHelper {

	private TableHelper() {
		// to prevent instanciation
	}

	/**
	 *
	 * @param configuration
	 *            the configuration used to create the table
	 * @return
	 *         the table created from this configuration
	 */
	public static final Table createTable(final TableConfiguration configuration) {
		return createTable(configuration, null);
	}

	/**
	 *
	 * @param configuration
	 *            the configuration used to create the table
	 * @param context
	 *            the context of the table
	 * @return
	 *         the table created from these parameters
	 */
	public static final Table createTable(final TableConfiguration configuration, final EObject context) {
		return createTable(configuration, null, null);
	}

	/**
	 *
	 * @param configuration
	 *            the configuration used to create the table
	 * @param context
	 *            the context of the table
	 * @param name
	 *            the name for the table
	 * @return
	 *         the table created from these parameters
	 */
	public static final Table createTable(final TableConfiguration configuration, final EObject context, final String name) {
		return createTable(configuration, null, null, null);
	}

	/**
	 *
	 * @param configuration
	 *            the configuration used to create the table
	 * @param context
	 *            the context of the table
	 * @param name
	 *            the name for the table
	 * @param description
	 *            the description for the table
	 * @return
	 *         the table created from these parameters
	 */
	public static final Table createTable(final TableConfiguration configuration, final EObject context, final String name, final String description) {
		assert configuration != null;

		Table table = NattableFactory.eINSTANCE.createTable();
		table.setTableConfiguration(configuration);
		table.setDescription(description);
		table.setName(name);
		table.setContext(context);

		// the configuration always provides axis provider
		AbstractAxisProvider rowProvider = configuration.getDefaultRowAxisProvider();
		rowProvider = EcoreUtil.copy(rowProvider);

		AbstractAxisProvider columnProvider = configuration.getDefaultColumnAxisProvider();
		columnProvider = EcoreUtil.copy(columnProvider);

		table.setCurrentRowAxisProvider(rowProvider);
		table.setCurrentColumnAxisProvider(columnProvider);
		table.getRowAxisProvidersHistory().add(rowProvider);
		table.getColumnAxisProvidersHistory().add(columnProvider);

		IntListValueStyle style = (IntListValueStyle) configuration.getNamedStyle(NattablestylePackage.eINSTANCE.getIntListValueStyle(), NamedStyleConstants.HIDDEN_CATEGORY_FOR_DEPTH);
		// we copy this style, because the solution to override it in the table required to have a style to store displayed depth and another one to store hidden depth.
		if (style != null) {
			IntListValueStyle copy = EcoreUtil.copy(style);
			table.getStyles().add(copy);
		}
		return table;
	}

	/**
	 *
	 * @param table
	 *            a table
	 * @return
	 *         the display style to use for the table, the return value is never <code>null</code>
	 */
	public static final DisplayStyle getTableDisplayStyle(final Table table) {
		TableDisplayStyle displayStyle = (TableDisplayStyle) table.getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		if (displayStyle == null) {
			displayStyle = (TableDisplayStyle) table.getTableConfiguration().getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		}
		if (displayStyle != null) {
			return displayStyle.getDisplayStyle();
		}
		return DisplayStyle.NORMAL;
	}


	/**
	 *
	 * @param table
	 *            a table manager
	 * @return
	 *         the display style to use for the managed table, the return value is never <code>null</code>
	 */
	public static final DisplayStyle getTableDisplayStyle(final INattableModelManager tableManager) {
		return getTableDisplayStyle(tableManager.getTable());
	}

	/**
	 *
	 * @param tableManager
	 *            a table manager
	 * @return
	 *         <code>true</code> if the managed table is a tree table
	 */
	public static final boolean isTreeTable(final INattableModelManager tableManager) {
		return isTreeTable(tableManager.getTable());
	}

	/**
	 *
	 * @param table
	 *            a table
	 * @return
	 *         <code>true</code> if the table is a tree table
	 */
	public static final boolean isTreeTable(final Table table) {
		TableDisplayStyle style = (TableDisplayStyle) table.getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		if (style == null) {
			style = (TableDisplayStyle) table.getTableConfiguration().getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		}
		if (style != null) {
			final DisplayStyle displayStyle = style.getDisplayStyle();
			return DisplayStyle.HIERARCHIC_MULTI_TREE_COLUMN.equals(displayStyle) || DisplayStyle.HIERARCHIC_SINGLE_TREE_COLUMN.equals(displayStyle);
		}
		return false;
	}

	/**
	 *
	 * @param table
	 *            a table
	 * @return
	 *         <code>true</code> if the table is a tree table displayed on single column
	 */
	public static final boolean isSingleColumnTreeTable(final Table table) {
		TableDisplayStyle style = (TableDisplayStyle) table.getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		if (style == null) {
			style = (TableDisplayStyle) table.getTableConfiguration().getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		}
		if (style != null) {
			final DisplayStyle displayStyle = style.getDisplayStyle();
			return DisplayStyle.HIERARCHIC_SINGLE_TREE_COLUMN.equals(displayStyle);
		}
		return false;
	}

	/**
	 *
	 * @param table
	 *            a table
	 * @return
	 *         <code>true</code> if the tableManager manages a tree table displayed on single column
	 */
	public static final boolean isSingleColumnTreeTable(final INattableModelManager tableManager) {
		return isSingleColumnTreeTable(tableManager.getTable());
	}


	/**
	 *
	 * @param table
	 *            a table
	 * @return
	 *         <code>true</code> if the table is a tree table displayed on multi column
	 */
	public static final boolean isMultiColumnTreeTable(Table table) {
		TableDisplayStyle style = (TableDisplayStyle) table.getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		if (style == null) {
			style = (TableDisplayStyle) table.getTableConfiguration().getStyle(NattablestylePackage.eINSTANCE.getTableDisplayStyle());
		}
		if (style != null) {
			final DisplayStyle displayStyle = style.getDisplayStyle();
			return DisplayStyle.HIERARCHIC_MULTI_TREE_COLUMN.equals(displayStyle);
		}
		return false;
	}

	/**
	 *
	 * @param table
	 *            a table
	 * @return
	 *         <code>true</code> if the tableManager manages a tree table displayed on multi column
	 */
	public static final boolean isMultiColumnTreeTable(final INattableModelManager tableManager) {
		return isMultiColumnTreeTable(tableManager.getTable());
	}

	/**
	 *
	 * @param anEObject
	 * @return
	 * @throws Exception
	 */
	public static final Table findTable(final EObject anEObject) throws Exception { // TODO : move me
		if (EcoreUtil.getRootContainer(anEObject.eClass()) != NattablePackage.eINSTANCE) {
			throw new Exception("The eobject is not an element of the Papyrus table metamodel"); //$NON-NLS-1$
		}
		EObject container = anEObject;
		while (container != null && !(container instanceof Table)) {
			container = container.eContainer();
		}
		if (container == null) {
			return null;
			//			throw new Exception("Table not found, this method should be completed"); //$NON-NLS-1$
		}

		return (Table) container;
	}



}

Back to the top