how to disable gwt.material.design.client.ui.MaterialLink on specific situation? - gwt

I have this problem, I prepare a page with GWT, with the Materialink disabled.
my Java view:
#UiField
MaterialLink exportLink;
my view.xml:
<m:MaterialLink ui:field="exportLink" text="Esporta Report" addStyleNames="btn" target="_blank" float="RIGHT" enabled="false"/>
The button is disabled and the click not work (GOOD).
if the user complete the Form, I will enable it.
exportLink.setHref(updatedUrl());
exportLink.setEnabled(true);
and work perfert.
When I try to disable it becouse the user put a wrong value.
exportLink.setHref(null);
exportLink.setEnabled(false);
the button looks ok but is possible to click it.
Any suggest?

I found this solution:
public class MyMaterialLink extends MaterialLink {
#Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if(!enabled){
if (getElement().getAttribute("href") != null) {//per prevenire doppi disabled che cancellano l'href-disabled
getElement().setAttribute("data-href-disabled", getElement().getAttribute("href"));
getElement().removeAttribute("href");
}
}else {
getElement().setAttribute("href",getElement().getAttribute("data-href-disabled"));
getElement().removeAttribute("data-href-disabled");
}
}
#Override
public void setHref(String href) {
if(!isEnabled()){
getElement().setAttribute("data-href-disabled",href);
}else {
super.setHref(href);
}
}

Related

GWT Polymer UiBinder Vaadin Elements addEventListener Event KeysPressedEvent

I'm trying to follow the basic hello world examples that use GWT Polymer with the UiBinder Elements.
The basic GWT example stub generated code handles the key to specify input.
I thought it would be easy to have an event handler and check the key that was pressed.
I'm probably doing something very basic wrong.
Maybe some import I'm not doing or Polymer.importHref I forgot to include.
The event does trigger but I get undefined when I attempt to get the key from the event.
I guessed using "keypress" as the other examples use "click" and it did trigger but what is the right type to use?
I outputed some of the values and get the following:
event.getDetail().getKey() -> undefined
event.toString() -> [object KeyboardEvent]
nameFieldInput.getValue() ->
nameFieldInput.getInnerHTML() -> show what was typed before processing the current key
I also need to know what the string value or Constant to use for the key to do the test.
Please advise how to make this work using the event listener.
Here is my current code:
Main.ui.xml file
<paper-fab ui:field="sendButton" icon="gavel" title="send rpc" class="sendrpc" />
<paper-dialog ui:field="sendMsgDialog" entry-animation="fade-in-animation"
class="dialog" modal="">
<h2>Send RPC to Server</h2>
<paper-input ui:field="nameFieldInput" label="Please enter your name:"
required="" auto-validate="" pattern="[a-zA-Z]*"
minlength="4" char-counter="" error-message="required input!"/>
<div class="buttons">
<paper-button dialog-dismiss="">Cancel</paper-button>
<paper-button ui:field="sendFieldButton"
dialog-confirm="">Send</paper-button>
</div>
</paper-dialog>
Main.java class
#UiField PaperInputElement nameFieldInput;
...
nameFieldInput.addEventListener("keypress", new EventListener<KeysPressedEvent>() {
public void handleEvent(KeysPressedEvent event) {
if (event.getDetail().getKey() == "enter" && event.getDetail().getCtrl() == false) {
sendMsgDialog.close();
sendNameToServer();
}
}
});
I used the two following examples documents to get to this point and they show the following examples of using the listener. Unfortunately the gwtproject example only uses the event trigger and does not use the event object information..
http://www.gwtproject.org/doc/latest/polymer-tutorial/elements-applogic.html
...
#UiField PaperFabElement addButton;
...
public Main() {
initWidget(ourUiBinder.createAndBindUi(this));
addButton.addEventListener("click", new EventListener() {
#Override
public void handleEvent(Event event) {
addItemDialog.open();
}
});
}
http://vaadin.github.io/gwt-polymer-elements/demo/#gwt/UiBinderElement
...
#UiField PaperTabsElement paperTabs;
...
paperTabs.addEventListener(IronSelectEvent.NAME, new EventListener<IronSelectEvent>() {
public void handleEvent(IronSelectEvent event) {
PaperTabElement tab = (PaperTabElement)event.getDetail().getItem();
toast.close();
toast.setText("Tab \"" + tab.getTextContent() + "\" has been selected");
toast.open();
}
});
Here is an example that uses GWT standard Ui instead of polymer from:
h2g2java.blessedgeek.com/2010/02/tutorial-gwt-rpc-stub-modified-with.html
z.ui.xml file
<g:HorizontalPanel ui:field="hPanel">
<g:Button ui:field="sendButton" text="Send"
styleName="{style.sendButton}" />
<g:TextBox ui:field="nameField" text="GWT User" />
</g:HorizontalPanel>
z.java file
#UiField
HorizontalPanel hPanel;
#UiField
Button sendButton;
#UiField
TextBox nameField;
//Fired when user clicks send Button
#UiHandler("sendButton")
public void sendOnClick(ClickEvent event){
sendNameToServer();
}
//Fired when user types in the nameField.
#UiHandler("nameField")
public void nameOnKeyUp(KeyUpEvent event){
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
sendNameToServer();
}
}
What about this:
nameFieldInput.getPolymerElement().addEventListener("keyup", new EventListener() {
#Override
public void handleEvent(Event e) {
NativeEvent ne = (NativeEvent)e;
if (ne.getKeyCode() == KeyCodes.KEY_ENTER && !ne.getCtrlKey()) {
sendMsgDialog.close();
sendNameToServer();
}
}
});
With the help of #Euclides answer I was able to fix the code and get it working.
Here is the working corrected version.
Main.java class
sendButton.addEventListener("click", new EventListener() {
public void handleEvent(Event event) {
sendMsgDialog.open();
nameFieldInput.setAutofocus(true);
}
});
...
nameFieldInput.addEventListener("keyup", new EventListener<KeysPressedEvent>() {
public void handleEvent(KeysPressedEvent event) {
NativeEvent nativeEvent = (NativeEvent)event;
// CharCode is blank unless you use "keypress" as the event
// nameFieldInput.setErrorMessage(nativeEvent.getCharCode()+":"+nativeEvent.getKeyCode()+":"+nativeEvent.getAltKey()+":"+nativeEvent.getCtrlKey()+":"+nativeEvent.getMetaKey()+":"+nativeEvent.getShiftKey());
if (nativeEvent.getKeyCode() == KeyCodes.KEY_ENTER
&& !nativeEvent.getAltKey() && !nativeEvent.getCtrlKey()
&& !nativeEvent.getMetaKey() && !nativeEvent.getShiftKey()) {
sendMsgDialog.close();
sendNameToServer();
}
}
});

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

