How to handle the drag & drop of a hyperlink in GWT - gwt

I want to handle the drag & drop of hyperlinks in my app. The hyperlink could be from any where, therefore I cannot make it setDragable(true) and setData("link", "the URL") to mark it.
A very similar scenario would be Google Image search by image, where you can drag & drop a link of image to the search box.
The sample code,
Label lblDropLink = new Label("Drop a link here");
lblDropLink.addDragOverHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
lblDropLink.setText("Drop here to add the link.");
lblDropLink.setStyleName("dragOverFade");
}
});
lblDropLink.addDropHandler(new DropHandler() {
#Override
public void onDrop(DropEvent event) {
event.preventDefault();
// QUESTION: how to get the link, and even its text?
}
});
Thanks!

The only thing you can get when dropping a link is the URL!
You can get it by calling the event.getData(format) method.
format can either be "text" or "url" (see dom-datatransfer-getdata). When testing it "text" and "url" always deliverd the same result.
So the code you need in "// QUESTION: how to get the link, and even its text?" is one of the two
event.getData("text")
event.getData("url")
Here is a little sample prgramm:
final Label lblDropLink = new Label(defaultText);
lblDropLink.addDragOverHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
lblDropLink.setText(dragOverText);
}
});
lblDropLink.addDropHandler(new DropHandler() {
#Override
public void onDrop(DropEvent event) {
event.preventDefault();
RootPanel.get().add(new Label("Dropped source formated with \"text\": " + event.getData("text")));
RootPanel.get().add(new Label("Dropped source formated with \"url\": " + event.getData("url")));
}
});
RootPanel.get().add(lblDropLink);
}
That should be all...

There is a function called getSource(). Use it to cast the source of the dragged object and get the link property of the source object in the onDrop event.
Example:
public void onDrop(DropEvent event) {
event.preventDefault();
Link link = (Link)event.getSource();
}
Link is the object being dragged. Then you can call Link.getLink() or whatever you use to get the value of the link.
Hpope you get the concept :)

Related

How to fix the conflict between mySuggestBox.addKeyDownHandler & mySuggestBox.addSelectionHandler in GWT?

I have a SuggestBox. I want 2 things:
-1st, when user Type in a word if the SuggestOracle suggest any word & then if i select that word by hitting the Enter key on the selected word or clicking on that word it will call a methodX
-2st, suppose I typed a word into a suggest & SuggestOracle suggests NO word, then I want that when I hit Enter key it will fire methodX
This below code met the 1st requirement.
getView().getSuggestBox().addSelectionHandler(new SelectionHandler(){
#Override
public void onSelection(SelectionEvent event) {
methodX();
}
});
This below code met the 2nd requirement.
getView().getSuggestBox().addKeyDownHandler(new KeyDownHandler(){
#Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
methodX();
}
}
});
However, if i use both of them, then I got this problem.
FOr example, When I type "car" the suggest oracle show "car insurance" "car buy" & then when I use arrow down key to select "car buy" & hit the Enter key, then the system call methodX("car"); not "methodX("car buy")"
WHat is the problem? I think they got conflict or something
How to fix it?
I think you simply need to add a ValueChangeHandler to the ValueBox
getView().getSuggestBox()getValueBox().addValueChangehandler( ... );
As far as I remember, this event is triggered, if you hit enter in the Box.
There is no need to register two handlers. This can be achieved by single handler as shown below that will be fired event value is selected via mouse click or by pressing enter key.
suggestionBox.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {
#Override
public void onSelection(SelectionEvent<Suggestion> event) {
System.out.println("selection changed :" + event.getSelectedItem().getDisplayString());
methodX();
}
});
If nothing works then try with below handler.
suggestionBox.getValueBox().addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
System.out.println("value changed :" + event.getValue());
}
});
I think you should try to use a custom SelectionHandler class, and play with
getDisplayString and getReplacementString
here is a quick copy/paste of my code:
public class SearchStuff Composite implements SelectionHandler<ItemSuggestion> {
#Override
public void onSelection(SelectionEvent<ItemSuggestion> event) {
System.out.println(" getDisplayString( ) = " + event.getSelectedItem().getDisplayString()) ;
System.out.println("getReplacementString( ) = " + event.getSelectedItem().getReplacementString());
}
public class ItemSuggestion implements IsSerializable, com.google.gwt.user.client.ui.SuggestOracle.Suggestion{
#Override
public String getReplacementString() {
....
}
}

GWT - enable button on KeyDownEvent in RichTextArea

I have following handler
textArea.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
//here
}
});
I need to enable save button with id "idsave", but I am not able to refer the button.
I am new to GWT, any help would be appreciated.
Typically, you do not use element ids in GWT. If you created a button, you can simply use it:
private Button saveButton;
...
saveButton = new Button("Save");
textArea.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
saveButton.setEnabled(true);
}
});
If you don't have the reference of the button then try with the id.
// get element by id
Element saveButtonElement = RootPanel.get("idsave").getElement();
// remove disabled attribute to make it enable
saveButtonElement.removeAttribute("disabled");

