Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStéphane Bégaudeau2017-03-08 11:46:33 -0500
committerStéphane Bégaudeau2017-04-19 05:07:18 -0400
commit79dd2bc4413dc3d0de7b68319617b2d1e222d8bc (patch)
treeb25e3af90167b2c4c63d09c6395e1416fe3d48c5 /samples
parentcf865e177307774678976d2b0a6bc94dd1f0a647 (diff)
downloadorg.eclipse.eef-79dd2bc4413dc3d0de7b68319617b2d1e222d8bc.tar.gz
org.eclipse.eef-79dd2bc4413dc3d0de7b68319617b2d1e222d8bc.tar.xz
org.eclipse.eef-79dd2bc4413dc3d0de7b68319617b2d1e222d8bc.zip
[refactoring] Reduce the number of potential NPEs
Change-Id: I62a933320eb08dfe3df8e4f93d39cbc903dd753d Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
Diffstat (limited to 'samples')
-rw-r--r--samples/org.eclipse.eef.sample.custom.widget.colorpicker/src/org/eclipse/eef/sample/custom/widget/colorpicker/ColorPickerController.java63
1 files changed, 39 insertions, 24 deletions
diff --git a/samples/org.eclipse.eef.sample.custom.widget.colorpicker/src/org/eclipse/eef/sample/custom/widget/colorpicker/ColorPickerController.java b/samples/org.eclipse.eef.sample.custom.widget.colorpicker/src/org/eclipse/eef/sample/custom/widget/colorpicker/ColorPickerController.java
index a71a5b3a0..b69a0bdd7 100644
--- a/samples/org.eclipse.eef.sample.custom.widget.colorpicker/src/org/eclipse/eef/sample/custom/widget/colorpicker/ColorPickerController.java
+++ b/samples/org.eclipse.eef.sample.custom.widget.colorpicker/src/org/eclipse/eef/sample/custom/widget/colorpicker/ColorPickerController.java
@@ -12,6 +12,7 @@ package org.eclipse.eef.sample.custom.widget.colorpicker;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import java.util.function.Consumer;
import org.eclipse.eef.EEFCustomWidgetDescription;
@@ -82,26 +83,40 @@ public class ColorPickerController extends AbstractEEFCustomWidgetController imp
public void refresh() {
super.refresh();
- String valueExpression = getCustomExpression(VALUE_EXPRESSION_ID);
- this.newEval().logIfInvalidType(String.class).call(valueExpression, (value) -> {
- int red = DEFAULT_COLOR_CODE;
- int green = DEFAULT_COLOR_CODE;
- int blue = DEFAULT_COLOR_CODE;
- if (value != null) {
- String[] rgb = value.split(SEPARATOR);
- if (rgb.length == 3) {
- try {
- red = Integer.parseInt(rgb[0]);
- green = Integer.parseInt(rgb[1]);
- blue = Integer.parseInt(rgb[2]);
- Color color = ColorHelper.getColor(red, green, blue);
- ColorPickerController.this.newValueConsumer.accept(color);
- } catch (NumberFormatException e) {
- // TODO Log warning about unexpected result format from the expression.
- }
+ Optional<String> optionalValueExpression = this.getCustomExpression(VALUE_EXPRESSION_ID);
+ optionalValueExpression.ifPresent(valueExpression -> {
+ this.newEval().logIfInvalidType(String.class).call(valueExpression, (value) -> {
+ this.computeNewColorValue(value);
+ });
+ });
+ }
+
+ /**
+ * Computes the new value of the color.
+ *
+ * @param value
+ * The string representation of the new color
+ */
+ private void computeNewColorValue(String value) {
+ int red = DEFAULT_COLOR_CODE;
+ int green = DEFAULT_COLOR_CODE;
+ int blue = DEFAULT_COLOR_CODE;
+ if (value != null) {
+ String[] rgb = value.split(SEPARATOR);
+ if (rgb.length == 3) {
+ try {
+ red = Integer.parseInt(rgb[0]);
+ green = Integer.parseInt(rgb[1]);
+ blue = Integer.parseInt(rgb[2]);
+ Color color = ColorHelper.getColor(red, green, blue);
+ Optional.ofNullable(this.newValueConsumer).ifPresent(consumer -> {
+ consumer.accept(color);
+ });
+ } catch (@SuppressWarnings("unused") NumberFormatException e) {
+ // TODO Log warning about unexpected result format from the expression.
}
}
- });
+ }
}
@Override
@@ -122,13 +137,13 @@ public class ColorPickerController extends AbstractEEFCustomWidgetController imp
@Override
public void updateValue(final RGB color) {
this.editingContextAdapter.performModelChange(() -> {
- String editExpression = getCustomExpression(EDIT_EXPRESSION_ID);
-
- Map<String, Object> variables = new HashMap<String, Object>();
- variables.putAll(this.variableManager.getVariables());
- variables.put(EEFExpressionUtils.EEFText.NEW_VALUE, color.red + SEPARATOR + color.green + SEPARATOR + color.blue);
+ this.getCustomExpression(EDIT_EXPRESSION_ID).ifPresent(editExpression -> {
+ Map<String, Object> variables = new HashMap<String, Object>();
+ variables.putAll(this.variableManager.getVariables());
+ variables.put(EEFExpressionUtils.EEFText.NEW_VALUE, color.red + SEPARATOR + color.green + SEPARATOR + color.blue);
- EvalFactory.of(this.interpreter, variables).call(editExpression);
+ EvalFactory.of(this.interpreter, variables).call(editExpression);
+ });
});
}

Back to the top