Best OO way to handle "cancel button" - oop

I always wondered what's the best way of handling a cancel button in a more OO way. In the hurry I always end up putting the ugly checking of a boolean form property if the button was canceled of not.
The thing is that way makes the code dirty, having a lot of "cancel checks" between logic that matters.
I always get to something like this:
void doLogic()
{
checkIfIsCancelled();
callOtherFunction();
checkIfIsCancelled();
callAnotherFunction();
checkIfIsCancelled();
callAnotherFunction();
checkIfIsCancelled();
callAnotherFunction();
}
I hope I was clear enough. I just want a neater way to do this :)

A proper way to handle this is the strategy pattern, where you have a default strategy where you do the normal processing and you have a Cancelled strategy.
Canceling changes the strategy to the cancelledStrategy that does nothing but some cleanup. The next call will go to the cancelledStrategy.
In this way even the cleanup is pretty straight forward because you know exactly where in the flow it was cancelled.
Another possible solution (but very dependent on your situation) would be the state pattern, but if you only need it for canceling it creates a lot of overhead.

it would REALLY help to know what GUI kit you're using here. Just from this it's impossible to know if you're taking about a windows, linux or mac machine. Add to that I can't think of a single GUI that that would function in this manner.
Most GUI's operate with a 'callback' pattern Widgets(buttons, menus, listboxes etc) are created and your code attaches a 'callback', a piece code or object&method that is executed when an action is performed on a widget.
In java for example:
Button b = JButton("Push") ;
listener = new ActionListener()_ {
public void actionPerformed(ActionEvent e) {
System.out.println("I was pushed!") ;
}
} ;
b.addActionListener(listener)
Arranges for the message "I was pushed!" to be printed when the button is pressed. Of course this thin examples omits all of the work you need to do to setup your window, populate this widget etc.

1st what comes to mind is http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern but I'm not sure, it's good here.

You can use the command pattern alongwith a stack to implement multi level undo support.

Related

Disable control callbacks when setting values in code?

I've noticed that when a control such as an AutoSuggestBox has a callback, the callback is executed both when a user interacts with the control and when my code changes the associated value.
For example, if I set the TextChanged property on an AutoSuggestBox, the function is called even when my code sets the Text property to an initial value.
This is causing problems in my application in the form of both bugs and unnecessary function calls. You may be wondering how the code came to be in this state -- the answer is, I don't know. The project was handed off to me from another developer and I've been tasked to fix a number of bugs.
Although I can individually hunt down all the places in the code where this happens and temporarily remove the callback, I'm wondering if there is an easier way, for example a property I can set on the control that says, "don't call the callbacks when it is the code making a change rather than the UI".
For the AutoSuggestBox.TextChanged event, the attribute of the trigger reason is provided in AutoSuggestBoxTextChangedEventArgs, and you can judge based on this
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.ProgrammaticChange)
{
return;
}
//other code
}
For other controls, you need to deal with them according to your situation.
Best regards.

what is difference between Alert al=new Alert(driver); al.accept() and driver.switchTo().alert().accept();

Alert al=new Alert(driver);
al.accept();
driver.switchTo().alert().accept();
There isn't much difference between the two. However one is much cleaner than the other.
Take the following code into consideration:
driver.SwitchTo().Alert().Accept();
This code will tell the driver to switch to the active alert present within the driver, then accept it. All in 1 easy line of code.
Now take this code into consideration:
Alert al = new Alert(driver);
al.Accept();
Here we have to create the Alert object, pass in the driver as a parameter to the constructor, and then accept the alert.
The first code segment is preferred because the way the developers have set up the driver methods is so we can chain commands together. This allows us to use a method, then pass the return immediately into another method without having to tell our code to create an object for it. The object is still created, but we (as developers) do not have to know about its existence. Therefore making our code easier to read and maintain.

Identifying objects in IBM RFT