How to get an editable ComboBox to respond to ENTER

This code is what I want to do. While typing in an editable ComboBox I want to release ENTER and handle that enter event. However, I cannot get the application to respond, a message was not printed. I wrote basically the same code for a text box and it worked fine, a message was printed. I also wrote the handler for any KeyReleased event for a ComboBox and that worked fine also, a message was printed. The trouble is the enter key. Why does this code not do what I want in an editable ComboBox?
#FXML
ComboBox comboBox;
public class ScreenController implements Initializable {
#Override
public void initialize(...) {
...
comboBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
if (ke.getCode == KeyCode.ENTER) {
System.out.println("ENTER was released");
}
}
});
}
}
I was suffering from the same bug/feature. Luckily I found this posting
The solution is not to register your handler via comboBox.setOnKeyReleased(). instead, use EventFilter:
comboBox.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
if (ke.getCode == KeyCode.ENTER) {
System.out.println("ENTER was released");
}
}
});
This actually works as expected.
It's look to be a JavaFX bug. setOnKeyPressed doesn't work to. look at this
javafx jira

style for GXT menu button

I see GXT Menu can only be set on CellButtonBase subclasses' instances through setMenu() method.
I'd like to show an image instead of a button and show a menu when user clicks that image. unfortunately, Image is not a subclass of CellButtonBase and thus I can't attach a GXT Menu to it.
so how can I make TextButton (which seems to be my only choice here) look like an image if I have to use it?
There's no documentation or examples on this subject. I asked on Sencha GXT forum support, but got no response.
ok, I found a way to do this without TextButton. add an Image and call menu.show(...) in click handler.
private void createMenu() {
menu = new Menu();
Image menuButtonImage = new Image(Resources.INSTANCE.nav_preferences());
menuButtonImage.addStyleName(CSS.header_bar_icon());
menuButtonImage.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
menu.showAt(getAbsoluteLeft(), getAbsoluteTop() + MENU_OFFSET_FROM_IMAGE_TOP);
}
});
menu.addShowHandler(new ShowEvent.ShowHandler() {
#Override
public void onShow(ShowEvent event) {
highlight();
}
});
menu.addHideHandler(new HideEvent.HideHandler() {
#Override
public void onHide(HideEvent event) {
removeHighlight();
}
});
menu.setStyleName(CSS.menu());
add(menuButtonImage);
}
private void addUserSettings() {
MenuItem userSettingsItem = new MenuItem("User Settings");
userSettingsItem.addSelectionHandler(new SelectionHandler<Item>() {
#Override
public void onSelection(SelectionEvent<Item> event) {
_coreLayout.showUserSettingsPage();
}
});
userSettingsItem.setStyleName(CSS.menu_item());
menu.add(userSettingsItem);
}
private void highlight() {
addStyleName(CSS.header_bar_icon_box_selected());
}
private void removeHighlight() {
removeStyleName(CSS.header_bar_icon_box_selected());
}

Gwt form question

I have a gwt form which has about 70-100 widgets (textboxes,listboxes,custom widgets etc)
I am trying to implement the features of CUT ,COPY in this form .For this i have 2 buttons right on top of the form.
Now the problem i have is that when i click on the copy button , the widget that was focused in the form looses focus and i dont know which text to copy(or which widget was last focused before the focus getting to the copy button)
I was planning to implement blur handlers on all the widgets but i feel is a very laborious and not a good solution.
How can i get around this issue?
Thanks
Perhaps someone with a deeper insight might provide a better approach but I beleive adding blur handlers is perfectly valid. I do not quite see why you think it would be laborious, after all you don't need a different handler for each of your widgets, you can get away with only one(at most a couple for a variety of controls..), here is a very simple example,
public class CustomBlurHandler implements BlurHandler{
Object lastSource;
String text;
#Override
public void onBlur(BlurEvent event) {
if (event.getSource() instanceof TextBox) {
lastSource = event.getSource();
text = textBox.getSelectedText();
}
}
public Object getLastSource() {
return lastSource;
}
public String getText() {
return text;
}
}
and onModuleLoad :
public class Test implements EntryPoint {
CustomBlurHandler handler = new CustomBlurHandler();
public void onModuleLoad() {
TextBox text1 = new TextBox();
TextBox text2 = new TextBox();
text1.addBlurHandler(handler);
text2.addBlurHandler(handler);
Button b = new Button("Get last selected text");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.alert(handler.getLastSource()+ " " + handler.getText());
}
});
RootPanel.get().add(text1);
RootPanel.get().add(text2);
RootPanel.get().add(b);
}
}