how to disable pasting in one of the gwt textbox - gwt

I have two textboxes and i want to just disable one of them from pasting anything. I have tried
sinkEvents( Event.ONPASTE );
but it disables both of the textboxes from pasting.

Try to create a custom TextBox wherein you have to override onBrowserEvent function.
public TextInput() {
super();
sinkEvents( Event.ONPASTE );
}
#Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent( event );
switch (DOM.eventGetType(event)) {
case Event.ONPASTE:
event.preventDefault();
break;
}
}

Related

How to enable content proposal in NatTable TextCellEditor?

I am currently looking for content assist feature in Nattable TextCellEditor.I have found the way to attach the ContentProposalAdapter and IContentProposalProvider by extending the Nattable TextCellEditor. but ,The selected value from the proposed list is not updating in the text control.
Snippet :
#Override
protected Text createEditorControl(final Composite parent, final int Style) {
this.textControl = super.createEditorControl(parent, style);
contentProposalAdapter =
new ContentProposalAdapter(this.textControl, new TextContentAdapter(), contentProposalProvider, keyStroke,
null);
contentProposalAdapter.addContentProposalListener(new IContentProposalListener() {
#Override
public void proposalAccepted(IContentProposal proposal) {
System.out.println(proposal.getContent());
}
});
}
The problem you have is the internal FocusListener that is triggered while selecting a value in the popup. To add the support you also need to override the internal FocusListener with a listener that doesn't fire if the content proposal popup is open.
An example would be to add a boolean flag that indicates that the popup is open and add a listener that sets the flag accordingly.
private boolean popupOpen = false;
...
contentProposalAdapter.addContentProposalListener(new IContentProposalListener2() {
#Override
public void proposalPopupClosed(ContentProposalAdapter adapter) {
this.popupOpen = false;
}
#Override
public void proposalPopupOpened(ContentProposalAdapter adapter) {
this.popupOpen = true;
}
});
And then implement and set a FocusListener in the constructor that takes care of that flag.
this.focusListener = new FocusAdapter() {
#Override
public void focusLost(FocusEvent e) {
if (!TextCellEditor.this.popupOpen) {
if (!commit(MoveDirectionEnum.NONE, true)) {
if (e.widget instanceof Control && !e.widget.isDisposed()) {
((Control) e.widget).forceFocus();
}
} else {
if (!TextCellEditor.this.parent.isDisposed())
TextCellEditor.this.parent.forceFocus();
}
}
}
};
In case the value should be immediately committed after it is selected, you need to add a listener that performs the commit after selection.
contentProposalAdapter.addContentProposalListener(new IContentProposalListener() {
#Override
public void proposalAccepted(IContentProposal proposal) {
commit(MoveDirectionEnum.NONE);
}
});
Unfortunately the AbstractCellEditor#InlineFocusListener is private and can therefore not be extended.
Feel free to file an enhancement ticket for NatTable to introduce the ability to easily add content proposals to a text cell editor.
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=NatTable

GWT: Adding a value change listener to EditTextCell widget

I want to do validation for EditTextCell widget in the grid. So I am trying to extend the EditTextCell and add a value change listener to the same, so that I can continue with the validation. But I am unable to add the same. Any help would be appreciated. Am using GWT 2.4.0
My code looks like this.
public class MyEditTextCell extends EditTextCell {
public MyEditTextCell() {
super();
onValueChange( new ValueChangeHandler<String>() {
#Override public void onValueChange( ValueChangeEvent event ) {
Window.alert( "jsdfk" );
}
} );
}
private void onValueChange( ValueChangeHandler<String> valueChangeHandler ) {
Window.alert( "jsdfk" );
}
}
It seems like the only adequate way to do this with EditTextCell is to override onBrowserEvent() similarly to private editEvent() method of EditTextCell class.
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
String type = event.getType();
if ("keyup".equals(type) || "keydown".equals(type) || "blur".equals(type) {
validateValue();
}
}
But listening keyup, keydown and blur event doesn't guarantee handling actual change.
If you don't want to skip any changes(for example pasting text by context right-click menu) you should to add com.google.gwt.user.client.Timer which checks value. Timer should be ran on focus and stoped on blur.
If you want to code in 'event way'
1) replace validateValue() by ValueChangeEvent.fire(this, vale);
2) add to your MyEditTextCEll interface HasValueChangeHandlers and implement it like this
public HandlerRegistration addValueChangeHandler(
ValueChangeHandler<String> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}

