Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Nebula » FormattedText without DataBinding Clashes - Sample Code
FormattedText without DataBinding Clashes - Sample Code [message #56859] Fri, 01 August 2008 16:39 Go to next message
Eclipse UserFriend
Originally posted by: java14.cshg.com.br

I created a constructor in the FormattedText that removes the databinding
before the setText method of the Text widget being called so that the
binding events over the text property of the Text widget don
Re: FormattedText without DataBinding Clashes - Sample Code [message #56889 is a reply to message #56859] Sat, 02 August 2008 20:20 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: maralc.gmail.com

I have the exact same problem.

I think it is a bug.

Somebody from the Nebula project can give us a hand about this issue?

The code posted really fixes the problem, but is it the best way to
solve the problem?

Thanks in advance.

Marcelo

Nelson Fernando wrote:
> I created a constructor in the FormattedText that removes the databinding
> before the setText method of the Text widget being called so that the
> binding events over the text property of the Text widget don´t get fired
> because of it .
>
> I was having event clashes between the databinding and the FormattedText
> before doing it if . Maybe it can help other people that have trouble with
> binding so heres the code :
>
> /*********************************************************** ********************
> * Copyright (c) 2005, 2007 Eric Wuillai.
> * All rights reserved. This program and the accompanying materials
> * are made available under the terms of the Eclipse Public License v1.0
> * which accompanies this distribution, and is available at
> * http://www.eclipse.org/legal/epl-v10.html
> *
> * Contributors:
> * Eric Wuillai (eric@wdev91.com) - initial API and implementation
> ************************************************************ *******************/package org.eclipse.nebula.widgets.formattedtext;import org.eclipse.core.databinding.Binding;import org.eclipse.core.databinding.DataBindingContext;import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.FocusEvent;import org.eclipse.swt.events.FocusListener;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Text;/** * Formatted text viewer. Add formating capabilities to the<code>Text</code> * widget of SWT. This control works on the same principle than the JFace * viewers. The embedded text widget is accessible by the getControl()method, * allowing to apply to it all necessary behaviors (layout,listeners...).<p> * * Formatting is
delegated to formatter objects implementing the<code>ITextFormatter</code> * interface. Each formatter class manages a base class of values (date,number...).<br> * Formatters are associated by 2 differents means : * <ul> * <li>By the <code>setFormatter()</code> method.</li> * <li>When <code>setValue()</code> is called and there is currently noformatter, * a new one is automatically created based on the class of thevalue.</li> * </ul> * * <h4>Styles:</h4> * <blockquote> * CENTER, LEFT, RIGHT, READ_ONLY * </blockquote> */public class FormattedText{ /** Encapsulated Text widget */ protected Text text; /** Formatter */ protected ITextFormatter formatter = null; /** Save position of cursor when the focus is lost */ protected int caretPos; /** Layout */ protected GridLayout layout; /** Filter for modify events */ protected Listener modifyFilter; /** * Databinding context */ protected DataBindingContext context ; /** * Binding of the text Field */ prot
ected Binding binding; protected static int count = 0; protected int id = ++count; /** * Creates a formatted text on a newly-created text control under the given * parent. The text control is created using the SWT style bits * <code>BORDER</code>. * * @param parent the parent control */ public FormattedText(Composite parent) { this(parent, SWT.BORDER); } /** * Creates a formatted text on a newly-created text control under thegiven * parent. The text control is created using the given SWT style bits. * * @param parent the parent control * @param style the SWT style bits used to create the text */ public FormattedText(Composite parent, int style) { this(new Text(parent, style & (~ (SWT.MULTI | SWT.PASSWORD |SWT.WRAP)))); } /** * Creates a formatted text on the given text control. * AND adds the binding context and text binding to the FormattedText * to avoid event clashes * @param t the text control */public FormattedText(Text t , DataBindingC
ontext context ,Binding binding) { // Adding the binding context this.context = context; //Adding the binding this.binding = binding; this.text = t; text.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); setText(formatter.getEditString()); text.setSelection(caretPos); formatter.setIgnore(false); } } public void focusLost(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); caretPos = text.getCaretPosition(); setText(formatter.getDisplayString()); formatter.setIgnore(false); } } }); modifyFilter = new Listener() { public void handleEvent(Event event) { event.type = SWT.None; } }; text.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { text = null; modifyFilter = null; formatter =
null; } });} /** * Creates a formatted text on the given text control. * * @param t the text control */ public FormattedText(Text t) { this.text = t; text.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); setText(formatter.getEditString()); text.setSelection(caretPos); formatter.setIgnore(false); } } public void focusLost(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); caretPos = text.getCaretPosition(); setText(formatter.getDisplayString()); formatter.setIgnore(false); } } }); modifyFilter = new Listener() { public void handleEvent(Event event) { event.type = SWT.None; } }; text.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { text
= null; modifyFilter = null; formatter = null; } }); } /** * Returns the primary <code>Text</code> control associated with thisviewer. * * @return the SWT text control which displays this viewer's content */ public Text getControl() { return text; } /** * Returns the formatter associated to the <code>Text</code> widget. * * @return Formatter, or <code>null</code> if no formatter is currentlyassociated */ public ITextFormatter getFormatter() { return formatter; } /** * Returns the current value of the widget.<p> * * The returned value is provided by the formatter and is of the typemanaged * bu the formatter. For exemple a <code>DateFormatter</code> will returna * <code>Date</code> value.<br> * If no formatter is associated, the <code>String</code> contained in the * <code>Text</code> widget is returned. * * @return Current value */ public Object getValue() { return formatter != null ? formatter.getVal
ue() : text.getText(); } /** * Returns <code>true</code> if the current value is valid, else<code>false</code>. * * @return <code>true</code> if valid. */ public boolean isValid() { return formatter != null ? formatter.isValid() : true; } /** * Associates a formatter to the widget. * * @param formatter formatter */ public void setFormatter(ITextFormatter formatter) { if ( formatter == null ) SWT.error(SWT.ERROR_NULL_ARGUMENT); if ( this.formatter != null ) { text.removeVerifyListener(this.formatter); this.formatter.detach(); } this.formatter = formatter; this.formatter.setText(text); text.addVerifyListener(this.formatter); formatter.setIgnore(true); text.setText(formatter.getDisplayString()); formatter.setIgnore(false); } /** * Sets the Text widget value, preventing fire of Modify events. * * @param value The String value to display in the widget */ private void setText(String value) { Display display = text.g
etDisplay(); try { display.addFilter(SWT.Modify, modifyFilter); // Heres all the diference // Removing this binding before setting anything if(context != null && binding != null ){ context.removeBinding(binding); } text.setText(value); } finally { display.removeFilter(SWT.Modify, modifyFilter); // here whe can put bakc the binding if(context != null && binding != null ){ context.addBinding(binding); } } } /** * Sets a new value.<p> * * If no formatter is currently associated to he widget, a new one iscreated * by the factory based on the value's class.<br> * If the value is incompatible with the formatter, an<code>IllegalArgumentException</code> * is returned. * * @param value new value */ public void setValue(Object value) { if ( formatter == null ) { setFormatter(DefaultFormatterFactory.createFormatter(value)) ; } formatter.setValue(value); formatter.setIgnore(true); text.setText(text.isF
ocusControl() ? formatter.getEditString() : formatter.getDisplayString()); formatter.setIgnore(false); }}
>
Re: FormattedText without DataBinding Clashes - Sample Code [message #56965 is a reply to message #56889] Mon, 04 August 2008 10:03 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You you file a bug and add a snippet to test and patch against CVS?

Tom

Marcelo Alcantara schrieb:
> I have the exact same problem.
>
> I think it is a bug.
>
> Somebody from the Nebula project can give us a hand about this issue?
>
> The code posted really fixes the problem, but is it the best way to
> solve the problem?
>
> Thanks in advance.
>
> Marcelo
>
> Nelson Fernando wrote:
>> I created a constructor in the FormattedText that removes the
>> databinding before the setText method of the Text widget being called
>> so that the binding events over the text property of the Text widget
>> don´t get fired because of it .
>>
>> I was having event clashes between the databinding and the
>> FormattedText before doing it if . Maybe it can help other people that
>> have trouble with binding so heres the code :
>>
>> /*********************************************************** ********************
>>
>> * Copyright (c) 2005, 2007 Eric Wuillai.
>> * All rights reserved. This program and the accompanying materials
>> * are made available under the terms of the Eclipse Public License v1.0
>> * which accompanies this distribution, and is available at
>> * http://www.eclipse.org/legal/epl-v10.html
>> *
>> * Contributors:
>> * Eric Wuillai (eric@wdev91.com) - initial API and implementation
>> ************************************************************ *******************/package
>> org.eclipse.nebula.widgets.formattedtext;import
>> org.eclipse.core.databinding.Binding;import
>> org.eclipse.core.databinding.DataBindingContext;import
>> org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import
>> org.eclipse.swt.events.DisposeListener;import
>> org.eclipse.swt.events.FocusEvent;import
>> org.eclipse.swt.events.FocusListener;import
>> org.eclipse.swt.layout.GridLayout;import
>> org.eclipse.swt.widgets.Composite;import
>> org.eclipse.swt.widgets.Display;import
>> org.eclipse.swt.widgets.Event;import
>> org.eclipse.swt.widgets.Listener;import
>> org.eclipse.swt.widgets.Text;/** * Formatted text viewer. Add
>> formating capabilities to the<code>Text</code> * widget of SWT. This
>> control works on the same principle than the JFace * viewers. The
>> embedded text widget is accessible by the getControl()method, *
>> allowing to apply to it all necessary behaviors
>> (layout,listeners...).<p> * * Formatting is
> delegated to formatter objects implementing
> the<code>ITextFormatter</code> * interface. Each formatter class manages
> a base class of values (date,number...).<br> * Formatters are associated
> by 2 differents means : * <ul> * <li>By the
> <code>setFormatter()</code> method.</li> * <li>When
> <code>setValue()</code> is called and there is currently noformatter,
> * a new one is automatically created based on the class of
> thevalue.</li> * </ul> * * <h4>Styles:</h4> * <blockquote> * CENTER,
> LEFT, RIGHT, READ_ONLY * </blockquote> */public class FormattedText{
> /** Encapsulated Text widget */ protected Text text; /** Formatter */
> protected ITextFormatter formatter = null; /** Save position of cursor
> when the focus is lost */ protected int caretPos; /** Layout */
> protected GridLayout layout; /** Filter for modify events */ protected
> Listener modifyFilter; /** * Databinding context */ protected
> DataBindingContext context ; /** * Binding of the text Field */ prot
> ected Binding binding; protected static int count = 0; protected int
> id = ++count; /** * Creates a formatted text on a newly-created text
> control under the given * parent. The text control is created using the
> SWT style bits * <code>BORDER</code>. * * @param parent the parent
> control */ public FormattedText(Composite parent) { this(parent,
> SWT.BORDER); } /** * Creates a formatted text on a newly-created
> text control under thegiven * parent. The text control is created using
> the given SWT style bits. * * @param parent the parent control *
> @param style the SWT style bits used to create the text */ public
> FormattedText(Composite parent, int style) { this(new Text(parent,
> style & (~ (SWT.MULTI | SWT.PASSWORD |SWT.WRAP)))); } /** * Creates a
> formatted text on the given text control. * AND adds the binding
> context and text binding to the FormattedText * to avoid event clashes
> * @param t the text control */public FormattedText(Text t , DataBindingC
> ontext context ,Binding binding) { // Adding the binding context
> this.context = context; //Adding the binding this.binding = binding;
> this.text = t; text.addFocusListener(new FocusListener() { public
> void focusGained(FocusEvent e) { if ( formatter != null &&
> text.getEditable() ) { formatter.setIgnore(true);
> setText(formatter.getEditString());
> text.setSelection(caretPos); formatter.setIgnore(false);
> } } public void focusLost(FocusEvent e) { if ( formatter !=
> null && text.getEditable() ) { formatter.setIgnore(true);
> caretPos = text.getCaretPosition();
> setText(formatter.getDisplayString());
> formatter.setIgnore(false); } } }); modifyFilter = new
> Listener() { public void handleEvent(Event event) { event.type =
> SWT.None; } }; text.addDisposeListener(new DisposeListener() { public
> void widgetDisposed(DisposeEvent e) { text = null;
> modifyFilter = null; formatter =
> null; } });} /** * Creates a formatted text on the given text
> control. * * @param t the text control */ public FormattedText(Text
> t) { this.text = t; text.addFocusListener(new FocusListener() {
> public void focusGained(FocusEvent e) { if ( formatter != null &&
> text.getEditable() ) { formatter.setIgnore(true);
> setText(formatter.getEditString());
> text.setSelection(caretPos); formatter.setIgnore(false);
> } } public void focusLost(FocusEvent e) { if (
> formatter != null && text.getEditable() ) {
> formatter.setIgnore(true); caretPos =
> text.getCaretPosition();
> setText(formatter.getDisplayString());
> formatter.setIgnore(false); } } }); modifyFilter = new
> Listener() { public void handleEvent(Event event) { event.type =
> SWT.None; } }; text.addDisposeListener(new DisposeListener() {
> public void widgetDisposed(DisposeEvent e) { text = null;
> modifyFilter = null; formatter = null; } }); } /** *
> Returns the primary <code>Text</code> control associated with
> thisviewer. * * @return the SWT text control which displays this
> viewer's content */ public Text getControl() { return text; }
> /** * Returns the formatter associated to the <code>Text</code>
> widget. * * @return Formatter, or <code>null</code> if no formatter
> is currentlyassociated */ public ITextFormatter getFormatter() {
> return formatter; } /** * Returns the current value of the
> widget.<p> * * The returned value is provided by the formatter and
> is of the typemanaged * bu the formatter. For exemple a
> <code>DateFormatter</code> will returna * <code>Date</code>
> value.<br> * If no formatter is associated, the <code>String</code>
> contained in the * <code>Text</code> widget is returned. * *
> @return Current value */ public Object getValue() { return
> formatter != null ? formatter.getVal
> ue() : text.getText(); } /** * Returns <code>true</code> if the
> current value is valid, else<code>false</code>. * * @return
> <code>true</code> if valid. */ public boolean isValid() { return
> formatter != null ? formatter.isValid() : true; } /** * Associates a
> formatter to the widget. * * @param formatter formatter */ public
> void setFormatter(ITextFormatter formatter) { if ( formatter == null )
> SWT.error(SWT.ERROR_NULL_ARGUMENT); if ( this.formatter != null ) {
> text.removeVerifyListener(this.formatter); this.formatter.detach();
> } this.formatter = formatter; this.formatter.setText(text);
> text.addVerifyListener(this.formatter); formatter.setIgnore(true);
> text.setText(formatter.getDisplayString());
> formatter.setIgnore(false); } /** * Sets the Text widget value,
> preventing fire of Modify events. * * @param value The String value
> to display in the widget */ private void setText(String value) {
> Display display = text.g
> etDisplay(); try { display.addFilter(SWT.Modify, modifyFilter);
> // Heres all the diference // Removing this binding before setting
> anything if(context != null && binding != null ){
> context.removeBinding(binding); } text.setText(value); } finally
> { display.removeFilter(SWT.Modify, modifyFilter); // here whe
> can put bakc the binding if(context != null && binding != null ){
> context.addBinding(binding); } } } /** * Sets a new value.<p>
> * * If no formatter is currently associated to he widget, a new one
> iscreated * by the factory based on the value's class.<br> * If the
> value is incompatible with the formatter,
> an<code>IllegalArgumentException</code> * is returned. * * @param
> value new value */ public void setValue(Object value) { if (
> formatter == null ) {
> setFormatter(DefaultFormatterFactory.createFormatter(value)) ; }
> formatter.setValue(value); formatter.setIgnore(true);
> text.setText(text.isF
> ocusControl() ?
> formatter.getEditString() :
> formatter.getDisplayString()); formatter.setIgnore(false); }}
>>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: FormattedText without DataBinding Clashes - Sample Code [message #591408 is a reply to message #56859] Sat, 02 August 2008 20:20 Go to previous message
Marcelo Alcantara is currently offline Marcelo AlcantaraFriend
Messages: 48
Registered: July 2009
Member
I have the exact same problem.

I think it is a bug.

Somebody from the Nebula project can give us a hand about this issue?

The code posted really fixes the problem, but is it the best way to
solve the problem?

Thanks in advance.

Marcelo

Nelson Fernando wrote:
> I created a constructor in the FormattedText that removes the databinding
> before the setText method of the Text widget being called so that the
> binding events over the text property of the Text widget don´t get fired
> because of it .
>
> I was having event clashes between the databinding and the FormattedText
> before doing it if . Maybe it can help other people that have trouble with
> binding so heres the code :
>
> /*********************************************************** ********************
> * Copyright (c) 2005, 2007 Eric Wuillai.
> * All rights reserved. This program and the accompanying materials
> * are made available under the terms of the Eclipse Public License v1.0
> * which accompanies this distribution, and is available at
> * http://www.eclipse.org/legal/epl-v10.html
> *
> * Contributors:
> * Eric Wuillai (eric@wdev91.com) - initial API and implementation
> ************************************************************ *******************/package org.eclipse.nebula.widgets.formattedtext;import org.eclipse.core.databinding.Binding;import org.eclipse.core.databinding.DataBindingContext;import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.FocusEvent;import org.eclipse.swt.events.FocusListener;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Text;/** * Formatted text viewer. Add formating capabilities to the<code>Text</code> * widget of SWT. This control works on the same principle than the JFace * viewers. The embedded text widget is accessible by the getControl()method, * allowing to apply to it all necessary behaviors (layout,listeners...).<p> * * Formatting is
delegated to formatter objects implementing the<code>ITextFormatter</code> * interface. Each formatter class manages a base class of values (date,number...).<br> * Formatters are associated by 2 differents means : * <ul> * <li>By the <code>setFormatter()</code> method.</li> * <li>When <code>setValue()</code> is called and there is currently noformatter, * a new one is automatically created based on the class of thevalue.</li> * </ul> * * <h4>Styles:</h4> * <blockquote> * CENTER, LEFT, RIGHT, READ_ONLY * </blockquote> */public class FormattedText{ /** Encapsulated Text widget */ protected Text text; /** Formatter */ protected ITextFormatter formatter = null; /** Save position of cursor when the focus is lost */ protected int caretPos; /** Layout */ protected GridLayout layout; /** Filter for modify events */ protected Listener modifyFilter; /** * Databinding context */ protected DataBindingContext context ; /** * Binding of the text Field */ prot
ected Binding binding; protected static int count = 0; protected int id = ++count; /** * Creates a formatted text on a newly-created text control under the given * parent. The text control is created using the SWT style bits * <code>BORDER</code>. * * @param parent the parent control */ public FormattedText(Composite parent) { this(parent, SWT.BORDER); } /** * Creates a formatted text on a newly-created text control under thegiven * parent. The text control is created using the given SWT style bits. * * @param parent the parent control * @param style the SWT style bits used to create the text */ public FormattedText(Composite parent, int style) { this(new Text(parent, style & (~ (SWT.MULTI | SWT.PASSWORD |SWT.WRAP)))); } /** * Creates a formatted text on the given text control. * AND adds the binding context and text binding to the FormattedText * to avoid event clashes * @param t the text control */public FormattedText(Text t , DataBindingC
ontext context ,Binding binding) { // Adding the binding context this.context = context; //Adding the binding this.binding = binding; this.text = t; text.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); setText(formatter.getEditString()); text.setSelection(caretPos); formatter.setIgnore(false); } } public void focusLost(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); caretPos = text.getCaretPosition(); setText(formatter.getDisplayString()); formatter.setIgnore(false); } } }); modifyFilter = new Listener() { public void handleEvent(Event event) { event.type = SWT.None; } }; text.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { text = null; modifyFilter = null; formatter =
null; } });} /** * Creates a formatted text on the given text control. * * @param t the text control */ public FormattedText(Text t) { this.text = t; text.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); setText(formatter.getEditString()); text.setSelection(caretPos); formatter.setIgnore(false); } } public void focusLost(FocusEvent e) { if ( formatter != null && text.getEditable() ) { formatter.setIgnore(true); caretPos = text.getCaretPosition(); setText(formatter.getDisplayString()); formatter.setIgnore(false); } } }); modifyFilter = new Listener() { public void handleEvent(Event event) { event.type = SWT.None; } }; text.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { text
= null; modifyFilter = null; formatter = null; } }); } /** * Returns the primary <code>Text</code> control associated with thisviewer. * * @return the SWT text control which displays this viewer's content */ public Text getControl() { return text; } /** * Returns the formatter associated to the <code>Text</code> widget. * * @return Formatter, or <code>null</code> if no formatter is currentlyassociated */ public ITextFormatter getFormatter() { return formatter; } /** * Returns the current value of the widget.<p> * * The returned value is provided by the formatter and is of the typemanaged * bu the formatter. For exemple a <code>DateFormatter</code> will returna * <code>Date</code> value.<br> * If no formatter is associated, the <code>String</code> contained in the * <code>Text</code> widget is returned. * * @return Current value */ public Object getValue() { return formatter != null ? formatter.getVal
ue() : text.getText(); } /** * Returns <code>true</code> if the current value is valid, else<code>false</code>. * * @return <code>true</code> if valid. */ public boolean isValid() { return formatter != null ? formatter.isValid() : true; } /** * Associates a formatter to the widget. * * @param formatter formatter */ public void setFormatter(ITextFormatter formatter) { if ( formatter == null ) SWT.error(SWT.ERROR_NULL_ARGUMENT); if ( this.formatter != null ) { text.removeVerifyListener(this.formatter); this.formatter.detach(); } this.formatter = formatter; this.formatter.setText(text); text.addVerifyListener(this.formatter); formatter.setIgnore(true); text.setText(formatter.getDisplayString()); formatter.setIgnore(false); } /** * Sets the Text widget value, preventing fire of Modify events. * * @param value The String value to display in the widget */ private void setText(String value) { Display display = text.g
etDisplay(); try { display.addFilter(SWT.Modify, modifyFilter); // Heres all the diference // Removing this binding before setting anything if(context != null && binding != null ){ context.removeBinding(binding); } text.setText(value); } finally { display.removeFilter(SWT.Modify, modifyFilter); // here whe can put bakc the binding if(context != null && binding != null ){ context.addBinding(binding); } } } /** * Sets a new value.<p> * * If no formatter is currently associated to he widget, a new one iscreated * by the factory based on the value's class.<br> * If the value is incompatible with the formatter, an<code>IllegalArgumentException</code> * is returned. * * @param value new value */ public void setValue(Object value) { if ( formatter == null ) { setFormatter(DefaultFormatterFactory.createFormatter(value)) ; } formatter.setValue(value); formatter.setIgnore(true); text.setText(text.isF
ocusControl() ? formatter.getEditString() : formatter.getDisplayString()); formatter.setIgnore(false); }}
>
Re: FormattedText without DataBinding Clashes - Sample Code [message #591437 is a reply to message #56889] Mon, 04 August 2008 10:03 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You you file a bug and add a snippet to test and patch against CVS?

Tom

Marcelo Alcantara schrieb:
> I have the exact same problem.
>
> I think it is a bug.
>
> Somebody from the Nebula project can give us a hand about this issue?
>
> The code posted really fixes the problem, but is it the best way to
> solve the problem?
>
> Thanks in advance.
>
> Marcelo
>
> Nelson Fernando wrote:
>> I created a constructor in the FormattedText that removes the
>> databinding before the setText method of the Text widget being called
>> so that the binding events over the text property of the Text widget
>> don´t get fired because of it .
>>
>> I was having event clashes between the databinding and the
>> FormattedText before doing it if . Maybe it can help other people that
>> have trouble with binding so heres the code :
>>
>> /*********************************************************** ********************
>>
>> * Copyright (c) 2005, 2007 Eric Wuillai.
>> * All rights reserved. This program and the accompanying materials
>> * are made available under the terms of the Eclipse Public License v1.0
>> * which accompanies this distribution, and is available at
>> * http://www.eclipse.org/legal/epl-v10.html
>> *
>> * Contributors:
>> * Eric Wuillai (eric@wdev91.com) - initial API and implementation
>> ************************************************************ *******************/package
>> org.eclipse.nebula.widgets.formattedtext;import
>> org.eclipse.core.databinding.Binding;import
>> org.eclipse.core.databinding.DataBindingContext;import
>> org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import
>> org.eclipse.swt.events.DisposeListener;import
>> org.eclipse.swt.events.FocusEvent;import
>> org.eclipse.swt.events.FocusListener;import
>> org.eclipse.swt.layout.GridLayout;import
>> org.eclipse.swt.widgets.Composite;import
>> org.eclipse.swt.widgets.Display;import
>> org.eclipse.swt.widgets.Event;import
>> org.eclipse.swt.widgets.Listener;import
>> org.eclipse.swt.widgets.Text;/** * Formatted text viewer. Add
>> formating capabilities to the<code>Text</code> * widget of SWT. This
>> control works on the same principle than the JFace * viewers. The
>> embedded text widget is accessible by the getControl()method, *
>> allowing to apply to it all necessary behaviors
>> (layout,listeners...).<p> * * Formatting is
> delegated to formatter objects implementing
> the<code>ITextFormatter</code> * interface. Each formatter class manages
> a base class of values (date,number...).<br> * Formatters are associated
> by 2 differents means : * <ul> * <li>By the
> <code>setFormatter()</code> method.</li> * <li>When
> <code>setValue()</code> is called and there is currently noformatter,
> * a new one is automatically created based on the class of
> thevalue.</li> * </ul> * * <h4>Styles:</h4> * <blockquote> * CENTER,
> LEFT, RIGHT, READ_ONLY * </blockquote> */public class FormattedText{
> /** Encapsulated Text widget */ protected Text text; /** Formatter */
> protected ITextFormatter formatter = null; /** Save position of cursor
> when the focus is lost */ protected int caretPos; /** Layout */
> protected GridLayout layout; /** Filter for modify events */ protected
> Listener modifyFilter; /** * Databinding context */ protected
> DataBindingContext context ; /** * Binding of the text Field */ prot
> ected Binding binding; protected static int count = 0; protected int
> id = ++count; /** * Creates a formatted text on a newly-created text
> control under the given * parent. The text control is created using the
> SWT style bits * <code>BORDER</code>. * * @param parent the parent
> control */ public FormattedText(Composite parent) { this(parent,
> SWT.BORDER); } /** * Creates a formatted text on a newly-created
> text control under thegiven * parent. The text control is created using
> the given SWT style bits. * * @param parent the parent control *
> @param style the SWT style bits used to create the text */ public
> FormattedText(Composite parent, int style) { this(new Text(parent,
> style & (~ (SWT.MULTI | SWT.PASSWORD |SWT.WRAP)))); } /** * Creates a
> formatted text on the given text control. * AND adds the binding
> context and text binding to the FormattedText * to avoid event clashes
> * @param t the text control */public FormattedText(Text t , DataBindingC
> ontext context ,Binding binding) { // Adding the binding context
> this.context = context; //Adding the binding this.binding = binding;
> this.text = t; text.addFocusListener(new FocusListener() { public
> void focusGained(FocusEvent e) { if ( formatter != null &&
> text.getEditable() ) { formatter.setIgnore(true);
> setText(formatter.getEditString());
> text.setSelection(caretPos); formatter.setIgnore(false);
> } } public void focusLost(FocusEvent e) { if ( formatter !=
> null && text.getEditable() ) { formatter.setIgnore(true);
> caretPos = text.getCaretPosition();
> setText(formatter.getDisplayString());
> formatter.setIgnore(false); } } }); modifyFilter = new
> Listener() { public void handleEvent(Event event) { event.type =
> SWT.None; } }; text.addDisposeListener(new DisposeListener() { public
> void widgetDisposed(DisposeEvent e) { text = null;
> modifyFilter = null; formatter =
> null; } });} /** * Creates a formatted text on the given text
> control. * * @param t the text control */ public FormattedText(Text
> t) { this.text = t; text.addFocusListener(new FocusListener() {
> public void focusGained(FocusEvent e) { if ( formatter != null &&
> text.getEditable() ) { formatter.setIgnore(true);
> setText(formatter.getEditString());
> text.setSelection(caretPos); formatter.setIgnore(false);
> } } public void focusLost(FocusEvent e) { if (
> formatter != null && text.getEditable() ) {
> formatter.setIgnore(true); caretPos =
> text.getCaretPosition();
> setText(formatter.getDisplayString());
> formatter.setIgnore(false); } } }); modifyFilter = new
> Listener() { public void handleEvent(Event event) { event.type =
> SWT.None; } }; text.addDisposeListener(new DisposeListener() {
> public void widgetDisposed(DisposeEvent e) { text = null;
> modifyFilter = null; formatter = null; } }); } /** *
> Returns the primary <code>Text</code> control associated with
> thisviewer. * * @return the SWT text control which displays this
> viewer's content */ public Text getControl() { return text; }
> /** * Returns the formatter associated to the <code>Text</code>
> widget. * * @return Formatter, or <code>null</code> if no formatter
> is currentlyassociated */ public ITextFormatter getFormatter() {
> return formatter; } /** * Returns the current value of the
> widget.<p> * * The returned value is provided by the formatter and
> is of the typemanaged * bu the formatter. For exemple a
> <code>DateFormatter</code> will returna * <code>Date</code>
> value.<br> * If no formatter is associated, the <code>String</code>
> contained in the * <code>Text</code> widget is returned. * *
> @return Current value */ public Object getValue() { return
> formatter != null ? formatter.getVal
> ue() : text.getText(); } /** * Returns <code>true</code> if the
> current value is valid, else<code>false</code>. * * @return
> <code>true</code> if valid. */ public boolean isValid() { return
> formatter != null ? formatter.isValid() : true; } /** * Associates a
> formatter to the widget. * * @param formatter formatter */ public
> void setFormatter(ITextFormatter formatter) { if ( formatter == null )
> SWT.error(SWT.ERROR_NULL_ARGUMENT); if ( this.formatter != null ) {
> text.removeVerifyListener(this.formatter); this.formatter.detach();
> } this.formatter = formatter; this.formatter.setText(text);
> text.addVerifyListener(this.formatter); formatter.setIgnore(true);
> text.setText(formatter.getDisplayString());
> formatter.setIgnore(false); } /** * Sets the Text widget value,
> preventing fire of Modify events. * * @param value The String value
> to display in the widget */ private void setText(String value) {
> Display display = text.g
> etDisplay(); try { display.addFilter(SWT.Modify, modifyFilter);
> // Heres all the diference // Removing this binding before setting
> anything if(context != null && binding != null ){
> context.removeBinding(binding); } text.setText(value); } finally
> { display.removeFilter(SWT.Modify, modifyFilter); // here whe
> can put bakc the binding if(context != null && binding != null ){
> context.addBinding(binding); } } } /** * Sets a new value.<p>
> * * If no formatter is currently associated to he widget, a new one
> iscreated * by the factory based on the value's class.<br> * If the
> value is incompatible with the formatter,
> an<code>IllegalArgumentException</code> * is returned. * * @param
> value new value */ public void setValue(Object value) { if (
> formatter == null ) {
> setFormatter(DefaultFormatterFactory.createFormatter(value)) ; }
> formatter.setValue(value); formatter.setIgnore(true);
> text.setText(text.isF
> ocusControl() ?
> formatter.getEditString() :
> formatter.getDisplayString()); formatter.setIgnore(false); }}
>>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Previous Topic:FormattedText without DataBinding Clashes - Sample Code
Next Topic:ExtendedCellDialog
Goto Forum:
  


Current Time: Thu Mar 28 21:31:46 GMT 2024

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

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

Back to the top