Can SWTBot recognize Action Buttons? - java

I have an RCP Application with a ViewPart that has a toolbar with some Actions on it. These Actions are put on the toolbar by the system as simple Buttons with an icon and a tooltip.
The Action looks like this:
public class MyAction extends Action {
public static final String TITLE = "My Action Tooltip";
public MyAction() {
super(TITLE, Activator.getImageDescriptor("icons/clock_edit.png"));
setToolTipText(TITLE);
}
// ...
}
Now I am trying to invoke a button click on them with SWTBot, like this:
SWTBotButton myButton = bot.buttonWithTooltip(MyAction.TITLE);
myButton.click();
And if I let the SWTBot test run, I get the error message that it couldn't find the Button:
org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: Could not find widget matching: (of type 'Button' and with tooltip 'My Action Tooltip' and with style 'SWT.PUSH')
at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntilWidgetAppears(SWTBotFactory.java:362)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.widget(SWTBotFactory.java:309)
at org.eclipse.swtbot.swt.finder.SWTBot.buttonWithTooltip(SWTBot.java:205)
at org.eclipse.swtbot.swt.finder.SWTBot.buttonWithTooltip(SWTBot.java:193)
Now I'm wondering, is an Action not put onto the Toolbar as an SWT.PUSH Button?
Or what could be the reason that it can't find it?

The Buttons on the Toolbar can be found by SWTBot a bit differently. I was finally able to do that like this:
List<SWTBotToolbarButton> items = view.getToolbarButtons();
for (SWTBotToolbarButton button : items) {
if (MyAction.TITLE.equals(button.getToolTipText())) {
button.click();
break;
}
}

Try bot.toolbarButtonWithTooltip(MyAction.TITLE).click();
Also, you can use the EclipseSpy View to determine the type of the widget that you want to work on(Window-Show View-SWTBot Category)

Related

Get a reference to the view for a PopUp Menu

