The removeCommand is not working for the toolbar commands - codenameone - codenameone

The removeCommand and removeAllCommand is not working. If i put it in a button action listener removeCommand(command) works but removeAllcommand() is not working?
Is it a bug? Please check it out. Thankyou
Command d = new Command("back") {
#Override
public void actionPerformed(ActionEvent evt) {
}
};
f.setBackCommand(d);
f.getToolbar().addCommandToLeftBar(d);
//Either of these two doesnt remove above back command...
f.removeAllCommands();
f.removeCommand(d);
If i put it in action listener, one of them work
Button add = new Button("remove");
add.addActionListener((e) -> {
f.removeCommand(d);// it removes back command
f.removeAllCommands(); // it doesnt work
});

Try to revalidate the Form
Button add = new Button("remove");
add.addActionListener((e) -> {
f.removeAllCommands();
f.revalidate();
});

Related

Why ComboBox setValue do not work in Vaadin?

I have simple code:
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
When I first time click button i have items "11" and "22" present in combobox and value "22" is selected.
Second click makes value cleared but items "11" and "22" are still present.
In case I select "11" or leave "22" selected in combobox and click button - value clears.
It seems that setValue() only works when combobox is empty but following code do not helps as well:
combo.setValue(null);
combo.clear();
combo.setItems("11","22");
combo.setValue(null);
combo.clear();
combo.setValue("22");
Following code sets value of ComboBox correctly, no matter if I select some value or clear it before click:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems("11","22");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setValue("22");
}
});
But I have to set items of Combobox dynamically and the last solution do not suitable for me.
Vaadin version is 10.0.9.
Has anyone some suggestions or advices ?
PS.
Thanks !
I've tried following code:
combo.setItems(Collections.emptyList());
combo.setItems("11","22");
combo.setValue("22");
But it do not work as well.
This code works only if value in combo is empty, but if I input something in combo the code just clears value by .setItems() and further .setValue() do not work.
If value of combo is empty the code works well.
Your code works perfectly fine in a minimum project based on https://vaadin.com/start/latest/project-base (which uses Vaadin 12.0.7)
#Route("")
#PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {
public MainView() {
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
add(combo,button);
}
}
Whatever value you set in the ComboBox via UI .. when the button is clicked, the selected value will switch to 22.
If that's an option for you, you could update to a newer Vaadin version and try it with that.
To better show what I meant in my comment, I'm writing it as an answer.
What I meant was to set an empty collection not in the clickListener but directly after initializing the ComboBox:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems(Collections.emptyList());
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
please try it out and let me know if that works

How to focus on second tab and work on it using selenium webdriver

I have opened the link now i am clicking on login link on that page,
By clicking on login button its opening in a new tab automatically. I have to fill the form in that tab. For that i tried following code:
Actions act = new Actions(Initialsetupfirefox.driver);
new Actions(Initialsetupfirefox.driver) .keyDown(Keys.CONTROL).click(Initialsetupfirefox.driver.findElement(By.xpath("//a[contains(text(),'LOGIN')]")))
.keyUp(Keys.CONTROL)
.perform();
Thread.sleep(3000);
new Actions(Initialsetupfirefox.driver).sendKeys(Initialsetupfirefox.driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(Initialsetupfirefox.driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();
By this i am able to switch the focus but not able to do anything in this tab.
Please suggest
you can do like this:
Set<String> handles = driver.getWindowHandles();
String currentHandle = driver.getWindowHandle();
for (String handle : handles) {
if (!handle .equals(currentHandle))
{
driver.switchTo().window(handle);
}
}
//fill your form in the other tab
......
//go back to first tab if you want
driver.switchTo().window(currentHandle);
This it the code when using selenium with Node.js and Typescript
const mainHandle = await this.driver.getWindowHandle();
// load new tab here ..
const allHandles = await this.driver.getAllWindowHandles();
for (const handle of allHandles) {
if (handle !== mainHandle) {
await this.driver.switchTo().window(handle);
}
}
Get the browser-tabs and do as you like:
List<String> browserTabs = Lists.newArrayList(driver.getWindowHandles());
driver.switchTo().window(browserTabs.get(1)); // Switch to second tab
// Do something on second tab here...
driver.switchTo().window(browserTabs.get(0)); // Return to first tab
// Do something on first tab here...

Issue with focus element in GotFocus/Activated event

There is form with text element that should receive focus every time form shown.
In .NET CF Form has no OnShow(n) event
I try to use workaround:
MyForm_GotFocus() // or MyForm_Activated
{
txtTextControl.Text = string.Empty;
txtTextControl.Focus()
}
txtTextControl_GotFocus()
{
txtTextControl.SelectAll()
}
Code for getting form instance:
public static MyForm GetForm
{
get
{
if (s_inst == null || s_inst.IsDisposed)
{
s_inst = new MyForm();
}
return s_inst;
}
}
public static void ShowForm()
{
var frm = GetForm;
frm.Show();
}
1) First time ShowForm (Form instance has been created): txtTextControl emptied and got focus, txtTextControl_GotFocus event raised
2) Second time ShowForm : OK too
3) Third time ShowForm : txtTextControl emptied, but does not get focus
Is there bug or feature? Is there workaround? Show I rewrite ShowForm? Is OpenNETCF.IOC.UI is better solution (50 forms in project)?
I had that same question once.
Set the TabIndex for the control to 0.

