Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9e3dd1c01b956b429c79cfd8e2d3d24490f52606 (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
/*****************************************************************************
 * 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.propertyeditor;

import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.gmf.runtime.common.core.service.ExecutionStrategy;
import org.eclipse.gmf.runtime.common.core.service.IOperation;
import org.eclipse.gmf.runtime.common.core.service.IProvider;
import org.eclipse.gmf.runtime.common.core.service.Service;
import org.eclipse.gmf.runtime.common.ui.services.util.ActivityFilterProviderDescriptor;
import org.eclipse.papyrus.views.properties.runtime.Activator;
import org.eclipse.papyrus.views.properties.runtime.controller.PropertyEditorController;
import org.eclipse.papyrus.views.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.w3c.dom.Node;


/**
 * Service to provide property editors.
 */
public class PropertyEditorService extends Service {

	/** instance of this service */
	protected static PropertyEditorService instance;

	/**
	 * Creates a new PropertyEditorService. This constructor is not visible, using singleton pattern.
	 */
	protected PropertyEditorService() {
		super();
	}

	/**
	 * Returns the singleton instance of this service.
	 * 
	 * @return the singleton instance of this service
	 */
	/**
	 * Returns the singleton instance of this service
	 * 
	 * @return the singleton instance of this service
	 */
	public synchronized static PropertyEditorService getInstance() {
		if(instance == null) {
			instance = new PropertyEditorService();
			instance.configureProviders(Activator.ID, "propertyEditorProvider"); //$NON-NLS-1$
		}
		return instance;
	}

	/**
	 * Creates the property editor
	 * 
	 * @param controller
	 *        the controller that manages this editors
	 * @param id
	 *        the identifier of the editor to create
	 * @param widgetFactory
	 *        widget factory used to create the content of the editor
	 * @return the created property editor
	 */
	public AbstractPropertyEditor createPropertyEditor(PropertyEditorController controller, String id, TabbedPropertySheetWidgetFactory widgetFactory) {
		List<Object> results = (List<Object>)execute(ExecutionStrategy.FORWARD, new CreatePropertyEditorOperation(controller, id));
		if(results == null || results.size()==0) {
			Activator.log.error("impossible to create the editor using id " + id, null);
			return null;
		}
		Object result = results.get(0);
		if(result instanceof AbstractPropertyEditor) {
			((AbstractPropertyEditor)result).setWidgetFactory(widgetFactory);
			return (AbstractPropertyEditor)result;
		}
		Activator.log.error("impossible to create the editor using id " + id, null);
		return null;
	}

	/**
	 * Creates the property editor descriptor
	 * 
	 * @param editorID
	 *        id of the editor described by this element
	 * @param editorNode
	 *        the configuration node of the editor
	 * @return the configuration descriptor for the property editor
	 */
	public IPropertyEditorDescriptor createPropertyEditorDescriptor(String editorID, Node editorNode) {
		List<IPropertyEditorDescriptor> results = (List<IPropertyEditorDescriptor>)execute(ExecutionStrategy.FORWARD, new CreatePropertyEditorDescriptorOperation(editorID, editorNode));
		if(results == null || results.size()==0) {
			throw new RuntimeException("Impossible to create this property editor: "+editorID);
		} else {
			IPropertyEditorDescriptor descriptor = results.get(0);
			return results.get(0);
		}
	}

	/**
	 * @see org.eclipse.gmf.runtime.common.core.service.Service#newProviderDescriptor(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected Service.ProviderDescriptor newProviderDescriptor(IConfigurationElement element) {
		return new ProviderDescriptor(element);
	}

	/**
	 * A descriptor for property editor providers defined by a configuration element.
	 */
	protected static class ProviderDescriptor extends ActivityFilterProviderDescriptor {

		/**
		 * Constructs a <code>ISemanticProvider</code> descriptor for
		 * the specified configuration element.
		 * 
		 * @param element
		 *        The configuration element describing the provider.
		 */
		public ProviderDescriptor(IConfigurationElement element) {
			super(element);
		}

		/**
		 * @see org.eclipse.gmf.runtime.common.core.service.IProvider#provides(org.eclipse.gmf.runtime.common.core.service.IOperation)
		 */
		public boolean provides(IOperation operation) {
			if(!super.provides(operation)) {
				return false;
			}
			if(operation instanceof CreatePropertyEditorOperation || operation instanceof CreatePropertyEditorDescriptorOperation) {
				// test if the configuration corresponds to the feature or the id of the operation
				return getProvider().provides(operation);
			}
			return false;
		}

		/**
		 * @see org.eclipse.gmf.runtime.common.core.service.Service.ProviderDescriptor#getProvider()
		 */
		public IProvider getProvider() {
			if(provider == null) {
				IProvider newProvider = super.getProvider();
				if(provider instanceof PropertyEditorProvider) {
					PropertyEditorProvider defaultProvider = (PropertyEditorProvider)newProvider;
					defaultProvider.configure(getElement());
				}
				return newProvider;
			}
			return super.getProvider();
		}
	}

}

Back to the top