I have a PopupMenu that is created dynamically. I need to pass the underlying view for this menu to another method, but I cannot find any way to get a reference to the view itself, just the menu interface and its items.
Ideally, I would do something like this:
val popMenu = PopupMenu(mainActivity, view)
popMenu.inflate(menu.route_mode)
popMenu.setOnMenuItemClickListener(this#Class)
menu.show()
viewFunction(popMenu.view)
This is not happening in the activity. This is not an Options or Context menu and is not shown in the action bar.
Unfortunately there seems no such method, i.e., you can't find the anchor view from the PopupMenu itself.
I usually create a member in the Activity to remember which View was clicked. Assume it is a Button btnPopupMenu clicked to show the popup menu, something like this:
public class MyActivity : Activity {
...
private var _clickedView:View? = null
...
override onCreate() {
...
btnPopupMenu.setOnClickedListener { v ->
_clickedView = v
val popupMenu = PopupMenu(this, v)
...
}
}
...
override onOptionItemSelected(menuItem:MenuItem) {
// use _clickedView here
}

Java - Android - How to programmatically hide or show controls using ID

I don't have my source code right now, but I was wondering if it is possible to use visibility(GONE) with the ID's or something like that?
The reason : I have a form where I want to have 20 dropdowns, and a button "add a new activity" . When you click on the button, it unhide a new control. The problem is in how to tell the app what dropdown to unhide...
Example :
dropdown1 (visible) [Button add new]
dropdown2 (invisible)
dropdown3 (invisible)
[...]
user click on [Button Add new]
dropdown1 (visible)
dropdown2 (visible)
dropdown3 (invisible)
[...]
Or something similar in process.
Thanks! (sorry not english speaking person... I hope this is understandable as a question!)
In xml give id to all spinner (Dropdown) like drop1,drop2,...........
like this
<Spinner
android:id="#+id/drop1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
In Java file add
Spinner drop1,drop2,............drop20;
Under onCreate Method
drop1 = (Spinner)findViewById(R.id.drop1);
drop2 = (Spinner)findViewById(R.id.drop2);
--------------------------------------
drop20 = (Spinner)findViewById(R.id.drop20);
Set Visibility
if(condition) // your condition to hide dropdown
{
drop1.setVisibility(Visible.GONE);
}
else
{
drop1.setVisibility(Visible.VISIBLE);
}
After mapping the control you can use setVisibility() method with it
eg:
Button btn=(Button)findViewById(R.id.button1);
btn.setVisibility(View.Visible);`
Yes, you can do it. Using android:id tag in XML layout.
In your Java code you can do it like this:
Button button1 = (Button) findViewById('button1');
Button button2 = (Button) findViewById('button2');
buton1.setOnClickListener( new View.OnClickListener( ) {
#Override
public void onClick( View v ) {
button2.setVisibility( View.VISIBLE );
}
} );

How to add toolbar to java text hover eclipse

I am tring to create my own text hover plugin for eclipse.
I success to write my own code in my hover, but I try to add a toolbar to the hover (inside the new tooltip opened).
I read that I need to use the getHoverControlCreator function, and I managed to add the toolbar manager that I see when the text hover is opened while running the plugin,in the debbuger I can see that the ToolBarManger has the ToolBar that has the ToolItems, but I can't see them in the real text hover when I opened it.
this is my code:
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
public IInformationControl createInformationControl(Shell parent) {
ToolBar tb = new ToolBar(parent, SWT.HORIZONTAL);
ToolBarManager tbm = new ToolBarManager(tb);
DefaultInformationControl dic = new DefaultInformationControl(parent, tbm);
ToolItem ti = new ToolItem(tb, SWT.PUSH);
ti.setText("hello");
tb.update();
tb.redraw();
tbm.update(true);
parent.update();
parent.redraw();
parent.layout();
return dic;
}
This is what one of the Eclipse hover controls does:
#Override
public IInformationControl doCreateInformationControl(Shell parent) {
ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
DefaultInformationControl iControl = new DefaultInformationControl(parent, tbm);
IAction action = new MyAction();
tbm.add(action);
tbm.update(true);
return iControl;
}
So it does not create the ToolBar - leave that up to DefaultInformationControl. It uses an Action in the tool bar and adds it after creating the DefaultInformationControl. It just calls update(true) at the end.
(This is a modified version of parts of org.eclipse.jdt.internal.ui.text.java.hover.NLSStringHover)
MyAction would be something like:
private class MyAction extends Action
{
MyAction()
{
super("Title", .. image descriptor ..);
setToolTipText("Tooltip");
}
#Override
public void run()
{
// TODO your code for the action
}
}

Show delete button inside row of a ListView when clicking a button Android

I need to implement a kind of Slide to Delete inside my ListView but what I want is not to slide the row but when the user clicks a button inside the row the delete button should show up. Is it possible? I've already implemented the Button and I know when the user press it but I pretty much have no idea on how to implement the delete button slide in effect from the right of the row.
Can someone give me an Idea on how to start?
If you don't want Swipe-to-delete, then the implementation is pretty simple.
Basically you are gonna show/hide that button on button click.
Since Android ListView reuses the Views, if you show/hide the button of a View of ListItem in onItemClick(), then when the list is scrolled the state will be lost and will result in incorrect behavior.
So we need to maintain this selection state in the Adapter's modal class. Just add a field like selected in the modal class. For instance, if you are showing list of Contact objects, then your modal class will be all like,
class Contact {
private String name;
private String number;
private boolean selected;
..........
}
from what I've understand; you need to implement lets say an edit button, then when user clicks on it, it will disappear and another button called delete will popup instead of it on the same position.
here is a possible approach to achieve that:
in your list_view_adapter.xml:
1-create a frame layout contain both of the buttons on top of each other.
2-default state of delete button is GONE -> android:visibility="gone"
3-when edit is pressed delete button will be visible and you will programmatically set Edit visibility to GONE
4-set your delete button:
#Override
public View getView(final int pos, View convertView, ViewGroup parent) {
View v = convertView;
// Some other things...
Button delete = (Button) v.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// After you delete the object from Parse database here,
notifyDataSetChanged();
}
}

SWT/JFace: Disable focus of toolbar item in scrolled form

I have an instance of a ScrolledForm in a form editor page and when I add a toolbar item to this form it always gets the focus (a blue background):
...
import org.eclipse.ui.forms.editor.FormPage;
...
class ModelPage extends FormPage {
...
#Override
protected void createFormContent(IManagedForm mform) {
Action action = …
ScrolledForm form = mform.getForm();
IToolBarManager toolbar = form.getToolBarManager();
toolbar.add(action);
toolbar.update(true);
...
}
#Override
public void setFocus() {
// this does not work
someText.setFocus();
}
}
Is there an easy method for removing the focus from this item? I could do this via setting the focus to other controls in listeners but this is not very nice (the item seems to get the focus always when the editor page is activated). I cannot set the style flag SWT.NO_FOCUS as the toolbar is already created by the form, right?
Thanks for help

Categories

Resources