Suppress control-click context menu, gwt 1.6 - gwt

My gwt 1.6 application intercepts mouse clicks on hyperlinks, so when a user shift-clicks on links to "authors" they get an Edit... dialog box instead of navigating to the author's page. That's working nicely.
I'd now like to allow the user to control-click to select more than one author, but I can't figure out how to suppress the browser's default popup menu. This code handles shift-clicks correctly, but fails in the hosted browser when I control-click and half-fails in Firefox (handleCtrlClick() gets called, but I still get the browser menu):
public void onModuleLoad() {
Event.addNativePreviewHandler(this);
}
//
// Preview events-- look for shift-clicks on paper/author links, and pops up
// edit dialog boxes.
// And looks for control-click to do multiple selection.
//
public void onPreviewNativeEvent(Event.NativePreviewEvent pe) {
NativeEvent e = pe.getNativeEvent();
switch (Event.getTypeInt(e.getType())) {
case Event.ONCLICK:
if (e.getShiftKey()) { handleShiftClick(e); }
if (e.getCtrlKey()) { handleCtrlClick(e); }
break;
case Event.ONCONTEXTMENU:
if (e.getCtrlKey()) { // THIS IS NOT WORKING...
e.preventDefault();
e.stopPropagation();
}
break;
}
}
A breakpoint set inside the ONCONTEXTMENU case is never called.

IIRC ctrl + click is the correct way to select multiple items not ctrl + right click unless you're using a one button mouse (iMac), in that case I can't help you.
Could you provide more details?
Edit:
Why not override the contextmenu (e.g. disable it) then create your own context menu widget (perhaps based on vertical MenuBar + MenuItems) and display it only on Ctrl + RightClick?
In other words you'd create a MouseHandler somewhat like this (pseudo code):
public void onMouseDown(MouseDownEvent event) {
Widget sender = (Widget) event.getSource();
int button = event.getNativeButton();
if (button == NativeEvent.BUTTON_LEFT) {
if(event.is_ctrl_also)
{
// Add to selection
selection = selection + sender;
}
else
{
// Lose selection and start a new one
selection = sender;
}
}
else if(button == NativeEvent.BUTTON_RIGHT) {
if(event.is_ctrl_also)
{
// show context menu
this.contextmenu.show();
}
else
{
// do something else
}
}
return;
}
I've not encountered the bug with Ctrl-Leftclick firing a ContextMenu event, but I'm sure you could also make a workaround for Firefox only using permutations.

I'm getting closer:
public void onModuleLoad() {
Event.addNativePreviewHandler(this); // Catch shift- or control- clicks on links
addContextMenuEventListener(RootPanel.getBodyElement());
}
protected native void addContextMenuEventListener(Element elem) /-{
elem.oncontextmenu = function(e) {
return false; // TODO: only return false if control key down...
};
}-/;
That disables the right-click menu entirely; I'd really like to disable it ONLY if the control key is pressed...

Related

Swap the type of link depending on model object

