Skip to main content
summaryrefslogtreecommitdiffstats
blob: db553033934c64298d201398c557b1d70e86d934 (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
246
247
248
249
/*****************************************************************************
 * Copyright (c) 2012 Cedric Dumoulin.
 *
 *    
 * 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 - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.example.infra.servicesregistry.retrieval.menu.handlers;

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

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;


/**
 * A menu handler showing :
 * <ul>
 *   <li>How to retrieve a UML element from any kind of selected object (using adapters)</li>
 *   <li>How to get the ServicesRegistry from an EObject</li>
 * </ul>
 * 
 * @author Cedric dumoulin 
 * 
 */
public class LookupServiceRegistryFromEObjectHandler extends AbstractHandler implements IHandler {


	/**
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 * @param event
	 * @return
	 * @throws ExecutionException
	 * 
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		
		List<EObject> selectedProperties = getAllSelectionAdaptedToType( event, EObject.class );
		if( selectedProperties == null) {
			
			showErrorDialog(event, "No object selected", "Please select a Property First.");	
			return null;
		}
		
		
		// Get the ServiceRegistry for each of the selected object
		List<ServicesRegistry> res = new ArrayList<ServicesRegistry>();
		for( EObject selected : selectedProperties) {
			
			// Get the servicesRegistry
			try {
				ServicesRegistry registry = ServiceUtilsForResource.getInstance().getServiceRegistry(selected.eResource());
				res.add(registry);
				System.out.println(" - " + selected + " [" + registry + "]");
			} catch (ServiceException e) {
				// Not found
				res.add(null);
				System.out.println(" - " + selected + " [Can't get ServicesRegistry !!!]");
			}
            // 
		
		}

		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		

		showResults( window.getShell(), selectedProperties, res);
		
		return null;
	}

	/**
	 * Show results in popup.
	 * @param selectedProperties
	 * @param res
	 */
	private void showResults(Shell shell, List<EObject> selectedProperties, List<ServicesRegistry> res) {
		
		StringBuffer buff = new StringBuffer();

		Iterator<EObject> selectedIter = selectedProperties.iterator();
		Iterator<ServicesRegistry> registryIter = res.iterator();
		while(selectedIter.hasNext()) {
			EObject selected = selectedIter.next();
			ServicesRegistry registry = registryIter.next();
			
			buff.append("\n[ServicesRegistry]\n");
			if( registry != null )
			  buff.append(registry.toString());
			else
				buff.append("Registry not found !!!");
			buff.append( "\n[EOBject]\n");
			buff.append(selected.toString());
			buff.append("\n");
			
		}
		
		String message = buff.toString();
		MessageDialog.openInformation(shell, "ServicesRegistry found for selected objects:", message);
	}

	/**
	 * Get all selected element of the specified type.
	 * 
	 * @param expectedType
	 * @return
	 * @throws ExecutionException 
	 */
	@SuppressWarnings("unchecked")
	private <T> List<T> getAllSelectionAdaptedToType(ExecutionEvent event, Class<T> expectedType) throws ExecutionException {
		
		// Get selection from the workbench
		ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);

		// Get the selected objects according to the type of the selected
		if(selection instanceof IStructuredSelection)
		{
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			return getAllElementAdaptedToType( structuredSelection.toList(), expectedType);
		}
		else if( selection instanceof TreeSelection) {
			TreeSelection treeSelection = (TreeSelection)selection;
			return getAllElementAdaptedToType( treeSelection.toList(), expectedType );

		}
		return null;
	}

	/**
	 * Filter the list, and only retain objects that can be adapted to the specified type
	 * @param objects
	 * @param class1
	 * @return
	 */
	private <T> List<T> getAllElementAdaptedToType(List<Object> list, Class<T> expectedClassType) {
		
		List<T> res = new ArrayList<T>();
		
		for( Object cur : list) {
			
			T adapted = adapt( cur, expectedClassType);
			if( adapted != null)
				res.add(adapted);
		}
		return res;
	}

	/**
	 * Adapt the specified object to the requested type, if possible.
	 * Return null if the object can't be adapted.
	 * 
	 * @param object
	 * @param expectedClassType
	 * @return The adapted object, or null.
	 */
	@SuppressWarnings("unchecked")
	private <T> T adapt( Object object, Class<T> expectedClassType) {
		
		if( object instanceof IAdaptable ) {
			T ele = (T)((IAdaptable)object).getAdapter(expectedClassType);
			if(ele != null) {
			  return ele;
			}
			// Try as EObject if the expectedClasType is sub-type of EObject.
			if( EObject.class.isAssignableFrom( expectedClassType) ) {
				// to EObject
				EObject eobject = (EObject)((IAdaptable)object).getAdapter(EObject.class);
				
				if(eobject != null && expectedClassType.isInstance(eobject)) {
				  return (T)eobject;
				}
			}
		}
			
		// Try global mechanism
		{
			T ele = (T)Platform.getAdapterManager().getAdapter(object, expectedClassType);
			if(ele != null) {
				return ele;
			}
			// Try as EObject if the expectedClasType is sub-type of EObject.
			if( EObject.class.isAssignableFrom( expectedClassType) ) {
				// to EObject
				EObject eobject = (EObject)Platform.getAdapterManager().getAdapter(object, EObject.class);
				
				if(eobject != null && expectedClassType.isInstance(eobject)) {
				 
				  return (T)eobject;
				}
			}
		}
		// Can't be adapted
		return null;

	}
	
	
	/**
	 * 
	 * @param event
	 * @param errorMessage
	 * @throws ExecutionException
	 */
	private void showErrorDialog(ExecutionEvent event, String title, String errorMessage) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		MessageDialog.openError(
				window.getShell(),
				title,
				errorMessage);
	}
	

	/**
	 * Get the name used in the {@link RecordingCommand}. This name will be visible in 
	 * undo/redo.
	 * 
	 * @return The command name to show.
	 */
	public String getCommandName() {
		return "Show ServiceRegistry";
	}



}

Back to the top