How to disable/enable view toolbar menu/action in Eclipse Plugin Development - eclipse

I have view that extends ViewPart. In this view, I want to add toolbar menu.
What I know, we can add toolbar menu by using ActionContributionItem or Action, and add it to ToolBarMenu from createPartControl method in ViewPart.
But what I don't know is this: How can we disable/enable the toolbar menu programmatically?
So basically, I want to add Play, Stop, and Pause button to toolbar view. So at first, the Play button is on enabled mode, and the others are disabled. When I pressed Play button, it is disabled, and others will be enabled.
For more details, what I want to achieve is something like the following image.
In the red circle are disabled button, and in the blue circle are enabled button.

Instead of using Actions, have a look at Eclipse commands (they are the replacement for actions and function in a cleaner way): http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/guide/workbench_cmd.htm
You will see in the documentation that you can enable and disable a command and all places where it's used will properly update their state automatically.

There is another approach which I found by stumbling upon on google. This approach is using ISourceProvider to provide variable state. So we can provide the state of enablement/disablement of command in that class (that implementing ISourceProvider). Here is the detail link http://eclipse-tips.com/tutorials/1-actions-vs-commands?showall=1

Try this..
1: Implement your actions. ex: PlayAction, StopAction.
Public class StartAction extends Action {
#Override
public void run() {
//actual code run here
}
#Override
public boolean isEnabled() {
//This is the initial value, Check for your respective criteria and return the appropriate value.
return false;
}
#Override
public String getText() {
return "Play";
}
}
2: Register your view part(Player view part)
Public class Playerview extends ViewPart
{
#Override
public void createPartControl(Composite parent) {
//your player UI code here.
//Listener registration. This is very important for enabling and disabling the tool bar level buttons
addListenerObject(this);
//Attach selection changed listener to the object where you want to perform the action based on the selection type. ex; viewer
viewer.addselectionchanged(new SelectionChangedListener())
}
}
//selection changed
private class SelectionChangedListener implements ISelectionChangedListener {
#Override
public void selectionChanged(SelectionChangedEvent event) {
ISelection selection = Viewer.getSelection();
if (selection != null && selection instanceof StructuredSelection) {
Object firstElement = ((StructuredSelection)selection).getFirstElement();
//here you can handle the enable or disable based on your selection. that could be your viewer selection or toolbar.
if (playaction.isEnabled()) { //once clicked on play, stop should be enabled.
stopaction.setEnabled(true); //Do required actions here.
playaction.setEnabled (false); //do
}
}
}
}
Hope this would help you.

Related

Codename One - Popup like Form Transition

as per the material guidelines on transitions, I want to establish a certain look and feel on app screens to convey a hierarchy for these. Meaning, everything that transitions left to right is on same level or importance. Smaller forms or brief user inputs will transition in and out as simple popups, not horizontally but vertically.
Expected behaviour:
The slide form uses the default transition. Show() will slide the source to the left and the destination slides in from the right. showback() will slide the source in from the left and the destination leaves to the right.
The popup form uses a custom transition: show() will cause the source to remaining in place (not slide or transition in any other way) and the destination (the popup) will slide in from below. Showback() will cause the source (the popup) to slide out towards the bottom, revealing the destination (the main window) underneath.
Actual Behaviour
slide form works as expected in my scenario.
show() causes the popup form to slide into the screen from the bottom, while the source form stays in place, being covered up (as expected). BUT the showback() causes the main window to slide in from the top, covering the popup screen.
Full code sample to show actual behavior
public class MyApplication {
private Form current;
private Resources theme;
private Transition defaultInTrans = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 300);
private Transition defaultOutTrans = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 300);
private Transition popupInTrans = CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, false, 300);
private Transition popupOutTrans = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300);
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
Toolbar.setGlobalToolbar(true);
}
public void start() {
if (current != null) {
current.show();
return;
}
new MainForm().show();
}
public void stop() {
current = getCurrentForm();
if (current instanceof Dialog) {
((Dialog) current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
class MainForm extends Form {
public MainForm() {
setLayout(BoxLayout.y());
Button slideBut = new Button("Slide Form");
Button popBut = new Button("Popup Form");
add(slideBut).add(popBut);
slideBut.addActionListener(e -> {
new SlideForm().show();
});
popBut.addActionListener(e -> {
new PopupForm(this).show();
});
}
}
class SlideForm extends Form {
public SlideForm() {
Style bg = getContentPane().getUnselectedStyle();
bg.setBgTransparency(255);
bg.setBgColor(0x00ff00);
getToolbar().setBackCommand("", e -> {
new MainForm().showBack();
});
add(new Label("Slide Form content"));
}
}
class PopupForm extends Form {
public PopupForm(Form orig) {
Style bg = getContentPane().getUnselectedStyle();
bg.setBgTransparency(255);
bg.setBgColor(0xff0000);
getToolbar().setBackCommand("", e -> {
new MainForm().showBack();
orig.setTransitionInAnimator(defaultInTrans);
orig.setTransitionOutAnimator(defaultOutTrans);
});
add(new Label("This is a popup!"));
// remove source animation to remain in place
orig.setTransitionInAnimator(null);
orig.setTransitionOutAnimator(null);
// add transition for target popup to appear and vanish from/to the bottom
setTransitionInAnimator(popupInTrans);
setTransitionOutAnimator(popupOutTrans);
}
}
}
Having different CommonTransition types, the in transition vs the out transition, the transition direction parameterand on top of that the direction of show() vs showback() is quite confusing.
how can I achieve the expected behaviour for the popup form to slide OUT correctly?
is there a better way or less code required to achieve this?
Thank you.
Cover has an in/out effect where slide only has an out effect. When you slide from form A to form B there is one motion including both forms that works exactly the same in reverse. However, with cover it slides on top of form A while the latter stays in place then slides off of it making it look like form A has been under it all along.
That means both transition in and out are used to convey both cover modes. However this can collide with the transition out of form A so we need to temporarily disable it.
E.g.:
removeTransitionsTemporarily(backTo);
f.setTransitionInAnimator(CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, false, 300));
f.setTransitionOutAnimator(CommonTransitions.createUncover(CommonTransitions.SLIDE_VERTICAL, true, 300));
public static void removeTransitionsTemporarily(final Form f) {
final Transition originalOut = f.getTransitionOutAnimator();
final Transition originalIn = f.getTransitionInAnimator();
f.setTransitionOutAnimator(CommonTransitions.createEmpty());
f.setTransitionInAnimator(CommonTransitions.createEmpty());
f.addShowListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
f.setTransitionOutAnimator(originalOut);
f.setTransitionInAnimator(originalIn);
f.removeShowListener(this);
}
});
}