Hyperlink.Click not being executed

I'm writing a Windows Phone Mango app. I have a hyperlink that contains email addresses. I want it to show a message box on tap:
Hyperlink href = new Hyperlink();
href.Click += (s, r) =>
{
MessageBox.Show("href clicked");
};
// add href to RichTextBox
When I click on the hyperlink in the emulator, nothing happens. The click += line is hit in a debugger, but the new EmailComposeTask() line isn't.
What am I doing wrong? Is Click not the event I want?
Update: I switched EmailComposeTask to MessageBox to verify that this issue isn't related to the email compose task not running on the emulator. I've reproduced this issue on my device.
The following code works:
GestureService.GetGestureListener(href); // adding this makes it work
href.Click += (s, r) =>
{
MessageBox.Show("href clicked");
new EmailComposeTask() { To = entireLink.Value }.Show();
};
href.Inlines.Add(new Run() { Text = entireLink.Value });
GetGestureListener creates a gesture listener if it doesn't already exist.
You should use HyperlinkButton Class, Hyperlink is a class used with rich text document rendering.
Following code is runs on device. text is RichTextBox object.
var linkText = new Run() { Text = "Link text" };
var link = new Hyperlink();
link.Inlines.Add(linkText);
link.Click += (o, e) =>
{
MessageBox.Show("Link clicked");
};
var paragraph = new Paragraph();
paragraph.Inlines.Add(link);
text.Blocks.Add(paragraph);

HowTo: WPF Webbrowser return current url / filter porn

I am using the WPF webbrowser and basically when I load an external url, i want to filter any pages that are loaded to make sure the url doesnt contain a swear or porn type word.
This is easy to do on page load, as I check the URL against my List badwords; I have also set up a Load Completed method which gets me the url of clicked words, however this isnt working properly :-
void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)this.webBrowser1.Document;
foreach (IHTMLElement link in doc.links)
{
HTMLAnchorElement anchor = link as HTMLAnchorElement;
if (anchor != null)
{
HTMLAnchorEvents_Event handler = anchor as HTMLAnchorEvents_Event;
if (handler != null)
{
handler.onclick += new HTMLAnchorEvents_onclickEventHandler(delegate()
{
uxURLText.Text = anchor.href;
//if (HelperClass.isNotFile(anchor.href))
// {
if (basepage.nonSafeWords.WordsContainSwearWord(anchor.href))
{
System.Windows.MessageBox.Show(basepage.INTERNET_RESTRICTION_NOTICE);
}
else
{
System.Windows.MessageBox.Show("Word Ok");
}
return true;
});
}
}
}
}
I need to basically stop any bad content from loading in the window, either from link, button, or ajax if a bad link is clicked a popup needs to notify us. I also need to show the current url in the address bar
Please help many thanks
:)
Listen to the WebBrowser.Navigating Event (docs) and check the Uri in NavigatingCancelEventArgs. Then set e.Cancel = true if you to not want to allow the navigation.
But your valid Uri check is the more complex problem.
private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
string currentURl= e.Uri.ToString();
_addrBox.Text = currentURl;
}
this was the solution in my case.
maybe itll help somebody

Resources