I'm at complete loss how to proceed further:
I have panel with a DropDownChoice and a submit button next to it. Depending on the selected value of the DropDownChoice (Obtained upon the firing of a OnChangeAjaxBehavior attached to it, the submit button needs to either replace the whole panel with a different one, OR become an ExternalLink.
Currently, the code looks like that:
public class ReportSelectionPanel extends Panel {
protected OptionItem selectedOption ;
public ReportSelectionPanel(String id) {
super(id);
IModel<List<OptionItem>> choices = new AbstractReadOnlyModel() {
// Create a list of options to be displayed in the DropDownChoice
} ;
final IModel<OptionItem> optionModel =
new PropertyModel<OptionItem>(this,"selectedOption") ;
final DropDownChoice<OptionItem> options =
new DropDownChoice("selectChoice",optionModel,choices) ;
// I don't know what the button should be... Plain Button? A Link?
final Component button = ???
options.add( new OnChangeAjaxBehavior() {
protected void onUpdate(AjaxRequestTarget target) {
if ( selectedOption.getChild() == null ) {
// button becomes an ExternalLink.
// A new window will popup once button is clicked
} else {
// button becomes a Something, and upon clicking,
// this ReportSelectionPanel instance gets replaced by
// an new Panel instance, the type of which is
// selectedOption.getChild()
}
} ) ;
I'm really not quite sure what the commented lines should become to achieve the result. Any suggestions?
Thanks!
Eric
IMHO it's nicer to keep just one button and just react differently depending on the selected option:
final Component button = new AjaxButton("button") {
public void onClick(AjaxRequestTarget target) {
if (selectedOption.getChild() == null) {
PopupSettings popup = new PopupSettings();
popup.setTarget("'" + externalUrl + "'");
target.appendJavascript(popup.getPopupJavaScript());
} else {
ReportSelectionPanel.this.replaceWith(new ReportResultPanel("..."));
}
}
};
// not needed if options and button are inside a form
// options.add( new OnChangeAjaxBehavior() { } ) ;

JavaFx how to hide popup when mouse is clicked on owner window?

Hi i have javafx app which has only one stage.On tab key press event of text field, a popup showed on primary stage of application. like below
private void tripNoKeyPressEventAction(KeyEvent event){
if(event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) {
popup.show(GateIn.primaryStage);
}
}
popup.requestFocus();
popup.focusedProperty().addListener(new ChangeListener<Boolean>
() {
#Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
if(t1==false)
{
System.out.println("focus lost");
popup.hide();
}
}
});
I don't click on the popup and don't select anything in popup. I will just click on the stage behind it.I expect popup to be closed but It gives me IllegalArgumentException before executing popup's focusedProperty Listener.
If popup is on a different stage (other than primary stage of aaplication),based on stage focusedProperty() i can hide popup.
How to hide popup in case popup is shown on primary stage?
With FX 8, you can simply do
popup.setAutoHide(true)
You should set a event dispatcher for most top level window then all event will cross it.
In the popup window:
getScene().getWindow().setEventDispatcher((event, tail) -> {
if (event.getEventType() == RedirectedEvent.REDIRECTED) {
// RedirectedEvent is a box that contains original event from other target
RedirectedEvent ev = (RedirectedEvent) event;
if (ev.getOriginalEvent().getEventType() == MouseEvent.MOUSE_PRESSED) {
hide();
}
}else {
// if click in the popup window. handle the event by default
tail.dispatchEvent(event);
}
return null;
});
More information please see javafx.event.EventDispatcher

add View by using WindowManager, but can back key press

I've add a View, by using WindowManager.
It shows properly what I wanted to do,
but I have a problem.
this is the problem.
back key press doesn't affect under android component(like activity)
what I want is my added view can focusable, ( can click the view's inner button )
only when click the view,
and outside of the view can process their work.
( for example, if there is a button, can be clicked, and when back key press, top activity was gone )
but if I add a flag - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
then I can't receive onClick method on my added view's button.
but back button work correctly.
otherwise, if i remove the flag -I can receive onClick callback,
but now back button doesn't work.
I have a dilema. :(
Thank you.
Have your View override
public boolean dispatchKeyEvent(KeyEvent event)
to do something when back is pressed.
Override dispatchKeyEvent of your View
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
// handle back press
// if (event.getAction() == KeyEvent.ACTION_DOWN)
return true;
}
return super.dispatchKeyEvent(event);
}
You can do like this:
Add a Float window by WindowManager, for example, add a view to screen bottom:
WindowManager windowManager = (WindowManager) getActivity().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
FrameLayout view = new FrameLayout(getActivity()) {
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
// do you code
return true;
}
return super.dispatchKeyEvent(event);
}
};
windowManager.addView(view, layoutParams);

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

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.

Is it possible to expand GWT droplist(ListBox) by keyboard?

I have a flextable full of listboxes set to listBox.setVisibleItemCount(1), so they act as droplists.
When clicked on with the left mouse button they expand and let the user select an item.
Is it possible to mimic the mouse click with a keyboard key?
I've already tried to add keypress handler to the listbox that will fire a mousedown native event, but that did nothing.
Anyone have any idead?
Thanks in advance
I haven't found a solution yet for my problem but I have this workaround that works for now:
listBox.addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
ListBox listBox = ((ListBox)event.getSource());
SelectElement.as(listBox.getElement()).setSize(1);
}
});
listBox.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event ) {
if (event.getCharCode() == 32) {
ListBox listBox = ((ListBox)event.getSource());
SelectElement.as(listBox.getElement()).setSize(listBox.getItemCount());
}
}
});