Skip to main content
summaryrefslogtreecommitdiffstats
blob: a4b1567925d478c493130316e5411d96a0b03171 (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
/*****************************************************************************
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.runtime.dialogs;

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

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.StatusDialog;
import org.eclipse.papyrus.views.properties.runtime.Activator;
import org.eclipse.papyrus.views.properties.runtime.controller.descriptor.IBindingLabelProviderDescriptor;
import org.eclipse.papyrus.views.properties.runtime.view.DialogDescriptor;
import org.eclipse.papyrus.views.properties.runtime.view.IFragmentDescriptor;
import org.eclipse.papyrus.views.properties.runtime.view.content.AbstractContainerDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;


/**
 * Dialog displaying properties of an element
 */
public class PropertyDialog extends StatusDialog {

	/** descriptor of this dialog */
	private final DialogDescriptor descriptor;

	/** list of objects to edit */
	private final List<Object> objectsToEdit;

	/** widget factory */
	private final TabbedPropertySheetWidgetFactory widgetFactory;

	/** ok button */
	protected Button okButton;

	/**
	 * Creates a new PropertyDialog.
	 * 
	 * @param parent
	 *        the parent shell for this dialog
	 * @param descriptor
	 *        the dialog descriptor used to create this dialog
	 * @param objectsToEdit
	 *        the list of objects to edit
	 * @param widgetFactory
	 *        the factory used to create the content of the dialog
	 */
	public PropertyDialog(Shell parent, DialogDescriptor descriptor, List<Object> objectsToEdit, TabbedPropertySheetWidgetFactory widgetFactory) {
		super(parent);
		this.descriptor = descriptor;
		this.objectsToEdit = objectsToEdit;
		this.widgetFactory = widgetFactory;
		// adding resize, close and trim abilities
		setShellStyle(getShellStyle() | SWT.SHELL_TRIM);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		//no cancel button
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setSize(640, 480);
		shell.setText(getTitle());
	}

	/**
	 * Returns the title, using the dialog descriptor
	 * 
	 * @return the string to display as title
	 */
	protected String getTitle() {
		return getMessageFromDescriptor(descriptor.getTitle());
	}

	/**
	 * Returns the message, using the dialog descriptor
	 * 
	 * @return the string to display as message
	 */
	protected String getMessage() {
		return getMessageFromDescriptor(descriptor.getMessage());
	}

	/**
	 * Returns a string, given an object
	 * 
	 * @param object
	 *        should be a {@link String} or a {@link IBindingLabelProviderDescriptor}
	 * @return the string given by the object
	 */
	protected String getMessageFromDescriptor(Object object) {
		if(object instanceof String) {
			return ((String)object);
		} else if(object instanceof IBindingLabelProviderDescriptor) {
			if(objectsToEdit.size() == 1) {
				return ((IBindingLabelProviderDescriptor)object).computeBindings(objectsToEdit.get(0));
			} else if(objectsToEdit.size() > 1) {
				return ((IBindingLabelProviderDescriptor)object).computeBindings(objectsToEdit.get(0)) + " (and others)";
			} else {
				// should never happen
				return "No object to edit";
			}
		} else {
			Activator.log.warn("the object from which string is computed is not a String either a label descriptor");
			return "";
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite)super.createDialogArea(parent);

		getWidgetFactory().setBackground(composite.getBackground());

		// creates the content, given the configuration
		Label messageLabel = getWidgetFactory().createLabel(composite, getMessage(), SWT.NONE);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
		messageLabel.setLayoutData(data);

		Label separator = getWidgetFactory().createSeparator(composite, SWT.HORIZONTAL);
		separator.setLayoutData(data);

		Color defaultColor = getShell().getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
		getWidgetFactory().setBackground(defaultColor);
		ScrolledComposite scrolledComposite = getWidgetFactory().createScrolledComposite(composite, SWT.BORDER);
		scrolledComposite.setAlwaysShowScrollBars(false);
		GridLayout layout = new GridLayout(1, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		scrolledComposite.setLayout(layout);
		data = new GridData(SWT.FILL, SWT.FILL, true, true);
		scrolledComposite.setLayoutData(data);

		Composite containerComposite = getWidgetFactory().createComposite(scrolledComposite);
		layout = new GridLayout(1, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		containerComposite.setLayout(layout);
		scrolledComposite.setLayout(layout);
		scrolledComposite.setContent(containerComposite);
		scrolledComposite.setExpandVertical(true);
		scrolledComposite.setExpandHorizontal(true);

		List<AbstractContainerDescriptor> containers = new ArrayList<AbstractContainerDescriptor>();
		for(IFragmentDescriptor fragmentDescriptor : getFragmentsId()) {
			if(fragmentDescriptor != null) {
				for(AbstractContainerDescriptor descriptor : fragmentDescriptor.getContainerDescriptors()) {
					descriptor.createContent(containerComposite, getWidgetFactory(), objectsToEdit);
					containers.add(descriptor);
				}
			}
		}

		return composite;
	}

	/**
	 * Returns the list of identifier of fragments for this dialog
	 * 
	 * @return the list of identifier of fragments for this dialog
	 */
	protected List<IFragmentDescriptor> getFragmentsId() {
		return descriptor.getFragmentDescriptors();
	}

	/**
	 * Returns the objects edited in this dialog
	 * 
	 * @return the objects edited in this dialog
	 */
	protected List<Object> getObjectsToEdit() {
		return objectsToEdit;
	}

	/**
	 * Returns the widget factory
	 * 
	 * @return the widget factory
	 */
	protected TabbedPropertySheetWidgetFactory getWidgetFactory() {
		return widgetFactory;
	}

}

Back to the top