Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7b12dcca3b64c6e9ab2367f86c972d1934dd7ef (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
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.widgets;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.tools.databinding.MultipleObservableValue;
import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor;
import org.eclipse.papyrus.views.properties.Activator;
import org.eclipse.papyrus.views.properties.contexts.Context;
import org.eclipse.papyrus.views.properties.contexts.Section;
import org.eclipse.papyrus.views.properties.contexts.View;
import org.eclipse.papyrus.views.properties.runtime.ConfigurationManager;
import org.eclipse.papyrus.views.properties.runtime.DefaultDisplayEngine;
import org.eclipse.papyrus.views.properties.runtime.DisplayEngine;
import org.eclipse.papyrus.views.properties.widgets.layout.PropertiesLayout;
import org.eclipse.papyrus.views.properties.xwt.XWTSection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * An Editor for displaying a whole property {@link View} on a sub-object.
 * If the property is a list, there will be one view per element in the list.
 *
 * The view's tabs will be ignored : the sections are embedded in the caller's
 * tab.
 */
public class ViewEditor extends AbstractPropertyEditor {

	private String viewPath;

	private Composite self;

	private Collection<XWTSection> sections;

	/**
	 * Constructor.
	 *
	 * @param parent
	 *            The composite in which the widget will be displayed
	 * @param style
	 *            The style for the widget
	 */
	public ViewEditor(Composite parent, int style) {
		self = new Composite(parent, SWT.NONE);
		PropertiesLayout layout = new PropertiesLayout(1, true);
		layout.horizontalSpacing = 0;
		layout.marginWidth = 0;
		self.setLayout(layout);
	}

	@Override
	public void checkInput() {
		if (propertyPath != null && input != null && viewPath != null) {
			display();
		}
	}

	/**
	 * Sets the number of columns for this editor. If the property is a list,
	 * there will be one view per element in the list : these views will be
	 * distributed in the given number of columns.
	 *
	 * @param numColumns
	 */
	public void setNumColumns(int numColumns) {
		((PropertiesLayout) self.getLayout()).numColumns = numColumns;
	}

	/**
	 * @return the number of columns for this editor.
	 */
	public int getNumColumns() {
		return ((PropertiesLayout) self.getLayout()).numColumns;
	}

	/**
	 * Sets the view for this editor. The view is represented by its viewPath, which is
	 * of form ContextName:ViewName
	 * e.g. : UML:Class
	 * The Context should be registered in the ConfigurationManager
	 *
	 * @param viewPath
	 *            The path of the view used to display the given property
	 */
	public void setView(String viewPath) {
		this.viewPath = viewPath;
		checkInput();
	}

	/**
	 * @return the qualified name of the view associated to this editor
	 */
	public String getView() {
		return viewPath;
	}

	private View resolveView() {
		String contextName = viewPath.substring(0, viewPath.indexOf(":")); //$NON-NLS-1$
		String viewName = viewPath.substring(viewPath.indexOf(":") + 1); //$NON-NLS-1$
		Context context = ConfigurationManager.getInstance().getContext(contextName);
		for (View view : context.getViews()) {
			if (view.getName().equals(viewName)) {
				return view;
			}
		}
		return null;
	}

	private void display() {
		View view = resolveView();

		if (view == null) {
			Activator.log.warn("Unabled to resolve view : " + viewPath); //$NON-NLS-1$
			return;
		}

		IObservable observable = getInputObservable();
		if (observable == null) {
			return;
		}

		DisplayEngine display = new DefaultDisplayEngine(true);

		sections = new LinkedList<XWTSection>();

		if (observable instanceof IObservableValue) {
			IObservableValue observableValue = (IObservableValue) observable;
			if (observableValue instanceof MultipleObservableValue) {
				MultipleObservableValue multipleObservable = (MultipleObservableValue) observableValue;
				display(display, multipleObservable.getObservedValues(), view);
			} else {
				Object value = observableValue.getValue();
				display(display, value, view);
			}
		} else if (observable instanceof IObservableList) {
			IObservableList observableList = (IObservableList) observable;
			for (Object value : observableList) {
				display(display, value, view);
			}
		}

		updateControls();
	}

	/**
	 * Displays the given view in the display engine, with the given object.
	 *
	 * @param display
	 *            The Display engine used to display the view. It should allow duplication,
	 *            as for list properties, the same section will be displayed for each element
	 *            in the list.
	 * @param data
	 *            The raw object for which we are displaying the view.
	 * @param view
	 *            The view to display
	 */
	protected void display(DisplayEngine display, Object data, View view) {
		display(display, Collections.singletonList(data), view);
	}

	/**
	 * Displays the given view in the display engine, with the given object.
	 *
	 * @param display
	 *            The Display engine used to display the view. It should allow duplication,
	 *            as for list properties, the same section will be displayed for each element
	 *            in the list.
	 * @param selectedElements
	 *            The list of objects for which we are displaying the view
	 * @param view
	 *            The view to display
	 */
	protected void display(DisplayEngine display, List<Object> selectedElements, View view) {
		for (Section section : view.getSections()) {
			XWTSection xwtSection = new XWTSection(section, view, display);
			sections.add(xwtSection);

			ISelection selection = new StructuredSelection(selectedElements);

			xwtSection.createControls(new Composite(self, SWT.NONE), null);
			xwtSection.setInput(null, selection);
			xwtSection.refresh();
		}
	}

	/**
	 * Updates the displayed widgets to mark them as readOnly if needed.
	 */
	protected void updateControls() {
		for (Control container : self.getChildren()) {
			// TODO : Browse recursively ?
			if (container instanceof Composite) {
				for (Control control : ((Composite) container).getChildren()) {
					if (control instanceof AbstractEditor) {
						AbstractEditor editor = (AbstractEditor) control;
						editor.setReadOnly(getReadOnly() || editor.isReadOnly());
					}
				}
			}
		}

		self.setEnabled(!getReadOnly());
	}

	@Override
	protected IObservable getInputObservable() {
		// Override the generic behavior: ViewEditor doesn't rely on an AbstractEditor
		return input.getObservable(propertyPath);
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		super.setReadOnly(readOnly);
		updateControls();
	}

	@Override
	protected void doBinding() {
		// Nothing to do here
	}

	@Override
	public Control getControl() {
		return self;
	}
}

Back to the top