1 /******************************************************************************* 2 * Copyright (c) 2011, 2017 BestSolution.at and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation 13 ******************************************************************************/ 14 package org.eclipse.e4.tools.emf.ui.internal.common.uistructure; 15 16 import java.util.Collections; 17 18 import org.eclipse.core.databinding.observable.value.IObservableValue; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtensionPoint; 22 import org.eclipse.core.runtime.IExtensionRegistry; 23 import org.eclipse.core.runtime.RegistryFactory; 24 import org.eclipse.e4.tools.emf.ui.common.IScriptingSupport; 25 import org.eclipse.e4.tools.emf.ui.internal.Messages; 26 import org.eclipse.e4.tools.emf.ui.internal.common.ControlHighlighter; 27 import org.eclipse.e4.tools.services.IResourcePool; 28 import org.eclipse.e4.ui.model.application.MApplicationElement; 29 import org.eclipse.e4.ui.model.internal.ModelUtils; 30 import org.eclipse.emf.databinding.EMFProperties; 31 import org.eclipse.emf.databinding.IEMFValueProperty; 32 import org.eclipse.emf.ecore.EStructuralFeature; 33 import org.eclipse.jface.action.Action; 34 import org.eclipse.jface.action.MenuManager; 35 import org.eclipse.jface.viewers.IStructuredSelection; 36 import org.eclipse.jface.viewers.TreeViewer; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 40 public class UIViewer { createViewer(Composite parent, EStructuralFeature feature, final IObservableValue<?> master, IResourcePool resourcePool, final Messages messages)41 public TreeViewer createViewer(Composite parent, EStructuralFeature feature, final IObservableValue<?> master, 42 IResourcePool resourcePool, final Messages messages) { 43 final TreeViewer viewer = new TreeViewer(parent); 44 viewer.setContentProvider(new WidgetContentProvider()); 45 viewer.setLabelProvider(new WidgetLabelProvider(resourcePool)); 46 IEMFValueProperty property = EMFProperties.value(feature); 47 @SuppressWarnings("unchecked") 48 IObservableValue<?> value = property.observeDetail(master); 49 value.addValueChangeListener(event -> { 50 if (event.diff.getNewValue() != null) { 51 viewer.setInput(Collections.singleton(event.diff.getNewValue())); 52 viewer.expandToLevel(2); 53 } else { 54 viewer.setInput(Collections.emptyList()); 55 } 56 }); 57 58 final MenuManager mgr = new MenuManager(); 59 mgr.setRemoveAllWhenShown(true); 60 mgr.addMenuListener(manager -> { 61 final Object o = ((IStructuredSelection) viewer.getSelection()).getFirstElement(); 62 if (o instanceof Control) { 63 manager.add(new Action(messages.ModelEditor_ShowControl) { 64 @Override 65 public void run() { 66 ControlHighlighter.show((Control) o); 67 } 68 }); 69 } 70 71 IExtensionRegistry registry = RegistryFactory.getRegistry(); 72 IExtensionPoint extPoint = registry.getExtensionPoint("org.eclipse.e4.tools.emf.ui.scripting"); //$NON-NLS-1$ 73 final IConfigurationElement[] elements = extPoint.getConfigurationElements(); 74 75 final IStructuredSelection s = (IStructuredSelection) viewer.getSelection(); 76 77 if (elements.length > 0 && !s.isEmpty()) { 78 MenuManager scriptExecute = new MenuManager(messages.ModelEditor_Script); 79 manager.add(scriptExecute); 80 for (IConfigurationElement e : elements) { 81 final IConfigurationElement le = e; 82 scriptExecute.add(new Action(e.getAttribute("label")) { //$NON-NLS-1$ 83 @Override 84 public void run() { 85 try { 86 MApplicationElement o = (MApplicationElement) master.getValue(); 87 IScriptingSupport support = (IScriptingSupport) le.createExecutableExtension("class"); //$NON-NLS-1$ 88 support.openEditor(viewer.getControl().getShell(), s.getFirstElement(), 89 ModelUtils.getContainingContext(o)); 90 } catch (CoreException e) { 91 // TODO Auto-generated catch block 92 e.printStackTrace(); 93 } 94 } 95 }); 96 } 97 } 98 }); 99 100 viewer.getControl().setMenu(mgr.createContextMenu(viewer.getControl())); 101 102 return viewer; 103 } 104 } 105