Primefaces :4
I need to create a Programmatic Menu & specify a javascript method which would be called on click of the menu item.
I am unable to understand how to specify the javascript method to be called on menu item click in the managed bean.
Only the client side method needs to be called. Server side method is not required to be called.
#ManagedBean
public class MenuView {
private MenuModel model;
#PostConstruct
public void init() {
model = new DefaultMenuModel();
//First submenu
DefaultSubMenu firstSubmenu = new DefaultSubMenu("Dynamic Submenu");
DefaultMenuItem item = new DefaultMenuItem("ABC");
item.setIcon("ui-icon-home");
firstSubmenu.addElement(item);
...
model.addElement(firstSubmenu);
}
public MenuModel getModel() {
return model;
}
}
You can use : item.setOnclick(String click)
Check out the API : http://www.primefaces.org/docs/api/4.0/org/primefaces/model/menu/DefaultMenuItem.html#setOnclick(java.lang.String)
Related
i have a class with the entry point and the onModuleLoad. That class have a getter for the panel public void sethauptPanel(Composite composite) {
hauptPanel.clear();
hauptPanel.add(composite);
}
but i need to send an object from my class to change it
MenuItem anmelden = new MenuItem(konstanten.anmelden(), new Command() {
#Override
public void execute() {
sethauptPanel(new AdminLoginView(!!!!this!!!));
}
});
I try this, but than he have the Command. I try classname classname = this.
But i can't change the panel.
You have to use className.this
So if your class is named : MainPanel
you should use : new AdminLoginView(MainPanel.this);
I would like automate a drag and drop functionality. I have written a function for that. In that function there is two activites
1. Get the drag-from and drag-to element
2. Perform the drag and drop
can i write this as two sub functions inside ?
The following is the code:
public class dunelmtest {
static WebDriver driver= new FirefoxDriver();
#Test
public void test() {
DragandDrop();
}
public static void DragandDrop(){
driver.manage().window().maximize();
driver.get("http://marcojakob.github.io/dart-dnd/basic/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Want a sub function for this
//Locate element which you wants to drag.
WebElement dragElementFrom = driver.findElement(By.xpath("//img[#class='document']"));
//Locate element where you wants to drop.
WebElement dropElementTo = driver.findElement(By.xpath("//div[#class='trash']"));
// Want another sub function fir this
//Use Actions class and Its members of WebDriver API to perform drag and drop operation.
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(dragElementFrom)
.moveToElement(dropElementTo)
.release(dropElementTo)
.build();
dragAndDrop.perform();
}
}
So how can i create two sub function inside DragandDrop?
I'm not sure if i understand your Question clearly but from what i can understand you want to do something more of a Fluent, the snippet below shows a Class structure that should help you achieve what you want.
Hope this helps
Public class DragandDrop{
public DragandDrop LocateElement(By locator){
//Implementation
return this;
}
public DragandDrop DragElement(toElement){
//Implementation
return this;
}
}
I want to add a ClickListener to an Item of a MenuBar in Vaadin.
I know about the normal situation, which i got working:
MenuBar menubar = new MenuBar();
menubar.addItem("Item", new MenuBar.Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
//Do sth. when item is clicked
}
});
In my application, I'm working with MVP, so the code which should run, is in an other class than the code which is defining the menubar.
Is there a way to add a listener to a specific item in the menubar ?
When you add an item to your MenuBar the function addItem(String,Command) actually returns a MenuItem which can be used later. You can do this :
MenuItem select = menuBar.addItem("Select", null);
And in another context you can add a listener on that MenuItem like this:
select.setCommand(new Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
System.out.println("You clicked on "+selectedItem.getText());
}
});
I am writing a javafx UI and would like to get the owner Node of a contextMenu from a eventHandler for the MenuItem that was clicked on.
My Code:
TabPane tabPane = new TabPane();
Tab tab1 = new Tab();
Tab tab2 = new Tab();
tabPane.getTabs().addAll(tab1,tab2);
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem("Do Some Action");
menuItem.setOnAction(new EventHandler<ActionEvent>(){
#override
public void handle(ActionEvent e){
// Get the tab which was clicked on and do stuffs with it
}
});
contextMenu.getItems().add(menuItem);
for(Tab tab: tabPane.getTabs()){
tab.setContextMenu(contextMenu);
}
What I would like to do is get a reference to the tab that had it's contextMenu selected.
I was able to get a reference to what appears to be the ContextMenu of the MenuItem with the following code inside of the handle(ActionEvent e) method for the menuItem eventHandler:
ContextMenu menu = ((ContextMenu)((MenuItem)e.getSource()).getParentPopup());
My idea from there was to use ContextMenu's .getOwnerNode() method on menu and then have a reference to the tab, but when running that I get a reference to an item that I can't make sense of.
The toString() method for the object returned by .getOwnerNode() returns "TabPaneSkin$TabHeaderSkin$3#14f59cef" which I can't figure out the meaning of.
Is my approach of trying to work my way up the chain until I get to the node correct or is there an entirely different approach that will work better?
All I need to have is the functionality of a ContextMenu, and when the MenuItem is clicked on, I need to have a reference to the tab for which the ContextMenu was selected so I can do cool stuffs with it.
Create a ContextMenu for each tab.
Make each tab final.
Directly reference the final tab in the menu item event handler for the context menu.
Here is a code snippet:
final Tab tab = new Tab("xyzzy");
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem("Do Some Action");
menuItem.setOnAction(new EventHandler<ActionEvent>(){
#Override public void handle(ActionEvent e){
tab.setText("Activated by User");
}
});
Every time the user right clicks on a tab header and selects the "Count Click" menu, the content of the related tab is updated with a count of the number of licks counted so far for that tab.
Here is an executable sample:
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class TabContext extends Application {
#Override public void start(Stage stage) {
TabPane tabPane = new TabPane();
tabPane.getTabs().addAll(
createTab("xyzzy", "aliceblue"),
createTab("frobozz", "blanchedalmond")
);
stage.setScene(new Scene(tabPane));
stage.show();
}
private Tab createTab(String tabName, String webColor) {
final Label content = new Label("0");
content.setAlignment(Pos.CENTER);
content.setPrefSize(200, 100);
content.setStyle("-fx-font-size: 30px; -fx-background-color: " + webColor + ";");
final Tab tab = new Tab(tabName);
tab.setContent(content);
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem("Count Click");
menuItem.setOnAction(new EventHandler<ActionEvent>(){
#Override public void handle(ActionEvent e){
content.setText(
"" + (Integer.parseInt(content.getText()) + 1)
);
}
});
contextMenu.getItems().add(menuItem);
tab.setContextMenu(contextMenu);
return tab;
}
public static void main(String[] args) { Application.launch(args); }
}
Alternately to using an anonymous inner class this way, you could create an EventHandler subclass with a constructor that includes the Tab for which the EventHandler is attached.
class TabContextMenuHandler implements EventHandler<ActionEvent> {
private final Tab tab;
TabContextMenuHandler(Tab tab) {
this.tab = tab;
}
#Override public void handle(ActionEvent event) {
tab.setText("Activated by User");
}
}
I have an MVVM Cross application running on Windows Phone 8 which I recently ported across to using Portable Class Libraries.
The view models are within the portable class library and one of them exposes a property which enables and disables a PerformanceProgressBar from the Silverlight for WP toolkit through data binding.
When the user presses a button a RelayCommand kicks off a background process which sets the property to true which should enable the progress bar and does the background processing.
Before I ported it to a PCL I was able to invoke the change from the UI thread to ensure the progress bar got enabled, but the Dispatcher object isn't available in a PCL. How can I work around this?
Thanks
Dan
All the MvvmCross platforms require that UI-actions get marshalled back on to the UI Thread/Apartment - but each platform does this differently....
To work around this, MvvmCross provides a cross-platform way to do this - using an IMvxViewDispatcherProvider injected object.
For example, on WindowsPhone IMvxViewDispatcherProvider is provided ultimately by MvxMainThreadDispatcher in https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.WindowsPhone/Views/MvxMainThreadDispatcher.cs
This implements the InvokeOnMainThread using:
private bool InvokeOrBeginInvoke(Action action)
{
if (_uiDispatcher.CheckAccess())
action();
else
_uiDispatcher.BeginInvoke(action);
return true;
}
For code in ViewModels:
your ViewModel inherits from MvxViewModel
the MvxViewModel inherits from an MvxApplicationObject
the MvxApplicationObject inherits from an MvxNotifyPropertyChanged
the MvxNotifyPropertyChanged object inherits from an MvxMainThreadDispatchingObject
MvxMainThreadDispatchingObject is https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross/ViewModels/MvxMainThreadDispatchingObject.cs
public abstract class MvxMainThreadDispatchingObject
: IMvxServiceConsumer<IMvxViewDispatcherProvider>
{
protected IMvxViewDispatcher ViewDispatcher
{
get { return this.GetService().Dispatcher; }
}
protected void InvokeOnMainThread(Action action)
{
if (ViewDispatcher != null)
ViewDispatcher.RequestMainThreadAction(action);
}
}
So... your ViewModel can just call InvokeOnMainThread(() => DoStuff());
One further point to note is that MvvmCross automatically does UI thread conversions for property updates which are signalled in a MvxViewModel (or indeed in any MvxNotifyPropertyChanged object) through the RaisePropertyChanged() methods - see:
protected void RaisePropertyChanged(string whichProperty)
{
// check for subscription before going multithreaded
if (PropertyChanged == null)
return;
InvokeOnMainThread(
() =>
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(whichProperty));
});
}
in https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross/ViewModels/MvxNotifyPropertyChanged.cs
This automatic marshalling of RaisePropertyChanged() calls works well for most situations, but can be a bit inefficient if you Raise a lot of changed properties from a background thread - it can lead to a lot of thread context switching. It's not something you need to be aware of in most of your code - but if you ever do find it is a problem, then it can help to change code like:
MyProperty1 = newValue1;
MyProperty2 = newValue2;
// ...
MyProperty10 = newValue10;
to:
InvokeOnMainThread(() => {
MyProperty1 = newValue1;
MyProperty2 = newValue2;
// ...
MyProperty10 = newValue10;
});
If you ever use ObservableCollection, then please note that MvvmCross does not do any thread marshalling for the INotifyPropertyChanged or INotifyCollectionChanged events fired by these classes - so it's up to you as a developer to marshall these changes.
The reason: ObservableCollection exists in the MS and Mono code bases - so there is no easy way that MvvmCross can change these existing implementations.
If you don't have access to the Dispatcher, you can just pass a delegate of the BeginInvoke method to your class:
public class YourViewModel
{
public YourViewModel(Action<Action> beginInvoke)
{
this.BeginInvoke = beginInvoke;
}
protected Action<Action> BeginInvoke { get; private set; }
private void SomeMethod()
{
this.BeginInvoke(() => DoSomething());
}
}
Then to instanciate it (from a class that has access to the dispatcher):
var dispatcherDelegate = action => Dispatcher.BeginInvoke(action);
var viewModel = new YourViewModel(dispatcherDelegate);
Or you can also create a wrapper around your dispatcher.
First, define a IDispatcher interface in your portable class library:
public interface IDispatcher
{
void BeginInvoke(Action action);
}
Then, in the project who has access to the dispatcher, implement the interface:
public class DispatcherWrapper : IDispatcher
{
public DispatcherWrapper(Dispatcher dispatcher)
{
this.Dispatcher = dispatcher;
}
protected Dispatcher Dispatcher { get; private set; }
public void BeginInvoke(Action action)
{
this.Dispatcher.BeginInvoke(action);
}
}
Then you can just pass this object as a IDispatcher instance to your portable class library.
Another option that could be easier is to store a reference to SynchronizationContext.Current in your class's constructor. Then, later on, you can use _context.Post(() => ...) to invoke on the context -- which is the UI thread in WPF/WinRT/SL.
class MyViewModel
{
private readonly SynchronizationContext _context;
public MyViewModel()
{
_context = SynchronizationContext.Current.
}
private void MyCallbackOnAnotherThread()
{
_context.Post(() => UpdateTheUi());
}
}