How to increase the Vaadin notification/warning time? I am using Vaadin Valo theme. Nothing from the Book of Vaadin Notifications page helps.
Default Delays
First note that the five types of Notification have different delays by default.
Notification.Type.HUMANIZED_MESSAGEDisplayed until user moves the mouse pointer.
Notification.Type.TRAY_NOTIFICATIONDisplayed until 3 seconds after user moves the mouse pointer.
Notification.Type.WARNING_MESSAGEDisplayed until 1.5 seconds after user moves the mouse pointer.
Notification.Type.ERROR_MESSAGEDisplayed until user clicks on the message. (Delay set to -1 milliseconds)
Notification.Type.ASSISTIVE_NOTIFICATIONDoes nothing for me in Safari on Mac OS X Mountain Lion.
See below for the source code of an example app demonstrating each type.
Controlling Delay
The Notification class has accessors for the delay: getDelayMsec and setDelayMsec.
For example, to set a delay to seven seconds first we must convert to milliseconds.
notification.setDelayMsec( 7 * 1_000 );
Or better yet, replace "magic" numbers with use of the TimeUnit converter. The converter produces only long values, so we must cast to an int to satisfy the Vaadin setter.
notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );
Beware of one big change in behavior from the default. If setting a positive delay number on an ERROR_MESSAGE, the user need not click to dismiss; the error message disappears after the delay expires after a move of the mouse pointer.
See this code in action in the following example app. Uncomment the line of code calling the setter.
Example App
Here is some Vaadin 7.5.3 code. This class is a complete Vaadin app. Just create a new Vaadin project and replace with this code.
When disabling/enabling the setter, you may need to restart your Vaadin app. (Or buy a license for JRebel to avoid restarts.)
package com.example.vaadinnotifs;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.util.concurrent.TimeUnit;
/**
* By Basil Bourque.
*
* Source code provided under MIT License.
* http://opensource.org/licenses/MIT
*/
#Theme ( "mytheme" )
#Widgetset ( "com.example.vaadinnotifs.MyAppWidgetset" )
#SuppressWarnings ( "serial" )
public class MyUI extends UI
{
#Override
protected void init ( VaadinRequest vaadinRequest ) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin( true );
layout.setSpacing( true );
setContent( layout );
layout.addComponent( this.makeButton( Notification.Type.HUMANIZED_MESSAGE ) );
layout.addComponent( this.makeButton( Notification.Type.TRAY_NOTIFICATION ) );
layout.addComponent( this.makeButton( Notification.Type.WARNING_MESSAGE ) );
layout.addComponent( this.makeButton( Notification.Type.ERROR_MESSAGE ) );
layout.addComponent( this.makeButton( Notification.Type.ASSISTIVE_NOTIFICATION ) );
}
#WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
#VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
}
private Button makeButton ( Notification.Type typeArg ) {
// Taking advantage of Java’s nifty Enum capability to generically produce each Button instance.
Button b = new Button( typeArg.name() );
b.setData( typeArg ); // "setData" and "getData" are accessors for a little hiding place for any object you wish to carry with a Vaadin widget.
b.addClickListener( new Button.ClickListener()
{
#Override
public void buttonClick ( ClickEvent event ) {
Notification.Type t = ( Notification.Type ) event.getButton().getData(); // Cast from Object type used by the widget’s hiding place to the `Notification.Type` type we know we used.
Notification notification = new Notification( "This is a Notification message." , t );
//notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );
Integer delayMillis = notification.getDelayMsec();
String description = "Notification.Type: " + t.name() + " has delay of " + delayMillis + " ms.";
notification.setDescription( description );
notification.show( Page.getCurrent() );
}
} );
return b;
}
}
Related
I would like to print out the content of a homepage created with Vaadin 14 from a button.
with Vaadin 8 was a solution that unfortunately is no longer applicable:
Button print = new Button("Print This Page");
print.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
// Print the current page
JavaScript.getCurrent().execute("print();");
}
});
Any Idea how to do this in Vaadin14 please?
In Vaadin 10 and later, you can run arbitrary JavaScript by calling Page::executeJs
UI.getCurrent().getPage().executeJs( … )
There you should be able to call the same print function. See: Executing JavaScript in the Browser.
So in your case, for printing the current page:
UI.getCurrent().getPage().executeJs( "print();" ) ;
Full example in Vaadin 14.0.12, using lambda syntax:
package com.example;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
/**
* The main view contains a button and a click listener.
*/
#Route ( "" )
public class MainView extends VerticalLayout
{
public MainView ( )
{
this.add( new H1( "Print example" ) );
Button printButton = new Button( "Print…" );
printButton.addClickListener( ( ClickEvent < Button > clickEvent ) ->
{
UI.getCurrent().getPage().executeJs( "print();" );
} );
this.add(printButton);
}
}
I created a basic Vaadin application then added my Domino Jar files.
When I run the application, I get
[com.vaadin.server.ServiceException: java.lang.NoClassDefFoundError: lotus/domino/NotesException]
I've read a bunch of articles that talk about using OSGI etc. Isn't there a simple way to access Domino data from Vaadin without all the plug-ins etc? If not can someone explain why?
This is the calling code
package com.lms.helloDomino;
import javax.servlet.annotation.WebServlet;
import com.lms.service.StarService;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import lotus.domino.NotesException;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of an HTML page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {#link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
#Theme("mytheme")
public class MyUI extends UI {
#Override
protected void init(VaadinRequest vaadinRequest) {
StarService myStarService = null;
try
{
myStarService = new StarService();
myStarService.openStarDB();
} catch ( Exception e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
final VerticalLayout layout = new VerticalLayout();
final TextField name = new TextField();
name.setCaption("Your Domino Name");
name.setValue( myStarService.getNABProfile( "" ).fullName.toString() );
Button button = new Button("Click Me");
button.addClickListener(e -> {
layout.addComponent(new Label("Thanks " + name.getValue()
+ ", it works!"));
});
layout.addComponents(name, button);
setContent(layout);
}
#WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
#VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
Here is the domino code
package com.lms.service;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.NotesFactory;
public class StarService
{
public static Session notesSession = null;
public static Session getNotesSession()
{
if( notesSession == null )
try
{
notesSession = NotesFactory.createSession( "testHostServer", "testUser", "testPassword" );
} catch ( NotesException e )
{
e.printStackTrace();
}
return notesSession;
}
public StarService() throws NotesException
{
System.out.println( "Begin StarService Constructor" );
// Setup the notes connectivity
getNotesSession();
System.out.print( getNotesSession().getUserName() );
System.out.println( "End STARService Constructor" );
}
}
Turns out it was a build path issue. A big thank you to Karsten Lehmann from mindoo.de who helped me figure this out.
I didn't realize when running an Apache web server which serves up the Vaadin application, required my Domino .jar files on it's build path as well. He showed my how to add the .jar files to Apache's as follows:
Double click the Apache server under the servers tab
Click the Open Launch Configuration
Click the Class Path Tab
Highlight User Entries and Add External Jar files.
I've been looking for this off / on for a year now. Can't believe it's finally working!!!
In a Vaadin 8.1.3 app, I have a NativeSelect component on a layout, with a CheckBox next to it. The listener on the checkbox calls setEnabled on the popup-menu (the NativeSelect), passing true or false.
If false, I expected the widget to be dimmed in appearance, per the documentation. And I expected the popup menu to no longer pop up when clicked. Both of these expectations were not met. The widget does not change in appearance whether enabled or not. The widget continues to pop-up a menu when clicked. That's bad.
When enabled is set to false, choosing an item from the menu no longer triggers an event. The listener that responds to a value change on the menu no longer runs. That's good.
Is this a bug on the NativeSelect? The NativeSelect::setEnabled method documentation promises:
… The user can not interact with disabled components, which are shown with a style that indicates the status, usually shaded in light gray color. …
Is there some other way to dim the appearance of the NativeSelect and disable the display of its menu?
In summary, I am looking for three behaviors on a disabled popup menu:
Grayed-out/dimmed appearance
No longer pops up when clicked
Generates no events when clicked
The setEnabled command seems to promise all three bullets but delivers only the last bullet.
I am surprised by this behavior, given that this is a "native" HTML object. The select HTML object is defined to have a disable attribute in both old HTML as well as HTML5. This attribute has both effects I need: alter display, and make un-clickable. See this explanation and this demo of the HTML select widget being disabled.
I filed Issue # 9956.
It's because Vaadin Component interface delivers two methods to adjust component: setEnabled() and setReadOnly() - which you are actually looking for.
Here is my solution for you:
NativeSelect<String> nativeSelect = new NativeSelect<String>() {
#Override
public void setEnabled(boolean enabled)
{
super.setEnabled(enabled);
setReadOnly(!enabled);
if (enabled)
removeStyleName("transparency");
else
addStyleName("transparency");
}};
Add to css:
.transparency {
opacity: 0.5;
}
Right and Wrong
You seem to be wrong about the NativeSelect popping up when clicked if disabled and/or read-only.
You seem to be right about the NativeSelect breaking its contract to display a graying-out or similar visual cue if disabled (“shown with a style that indicates the status, usually shaded in light gray color”).
Example app
Here is a complete example Vaadin 8.1.3/8.1.4 in a single class.
package com.example.vaadin.checkboxexample;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.HasValue;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of a html page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {#link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*
*/
#Theme ( "mytheme" )
public class MyUI extends UI
{
#Override
protected void init ( VaadinRequest vaadinRequest )
{
// Widgets
List < String > strings = Arrays.asList( "This" , "That" , "The other" , "Yet another" );
// Default
final NativeSelect < String > popupDefault = new NativeSelect( "Default:" , new ArrayList<>(strings) );
popupDefault.setValue( strings.get( 3 ) );
popupDefault.addValueChangeListener( ( HasValue.ValueChangeListener < String > ) valueChangeEvent ->
{ System.out.println( "popupDefault selected: " + valueChangeEvent.getValue( ) ); } );
// Disabled
final NativeSelect < String > popupDisabled = new NativeSelect( "Disabled:" , new ArrayList<>(strings) );
popupDisabled.setValue( strings.get( 3 ) );
popupDisabled.addValueChangeListener( ( HasValue.ValueChangeListener < String > ) valueChangeEvent ->
{ System.out.println( "popupDisabled selected: " + valueChangeEvent.getValue( ) ); } );
popupDisabled.setEnabled( false );
// ReadOnly
final NativeSelect < String > popupReadOnly = new NativeSelect( "ReadOnly:" , new ArrayList<>(strings) );
popupReadOnly.setValue( strings.get( 3 ) );
popupReadOnly.addValueChangeListener( ( HasValue.ValueChangeListener < String > ) valueChangeEvent ->
{ System.out.println( "popupReadOnly selected: " + valueChangeEvent.getValue( ) ); } );
popupReadOnly.setReadOnly( true );
// Disabled and ReadOnly
final NativeSelect < String > popupDisabledAndReadOnly = new NativeSelect( "Disabled & ReadOnly:" , new ArrayList<>(strings) );
popupDisabledAndReadOnly.setValue( strings.get( 3 ) );
popupDisabledAndReadOnly.addValueChangeListener( ( HasValue.ValueChangeListener < String > ) valueChangeEvent ->
{ System.out.println( "popupDisabledAndReadOnly selected: " + valueChangeEvent.getValue( ) ); } );
popupDisabledAndReadOnly.setEnabled( false );
popupDisabledAndReadOnly.setReadOnly( true );
// Layout
final VerticalLayout layout = new VerticalLayout( );
layout.addComponents( popupDefault , popupDisabled , popupReadOnly , popupDisabledAndReadOnly );
setContent( layout );
}
#WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
#VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
}
}
Here is a screen-shot when run in Safari Technology Preview browser. Same behaviors seen in Vivaldi browser.
The first popup-menu does indeed popup when clicked; the others three do not. (Good)
Note how at least the second and fourth should appear visually grayed-out but do not. (Bad)
In the new Vaadin 7.4 release, the new Grid widget debuted as an alternative to the venerable Table.
After getting a Grid displayed, I later want to replace the entire set of data with fresh data. Rather than update the individual rows, I want to simply replace them.
I happen to be using a BeanItemContainer for easy read-only display of some objects with JavaBeans-style getter methods.
I considered two approaches:
Two step process of replacing bean items.
(1) First remove all BeanItem objects with Container::removeAllItems method.
(2) Then add replacement BeanItem objects with the BeanItemContainer::addAll method.
Replace entire BeanItemContainer.
Call Grid::setContainerDataSource and pass a new instance of BeanItemContainer constructed with fresh data.
Below is a sample application (Vaadin 7.4.2) showing both approaches. A pair of identical Grid widgets appear. Each has a button that updates data with either approach.
Results
The first approach (removing items and adding items) works. The fresh data immediately appears.
The second approach (replacing container rather than items) seems like it should work, with nothing contrary suggested in the scant documentation. But nothing happens. No exceptions or errors occur, yet no fresh data appears. I opened Ticket # 17268 on Vaadin trac for this issue.
Perhaps there are other better ways. Please post or comment with any alternatives.
Example App
Three classes are displayed below. You should be able to copy-paste into a new Vaadin 7.4.x app.
One class is the usual "MyUI" created in every new Vaadin app.
Another is simple JavaBeans-style class, "Astronomer", providing data for the rows in our Grid. That Astronomer class includes a convenient static method for generating a List of instances. Each new Astronomer gets a random number of popularity votes, to show fresh data values.
The meaty part of the example is in the "AstronomersLayout" class which creates the pair of Grids with their assigned buttons.
I use Java 8 Lambda syntax and the new java.time classes. So you may need to change your project's settings to use Java 8. In NetBeans 8 that means Project > Properties > Sources > Source/Binary Format (popup menu) > 1.8.
MyUI.java
Get your Vaadin app going.
package com.example.vaadingridexample;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.UI;
/**
* Example app in Vaadin 7.4.2 experimenting with two ways to replace data in a
* displayed Grid.
*
* #author Basil Bourque
*/
#Theme ( "mytheme" )
#Widgetset ( "com.example.vaadingridexample.MyAppWidgetset" )
public class MyUI extends UI
{
#Override
protected void init ( VaadinRequest vaadinRequest )
{
this.setContent( new AstronomersLayout() );
}
#WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
#VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
}
}
AstronomersLayout.java
The main part of the example.
package com.example.vaadingridexample;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.VerticalLayout;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* Layout displays a pair of Grids, each with a Button to replace its contents
* with fresh data in either of two ways: (a) Replace all the items within the
* Container, or (b) Replace container itself.
*
* #author Basil Bourque
*/
#SuppressWarnings ( "serial" )
public class AstronomersLayout extends VerticalLayout
{
// -----| Member vars |--------------------------
Grid grid_ReplaceItems;
String gridCaption_ReplaceItems = "Astronomers - Replacing Items";
Button button_ReplaceItems;
Grid grid_ReplaceContainer;
String gridCaption_ReplaceContainer = "Astronomers - Replacing Container";
Button button_ReplaceContainer;
// -----| Constructor |--------------------------
public AstronomersLayout ()
{
this.prepareWidgets();
this.composeLayout();
}
// -----| Helper Methods |--------------------------
private void prepareWidgets ()
{
// Show updating a Grid by replacing the bean items within a container.
// Grid
List<Astronomer> listA = Astronomer.makeList();
BeanItemContainer<Astronomer> containerA = new BeanItemContainer<>( Astronomer.class , listA );
this.grid_ReplaceItems = new Grid( this.gridCaption_ReplaceItems , containerA );
//this.grid_ReplaceItems.setColumnOrder( "votes" , "givenName" , "surName" , "birthYear" );
this.grid_ReplaceItems.setColumnOrder( Astronomer.FIELD.VOTES.getName() , Astronomer.FIELD.GIVENNAME.getName() , Astronomer.FIELD.SURNAME.getName() , Astronomer.FIELD.BIRTHYEAR.getName() ); // Enum is a safer way of doing this: this.grid_ReplaceItems.setColumnOrder( "votes" , "givenName" , "surName" , "birthYear" );
this.grid_ReplaceItems.setHeightMode( HeightMode.ROW ); // Show all rows of data for this grid.
this.updateCaptionAndSize( this.grid_ReplaceItems , this.gridCaption_ReplaceItems );
// Button
this.button_ReplaceItems = new Button( "Replace Items" );
this.button_ReplaceItems.addClickListener( ( ClickEvent event ) -> {
#SuppressWarnings ( "unchecked" )
BeanItemContainer<Astronomer> bic = ( BeanItemContainer<Astronomer> ) this.grid_ReplaceItems.getContainerDataSource(); // Access existing container. Cast as need be.
bic.removeAllItems(); // Remove existing items.
bic.addAll( Astronomer.makeList() ); // Add fresh bean items to existing container.
this.updateCaptionAndSize( this.grid_ReplaceItems , this.gridCaption_ReplaceItems );
} );
// Show updating a Grid by replacing the container rather than its contents.
// Grid
List<Astronomer> listB = Astronomer.makeList();
BeanItemContainer<Astronomer> containerB = new BeanItemContainer<>( Astronomer.class , listB );
this.grid_ReplaceContainer = new Grid( this.gridCaption_ReplaceContainer , containerB );
this.grid_ReplaceContainer.setColumnOrder( Astronomer.FIELD.VOTES.getName() , Astronomer.FIELD.GIVENNAME.getName() , Astronomer.FIELD.SURNAME.getName() , Astronomer.FIELD.BIRTHYEAR.getName() );
this.grid_ReplaceContainer.setHeightMode( HeightMode.ROW ); // Show all rows of data for this grid.
this.updateCaptionAndSize( this.grid_ReplaceContainer , this.gridCaption_ReplaceContainer );
// Button
this.button_ReplaceContainer = new Button( "Replace Container" );
this.button_ReplaceContainer.addClickListener( ( ClickEvent event ) -> {
#SuppressWarnings ( "unchecked" )
BeanItemContainer<Astronomer> bic = new BeanItemContainer<>( Astronomer.class , listB ); // Create replacement container.
this.grid_ReplaceContainer.setContainerDataSource( bic );
this.updateCaptionAndSize( this.grid_ReplaceContainer , this.gridCaption_ReplaceContainer );
} );
}
private void updateCaptionAndSize ( final Grid grid , final String caption )
{
// Caption
grid.setCaption( caption + " ( updated " + this.now() + " )" ); // Update caption of Grid to indicate fresh data.
// Show all rows.
double h = grid.getContainerDataSource().size() > 0 ? grid.getContainerDataSource().size() : 3; // Cannot set height to zero rows. So if no data, set height to some arbitrary number of (empty) rows.
grid.setHeightByRows( h );
}
private void composeLayout ()
{
// Initialize this layout.
this.setMargin( true );
this.setSpacing( true );
// Content
this.addComponent( this.button_ReplaceItems );
this.addComponent( this.grid_ReplaceItems );
this.addComponent( this.button_ReplaceContainer );
this.addComponent( this.grid_ReplaceContainer );
}
// Helper method.
private String now ()
{
// Get current time in UTC. Truncate fractional seconds. Append a 'Z' to indicate UTC time zone.
return ZonedDateTime.now( ZoneOffset.UTC ).format( DateTimeFormatter.ISO_LOCAL_TIME ).substring( 0 , 8 ).concat( "Z" );
}
}
Astronomer.java
The data, the bean items, stored in a BeanItemContainer for display in a Grid.
A nested Enum provides a safer way to refer to the field names in the other class, AstronomersLayout for call to setColumnOrder.
package com.example.vaadingridexample;
import java.util.ArrayList;
import java.util.List;
/**
* Provides the beans to appear as rows in a BeanItemContainer backing a Grid.
*
* Note the static convenience method for generating a List of instances.
*
* #author Basil Bourque
*/
public class Astronomer
{
public enum FIELD
{
SURNAME( "surname" ),
GIVENNAME( "givenName" ),
BIRTHYEAR( "birthYear" ),
VOTES( "votes" );
private String name;
private FIELD ( String s )
{
this.name = s;
}
public String getName ()
{
return this.name;
}
}
// Members
private String surname;
private String givenName;
private Integer birthYear;
private Integer votes;
public Astronomer ( final String givenName , final String surName , final Integer birthYear )
{
this.surname = surName;
this.givenName = givenName;
this.birthYear = birthYear;
this.votes = this.random();
}
public static List<Astronomer> makeList ()
{
List<Astronomer> list = new ArrayList<>( 7 );
list.add( new Astronomer( "Hypatia" , "of Alexandria" , -370 ) );
list.add( new Astronomer( "Nicolaus" , "Copernicus" , 1473 ) );
list.add( new Astronomer( "Tycho" , "Brahe" , 1546 ) );
list.add( new Astronomer( "Giordano" , "Bruno" , 1548 ) );
list.add( new Astronomer( "Galileo" , "Galilei" , 1564 ) );
list.add( new Astronomer( "Johannes" , "Kepler" , 1571 ) );
list.add( new Astronomer( "Isaac" , "Newton" , 1643 ) );
list.add( new Astronomer( "Caroline" , "Herschel" , 1750 ) );
return list;
}
// ----| Helper Methods |----------------------------------
private Integer random ()
{
return ( int ) ( java.lang.Math.random() * 100 );
}
// ----| Bean Getters |----------------------------------
public String getSurname ()
{
return this.surname;
}
public String getGivenName ()
{
return this.givenName;
}
public Integer getBirthYear ()
{
return this.birthYear;
}
public Integer getVotes ()
{
return this.votes;
}
// ----| Object Superclass |----------------------------------
#Override
public String toString ()
{
return "Astronomer{ " + "surName=" + surname + " | givenName=" + givenName + " | birthYear=" + birthYear + " | votes=" + votes + " }";
}
}
You can simply get the record you have removed from button through clickListener with .getSelectedRow() .
After this you can remove your item from your grid with .removeItem().
IE:
Grid yourGrid = new Grid();
yourGrid.setContainerDataSource(yourData);
Button removeItem = new Button("Remove item");
removeItem.addClickListener(l -> {
Item selectedItem = (Item) yourGrid.getSelectedRow();
yourGrid.getContainerDataSource().removeItem(selectedItem);
});
Bye!
I am developing an application but I noticed that each class has a lot of similar code to the one below addRace method in the class below:
#CustomTag( 'race-view' )
class RaceViewForm extends PolymerElement
{
RaceViewForm.created() : super.created();
void addRace( Event e, var detail, Element target )
{
var raceElem = $['race'];
if( raceElem.children.length < 1 )
{
raceElem.children.add( new Element.tag( 'race-form' ) );
}
raceElem.on[ DELETE_BUTTON_FORM_EVENT ]
.listen( (Event e)
{
(e.target as Element).remove();
dispatchEvent( new CustomEvent( RACE_VIEW_EVENT, detail:new Race() ));
});
}
}
I have attempted to move the repeating code into a library called shared.dart with the following refactoring:
import 'dart:html';
import 'package:observe/observe.dart';
...
void addForm( Element target, String eventName, String dispatchEventName, dynamic instance )
{
var element = target;
if( element.children.length < 1 )
{
element.children.add( new Element.tag( 'race-form' ) );
}
element.on[ eventName ]
.listen( (Event e)
{
(e.target as Element).remove();
dispatchEvent( new CustomEvent( dispatchEventName, detail:instance ));
});
}
However, the dart IDE flags the dispatchEvent method in the addForm method as being not defined with parameter onData (dynamic) -> void. Given that dispatchEvent is in the 'dart:html' package, I am uncertain as to what to do next.
I didn't find a dispatchEvent() in dart:html (top level) only as method of the Node class. You need to pass your Polymer element instance to the library method.
You could implement it as a mixin to make it easier to reuse.
See https://github.com/donny-dont/Pixelate/blob/master/lib/components/expander/expander.dart#L41 (Expandable, Customizeable) for an example.