Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Scholz2018-06-22 13:35:32 +0000
committerWim Jongman2018-06-27 08:55:55 +0000
commit924ac453c5d2dcdea5ba7acc1e2e4b5b9f6bc978 (patch)
treed1d888574b1af2a802eb56a13d67abbb1d7c9c34 /org.eclipse.tips.ui/src/org/eclipse/tips
parent1c74f30ab93c0c3f14ece7fd43f1c481ffa2ed91 (diff)
downloadeclipse.platform.ua-I20180701-2000.tar.gz
eclipse.platform.ua-I20180701-2000.tar.xz
eclipse.platform.ua-I20180701-2000.zip
invoked from the Browser PS1-PS3 * Create API and implementation PS4 * Included example from Bug 536227 PS5 - PS7 * Minor refactoring. Change-Id: Ie6acd0998d73713ae4d8428a48d7abfc834d14f8 Signed-off-by: Wim Jongman <wim.jongman@remainsoftware.com>
Diffstat (limited to 'org.eclipse.tips.ui/src/org/eclipse/tips')
-rw-r--r--org.eclipse.tips.ui/src/org/eclipse/tips/ui/IBrowserFunctionProvider.java42
-rw-r--r--org.eclipse.tips.ui/src/org/eclipse/tips/ui/internal/TipComposite.java30
2 files changed, 72 insertions, 0 deletions
diff --git a/org.eclipse.tips.ui/src/org/eclipse/tips/ui/IBrowserFunctionProvider.java b/org.eclipse.tips.ui/src/org/eclipse/tips/ui/IBrowserFunctionProvider.java
new file mode 100644
index 000000000..d7d9691fc
--- /dev/null
+++ b/org.eclipse.tips.ui/src/org/eclipse/tips/ui/IBrowserFunctionProvider.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2018 vogella GmbH
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v20.html
+ *
+ * Contributors:
+ * simon.scholz@vogella.com - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tips.ui;
+
+import java.util.Map;
+import java.util.function.Function;
+
+import org.eclipse.swt.browser.BrowserFunction;
+import org.eclipse.tips.core.IHtmlTip;
+import org.eclipse.tips.core.IUrlTip;
+
+/**
+ * This interface is intended to be implemented by {@link IHtmlTip} or
+ * {@link IUrlTip} instances to provide Java functions that can be invoked from
+ * within an SWT browser.
+ *
+ * @see IHtmlTip
+ * @see IUrlTip
+ * @see BrowserFunction
+ */
+public interface IBrowserFunctionProvider {
+
+ /**
+ * Provides a map with functions which can be invoked by JavaScript code
+ * provided by an {@link IHtmlTip} or {@link IUrlTip}.
+ *
+ * @return a {@link Map} containing names for a JavaScript function as key and
+ * {@link Function} objects, which can be invoked from the JavaScript in
+ * a SWT Browser.
+ *
+ * @see BrowserFunction
+ */
+ Map<String, Function<Object[], Object>> getBrowserFunctions();
+}
diff --git a/org.eclipse.tips.ui/src/org/eclipse/tips/ui/internal/TipComposite.java b/org.eclipse.tips.ui/src/org/eclipse/tips/ui/internal/TipComposite.java
index b8e5d982b..8a81df8c3 100644
--- a/org.eclipse.tips.ui/src/org/eclipse/tips/ui/internal/TipComposite.java
+++ b/org.eclipse.tips.ui/src/org/eclipse/tips/ui/internal/TipComposite.java
@@ -18,6 +18,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
+import java.util.function.Function;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
@@ -26,6 +27,7 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
+import org.eclipse.swt.browser.BrowserFunction;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
@@ -51,6 +53,7 @@ import org.eclipse.tips.core.TipImage;
import org.eclipse.tips.core.TipProvider;
import org.eclipse.tips.core.internal.LogUtil;
import org.eclipse.tips.core.internal.TipManager;
+import org.eclipse.tips.ui.IBrowserFunctionProvider;
import org.eclipse.tips.ui.ISwtTip;
import org.eclipse.tips.ui.internal.util.ImageUtil;
import org.eclipse.tips.ui.internal.util.ResourceManager;
@@ -79,6 +82,7 @@ public class TipComposite extends Composite implements ProviderSelectionListener
private Button fMultiActionButton;
private Composite fContentComposite;
private List<Image> fActionImages = new ArrayList<>();
+ private List<BrowserFunction> fBrowserFunctions = new ArrayList<>();
private Menu fActionMenu;
private ToolBar ftoolBar;
private ToolItem fStartupItem;
@@ -379,18 +383,38 @@ public class TipComposite extends Composite implements ProviderSelectionListener
}
private void loadContent(Tip tip) {
+ disposeBrowserFunctions();
if (tip instanceof ISwtTip) {
loadContentSWT(tip);
} else if (tip instanceof IHtmlTip) {
loadContentHtml((IHtmlTip) tip);
+ applyBrowserFunctions(tip);
} else if (tip instanceof IUrlTip) {
loadContentUrl((IUrlTip) tip);
+ applyBrowserFunctions(tip);
} else {
fTipManager.log(LogUtil.error(getClass(), Messages.TipComposite_12 + tip));
}
+
fContentComposite.requestLayout();
}
+ private void applyBrowserFunctions(Tip tip) {
+ if (tip instanceof IBrowserFunctionProvider) {
+ ((IBrowserFunctionProvider) tip).getBrowserFunctions()
+ .forEach((name, function) -> fBrowserFunctions.add(createBrowserFunction(name, function)));
+ }
+ }
+
+ private BrowserFunction createBrowserFunction(String functionName, Function<Object[], Object> function) {
+ return new BrowserFunction(getBrowser(), functionName) {
+ @Override
+ public Object function(Object[] arguments) {
+ return function.apply(arguments);
+ }
+ };
+ }
+
private void loadContentHtml(IHtmlTip tip) {
fBrowser.setText(getHTML(tip).trim());
}
@@ -416,9 +440,15 @@ public class TipComposite extends Composite implements ProviderSelectionListener
private void prepareForHTML() {
fContentStack.topControl = fBrowserComposite;
loadTimeOutScript();
+
fBrowserComposite.requestLayout();
}
+ private void disposeBrowserFunctions() {
+ fBrowserFunctions.forEach(BrowserFunction::dispose);
+ fBrowserFunctions.clear();
+ }
+
/**
* Sets content in the browser that displays a message after 1500ms if the Tip
* could not load fast enough.

Back to the top