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

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

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
}

Accessing to navbar in vaadin 14

I'm using vaadin 14 for my application.
My MainView class extends Applayout class. This allows me to use addToNavBar(true, some Components) function which adds navigation bar to your application.
Now, In my main view, inside the navigation bar, I have register and login buttons populated. If click on these buttons, using addonclick listener I delegate to other views like Login and Registration. During these view changes, the navbar on top still stays there. However, if the user logged in or registered, I want to remove these login and register buttons in navigation bar and replace them with profile picture icon located inside the navbar. However, from child views(register,login) I couldn't find a way to access to navbar with vaadin 14. Accordingly, how can I access and change the content of the navbar from child views?
public class MainView extends AppLayout {
private static final long serialVersionUID = 1L;
private final Tabs menu;
private HorizontalLayout headerLayout;
public MainView() {
setPrimarySection(Section.NAVBAR);
headerLayout = createHeaderContent();
addToNavbar(true, headerLayout);
setDrawerOpened(false);
menu = createMenu();
addToDrawer(createDrawerContent(menu));
}
private HorizontalLayout createHeaderContent() {
headerLayout = new HorizontalLayout();
headerLayout.setId("header");
headerLayout.getThemeList().set("dark", true);
headerLayout.setWidthFull();
headerLayout.setSpacing(false);
headerLayout.setAlignItems(FlexComponent.Alignment.CENTER);
headerLayout.add(new DrawerToggle());
headerLayout.add(createWebsiteName());
headerLayout.add(createMiddleSpacingInHeader());
headerLayout.add(createLoginAndRegisterButtons());
return headerLayout;
}
private Component createLoginAndRegisterButtons() {
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.setSpacing(true);
layout.setAlignItems(Alignment.STRETCH);
Button register = createRegisterButton();
Button login = createLoginButton();
Image loggedInUserPicture = createLoggedInUserImage();
layout.add(register, login, loggedInUserPicture);
return layout;
}
There is currently no good API for this. One reason is that framework needs to be agnostic to how you build your menu. I have solved this by creating small interface of this kind
public interface HasTabsAccessor {
public default Tabs getTabs(Component component) {
Optional<Component> parent = component.getParent();
Tabs menu = null;
while (parent.isPresent()) {
Component p = parent.get();
if (p instanceof MainLayout) {
MainLayout main = (MainLayout) p;
menu = main.getMenu();
}
parent = p.getParent();
}
return menu;
}
}
Which I can then add to views where I need to access the menu.
#Route(value = FormLayoutView.ROUTE, layout = MainLayout.class)
#PageTitle(FormLayoutView.TITLE)
public class FormLayoutView extends VerticalLayout implements BeforeLeaveObserver, HasTabsAccessor {
...
And then just using getTabs() in the view.

Toolbar dissapears from custom hover in eclipse editor

I am working on an editor plugin for Eclipse that handles my own script language. In the editor, I have a hover that shows short information about element under the mouse cursor.
Now, I am trying to create a toolbar on the bottom of the hover and place a button there that will open a more detailed description online.
I have written my code based on answer to that question. The button is visible and it works when it is clicked.
However, it disappears a short time after I move my mouse over the hover. Why is this happening and how can I prevent that?
Here is the relevant part of my code:
#Override
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
#Override
public IInformationControl createInformationControl(final Shell parent) {
ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
DefaultInformationControl defaultInformationControl = new DefaultInformationControl(parent, tbm);
Action action = new Action() {
#Override
public void run() {
MessageDialog.openInformation(parent, "omg", "It works.");
}
};
action.setText("123 test 321");
Bundle bundle = FrameworkUtil.getBundle(this.getClass());
URL url = FileLocator.find(bundle, new Path("icons/test.gif"), null);
action.setImageDescriptor(ImageDescriptor.createFromURL(url));
tbm.add(action);
tbm.update(true);
return defaultInformationControl;
}
};
}
When hover is created with DefaultInformationControl(parent, tbm) then toolbar is visible. However when you move mouse over the hover, then it gains focus. Then method getInformationPresenterControlCreator() from DefaultInformationControl is called.
It looks like (from source code):
public IInformationControlCreator getInformationPresenterControlCreator() {
return new IInformationControlCreator() {
/*
* #see org.eclipse.jface.text.IInformationControlCreator#createInformationControl(org.eclipse.swt.widgets.Shell)
*/
public IInformationControl createInformationControl(Shell parent) {
return new DefaultInformationControl(parent,
(ToolBarManager) null, fPresenter);
}
};
}
Look at return line. It nulls your Toolbar manager. That is the reason is gone.
Quick solution might be to create a new class which extends DefaultInformationControl and then in overrides
#Override
public IInformationControlCreator getInformationPresenterControlCreator() {
return new YourOwnInformationControlCreator();
}
This way you can pass correct ToolbarManager.

