Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Text and Highlighter
Text and Highlighter [message #489249] Fri, 02 October 2009 06:08 Go to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 244
Registered: July 2009
Senior Member
Hello,

I have a SWT Text and if the textfield is empty i want a text hint e.g. "Enter firstname". When the textfield get the focus or the user enter a text. the hint should be disable.
How could I do this?

thx
Re: Text and Highlighter [message #489403 is a reply to message #489249] Fri, 02 October 2009 20:15 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Are you saying the hint text would appear within the text field but with a
different appearance? If so, Text does not provide a way to just set a text
hint and have it automatically managed, but implementing this in your app
should not be difficult. Set the Text's hint text with a different
foreground colour and font, and remove it when the Text receives a FocusIn
(and possibly put it back if the Text is empty when it receives a FocusOut).

If this is not what you were asking then please follow up here with a little
more description.

Grant


"SirWayne" <dennis.m@web.de> wrote in message
news:ha45ck$61p$1@build.eclipse.org...
> Hello,
>
> I have a SWT Text and if the textfield is empty i want a text hint e.g.
"Enter firstname". When the textfield get the focus or the user enter a
text. the hint should be disable.
> How could I do this?
>
> thx
Re: Text and Highlighter [message #489452 is a reply to message #489403] Sat, 03 October 2009 08:36 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 244
Registered: July 2009
Senior Member
I know this solution, but in my opinion i find this soultion bad, because if you call getText() you get the hint text and thats wrong(textfield is empty) and make problems.
Look at the attachment this is a FilteredTree with Highlighter.
Re: Text and Highlighter [message #489665 is a reply to message #489452] Mon, 05 October 2009 14:03 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
The attachment doesn't seem to have made it.

I know the solution that was provided is a bit hacky, but if there isn't
pre-canned or native support available for something then a solution like
this is sometimes the only choice. The getText() case would be handled by
having a property like "isActuallyEmpty" on the Text, which would be updated
to True/False by the listeners as needed. Regarding FilteredTree, since swt
Trees don't provide pre-canned support for this either, the FilteredTree
implementation is probably doing something similar to the Text suggestion.

Grant


"SirWayne" <dennis.m@web.de> wrote in message
news:ha72eh$52c$1@build.eclipse.org...
> I know this solution, but in my opinion i find this soultion bad, because
if you call getText() you get the hint text and thats wrong(textfield is
empty) and make problems.
> Look at the attachment this is a FilteredTree with Highlighter.
Re: Text and Highlighter [message #489770 is a reply to message #489665] Mon, 05 October 2009 20:29 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 244
Registered: July 2009
Senior Member
package test;
 
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
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.Label;
import org.eclipse.swt.widgets.Text;
 
 
public class MyText extends Composite {
 
    /**
     * The filter text widget to be used by this tree. This value may be
     * <code>null</code> if there is no filter widget, or if the controls have
     * not yet been created.
     */
    protected Text filterText;
 
    /**
     * The control representing the clear button for the filter text entry. This
     * value may be <code>null</code> if no such button exists, or if the
     * controls have not yet been created.
     * 
     * @since 3.5
     */
    protected Control clearButtonControl;
    /**
     * The Composite on which the filter controls are created. This is used to
     * set the background color of the filter controls to match the surrounding
     * controls.
     */
    protected Composite filterComposite;
 
    /**
     * The text to initially show in the filter text control.
     */
    protected String initialText = ""; //$NON-NLS-1$
 
    /**
     * The parent composite of the filtered tree.
     * 
     * @since 3.3
     */
    protected Composite parent;
 
    /**
     * Image descriptor for enabled clear button.
     */
    private static final String CLEAR_ICON = "icons/clear.gif"; //$NON-NLS-1$
 
    /**
     * Image descriptor for disabled clear button.
     */
    private static final String DISABLED_CLEAR_ICON = "icons/clear_co.gif"; //$NON-NLS-1$
 
    /**
     * Create a new instance of the receiver.
     * 
     * @param parent
     *            the parent <code>Composite</code>
     * @param treeStyle
     *            the style bits for the <code>Tree</code>
     * @param filter
     *            the filter to be used
     * @param useNewLook
     *            <code>true</code> if the new 3.5 look should be used
     * @since 3.5
     */
    public MyText(Composite parent, int style) {
        super(parent, style);
        this.parent = parent;
        init();
    }
 
    /**
     * Create the filtered tree.
     * 
     * @param treeStyle
     *            the style bits for the <code>Tree</code>
     * @param filter
     *            the filter to be used
     * 
     * @since 3.3
     */
    protected void init() {
        createControl(parent);
        setInitialText("InitialText");
        setFont(parent.getFont());
 
    }
 
    /**
     * Create the filtered tree's controls. Subclasses should override.
     * 
     * @param parent
     * @param treeStyle
     */
    protected void createControl(Composite parent) {
        GridLayout layout = new GridLayout();
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        setLayout(layout);
        setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        filterComposite = new Composite(this, SWT.BORDER);
        filterComposite.setBackground(getDisplay().getSystemColor(
                SWT.COLOR_LIST_BACKGROUND));
 
        GridLayout filterLayout = new GridLayout(2, false);
        filterLayout.marginHeight = 0;
        filterLayout.marginWidth = 0;
        filterComposite.setLayout(filterLayout);
        filterComposite.setFont(parent.getFont());
 
        createFilterControls(filterComposite);
        filterComposite.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING,
                true, false));
 
    }
 
    /**
     * Create the filter controls. By default, a text and corresponding tool bar
     * button that clears the contents of the text is created. Subclasses may
     * override.
     * 
     * @param parent
     *            parent <code>Composite</code> of the filter controls
     * @return the <code>Composite</code> that contains the filter controls
     */
    protected Composite createFilterControls(Composite parent) {
        createFilterText(parent);
        createClearTextNew(parent);
        if (clearButtonControl != null) {
            // initially there is no text to clear
            clearButtonControl.setVisible(false);
        }
 
        return parent;
    }
 
    protected void updateToolbar(boolean visible) {
        if (clearButtonControl != null) {
            clearButtonControl.setVisible(visible);
        }
    }
 
    /**
     * Creates the filter text and adds listeners. This method calls
     * {@link #doCreateFilterText(Composite)} to create the text control.
     * Subclasses should override {@link #doCreateFilterText(Composite)} instead
     * of overriding this method.
     * 
     * @param parent
     *            <code>Composite</code> of the filter text
     */
    protected void createFilterText(Composite parent) {
        filterText = doCreateFilterText(parent);
 
        filterText.addModifyListener(new ModifyListener() {
 
            @Override
            public void modifyText(ModifyEvent e) {
                if (filterText.getText().length() > 0) {
                    updateToolbar(true);
                } else {
                    updateToolbar(false);
                }
 
            }
        });
 
        filterText.addFocusListener(new FocusAdapter() {
            /*
             * (non-Javadoc)
             * 
             * @see
             *  org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.s wt
             * .events.FocusEvent)
             */
            public void focusGained(FocusEvent e) {
 
            }
 
            /*
             * (non-Javadoc)
             * 
             * @see
             *  org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.sw t
             * .events.FocusEvent)
             */
            public void focusLost(FocusEvent e) {
                if (filterText.getText().equals(initialText)) {
                    setFilterText(""); //$NON-NLS-1$
                }
            }
        });
 
        filterText.addMouseListener(new MouseAdapter() {
            /*
             * (non-Javadoc)
             * 
             * @see
             *  org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.sw t
             * .events.MouseEvent)
             */
            public void mouseDown(MouseEvent e) {
                if (filterText.getText().equals(initialText)) {
                    // XXX: We cannot call clearText() due to
                    // [url]https://bugs.eclipse.org/bugs/show_bug.cgi?id=260664[/url]
                    setFilterText(""); //$NON-NLS-1$
                }
            }
        });
 
        // if we're using a field with built in cancel we need to listen for
        // default selection changes (which tell us the cancel button has been
        // pressed)
        if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0) {
            filterText.addSelectionListener(new SelectionAdapter() {
                /*
                 * (non-Javadoc)
                 * 
                 * @see
                 *  org.eclipse.swt.events.SelectionAdapter#widgetDefaultSelecte d
                 * (org.eclipse.swt.events.SelectionEvent)
                 */
                public void widgetDefaultSelected(SelectionEvent e) {
                    if (e.detail == SWT.ICON_CANCEL)
                        clearText();
                }
            });
        }
 
        GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
        // if the text widget supported cancel then it will have it's own
        // integrated button. We can take all of the space.
        if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0)
            gridData.horizontalSpan = 2;
        filterText.setLayoutData(gridData);
    }
 
    /**
     * Creates the text control for entering the filter text. Subclasses may
     * override.
     * 
     * @param parent
     *            the parent composite
     * @return the text widget
     * 
     * @since 3.3
     */
    protected Text doCreateFilterText(Composite parent) {
        return new Text(parent, SWT.SINGLE | SWT.ICON_CANCEL);
    }
 
    /**
     * Set the background for the widgets that support the filter text area.
     * 
     * @param background
     *            background <code>Color</code> to set
     */
    public void setBackground(Color background) {
        super.setBackground(background);
    }
 
    /**
     * Create the button that clears the text.
     * 
     * @param parent
     *            parent <code>Composite</code> of toolbar button
     */
    private void createClearTextNew(Composite parent) {
        // only create the button if the text widget doesn't support one
        // natively
        if ((filterText.getStyle() & SWT.ICON_CANCEL) == 0) {
            final Image inactiveImage = Activator.getImageDescriptor(
                    DISABLED_CLEAR_ICON).createImage();
            final Image activeImage = Activator.getImageDescriptor(CLEAR_ICON)
                    .createImage();
            final Image pressedImage = new Image(getDisplay(), activeImage,
                    SWT.IMAGE_GRAY);
 
            final Label clearButton = new Label(parent, SWT.NONE);
            clearButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER,
                    false, false));
            clearButton.setImage(inactiveImage);
             clearButton.setBackground(parent.getDisplay().getSystemColor (
                    SWT.COLOR_LIST_BACKGROUND));
            clearButton.setToolTipText("Tooltip");
            clearButton.addMouseListener(new MouseAdapter() {
                private MouseMoveListener fMoveListener;
 
                public void mouseDown(MouseEvent e) {
                    clearButton.setImage(pressedImage);
                    fMoveListener = new MouseMoveListener() {
                        private boolean fMouseInButton = true;
 
                        public void mouseMove(MouseEvent e) {
                            boolean mouseInButton = isMouseInButton(e);
                            if (mouseInButton != fMouseInButton) {
                                fMouseInButton = mouseInButton;
                                clearButton
                                        .setImage(mouseInButton ? pressedImage
                                                : inactiveImage);
                            }
                        }
                    };
                    clearButton.addMouseMoveListener(fMoveListener);
                }
 
                public void mouseUp(MouseEvent e) {
                    if (fMoveListener != null) {
                        clearButton.removeMouseMoveListener(fMoveListener);
                        fMoveListener = null;
                        boolean mouseInButton = isMouseInButton(e);
                        clearButton.setImage(mouseInButton ? activeImage
                                : inactiveImage);
                        if (mouseInButton) {
                            clearText();
                            filterText.setFocus();
                        }
                    }
                }
 
                private boolean isMouseInButton(MouseEvent e) {
                    Point buttonSize = clearButton.getSize();
                    return 0 <= e.x && e.x < buttonSize.x && 0 <= e.y
                            && e.y < buttonSize.y;
                }
            });
            clearButton.addMouseTrackListener(new MouseTrackListener() {
                public void mouseEnter(MouseEvent e) {
                    clearButton.setImage(activeImage);
                }
 
                public void mouseExit(MouseEvent e) {
                    clearButton.setImage(inactiveImage);
                }
 
                public void mouseHover(MouseEvent e) {
                }
            });
            clearButton.addDisposeListener(new DisposeListener() {
                public void widgetDisposed(DisposeEvent e) {
                    inactiveImage.dispose();
                    activeImage.dispose();
                    pressedImage.dispose();
                }
            });
 
            this.clearButtonControl = clearButton;
        }
    }
 
    /**
     * Clears the text in the filter text widget.
     */
    protected void clearText() {
        setFilterText(""); //$NON-NLS-1$
    }
 
    /**
     * Set the text in the filter control.
     * 
     * @param string
     */
    protected void setFilterText(String string) {
        if (filterText != null) {
            filterText.setText(string);
            selectAll();
        }
    }

    /**
     * Get the filter text for the receiver, if it was created. Otherwise return
     * <code>null</code>.
     * 
     * @return the filter Text, or null if it was not created
     */
    public Text getFilterControl() {
        return filterText;
    }
 
    /**
     * Convenience method to return the text of the filter control. If the text
     * widget is not created, then null is returned.
     * 
     * @return String in the text, or null if the text does not exist
     */
    protected String getFilterString() {
        return filterText != null ? filterText.getText() : null;
    }
 
    /**
     * Set the text that will be shown until the first focus. A default value is
     * provided, so this method only need be called if overriding the default
     * initial text is desired.
     * 
     * @param text
     *            initial text to appear in text field
     */
    public void setInitialText(String text) {
        initialText = text;
        if (filterText != null) {
            filterText.setMessage(text);
            if (filterText.isFocusControl()) {
                setFilterText(initialText);
            } else {
                getDisplay().asyncExec(new Runnable() {
                    public void run() {
                        if (!filterText.isDisposed()
                                && filterText.isFocusControl()) {
                            setFilterText(initialText);
 
                        }
                    }
                });
            }
        } else {
            setFilterText(initialText);
        }
    }
 
    /**
     * Select all text in the filter text field.
     * 
     */
    protected void selectAll() {
        if (filterText != null) {
            filterText.selectAll();
        }
    }
 
    /**
     * Get the initial text for the receiver.
     * 
     * @return String
     */
    protected String getInitialText() {
        return initialText;
    }
 
}

[Updated on: Mon, 05 October 2009 20:31]

Report message to a moderator

Re: Text and Highlighter [message #490474 is a reply to message #489770] Thu, 08 October 2009 18:20 Go to previous message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 244
Registered: July 2009
Senior Member
Ok setMessage(text) is enough for a highlighter
Previous Topic:SWT/AWT-bridge on MacOS 10.6
Next Topic:In TextViewer, not able to position cursor
Goto Forum:
  


Current Time: Mon Dec 09 09:42:38 GMT 2019

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

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

Back to the top