Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d0575182e4afc8f5975cf5d345359a0bd58ae90d (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
/*******************************************************************************
 * 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:
 *     Cedric Dumoulin - cedric.dumoulin@lifl.fr
 ******************************************************************************/
package org.eclipse.papyrus.layers3.ui.commands;

import static org.eclipse.papyrus.layers.ui.Activator.log;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.layers.stackmodel.LayersException;
import org.eclipse.papyrus.layers.stackmodel.layers.AbstractLayer;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication;
import org.eclipse.papyrus.layers.stackmodel.layers.Property;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.dialogs.ListSelectionDialog;


/**
 * Attach a property to the selected Layer.
 * Property are proposed in a list.
 * 
 * @author cedric dumoulin
 *
 */
public class AttachPropertyFromListToLayer extends AbstractLayersCommand {

	/**
	 * Constructor.
	 *
	 */
	public AttachPropertyFromListToLayer() {
	}

	/**
	 * @see org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand#getCommandName()
	 *
	 * @return
	 */
	@Override
	public String getCommandName() {
		return "Attach a property";
	}

	/**
	 * @see org.eclipse.papyrus.layers3.ui.commands.AbstractLayersCommand#doExecute(org.eclipse.core.commands.ExecutionEvent, org.eclipse.core.expressions.IEvaluationContext, java.util.List)
	 *
	 * @param event
	 * @param context
	 * @param selections
	 */
	@Override
	protected void doExecute(ExecutionEvent event, IEvaluationContext context, List<Object> selections) {
		// check enable
		if( ! isEnabled(context, selections)) {
			return;
		}

		// Open the dialog to ask the new name
		// TODO dialog should not be in the transaction !! put it outside !
		
		try {
			// Get the layer and application
			LayersStackApplication application = lookupLayersStackApplicationChecked(context);
			AbstractLayer layer = (AbstractLayer)getSelections(context).get(0);

			// Get the list of available properties
			Property[] properties = application.getPropertyRegistry().getProperties().toArray(new Property[0]);

			IStructuredContentProvider contentProvider = new MyContentProvider();
			ILabelProvider labelProvider = new MyLabelProvider();

			ListSelectionDialog dialog = new ListSelectionDialog(Display.getCurrent().getActiveShell(), properties, contentProvider, labelProvider, "Select properties to attach");

			List<Property> initialSelection = layer.getAttachedProperties();
			dialog.setInitialSelections(initialSelection.toArray(new Property[initialSelection.size()]));

			if(dialog.open() != Window.OK) {
				return;
			} 

			// TODO: improve algorithm:
			// use only the two list (or arrays) of initialSelection and finalSelection
			// Walk the first, for each element, 
			// 		if the element is in the second list
			// 			remove element in both list (set list[i]=null)
			//   	else
			//			remove element from first list
			// At the end, 
			// initialCollection contains unsetted elements (with nulls)
			// finalSelection contains set elements (with nulls)
			// Walk each array/list, and skip nulls.
			
			
			
			// Process selected Properties
			Object[] res = dialog.getResult();
			// Create a list from the array. No better way ...
			// In the same time, create unchanged and set lists
			List<Property> finalSelection = new ArrayList<Property>(res.length);
			List<Property> unchangedProperties = new ArrayList<Property>(initialSelection.size());			
			List<Property> setProperties = new ArrayList<Property>(finalSelection.size());
	
			for( Object o : res ) {
				// Create a clone list of the result
				finalSelection.add((Property)o);
				// Create the unchanged and set list
				if( initialSelection.contains(o)) {
					unchangedProperties.add((Property)o);
				} 
				else {
					setProperties.add((Property)o);
				}
			}
			
			// We also need the unset list
			// Obtain it by removing unchanged from initialSelection
			List<Property> unsetProperties = new ArrayList<Property>(initialSelection);
			unsetProperties.removeAll(unchangedProperties);
			
			// Remove unset instances
			for( Property property : unsetProperties) {
				if(log.isDebugEnabled()) {
					log.debug("unset Property " + property.getName());
				}
				layer.removePropertyInstance(property);
			}

			// add set instances
			for( Property property : setProperties) {
				if(log.isDebugEnabled()) {
					log.debug("set Property " + property.getName());
				}
				layer.addPropertyInstance(property);
			}

		} catch (LayersException e) {
			// silently fails
			e.printStackTrace();
		} catch (org.eclipse.papyrus.infra.core.resource.NotFoundException e) {
			// silently fails
			e.printStackTrace();
		} catch (ServiceException e) {
			// silently fails
			e.printStackTrace();
		}


	}

	/**
	 * Return true if it is possible to attach a property.
	 */
	@Override
	public boolean isEnabled(IEvaluationContext context, List<Object> selections) {

		return selectionFirstElementInstanceOf(selections, AbstractLayer.class);
	}

	private class MyContentProvider implements IStructuredContentProvider {

		public Object[] getElements(Object inputElement) {
			return (Property[])inputElement;
		}

		public void dispose() {
			
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			
		}
		
	}

	private class MyLabelProvider extends LabelProvider {


		@Override
		public String getText(Object element) {
			return ((Property)element).getName();
		}
		
	}
}

Back to the top