Android spinner adapter setDropDownViewResource custom layout with radiobutton

I use Spinner in Dialog Mode.
I set SimpleCursorAdapter for the Spinner with setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
That works fine.
Now instead of simple_spinner_dropdown_item I'm trying to pass my custom layout it does work well too.
But there is a but... it does not have radio button that original simple_spinner_dropdown_item does.
Is it possible to add radio button inside of my custom spinner_dropdown_item that would be selected when spinner dialog is shown?
yes its possible but you have to define a another class for spinner.Just look at this
you have one more option to get your requirement. that is Alert dialog
just check out this Alert Dialog Window with radio buttons in Android and How to create custom and drop down type dialog and Dialog in android
Well I have found solution. ListView (what is inside of the spinners dialog) will check if your View is Checkable and call setChecked. Since android.R.layout.simple_spinner_dropdown_item is checkable it works.
So for my custom List item i have created LinearLayout that implements Checkable
public class CheckableLinearLayout extends LinearLayout implements Checkable
{
private boolean _isChecked = false;
public CheckableLinearLayout(Context context)
{
super(context);
}
public CheckableLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
public void setChecked(boolean checked)
{
_isChecked = checked;
for (int i = 0; i < getChildCount(); i++)
{
View child = getChildAt(i);
if (child instanceof Checkable)
{
((Checkable) child).setChecked(_isChecked);
}
}
}
#Override
public boolean isChecked()
{
return _isChecked;
}
#Override
public void toggle()
{
_isChecked = !_isChecked;
}
}
So ListView calls setChecked and I propagate that down to children views and my CheckBox / RadioButton will get checked / unchecked correctly.

DialogBox in GWT isn't draggable or centred

