Catel DataWindow OK button does not close view - catel

I have a DataWindow (MyWindow) with the OK Cancel and Apply buttons. Within this DataWindow is a View (MyView). For both MyWindow and MyView I have overridden GetViewModelType() like so:
protected override Type GetViewModelType()
{
return typeof(MyViewModel);
}
I also have MyViewModel registered with MyWindow via the UIVisualizerService:
_uiVisualizerService.Register(typeof(MyViewModel), typeof(MyWindow));
On MyViewModel I have overridden ViewModelBase.Save():
protected override bool Save()
{
if (HasErrors)
return false;
MyModel.SaveChanges();
return base.Save();
}
I use this to display MyWindow:
var myViewModel = TypeFactory.Default.CreateInstanceWithParametersAndAutoCompletion<MyViewModel>();
_uiVisualizerService.Show(myViewModel);
But when I click on the 'OK' Button of MyWindow, although the Save() method is called and base.Save() returns 'true', the Window is not closed.
If I open the MyWindow using,
new MyWindow().ShowDialog();
the 'OK' button works.
Am I missing something or doing something wrong? The Apply and Cancel Buttons work 100%
EDIT:
I just noticed something, using Show() does not close the window when OK is clicked (as said above) BUT ShowDialog() does.
Is this correct?

Issue was fixed by updating to Catel v4.0.

Related

How to test navigation from DialogFragment to another DialogFragment?

I'm using the Navigation component for my two DialogFragments and when I press a button on the first DialogFragment it is dismissed and then the second one is shown. I need to test that clicking this button will take me to the second dialog. I have a simple home fragment that is overlayed by the first DialogFragment at the start of the app. The following code is from the first DialogFragment.
/**
* Redirects users to another dialog after pressing button
*/
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.setOnClickListener {
if (findNavController().currentDestination?.id == R.id.firstDialogFragment) {
findNavController().navigateUp()
val action = HomeFragmentDirections.actionHomeFragmentToSecondDialogFragment()
findNavController().navigate(action)
}
}
}
This next bit of code comes from the developer's guide and only checks for the behavior of dismissing a DialogFragment back to the previous Fragment.
#RunWith(AndroidJUnit4::class)
class MyTestSuite {
#Test fun testDismissDialogFragment() {
// Assumes that "MyDialogFragment" extends the DialogFragment class.
with(launchFragment<MyDialogFragment>()) {
onFragment { fragment ->
assertThat(fragment.dialog).isNotNull()
assertThat(fragment.requireDialog().isShowing).isTrue()
fragment.dismiss()
fragment.requireFragmentManager().executePendingTransactions()
assertThat(fragment.dialog).isNull()
}
// Assumes that the dialog had a button
// containing the text "Cancel".
onView(withText("Cancel")).check(doesNotExist())
}
}
}
I need some way to test the behavior of a DialogFragment's button and see that it dismisses itself and starts the second DialogFragment.
When I test for the button being clicked, the first DialogFragment is correctly dismissed, but the second DialogFragment is not launched. I've used both Espresso and UiAutomator and the click does occur, but reading the code snippet's explanation for testing DialogFragments it says,
"Even though dialogs are instances of graphical fragments, you use the launchFragment() method so that the dialog's elements are populated in the dialog itself, rather than in the activity that launches the dialog".
Is the reason that I am unable to check if the second DialogFragment exists or not, because it is an instance of a graphical fragment and my click listener for the button on the first DialogFragment cannot implement launchFragment() for the second DialogFragment?

Codename One set action event from different class for button in dialog

I have an Android application with a dialog and a few buttons inside.
I want to reuse the dialog for different purposes and looking for a way to call the button from a separate class and define an action event for it.
Creating a test class, I managed to define an action event for a button inside a form, but the same code does not work for a button inside a dialog, and I can't get my head around why it is not working for the dialog.
Below is what I already have. Any suggestions would be appreciated.
public Class One {
Test test = new Test();
test.testOne(); // this is working : button prints
test.testTwo(); // this is not working : button does not print
buttonTest = test.getTestButton();
buttonTest.setText("Hello World"); // not working for a button in a dialog
buttonTest.addActionListener(l-> { // prints when the button in a Form
System.out.println("try"); // does not print when the button is in a dialog
});
}
public class Test {
Dialog dialog = new Dialog();
Form form = new Form();
Button button;
public void testOne() {
button = new Button("Test");
form.add(button);
form.show();
}
public void testTwo() {
button = new Button("Testing");
dialog.add(button);
dialog.show();
}
public Button getTestButton () {
return button;
}
}
You add the action listener after showing the form and dialog. This isn't a problem for the form since the forms show method will continue. But a dialogs show() method will block.
Two solutions:
Move the listener binding higher in the code (before the show) that would be a problem since the button doesn't exist yet so you will need some refactoring.
Change the show() call on the dialog to showModless()

Remove button bar from jface dialog

I would like to create a dialog without the OK/Cancel buttons. I know that if you override the createButton method, this can be achieved.
What do you think of overriding the createButtonBar method to return null if the button bar is not required at all? This would save some code.
Overriding createButtonBar is going to produce errors if you return null for the result composite as the Dialog code expects it to not be null.
You can override createButtonsForButtonBar and not create any buttons. It looks like Dialog always checks that individual buttons exist.
You can remove the space used by the buttons composite like this:
#Override
protected void createButtonsForButtonBar(final Composite parent)
{
GridLayout layout = (GridLayout)parent.getLayout();
layout.marginHeight = 0;
}
If you want to have the only one "Close" button on your dialog, you can do it so:
#Override
public void create() {
super.create();
getButton(IDialogConstants.OK_ID).setVisible(false);
getButton(IDialogConstants.CANCEL_ID).setText("Close");
}

Disable Dialog closing after ENTER button pressed in ControlsFX

I have extended org.controlsfx.dialog.Dialog and added some TextField to it, which is supposed to act when ENTER button is pressed (if TextField has focus). However when I press ENTER, my dialog takes over of steering, and act like on OK button was pressed.
Is there any method which I can override in order to change this behavior (to intercept Enter action)?
Thanks in advance
I have received some workaround to this problem at ControlsFX mailing list which is good for me. To disable Dialog closing after Enter button is pressed in TextField we must consume event in EventHandler attached to this TextField
textField.addEventFilter(new EventHandler() {
public void handle(KeyEvent evt) {
.......
evt.consume();
}
});
For me works this:
dialog.getDialogPane().addEventHandler(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.ENTER) {
event.consume();
}
});

Get KeyCode from Dialog in lwuit

I want to invoke the keyListener and get the keyCode while the dialog is being shown. I have tried extending Dialog and overrided the keyReleased() with no success. Below is my code, what went wrong?
public class MyDialog extends Dialog{
public void keyReleased(int keyCode) {
super.keyReleased(keyCode); //To change body of generated methods, choose Tools | Templates.
System.out.println("Keycode in Dialog: "+keyCode);
}
}
And in my form, I am using the custom Dialog like below:-
MyDialog dialog = new MyDialog();
dialog.show("INFO", "TEST CONTENT", "OK", "CANCEL");
You aren't using your dialog.
show(String, String, String, String) is a static method not an instance method so a new dialog instance is created and shown.
You need to use show() which is an instance method (or some other instance method like showDialog), but then you will have to actually add the components and "construct" your dialog.

Resources