Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c2a385be41afbec6a4cbb9c6ccc64db6f64fc784 (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
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST and others.
 *
 * 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:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.IntListValueStyle;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.NattablestyleFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.NattablestylePackage;
import org.eclipse.papyrus.infra.nattable.utils.FillingConfigurationUtils;
import org.eclipse.papyrus.infra.nattable.utils.NamedStyleConstants;
import org.eclipse.papyrus.infra.nattable.utils.StyleUtils;

/**
 * @author Vincent Lorenzo
 *
 */
public class TreeRowHideShowCategoryHandler extends AbstractTreeRowHideShowCategoryHandler {

	/**
	 * Boolean parameter : <code>true</code> to hide and <code>false</code> to show
	 */
	public static final String HIDE_CATEGORY_PARAMETER_KEY = "hideCategory"; //$NON-NLS-1$

	/**
	 * the depth on which we are working
	 */
	public static final String DEPTH_PARAMETER_KEY = "depth"; //$NON-NLS-1$

	/**
	 * the id of the command managed by this handler
	 */
	public static final String COMMAND_ID = "org.eclipse.papyrus.infra.nattable.row.show.hide.category.command"; //$NON-NLS-1$

	/**
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 *
	 * @param arg0
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		Boolean hide = (Boolean) arg0.getParameters().get(HIDE_CATEGORY_PARAMETER_KEY);
		Integer depth = (Integer) arg0.getParameters().get(DEPTH_PARAMETER_KEY);
		if (hide) {
			hideCategory(depth);
		} else {
			showCategory(depth);
		}
		return null;
	}

	/**
	 *
	 * @param depth
	 *            the depth to hide
	 */
	protected void hideCategory(Integer depth) {
		IntListValueStyle style = StyleUtils.getHiddenDepthsValueStyle(getTable());
		if (style != null && style.getIntListValue().contains(depth)) {
			return;
		}
		if (depth == 0 && !FillingConfigurationUtils.hasTreeFillingConfigurationForDepth(getTable(), 0)) {
			return;
		}


		if (style == null) {
			style = NattablestyleFactory.eINSTANCE.createIntListValueStyle();
			style.setName(NamedStyleConstants.HIDDEN_CATEGORY_FOR_DEPTH);
			style.eSet(NattablestylePackage.eINSTANCE.getIntListValueStyle_IntListValue(), Collections.singleton(depth));
			Command cmd = AddCommand.create(getTableEditingDomain(), getTable(), NattablestylePackage.eINSTANCE.getStyledElement_Styles(), Collections.singleton(style));
			getTableEditingDomain().getCommandStack().execute(cmd);
			return;
		}

		List<Integer> allValues = new ArrayList<Integer>();
		if (style != null) {
			allValues.addAll(style.getIntListValue());
		}

		if (allValues.contains(depth)) {
			// nothing to do
			return;
		}
		allValues.add(depth);
		Collections.sort(allValues);
		int index = allValues.indexOf(depth);
		Command cmd = AddCommand.create(getTableEditingDomain(), style, NattablestylePackage.eINSTANCE.getIntListValueStyle_IntListValue(), depth, index);
		getTableEditingDomain().getCommandStack().execute(cmd);


	}

	/**
	 *
	 * @param depth
	 *            the depth to show
	 */
	protected void showCategory(Integer depth) {
		IntListValueStyle style = StyleUtils.getHiddenDepthsValueStyle(getTable());
		if (style == null) {
			return;
		}
		List<Integer> newValues = new ArrayList<Integer>(new HashSet<Integer>(style.getIntListValue()));
		newValues.remove(depth);
		Command cmd;
		if (newValues.isEmpty()) {
			cmd = RemoveCommand.create(getTableEditingDomain(), getTable(), NattablestylePackage.eINSTANCE.getStyledElement_Styles(), style);
		} else {
			cmd = RemoveCommand.create(getTableEditingDomain(), style, NattablestylePackage.eINSTANCE.getIntListValueStyle_IntListValue(), depth);
		}
		getTableEditingDomain().getCommandStack().execute(cmd);
	}


}

Back to the top