I'm new to GWT programming. So far I have a DialogBox which is supposed to collect a login and a password, which can if required launch another DialogBox that allows someone to create a new account.
The first of these two DialogBoxes always appears at the top left of the browser screen, and can't be dragged, although part of the definition of a DialogBox is that it can be dragged. However, the second DialogBox can be dragged about the screen without any problem.
What I'd really like is for the first DialogBox to appear in the middle of the screen & be draggable, both of which I thought would happen automatically, but there's not.
So, what things can stop a DialogBox from being draggable? There is nothing on the RootPanel yet. Does that make a difference?
Code fragments available if they help, but perhaps this general outline is enough for some pointers.
Thanks
Neil
Use dialogBox.center() This will center your DialogBox in the middle of the screen. Normally a DialogBox is by default draggable.
Just tried it out and it doens't matter if your RootPanel is empty our not. When I just show the DialogBox on ModuleLoad it is draggable and it is centered. Probably the problem is situated somewhere else.
This is the example of google itself:
public class DialogBoxExample implements EntryPoint, ClickListener {
private static class MyDialog extends DialogBox {
public MyDialog() {
// Set the dialog box's caption.
setText("My First Dialog");
// DialogBox is a SimplePanel, so you have to set its widget property to
// whatever you want its contents to be.
Button ok = new Button("OK");
ok.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
MyDialog.this.hide();
}
});
setWidget(ok);
}
}
public void onModuleLoad() {
Button b = new Button("Click me");
b.addClickListener(this);
RootPanel.get().add(b);
}
public void onClick(Widget sender) {
// Instantiate the dialog box and show it.
new MyDialog().show();
}
}
Here more information about the DialogBox.
Without seeing any of your code it's hard to tell what's going wrong. The following code works for me (ignore the missing styling...):
public void onModuleLoad() {
FlowPanel login = new FlowPanel();
Button create = new Button("create");
login.add(new TextBox());
login.add(new TextBox());
login.add(create);
create.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final DialogBox box = new DialogBox();
FlowPanel panel = new FlowPanel();
Button close = new Button("close");
close.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
box.hide();
}
});
panel.add(new Label("some content"));
panel.add(close);
box.setWidget(panel);
box.center();
}
});
DialogBox firstBox = new DialogBox(false, true);
firstBox.setWidget(login);
firstBox.center();
}
Both boxes are draggable and shown in the center of your browser window.
Looks like you're overriding this method in Widget:
public void fireEvent(GwtEvent<?> event) {
if (handlerManager != null) {
handlerManager.fireEvent(event);
}
}
In Widget, handlerManager refers to a private HandlerManager.
Either add super.fireEvent(event) to your method or as you have done rename it.
Well, with vast amounts of trial and error I have found the problem, which was just this: I had a method in an object I'd based on DialogBox called fireEvent, which looked like this:
public void fireEvent(GwtEvent<?> event)
{
handlerManager.fireEvent(event);
}
Then, when a button was clicked on the DialogBox, an event would be created and sent off to the handlerManager to be fired properly.
And it turns out that if I change it to this (LoginEvent is a custom-built event):
public void fireEvent(LoginEvent event)
{
handlerManager.fireEvent(event);
}
... or to this ....
public void fireAnEvent(GwtEvent<?> event)
{
handlerManager.fireEvent(event);
}
the DialogBox is draggable. However, if the method begins with the line
public void fireEvent(GwtEvent<?> event)
then the result is a DialogBox which can't be dragged.
I'm a bit unsettled by this, because I can't fathom a reason why my choice of name of a method should affect the draggability of a DialogBox, or why using a base class (GwtEvent) instead of a custom class that extends it should affect the draggability. And I suspect there are dozens of similar pitfalls for a naive novice like me.
(Expecting the DialogBox to centre itself was simply my mistake.)

GWT - Implement programmatic tab selection of a TabLayoutPanel and then scroll to a particular element contained in the tab?

I have a TabLayout panel with 2 tabs. I would like to programmatically select the 2nd tab and then scroll to a particular element within the tab. This is how my code looks like:
public void scrollToTextArea(final String textArea)
{
TabPanel.selectTab(1); //tab selection
textArea.getElement().scrollIntoView(); //scroll to text area field
}
I tried using a deferred command to run the scroll portion, but was still unable to get the right display.
Is there a specific way to implement this functionality?
This worked:
public void scrollToTextArea(final String textArea)
{
TabPanel.selectTab(1); //tab selection
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand()
{
public void execute()
{
textArea.getElement().scrollIntoView();
}
});
}

GWT: context menu in RichTextArea

I'm using a RichTextArea in a GWT app. I want to add a context menu to my RichTextArea:
public class MyRichTextArea extends RichTextArea implements HasContextMenuHandlers {
public HandlerRegistration addContextMenuHandler(ContextMenuHandler h) {
return addDomHandler(h, ContextMenuEvent.getType());
}
}
(...)
myRichTextArea.addContextMenuHandler(new ContextMenuHandler() {
public void onContextMenu(ContextMenuEvent event) {
contextMenu.show();
}
});
This works, however, the context menu only appears when I right-click on the border of the RichTextArea. If I right-click into the RichTextArea, e.g. on the contained text, the browser's default context menu is shown.
How can I display my own context menu?
Prevent default context memu:
myRichTextArea.addDomHandler(new ContextMenuHandler() {
#Override public void onContextMenu(ContextMenuEvent event) {
event.preventDefault();
event.stopPropagation();
// do what you want to do instead
}
}, ContextMenuEvent.getType());
I would go after a method that tells you that the rich text area has the focus, like hasfocus, or maybe better, an event listener (addFocusListener) to tell you when the focus is there on a mouse click for the right mouse button?
Does that make sense?