Can SWTBot recognize Action Buttons?

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)

How can I make a drop down hidden menu

I want to make a drop down menu, like a status menu, that is hidden when the activity starts, and when it's pressed or slid it opens like the image below..
http://i.stack.imgur.com/jeq5z.png
My layout currently has a RelativeLayout for the top bar and a ScrollView for the text.. between those, i'd like to put the menu..
I'm not doing this app on phonegap or anything like that, just java and xml..
Thanks in advance
Edit:
Thank you all for your help! I end up doing a FrameLayout that was set off the screen with the translationY and then, when clicked, just slide up and down.. Here's the snipped.. I'll just leave it here in case someone else needs it.
on layout.xml
<FrameLayout
android:id="#+id/layout_FrameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffffff" >
<!-- stuf -->
</FrameLayout>
on activity.java
private FrameLayout statusDrawer = null;
private int statusDrawerHeight; // height of the FrameLayout (generated automatically)
private int statusDrawerDragButtonHeight = 30 + 5; //height of the DragButton + height of the border
private boolean statusDrawerOpened = false;
private int statusDrawerDuration = 750; //time in milliseconds
private TimeInterpolator interpolator = null; //type of animation see#developer.android.com/reference/android/animation/TimeInterpolator.html
#Override
protected void onCreated(Bundle savedInstanceState){
statusDrawer = (FrameLayout) findViewById(R.id.layout_FrameLayout);
interpolator = new AccelerateDecelerateInterpolator();
statusDrawer.post(new Runnable() {
#Override
public void run() {
statusDrawerHeight = statusDrawer.getHeight();
statusDrawer.setTranslationY(-statusDrawerHeight+statusDrawerDragButtonHeight);
}
});
statusDrawer.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if(statusDrawerOpened) {
statusDrawer.animate()
.translationY(-statusDrawerHeight+statusDrawerDragButtonHeight)
.setDuration(statusDrawerDuration)
.setInterpolator(interpolator)
.start();
} else {
statusDrawer.animate()
.translationY(0)
.setDuration(statusDrawerDuration)
.setInterpolator(interpolator)
.start();
}
statusDrawerOpened = !statusDrawerOpened;
}
});
}
Use a FrameLayout as the root layout. Add the drop menu layout as in the right side of your picture. Call
menuView.setTranslationY(-view.getHeight);
on this view to initially hide the drop down menu when the activity is started. Make sure menuView only refers to the drop down view part without the small tab button. When the user touches the tab animate translationY to 0 so that the layout will slide down
ObjectAnimator.ofFloat(dropDownView, "translationY", -view.getHeight, 0).setDuration(200).start();
whereby dropDownView refers to the complete drop down menu.
Using ObjectAnimator requires API level 11. If you need to support older API levels, use http://developer.android.com/reference/android/view/animation/TranslateAnimation.html (which has some down sides).
If you instead want add a sliding effect, e.g. the sliding menu is moving with together with the finger, install a OnTouchListener:
dropDownTab.setOnTouchListener(new OnTouchListener() {
public void onTouch(View v, MotionEvent e) {
// Make the drop down menu finger follow the finger position.
// Use again dropDownView.setTranslationY(...) to move the view.
// If the drop down menu has been dragged a certain distance, make it move out by itself using the animation as above.
}
});

Categories

Resources