diff options
6 files changed, 260 insertions, 17 deletions
diff --git a/bundles/org.eclipse.e4.tools.emf.ui.script.js/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.emf.ui.script.js/META-INF/MANIFEST.MF index edd5b51f..5a32c3a0 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui.script.js/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.e4.tools.emf.ui.script.js/META-INF/MANIFEST.MF @@ -6,4 +6,10 @@ Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Require-Bundle: org.eclipse.e4.tools.emf.ui;bundle-version="0.9.0", org.eclipse.jface;bundle-version="3.7.0", - org.mozilla.javascript;bundle-version="1.7.2" + org.mozilla.javascript;bundle-version="1.7.2", + org.eclipse.e4.ui.services;bundle-version="0.9.1", + org.eclipse.e4.core.contexts;bundle-version="0.9.0", + org.eclipse.e4.core.services;bundle-version="0.9.1", + org.eclipse.e4.core.di;bundle-version="0.9.0", + org.eclipse.osgi;bundle-version="3.7.0" +Bundle-ActivationPolicy: lazy diff --git a/bundles/org.eclipse.e4.tools.emf.ui.script.js/src/org/eclipse/e4/tools/emf/ui/script/js/JavaScriptSupport.java b/bundles/org.eclipse.e4.tools.emf.ui.script.js/src/org/eclipse/e4/tools/emf/ui/script/js/JavaScriptSupport.java index f8c0a4a2..a94673a4 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui.script.js/src/org/eclipse/e4/tools/emf/ui/script/js/JavaScriptSupport.java +++ b/bundles/org.eclipse.e4.tools.emf.ui.script.js/src/org/eclipse/e4/tools/emf/ui/script/js/JavaScriptSupport.java @@ -10,9 +10,18 @@ ******************************************************************************/ package org.eclipse.e4.tools.emf.ui.script.js; -import java.util.Map; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.annotation.Annotation; +import java.text.SimpleDateFormat; +import java.util.Date; +import org.eclipse.e4.core.contexts.ContextInjectionFactory; +import org.eclipse.e4.core.contexts.IEclipseContext; +import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.tools.emf.ui.common.IScriptingSupport; +import org.eclipse.e4.ui.services.IStylingEngine; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.TitleAreaDialog; import org.eclipse.jface.resource.JFaceResources; @@ -20,25 +29,36 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Widget; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.osgi.service.packageadmin.PackageAdmin; +@SuppressWarnings({ "restriction", "deprecation" }) public class JavaScriptSupport implements IScriptingSupport { - public void openEditor(Shell shell, final Object mainElement, final Map<String, Object> additionalData) { + public void openEditor(Shell shell, final Object mainElement, final IEclipseContext context) { TitleAreaDialog dialog = new TitleAreaDialog(shell) { private Text scriptField; + private Logger logger; @Override protected Control createDialogArea(Composite parent) { Composite container = (Composite) super.createDialogArea(parent); + logger = new Logger(getShell()); getShell().setText("Execute JavaScript"); setTitle("Execute JavaScript"); setMessage("Enter some JavaScript and execute it"); @@ -50,7 +70,7 @@ public class JavaScriptSupport implements IScriptingSupport { @Override protected void okPressed() { - execute(mainElement, additionalData, scriptField.getText()); + execute(logger, mainElement, context, scriptField.getText()); } @Override @@ -63,14 +83,160 @@ public class JavaScriptSupport implements IScriptingSupport { dialog.open(); } - private void execute(Object mainElement, Map<String, Object> additionalData, String script) { + private void execute(Logger logger, Object mainElement, IEclipseContext context, String script) { Context cx = Context.enter(); Scriptable sc = cx.initStandardObjects(); ScriptableObject.putProperty(sc, "mainObject", mainElement); - ScriptableObject.putProperty(sc, "additionalData", additionalData); + ScriptableObject.putProperty(sc, "eclipseContext", context); ScriptableObject.putProperty(sc, "swt", new SWTSupport(Display.getCurrent())); - cx.evaluateString(sc, script, "<cmd>", 1, null); + ScriptableObject.putProperty(sc, "service", new ServiceProvider(context)); + ScriptableObject.putProperty(sc, "di", new DiProvider(context)); + ScriptableObject.putProperty(sc, "log", logger); + + try { + cx.evaluateString(sc, script, "<cmd>", 1, null); + } catch (Exception e) { + try { + logger.error(e); + } catch (Exception e1) { + } + } + } + + public static class DiProvider { + private IEclipseContext context; + private PackageAdmin packageAdmin; + + public DiProvider(IEclipseContext context) { + this.context = context; + } + + public Object newInstance(String bundlename, String className) throws ClassNotFoundException { + Bundle bundle = getBundle(bundlename); + if( bundle != null ) { + Class<?> clazz = bundle.loadClass(className); + return ContextInjectionFactory.make(clazz, context); + } + return new IllegalArgumentException("Bundle '"+bundlename+"' is not known"); + } + + public Object execute(Object object) { + return ContextInjectionFactory.invoke(object, Execute.class, context); + } + + public Object invokeByAnnotation(Object object, String bundlename, String className) throws ClassNotFoundException { + Bundle bundle = getBundle(bundlename); + if( bundle != null ) { + @SuppressWarnings("unchecked") + Class<? extends Annotation> clazz = (Class<? extends Annotation>) bundle.loadClass(className); + return ContextInjectionFactory.invoke(object, clazz, context); + } + return new IllegalArgumentException("Bundle '"+bundlename+"' is not known"); + } + + private Bundle getBundle(String bundlename) { + if( packageAdmin == null ) { + Bundle bundle = FrameworkUtil.getBundle(getClass()); + BundleContext context = bundle.getBundleContext(); + ServiceReference<PackageAdmin> reference = context.getServiceReference(PackageAdmin.class); + packageAdmin = context.getService(reference); + } + + Bundle[] bundles = packageAdmin.getBundles(bundlename, null); + if (bundles == null) + return null; + // Return the first bundle that is not installed or uninstalled + for (int i = 0; i < bundles.length; i++) { + if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { + return bundles[i]; + } + } + return null; + } + } + + public static class ServiceProvider { + private IEclipseContext context; + + public ServiceProvider(IEclipseContext context) { + this.context = context; + } + + public Object getStyleEngine() { + return context.get(IStylingEngine.class); + } + + public Object getPartService() { + return context.get("org.eclipse.e4.ui.workbench.modeling.EPartService"); + } + + public Object getModelService() { + return context.get("org.eclipse.e4.ui.workbench.modeling.EModelService"); + } + } + + public static class Logger { + private Shell parentShell; + + private Shell shell; + private Text text; + + private static SimpleDateFormat DATEFORMAT = new SimpleDateFormat("hh:mm:ss.SSS"); + + public Logger(Shell parentShell) { + this.parentShell = parentShell; + } + + public void openLog() { + if( shell == null ) { + shell = new Shell(parentShell,SWT.SHELL_TRIM); + shell.setLayout(new GridLayout()); + text = new Text(shell, SWT.MULTI|SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL); + text.setLayoutData(new GridData(GridData.FILL_BOTH)); + text.setFont(JFaceResources.getTextFont()); + text.setEditable(false); + shell.setVisible(true); + } + } + + public void error(Object data) throws Exception { + _log(1, data); + } + + public void debug(Object data) throws Exception { + _log(0, data); + } + + private void _log(int type, Object data) throws Exception { + if( shell == null ) { + openLog(); + } + shell.setVisible(true); + if( data instanceof Throwable ) { + StringWriter w = new StringWriter(); + PrintWriter pw = new PrintWriter(w); + ((Throwable)data).printStackTrace(pw); + text.append(DATEFORMAT.format(new Date()) + " - " + w + "\n"); + pw.close(); + w.close(); + } else { + text.append(DATEFORMAT.format(new Date()) + " - " + data + "\n"); + } + + } + + public void clearLog() { + if( text != null ) { + text.setText(""); + } + } + + public void closeLog() { + shell.dispose(); + shell = null; + text = null; + } } public static class SWTSupport { @@ -99,8 +265,20 @@ public class JavaScriptSupport implements IScriptingSupport { return null; } - public Widget newText(Composite parent, int style) { + public Text newText(Composite parent, int style) { return new Text(parent, style); } + + public Widget newLabel(Composite parent, int style) { + return new Label(parent, style); + } + + public GridData newGridData() { + return new GridData(); + } + + public Combo newCombo(Composite parent, int style) { + return new Combo(parent, style); + } } } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/IScriptingSupport.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/IScriptingSupport.java index ef2a0714..b9690d4d 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/IScriptingSupport.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/IScriptingSupport.java @@ -1,8 +1,8 @@ package org.eclipse.e4.tools.emf.ui.common; -import java.util.Map; +import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.swt.widgets.Shell; public interface IScriptingSupport { - public void openEditor(Shell shell, Object scope, Map<String, Object> additionalData); + public void openEditor(Shell shell, Object scope, IEclipseContext context); } diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java index c5f16b04..7b184305 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java @@ -134,6 +134,7 @@ import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl; import org.eclipse.e4.ui.model.application.ui.MElementContainer; import org.eclipse.e4.ui.model.application.ui.MUIElement; import org.eclipse.e4.ui.model.application.ui.advanced.impl.AdvancedPackageImpl; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.model.application.ui.basic.impl.BasicPackageImpl; import org.eclipse.e4.ui.model.application.ui.impl.UiPackageImpl; import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuPackageImpl; @@ -198,7 +199,10 @@ import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.TreeItem; public class ModelEditor { @@ -269,6 +273,8 @@ public class ModelEditor { private AbstractComponentEditor currentEditor; + private Listener keyListener; + public ModelEditor(Composite composite, IEclipseContext context, IModelResource modelProvider, IProject project, final IResourcePool resourcePool) { this.resourcePool = resourcePool; this.modelProvider = modelProvider; @@ -295,6 +301,18 @@ public class ModelEditor { @PostConstruct void postCreate(Composite composite) { + if (project == null) { + keyListener = new Listener() { + + public void handleEvent(Event event) { + if ((event.stateMask & SWT.ALT) == SWT.ALT) { + findAndHighlight(context.get(Display.class).getFocusControl()); + } + } + }; + context.get(Display.class).addFilter(SWT.MouseUp, keyListener); + } + context.set(ModelEditor.class, this); context.set(IResourcePool.class, resourcePool); context.set(EditingDomain.class, modelProvider.getEditingDomain()); @@ -338,6 +356,38 @@ public class ModelEditor { folder.setSelection(0); } + private void findAndHighlight(Control control) { + if (control != null) { + MApplicationElement m = findModelElement(control); + MApplicationElement o = m; + if (m != null) { + List<MApplicationElement> l = new ArrayList<MApplicationElement>(); + do { + l.add(m); + m = (MApplicationElement) ((EObject) m).eContainer(); + } while (m != null); + + if (o instanceof MPart) { + System.err.println(o); + System.err.println(((EObject) o).eContainingFeature()); + } + + viewer.setSelection(new StructuredSelection(o)); + } + } + } + + private MApplicationElement findModelElement(Control control) { + do { + if (control.getData("modelElement") != null) { //$NON-NLS-1$ + return (MApplicationElement) control.getData("modelElement"); //$NON-NLS-1$ + } + control = control.getParent(); + } while (control != null); + + return null; + } + private Control createXMITab(Composite composite) { final AnnotationModel model = new AnnotationModel(); @@ -574,8 +624,9 @@ public class ModelEditor { @Override public void run() { try { + MApplicationElement o = (MApplicationElement) s.getFirstElement(); IScriptingSupport support = (IScriptingSupport) le.createExecutableExtension("class"); //$NON-NLS-1$ - support.openEditor(viewer.getControl().getShell(), s.getFirstElement(), new HashMap<String, Object>()); + support.openEditor(viewer.getControl().getShell(), s.getFirstElement(), project == null ? ModelUtils.getContainingContext(o) : null); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -976,6 +1027,10 @@ public class ModelEditor { // TODO: handle exception e.printStackTrace(); } + + if (project == null) { + context.get(Display.class).removeFilter(SWT.MouseUp, keyListener); + } } public IModelResource getModelProvider() { diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/objectdata/ObjectViewer.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/objectdata/ObjectViewer.java index 100b7a73..27b3f11f 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/objectdata/ObjectViewer.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/objectdata/ObjectViewer.java @@ -2,7 +2,6 @@ package org.eclipse.e4.tools.emf.ui.internal.common.objectdata; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.databinding.observable.value.IValueChangeListener; import org.eclipse.core.databinding.observable.value.ValueChangeEvent; @@ -16,6 +15,8 @@ import org.eclipse.e4.tools.emf.ui.internal.Messages; import org.eclipse.e4.tools.emf.ui.internal.ResourceProvider; import org.eclipse.e4.tools.emf.ui.internal.common.ModelEditor; import org.eclipse.e4.tools.services.IResourcePool; +import org.eclipse.e4.ui.model.application.MApplicationElement; +import org.eclipse.e4.ui.model.internal.ModelUtils; import org.eclipse.emf.databinding.EMFProperties; import org.eclipse.emf.databinding.IEMFValueProperty; import org.eclipse.emf.ecore.EStructuralFeature; @@ -42,7 +43,7 @@ import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; public class ObjectViewer { - public TreeViewer createViewer(Composite parent, EStructuralFeature feature, IObservableValue master, IResourcePool resourcePool, final Messages messages) { + public TreeViewer createViewer(Composite parent, EStructuralFeature feature, final IObservableValue master, IResourcePool resourcePool, final Messages messages) { final TreeViewer viewer = new TreeViewer(parent); viewer.setContentProvider(new ContentProviderImpl()); viewer.setLabelProvider(new LabelProviderImpl(resourcePool)); @@ -93,7 +94,8 @@ public class ObjectViewer { } if (mainObject != null) { - support.openEditor(viewer.getControl().getShell(), mainObject, new HashMap<String, Object>()); + MApplicationElement value = (MApplicationElement) master.getValue(); + support.openEditor(viewer.getControl().getShell(), mainObject, ModelUtils.getContainingContext(value)); } } catch (CoreException e) { // TODO Auto-generated catch block diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/uistructure/UIViewer.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/uistructure/UIViewer.java index 7cf82cb9..ac02009f 100644 --- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/uistructure/UIViewer.java +++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/uistructure/UIViewer.java @@ -11,7 +11,6 @@ package org.eclipse.e4.tools.emf.ui.internal.common.uistructure; import java.util.Collections; -import java.util.HashMap; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.databinding.observable.value.IValueChangeListener; import org.eclipse.core.databinding.observable.value.ValueChangeEvent; @@ -24,6 +23,8 @@ import org.eclipse.e4.tools.emf.ui.common.IScriptingSupport; import org.eclipse.e4.tools.emf.ui.internal.Messages; import org.eclipse.e4.tools.emf.ui.internal.common.ControlHighlighter; import org.eclipse.e4.tools.services.IResourcePool; +import org.eclipse.e4.ui.model.application.MApplicationElement; +import org.eclipse.e4.ui.model.internal.ModelUtils; import org.eclipse.emf.databinding.EMFProperties; import org.eclipse.emf.databinding.IEMFValueProperty; import org.eclipse.emf.ecore.EStructuralFeature; @@ -37,7 +38,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; public class UIViewer { - public TreeViewer createViewer(Composite parent, EStructuralFeature feature, IObservableValue master, IResourcePool resourcePool, final Messages messages) { + public TreeViewer createViewer(Composite parent, EStructuralFeature feature, final IObservableValue master, IResourcePool resourcePool, final Messages messages) { final TreeViewer viewer = new TreeViewer(parent); viewer.setContentProvider(new WidgetContentProvider()); viewer.setLabelProvider(new WidgetLabelProvider(resourcePool)); @@ -85,8 +86,9 @@ public class UIViewer { @Override public void run() { try { + MApplicationElement o = (MApplicationElement) master.getValue(); IScriptingSupport support = (IScriptingSupport) le.createExecutableExtension("class"); //$NON-NLS-1$ - support.openEditor(viewer.getControl().getShell(), s.getFirstElement(), new HashMap<String, Object>()); + support.openEditor(viewer.getControl().getShell(), s.getFirstElement(), ModelUtils.getContainingContext(o)); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); |