Change-Id: I8facb7e89af09eec3331c21e04124b5e5827a66etags/8.0.0.alpha1
public class MyClass { | public class MyClass { | ||||
private final static Logger logger = | private final static Logger logger = | ||||
Logger.getLogger(MyClass.class.getName()); | Logger.getLogger(MyClass.class.getName()); | ||||
public void myMethod() { | public void myMethod() { | ||||
try { | try { | ||||
// do something that might fail | // do something that might fail | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
logger.log(Level.SEVERE, "FAILED CATASTROPHICALLY!", e); | logger.log(Level.SEVERE, "FAILED CATASTROPHICALLY!", e); | ||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
---- | ---- | ||||
ifdef::vaadin7[] | |||||
((("static"))) | ((("static"))) | ||||
((("memory | ((("memory | ||||
hence leaking memory. As the size of the PermGen memory where class object are | hence leaking memory. As the size of the PermGen memory where class object are | ||||
stored is fixed, the leakage will lead to a server crash after many | stored is fixed, the leakage will lead to a server crash after many | ||||
redeployments. The issue depends on the way how the server manages classloaders, | redeployments. The issue depends on the way how the server manages classloaders, | ||||
on the hardness of the back-references, and may also be different between Java 6 | |||||
and 7. So, if you experience PermGen issues, or want to play it on the safe | |||||
side, you should consider using non-static [classname]#Logger# instances.//As | |||||
discussed in Forum thread 1175841 | |||||
(24.2.2012). | |||||
on the hardness of the back-references. | |||||
So, if you experience PermGen issues, or want to play it on the safe | |||||
side, you should consider using non-static [classname]#Logger# instances. | |||||
//As discussed in Forum thread 1175841 (24.2.2012). | |||||
endif::vaadin7[] | |||||
(((range="endofrange", startref="term.advanced.logging"))) | (((range="endofrange", startref="term.advanced.logging"))) | ||||
[[application.events.anonymous]] | [[application.events.anonymous]] | ||||
== Using Anonymous Classes | == Using Anonymous Classes | ||||
By far the easiest and the most common way to handle events in Java 6 and 7 is | |||||
to use anonymous local classes. It encapsulates the handling of events to where | |||||
By far the easiest and the most common way to handle events in Java 8 lambdas. | |||||
It encapsulates the handling of events to where | |||||
the component is defined and does not require cumbering the managing class with | the component is defined and does not require cumbering the managing class with | ||||
interface implementations. The following example defines an anonymous class that | interface implementations. The following example defines an anonymous class that | ||||
inherits the [classname]#Button.ClickListener# interface. | inherits the [classname]#Button.ClickListener# interface. | ||||
---- | ---- | ||||
// Have a component that fires click events | // Have a component that fires click events | ||||
final Button button = new Button("Click Me!"); | final Button button = new Button("Click Me!"); | ||||
// Handle the events with an anonymous class | // Handle the events with an anonymous class | ||||
button.addClickListener(new Button.ClickListener() { | button.addClickListener(new Button.ClickListener() { | ||||
public void buttonClick(ClickEvent event) { | public void buttonClick(ClickEvent event) { | ||||
---- | ---- | ||||
public class Java8Buttons extends CustomComponent { | public class Java8Buttons extends CustomComponent { | ||||
public Java8Buttons() { | public Java8Buttons() { | ||||
setCompositionRoot(new HorizontalLayout( | |||||
setCompositionRoot(new HorizontalLayout( | |||||
new Button("OK", this::ok), | new Button("OK", this::ok), | ||||
new Button("Cancel", this::cancel))); | new Button("Cancel", this::cancel))); | ||||
} | } | ||||
public void ok(ClickEvent event) { | public void ok(ClickEvent event) { | ||||
event.getButton().setCaption ("OK!"); | event.getButton().setCaption ("OK!"); | ||||
} | } | ||||
public MyComposite() { | public MyComposite() { | ||||
Layout layout = new HorizontalLayout(); | Layout layout = new HorizontalLayout(); | ||||
// Just a single component in this composition | // Just a single component in this composition | ||||
button = new Button("Do not push this"); | button = new Button("Do not push this"); | ||||
button.addClickListener(this); | button.addClickListener(this); | ||||
layout.addComponent(button); | layout.addComponent(button); | ||||
setCompositionRoot(layout); | setCompositionRoot(layout); | ||||
} | } | ||||
// The listener method implementation | // The listener method implementation | ||||
public void buttonClick(ClickEvent event) { | public void buttonClick(ClickEvent event) { | ||||
button.setCaption("Do not push this again"); | button.setCaption("Do not push this again"); | ||||
toobutton = new Button("A Button Too", this); | toobutton = new Button("A Button Too", this); | ||||
// Put them in some layout | // Put them in some layout | ||||
Layout root = new HorizontalLayout(); | |||||
Layout root = new HorizontalLayout(); | |||||
root.addComponent(onebutton); | root.addComponent(onebutton); | ||||
root.addComponent(toobutton); | root.addComponent(toobutton); | ||||
setCompositionRoot(root); | setCompositionRoot(root); | ||||
} | } | ||||
@Override | @Override | ||||
public void buttonClick(ClickEvent event) { | public void buttonClick(ClickEvent event) { | ||||
// Differentiate targets by event source | // Differentiate targets by event source | ||||
or any other visible text is generally discouraged, as it may create problems | or any other visible text is generally discouraged, as it may create problems | ||||
for internationalization. Using other symbolic strings can also be dangerous, | for internationalization. Using other symbolic strings can also be dangerous, | ||||
because the syntax of such strings is checked only at runtime. | because the syntax of such strings is checked only at runtime. | ||||
. Enter a [guilabel]#Project name# and [guilabel]#Project location#, and select | . Enter a [guilabel]#Project name# and [guilabel]#Project location#, and select | ||||
the [guilabel]#Java SDK# to be used for the project. | the [guilabel]#Java SDK# to be used for the project. | ||||
Vaadin requires at least Java 6. | |||||
Vaadin requires at least Java 8. | |||||
If you have not configured a Java SDK previously, you can configure it here. | If you have not configured a Java SDK previously, you can configure it here. | ||||
+ | + | ||||
image::img/idea-newproject-1.png[scaledwidth=100%] | image::img/idea-newproject-1.png[scaledwidth=100%] | ||||
//<?dbfo-need height="8cm" ?> | //<?dbfo-need height="8cm" ?> | ||||
. Enter a project name, location, and the Java SDK to be used for the project. | . Enter a project name, location, and the Java SDK to be used for the project. | ||||
Vaadin requires at least Java 6. | |||||
Vaadin requires at least Java 8. | |||||
+ | + | ||||
image::img/idea-maven-newproject-1.png[scaledwidth=100%] | image::img/idea-maven-newproject-1.png[scaledwidth=100%] | ||||
+ | + |
In this example, we use the following toolchain: | In this example, we use the following toolchain: | ||||
* Windows, Linux, or Mac OS X | * Windows, Linux, or Mac OS X | ||||
* link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[Oracle Java SE 8] (Java 6 or newer is required) | |||||
* link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[Oracle Java SE 8] | |||||
* link:http://www.eclipse.org/downloads/[Eclipse IDE for Java EE Developers] | * link:http://www.eclipse.org/downloads/[Eclipse IDE for Java EE Developers] | ||||
* link:http://tomcat.apache.org/[Apache Tomcat 8.0 (Core)] | * link:http://tomcat.apache.org/[Apache Tomcat 8.0 (Core)] | ||||
* link:http://www.getfirefox.com/[Mozilla Firefox] browser | * link:http://www.getfirefox.com/[Mozilla Firefox] browser |