GWT: How to disable the anchor link event when clicked

I want to disable the anchor link event when it clicked one time. I used anchor.setenabled(false) but nothing happend. When I click the same button again the event e is true. I want false at that time.
public void onCellClick(GridPanel grid, int rowIndex, int colindex,EventObject e)
{
if(rowIndex==0 && colindex==2){
tomcatHandler = "Start";
anchorStart.setEnabled(false);
}else if(rowIndex==0 && colindex==3){
tomcatHandler = "Stop";
****anchorStop.setEnabled(false);
anchorStart.setEnabled(false);
anchorRestart.setEnabled(true);****
}else if(rowIndex==0 &&colindex==4){
tomcatHandler = "Restart";
anchorRestart.setEnabled(false);
}
AdminService.Util.getInstance().tomcat(tomcatHandler,new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
imageChangeEvent(result);
}
#Override
public void onFailure(Throwable caught) {
}
});}
Anchors in GWT have always had a problem with setEnabled() because HTML doesn't support such a property. A quick workaround is to create a new widget that subclasses GWT's Anchor, adding the following override:
#Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
This disables the passing of the browser event to GWT's Anchor class (summarily disabling all related handlers) when the link is double clicked, focused or clicked and is in a disabled state.
Source
It doesn't seem to actually disable the anchor, but it does retain the status that has been set with anchor.setEnabled(), so just test that within your handler e.g.
myAnchor.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent evt) {
// write to filter and then call reload
if (((Anchor) evt.getSource()).isEnabled()) {
//do stuff
}
}
});

GWT pasting event

I want to handle events when user pastes some text in TextBox. Which event is fired in this situation? I tried ValueChange and Change handlers, but they didn't work.
This might help you. Describes a workaround to hook to the onpaste event.
In short:
subclass TextBox
sink the onpaste event in the constructor
sinkEvents(Event.ONPASTE);
override onBrowserEvent(Event event)
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (event.getTypeInt()) {
case Event.ONPASTE: {
// do something here
break;
}
}
}
GWT does not yet have support for cut, copy & paste: http://code.google.com/p/google-web-toolkit/issues/detail?id=4030
Edited:
Another option is to use JSNI. For example add this to your GWT class:
public native void addCutHandler(Element element)
/*-{
var temp = this; // hack to hold on to 'this' reference
element.oncut = function(e) {
temp.#org.package.YourClass::handleCut()();
}
}-*/;
public void handleCut() {
Window.alert("Cut!");
}
**(Write In the Constructor)**
sinkEvents( Event.ONPASTE );
**(After that write below code)**
public void onBrowserEvent( Event event )
{
super.onBrowserEvent( event );
switch ( event.getTypeInt() )
{
case Event.ONPASTE :
{
event.stopPropagation();
event.preventDefault();
break;
}
}
}

Adding style and removing style to a widget in gwt

I wrote a widget like this:
public class GroupLbl extends Composite implements ClickHandler, MouseOutHandler {
private Label lbl;
private GroupLblHandler lblHandler = null;
private HorizontalPanel hp;
public void onClick(ClickEvent event) {
hp.setStyleName("background-GroupLbl");
if (event.getSource().equals(folder) || event.getSource().equals(lbl)) {
lblHandler.onGroupLabelSelect(this);
}
}
public GroupLbl(String title, GroupLblHandler handler) {
hp.add(lbl);
lblHandler = handler;
if (handler != null) {
lbl.addClickHandler(this);
}
initWidget(hp);
}
#Override
public Widget getWidget() {
return hp;
}
public void onMouseOut(MouseOutEvent event) {
hp.removeStyleName("background-GroupLbl");
}
}
I use this widget in my form,when the user click on one of INSTANCE of widget the stylename should be assign to this and when the user click on other the stylename should assign to that and the first shoud remove the style name I implemented mouseouthandler but it doese not work,the style set the bgcolor to hp so the user understand which grouplbl he/she has selected.what shoul I do(It is part of my code)? tnx and excuse me for my bad english !!!
Define an additional CSS style with suffix -background with the bg color you wish to have.
Instead of setStyleName use addStyleDependentName( "background" ) along with removeStyleDependentName( "background" ) to add/remove -background style.