|
|
|
|
Re: Text and Highlighter [message #489770 is a reply to message #489665] |
Mon, 05 October 2009 20:29 |
Dennis Melzer 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03569 seconds