summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2012-08-03 17:02:56 +0300
committerLeif Åstrand <leif@vaadin.com>2012-08-03 17:02:56 +0300
commitb2761e03005e6fa6671a73e2aaceab0c6ae951f9 (patch)
treef61124138d578bef82869aad55bfff272336051f
parent20af2f5623d264a418f73cd9cd053462227d0eb4 (diff)
downloadvaadin-framework-b2761e03005e6fa6671a73e2aaceab0c6ae951f9.tar.gz
vaadin-framework-b2761e03005e6fa6671a73e2aaceab0c6ae951f9.zip
Add shorthand for avoiding null-checks with ResourceReference (#9211)
-rw-r--r--src/com/vaadin/terminal/gwt/server/ResourceReference.java16
-rw-r--r--src/com/vaadin/ui/AbstractComponent.java13
-rw-r--r--src/com/vaadin/ui/Video.java4
-rw-r--r--tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java7
4 files changed, 21 insertions, 19 deletions
diff --git a/src/com/vaadin/terminal/gwt/server/ResourceReference.java b/src/com/vaadin/terminal/gwt/server/ResourceReference.java
index 98ac7e0f4b..2104ad4b87 100644
--- a/src/com/vaadin/terminal/gwt/server/ResourceReference.java
+++ b/src/com/vaadin/terminal/gwt/server/ResourceReference.java
@@ -48,4 +48,20 @@ public class ResourceReference extends URLReference {
}
}
+
+ public static ResourceReference create(Resource resource) {
+ if (resource == null) {
+ return null;
+ } else {
+ return new ResourceReference(resource);
+ }
+ }
+
+ public static Resource getResource(URLReference reference) {
+ if (reference == null) {
+ return null;
+ }
+ assert reference instanceof ResourceReference;
+ return ((ResourceReference) reference).getResource();
+ }
}
diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java
index b71948e96e..e7cb38256c 100644
--- a/src/com/vaadin/ui/AbstractComponent.java
+++ b/src/com/vaadin/ui/AbstractComponent.java
@@ -294,12 +294,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
*/
@Override
public Resource getIcon() {
- ResourceReference ref = ((ResourceReference) getState().getIcon());
- if (ref == null) {
- return null;
- } else {
- return ref.getResource();
- }
+ return ResourceReference.getResource(getState().getIcon());
}
/**
@@ -311,11 +306,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
*/
@Override
public void setIcon(Resource icon) {
- if (icon == null) {
- getState().setIcon(null);
- } else {
- getState().setIcon(new ResourceReference(icon));
- }
+ getState().setIcon(ResourceReference.create(icon));
requestRepaint();
}
diff --git a/src/com/vaadin/ui/Video.java b/src/com/vaadin/ui/Video.java
index 48fe3e4192..d4f95a5be3 100644
--- a/src/com/vaadin/ui/Video.java
+++ b/src/com/vaadin/ui/Video.java
@@ -67,7 +67,7 @@ public class Video extends AbstractMedia {
* @param poster
*/
public void setPoster(Resource poster) {
- getState().setPoster(new ResourceReference(poster));
+ getState().setPoster(ResourceReference.create(poster));
requestRepaint();
}
@@ -75,7 +75,7 @@ public class Video extends AbstractMedia {
* @return The poster image.
*/
public Resource getPoster() {
- return ((ResourceReference) getState().getPoster()).getResource();
+ return ResourceReference.getResource(getState().getPoster());
}
}
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java
index f9d4cb38e6..b47f555640 100644
--- a/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java
+++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java
@@ -20,11 +20,6 @@ public class ResourceInStateComponent extends AbstractComponent {
}
public Resource getMyIcon() {
- ResourceReference ref = ((ResourceReference) getState().getMyIcon());
- if (ref != null) {
- return ref.getResource();
- } else {
- return null;
- }
+ return ResourceReference.getResource(getState().getMyIcon());
}
}