Enable/Disbale eclispe rcp editor via menu when clicking on a TreeViewer element

In my eclipse RCP application I have a TreeViewer from where I can select different editors, for drawing elements, which show up after double clicking. In my top menu I have an option that allows to enable/disbale the drawing. The action for the editors looks like the following:
public class EnableEditorAction implements IEditorActionDelegate {
IEditor hallEditor = null;
#Override
public void run(IAction action) {
if (hallEditor != null){
hallEditor.setMachineHallEditMode(true);
}
}
#Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
// check for enabled
boolean bEnabled = false;
if (targetEditor != null && targetEditor instanceof IMachineHallEditor) {
hallEditor = (IMachineHallEditor) targetEditor;
bEnabled = !hallEditor.isMachineHallEditingMode();
}
action.setEnabled(bEnabled);
}
#Override
public void selectionChanged(IAction action, ISelection selection) {
if (hallEditor != null) {
action.setEnabled(!hallEditor.isMachineHallEditingMode());
}
}
}
The problem I have is that the menu option is only enabled when clicking inside an editor. What i want is to enable the menu option also after clicking on one of the editors in the TreeViewer to the left.
How would I do that?
First, you don't need to check if targetEditor is null, since the action is already hooked to the editor via plugin.xml.
Second, I can see that you have an API isMachineHallEditingMode(). This should tell you if the left tree is selected, and the action should work properly.
It's important to set your action to always enabled in the plugin.xml. The Enables for: parameter should be empty, because enablement handling is done in your selectionChanged.
public class EnableEditorAction implements IEditorActionDelegate {
IEditor hallEditor;
#Override
public void run(IAction action) {
hallEditor.setMachineHallEditMode(true);
}
#Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
hallEditor = (IMachineHallEditor) targetEditor;
}
#Override
public void selectionChanged(IAction action, ISelection selection) {
action.setEnabled(!hallEditor.isMachineHallEditingMode());
}
}

GWT - Using a Checkbox as the header of a DisclosurePanel

I'm trying to create a DisclosurePanel that uses a Checkbox for a header. However, when I set this up, clicking on the Checkbox only opens and closes the DisclosurePanel; it doesn't check or uncheck the box. Anyone know how to make this happen?
Just add handlers to the open / close events and set the value of the checkbox there:
disclosurePanel.addOpenHandler<DisclosurePanel>(new OpenHandler<DisclosurePanel>() {
#Override
public void onOpen(OpenEvent<DisclosurePanel> event) {
checkbox.setValue(true);
}
));
disclosurePanel.addCloseHandler<DisclosurePanel>(new CloseHandler<DisclosurePanel>() {
#Override
public void onClose(CloseEvent<DisclosurePanel> event) {
checkbox.setValue(false);
}
));