Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » rich tooltips/managing colors with FormText
rich tooltips/managing colors with FormText [message #551364] Fri, 06 August 2010 07:56 Go to next message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
No sure which forum to post in. It has to do with SWT, JFace and UI forms.

I wish to have rich tooltips with very simple markup such as bold, bullets and colors. Ideally I would like to do it the Swing way...

I believe the only way is to extend org.eclipse.jface.window.ToolTip and then use one of these:

- SWT Browser. Tried a snippet. Tooltip had a scrollbar --> no way. Advantage: plain portable html.
- StyledText. Looks too complicated to me!
- UI forms FormText. Looks ok! But...

Or is there already a ToolTip implementation on which I can set a marked up text? I am not the only one wanting this right???

Anyway, for now I came to the conclusion that I need a UI forms FormText widget inside the JFace ToolTip.
But when I want to use color red or color yellow for example, I have to write this code:

FormText formText = toolkit.createFormText(form.getBody(), true);
FormColors colors = toolkit.getColors();
...
colors.createColor("red", colors.getSystemColor(SWT.COLOR_RED));
formText.setColor("red", colors.getColor("red"));


The last two lines I have to repeat for each color that I want.
Is there an easier way or should I just do this?
Re: rich tooltips/managing colors with FormText [message #551376 is a reply to message #551364] Fri, 06 August 2010 08:24 Go to previous messageGo to next message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
For interested people, here is my current tooltip implementation and a snippet that uses it:

package nl.hm.ilja.ui;

import org.eclipse.jface.window.ToolTip;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * A tooltip for a {@link Control} which shows a text that can be marked up
 * using Eclipse UI Forms {@link FormText} tags.
 * 
 * <p>
 * Our goal is to have a fancy tooltip with simple html markup like in Swings
 * tooltips. This is the best implementation I could think of; see my forum post
 * <a href="http://www.eclipse.org/forums/index.php?t=msg&goto=551364&S=15e3e271d91f521d26beb7b2ba809148#msg_551364"
 * >here</a>. I wouldn't cry if we could later switch back to a true html markup
 * tooltip, this could be easier if we separate tooltip view from data, see
 * issue: OBLII-154.
 * 
 * <p>
 * This class supports the following colors. Just <a
 * href="http://blog.platinumsolutions.com/node/155">add more</a> when needed!
 * <ul>
 * <li>"red": SWT.COLOR_RED</li>
 * <li>"yellow": SWT.COLOR_YELLOW</li>
 * </ul>
 * 
 * <p>
 * Idea was taken from <a href="http://www.ibm.com/developerworks/opensource/library/os-eclipse-forms/index.html"
 * > this blog</a>.
 * 
 * <p>
 * For more info on how to use see the {@link FormText} javadoc, the blog or <a
 * href="http://demo.spars.info/j/frameset.cgi?compo_id=150159&mode=frameset&ref=3&method=34&q=org&hl=org&CASE=0&MORPHO=1&location=1111111111111111111&LANG=1"
 * >this example</a>, or my {@link ManualTestFancyToolTip}.
 * 
 * @author Henno Vermeulen
 */
public class FancyToolTip extends ToolTip {

	private final String text;

	public FancyToolTip(Control control, String text) {
		super(control);
		this.text = text;
	}

	@Override
	protected Composite createToolTipContentArea(Event event, Composite parent) {
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
		Form form = toolkit.createForm(parent);
		form.getBody().setLayout(new FillLayout());

		FormText formText = toolkit.createFormText(form.getBody(), true);
		// Parse tags but do not expand URLs because they don't make much
		// sense in a tooltip.
		formText.setText(text, true, false);

		FormColors colors = toolkit.getColors();
		addColors(formText, colors);
		return parent;
	}

	private void addColors(FormText formText, FormColors colors) {
		addSystemColor(SWT.COLOR_RED, "red", formText, colors);
		addSystemColor(SWT.COLOR_YELLOW, "yellow", formText, colors);
	}

	private void addSystemColor(int colorCode, String key, FormText formText,
			FormColors colors) {
		colors.createColor(key, colors.getSystemColor(colorCode));
		formText.setColor(key, colors.getColor(key));
	}
}


package nl.hm.ilja.ui;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ManualTestFancyToolTip {

	public static void main(String[] args) {
		final Display display = new Display();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {

				Shell shell = new Shell(display);
				shell.setLayout(new FillLayout());
				new ManualTestFancyToolTip().createPartControl(shell);
				shell.pack();
				shell.open();

				while (!shell.isDisposed()) {
					if (!display.readAndDispatch())
						display.sleep();
				}
			}
		});
	}

	protected static void createPartControl(Shell shell) {
		shell.setLayout(new FillLayout());
		Text text = new Text(shell, SWT.NONE);
		text.setText("Hover over me to show tooltip");
		String string = "<form><p>This is a simple <b>test</b></p>"
				+ "<p>And this too.</p>"
				+ "<p>And <span color=\"red\">here</span> are "
				+ "some <span color=\"yellow\">colors</span>.</p>"
				+ "<p>Test with list items:</p>"
				+ "<li>A default (bulleted) list item.</li>"
				+ "<li>Another bullet list item.</li>" + "</form>";
		new FancyToolTip(text, string);
		shell.setSize(300, 200);
	}

}
Re: rich tooltips/managing colors with FormText [message #551383 is a reply to message #551376] Fri, 06 August 2010 08:27 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5400
Registered: July 2009
Senior Member
Hi,

I think JFace-Text has a component they use to display the JavaDoc
information and it understands HTML-Commands.

Tom

Am 06.08.10 10:24, schrieb SlowStrider:
> For interested people, here is my current tooltip implementation and a
> snippet that uses it:
>
>
> package nl.hm.ilja.ui;
>
> import org.eclipse.jface.window.ToolTip;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Control;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.ui.forms.FormColors;
> import org.eclipse.ui.forms.widgets.Form;
> import org.eclipse.ui.forms.widgets.FormText;
> import org.eclipse.ui.forms.widgets.FormToolkit;
>
> /**
> * A tooltip for a {@link Control} which shows a text that can be marked up
> * using Eclipse UI Forms {@link FormText} tags.
> * * <p>
> * Our goal is to have a fancy tooltip with simple html markup like in
> Swings
> * tooltips. This is the best implementation I could think of; see my
> forum post
> * <a
> href=" http://www.eclipse.org/forums/index.php?t=msg&goto=55136 4&S=15e3e271d91f521d26beb7b2ba809148#msg_551364"
>
> * >here</a>. I wouldn't cry if we could later switch back to a true html
> markup
> * tooltip, this could be easier if we separate tooltip view from data, see
> * issue: OBLII-154.
> * * <p>
> * This class supports the following colors. Just <a
> * href="http://blog.platinumsolutions.com/node/155">add more</a> when
> needed!
> * <ul>
> * <li>"red": SWT.COLOR_RED</li>
> * <li>"yellow": SWT.COLOR_YELLOW</li>
> * </ul>
> * * <p>
> * Idea was taken from <a
> href=" http://www.ibm.com/developerworks/opensource/library/os-ecli pse-forms/index.html"
>
> * > this blog</a>.
> * * <p>
> * For more info on how to use see the {@link FormText} javadoc, the blog
> or <a
> *
> href=" http://demo.spars.info/j/frameset.cgi?compo_id=150159&mo de=frameset&ref=3&method=34&q=org&hl=org& ;CASE=0&MORPHO=1&location=1111111111111111111&LA NG=1"
>
> * >this example</a>, or my {@link ManualTestFancyToolTip}.
> * * @author Henno Vermeulen
> */
> public class FancyToolTip extends ToolTip {
>
> private final String text;
>
> public FancyToolTip(Control control, String text) {
> super(control);
> this.text = text;
> }
>
> @Override
> protected Composite createToolTipContentArea(Event event, Composite
> parent) {
> FormToolkit toolkit = new FormToolkit(parent.getDisplay());
> Form form = toolkit.createForm(parent);
> form.getBody().setLayout(new FillLayout());
>
> FormText formText = toolkit.createFormText(form.getBody(), true);
> // Parse tags but do not expand URLs because they don't make much
> // sense in a tooltip.
> formText.setText(text, true, false);
>
> FormColors colors = toolkit.getColors();
> addColors(formText, colors);
> return parent;
> }
>
> private void addColors(FormText formText, FormColors colors) {
> addSystemColor(SWT.COLOR_RED, "red", formText, colors);
> addSystemColor(SWT.COLOR_YELLOW, "yellow", formText, colors);
> }
>
> private void addSystemColor(int colorCode, String key, FormText
> formText,
> FormColors colors) {
> colors.createColor(key, colors.getSystemColor(colorCode));
> formText.setColor(key, colors.getColor(key));
> }
> }
>
>
>
> package nl.hm.ilja.ui;
>
> import org.eclipse.core.databinding.observable.Realm;
> import org.eclipse.jface.databinding.swt.SWTObservables;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
>
> public class ManualTestFancyToolTip {
>
> public static void main(String[] args) {
> final Display display = new Display();
> Realm.runWithDefault(SWTObservables.getRealm(display), new
> Runnable() {
> public void run() {
>
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> new ManualTestFancyToolTip().createPartControl(shell);
> shell.pack();
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> }
> });
> }
>
> protected static void createPartControl(Shell shell) {
> shell.setLayout(new FillLayout());
> Text text = new Text(shell, SWT.NONE);
> text.setText("Hover over me to show tooltip");
> String string = "<form><p>This is a simple <b>test</b></p>"
> + "<p>And this too.</p>"
> + "<p>And <span color=\"red\">here</span> are "
> + "some <span color=\"yellow\">colors</span>.</p>"
> + "<p>Test with list items:</p>"
> + "<li>A default (bulleted) list item.</li>"
> + "<li>Another bullet list item.</li>" + "</form>";
> new FancyToolTip(text, string);
> shell.setSize(300, 200);
> }
>
> }
>
Re: rich tooltips/managing colors with FormText [message #551384 is a reply to message #551383] Fri, 06 August 2010 08:28 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5400
Registered: July 2009
Senior Member
Could be JDT-UI also - not sure yet.

Tom

Am 06.08.10 10:27, schrieb Tom Schindl:
> Hi,
>
> I think JFace-Text has a component they use to display the JavaDoc
> information and it understands HTML-Commands.
>
> Tom
>
> Am 06.08.10 10:24, schrieb SlowStrider:
>> For interested people, here is my current tooltip implementation and a
>> snippet that uses it:
>>
>>
>> package nl.hm.ilja.ui;
>>
>> import org.eclipse.jface.window.ToolTip;
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.widgets.Composite;
>> import org.eclipse.swt.widgets.Control;
>> import org.eclipse.swt.widgets.Event;
>> import org.eclipse.ui.forms.FormColors;
>> import org.eclipse.ui.forms.widgets.Form;
>> import org.eclipse.ui.forms.widgets.FormText;
>> import org.eclipse.ui.forms.widgets.FormToolkit;
>>
>> /**
>> * A tooltip for a {@link Control} which shows a text that can be marked up
>> * using Eclipse UI Forms {@link FormText} tags.
>> * * <p>
>> * Our goal is to have a fancy tooltip with simple html markup like in
>> Swings
>> * tooltips. This is the best implementation I could think of; see my
>> forum post
>> * <a
>> href=" http://www.eclipse.org/forums/index.php?t=msg&goto=55136 4&S=15e3e271d91f521d26beb7b2ba809148#msg_551364"
>>
>> * >here</a>. I wouldn't cry if we could later switch back to a true html
>> markup
>> * tooltip, this could be easier if we separate tooltip view from data, see
>> * issue: OBLII-154.
>> * * <p>
>> * This class supports the following colors. Just <a
>> * href="http://blog.platinumsolutions.com/node/155">add more</a> when
>> needed!
>> * <ul>
>> * <li>"red": SWT.COLOR_RED</li>
>> * <li>"yellow": SWT.COLOR_YELLOW</li>
>> * </ul>
>> * * <p>
>> * Idea was taken from <a
>> href=" http://www.ibm.com/developerworks/opensource/library/os-ecli pse-forms/index.html"
>>
>> * > this blog</a>.
>> * * <p>
>> * For more info on how to use see the {@link FormText} javadoc, the blog
>> or <a
>> *
>> href=" http://demo.spars.info/j/frameset.cgi?compo_id=150159&mo de=frameset&ref=3&method=34&q=org&hl=org& ;CASE=0&MORPHO=1&location=1111111111111111111&LA NG=1"
>>
>> * >this example</a>, or my {@link ManualTestFancyToolTip}.
>> * * @author Henno Vermeulen
>> */
>> public class FancyToolTip extends ToolTip {
>>
>> private final String text;
>>
>> public FancyToolTip(Control control, String text) {
>> super(control);
>> this.text = text;
>> }
>>
>> @Override
>> protected Composite createToolTipContentArea(Event event, Composite
>> parent) {
>> FormToolkit toolkit = new FormToolkit(parent.getDisplay());
>> Form form = toolkit.createForm(parent);
>> form.getBody().setLayout(new FillLayout());
>>
>> FormText formText = toolkit.createFormText(form.getBody(), true);
>> // Parse tags but do not expand URLs because they don't make much
>> // sense in a tooltip.
>> formText.setText(text, true, false);
>>
>> FormColors colors = toolkit.getColors();
>> addColors(formText, colors);
>> return parent;
>> }
>>
>> private void addColors(FormText formText, FormColors colors) {
>> addSystemColor(SWT.COLOR_RED, "red", formText, colors);
>> addSystemColor(SWT.COLOR_YELLOW, "yellow", formText, colors);
>> }
>>
>> private void addSystemColor(int colorCode, String key, FormText
>> formText,
>> FormColors colors) {
>> colors.createColor(key, colors.getSystemColor(colorCode));
>> formText.setColor(key, colors.getColor(key));
>> }
>> }
>>
>>
>>
>> package nl.hm.ilja.ui;
>>
>> import org.eclipse.core.databinding.observable.Realm;
>> import org.eclipse.jface.databinding.swt.SWTObservables;
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Shell;
>> import org.eclipse.swt.widgets.Text;
>>
>> public class ManualTestFancyToolTip {
>>
>> public static void main(String[] args) {
>> final Display display = new Display();
>> Realm.runWithDefault(SWTObservables.getRealm(display), new
>> Runnable() {
>> public void run() {
>>
>> Shell shell = new Shell(display);
>> shell.setLayout(new FillLayout());
>> new ManualTestFancyToolTip().createPartControl(shell);
>> shell.pack();
>> shell.open();
>>
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch())
>> display.sleep();
>> }
>> }
>> });
>> }
>>
>> protected static void createPartControl(Shell shell) {
>> shell.setLayout(new FillLayout());
>> Text text = new Text(shell, SWT.NONE);
>> text.setText("Hover over me to show tooltip");
>> String string = "<form><p>This is a simple <b>test</b></p>"
>> + "<p>And this too.</p>"
>> + "<p>And <span color=\"red\">here</span> are "
>> + "some <span color=\"yellow\">colors</span>.</p>"
>> + "<p>Test with list items:</p>"
>> + "<li>A default (bulleted) list item.</li>"
>> + "<li>Another bullet list item.</li>" + "</form>";
>> new FancyToolTip(text, string);
>> shell.setSize(300, 200);
>> }
>>
>> }
>>
>
Re: rich tooltips/managing colors with FormText [message #551389 is a reply to message #551384] Fri, 06 August 2010 09:02 Go to previous messageGo to next message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
Thank you Tom. I tried some googling and found for example this blog with hovers over a text editor.

My fear is that it will take me several hours or longer of searching google and noodling around with code until I can turn this info into a simple html tooltip on an SWT control... At least my time estimate is unclear.

I was hoping someone knew an out of the box solution. Otherwise for the moment, I think i'll stick to the FormEditor markup which seems to work good enough at the moment.

[Updated on: Fri, 06 August 2010 09:03]

Report message to a moderator

Re: rich tooltips/managing colors with FormText [message #772362 is a reply to message #551364] Thu, 29 December 2011 15:52 Go to previous messageGo to next message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
I originally solved my problem by extending org.eclipse.jface.window.ToolTip and using a form with formtext. Now I want some real html and I think this might be easier than I originally thought with the SWT browser control, because the following snippet is quite easy and works like a charm:

http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet136.java
Re: rich tooltips/managing colors with FormText [message #772642 is a reply to message #772362] Fri, 30 December 2011 09:18 Go to previous message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
I tried using a Browser control inside a org.eclipse.jface.window.ToolTip. This is easy, however you have the problem that it cannot calculate it's own size so that the contents fit nicely (https://bugs.eclipse.org/bugs/show_bug.cgi?id=232501) which is not nice for tooltips with dynamic content. You can provide a fixed size e.g. by overwriting computeSize.

So I think the best option for dynamic content is using a custom ToolTip with SWT controls or having a look at the Javadoc tooltip which Eclipse uses.
Previous Topic:ComboBox in Popup window
Next Topic:Help regarding image manipulation using eclipse plugin
Goto Forum:
  


Current Time: Tue Oct 21 05:22:37 GMT 2014

Powered by FUDForum. Page generated in 0.02217 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software