While executing my script in RFT, my script got failed due to the slight position change of a button. (This button's position slightly changes according to the option selected for previous combo box due to the label appearing near the button)
As there are 2 positions for this button in window, one of my script fails while other passes.
Please suggest how to identify this same object in 2 different places in RFT?
If you're alright with not using pre-mapped values and instead work with objects directly in code (which I've personally found to be extremely useful... it's allowed me to do great and wondrous things with RFT :), the following ought to work fine:
private void clickObject(String uniqueIdentifier) {
// Find object
RootTestObject root = RootTestObject.getRootTestObject();
TestObject[] matchingObjs = root.find(atProperty(".id", uniqueIdentifier));
if (matchingObjs.length > 0) {
// Click the object
((GuiTestObject) matchingObjs[0]).click();
}
// Clean-up
unregister(matchingObjs);
}
Feel free to replace ".id" with whatever property is best suited for the situation... since I work primarily with a web application, the ".id" property has worked splendidly for me.
Because the method finds the object anew each time, it'll grab the object's position wherever it's at at the time the method's called. The clean-up will also prevent any weird, horrible, and otherwise unfortunate UnregisteredObjectExceptions from cropping up.
Without looking at your pages I cannot be sure, but I think the buttons are actually two different buttons. Maybe they are generated by javascript, or they are just un-hidden after the option you select in the combobox.
If they are two different buttons (record them both and look at the recognition properties) you can either replace some properties with a regular expression or check wich button is visible/exists and then click it:
if (btn_button1.exists()) {
btn_button1.click();
} else if (btn_button2.exists()) {
btn_button1.click();
}
Here's a more complete tutorial on Object Recognition.
You can increase the tolerance of Rational Performance Tester AssureScript in the properties tab or you could set the description but hide the value. You can also make a custom code that updates the object map to prepare for this change in a java IF structure

Source code for WinRT UI controls publicly available?

Is the source for Windows Store (WinRT) UI controls publicly available? We would like to extend some of the controls and not have to start completely from scratch, like we can for SL and WPF. Googling and looking through SO doesn't turn up anything for Windows 8.
Thanks!
So unlike WPF, [WinRT-XAML] controls are written in C++/CX.
But, it sounds not so much like you want the source code as much as you want to derive from existing controls and extend or override their functionality. You know you can do this, right? It's easy enough and sounds like you will get the results you are asking in your question.
Something like this:
public class MonkeyTextBox : TextBox
{
public new string Text
{
get
{
return "Always Monkeys!";
}
set { /* do nothing */ }
}
}
This is my custom TextBox wherein I have replaced the base implementation of Text with my own. Granted, I hope your custom controls are better. Anyway, you can do this with almost every control, and you can add your own properties and events. Make sense?
Reference: http://blog.jerrynixon.com/2013/01/walkthrough-custom-control-in-xaml-isnt.html
But to answer your question: no, we have not released the source (yet). Hopefully, that will save you the time looking for it. Maybe someday we will - maybe.
Best of luck!

How to emulate mouse click using wxWidgets

Is there any function in wxWidgets framework, say, click(), whose function is to emulate a left-click mouse?
No, there is no function like this. If you really need to do it, e.g. because you want to perform some action in another application you need to write platform-specific code yourself (e.g. use SendInput() under Windows).
If you want to use this to execute some code in your own application though, there is a much better solution: instead of doing unsupported, fragile and ugly
void MyClass::OnLeftUp(wxMouseEvent& event)
{
... do something with click at event.GetPosition() ...
}
void MyOtherFunction()
{
wxMouseEvent event(...);
... initialization not shown because there is no right way to do it anyhow ...
myObject->ProcessEvent(event);
}
just refactor your code to do it like this instead:
void MyClass::OnLeftUp(wxMouseEvent& event)
{
HandleClick(event.GetPosition());
}
void MyClass::HandleClick(const wxPoint& pos)
{
... do something with click at pos ...
}
void MyOtherFunction()
{
myObject->HandleClick(position);
}
If you're just talking about emulating a click on another button in your wxWidgets app, I've tried this before:
Create a fake wxEvent of the right type, tell the event what object to care
about using SetEventObject(), and tell a parent wxWindow in the hierarchy to
ProcessEvent(myNewEvent). You might want to use wxGetTopLevelParent() to get the topmost frame/dialog
If you are talking about emulating a click in another, non-wxWidgets process, it's possible you could use the OS's accessibility APIs to do this. wxAccessibility should be able to help with this on Windows -- for other OSes, last I heard (granted, a few years ago), you'll have to use the native OS functions.
You can use the wxControl::Command() method. According to Documentation:
Simulates the effect of the user issuing a command to the item.
Documentation page
List of events to use with wxCommandEvent constructor
You can simulate a click on a button like this:
$this->MyButton->Command(new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED));
There is also the wxUIActionSimulator class that you can use to simulate UI actions.