Refactoring after review.

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
Jonathan Philip Knoblauch
2016-03-11 16:39:54 +01:00
parent 6cc64639bd
commit c91bb15ccb
3 changed files with 95 additions and 110 deletions

View File

@@ -15,16 +15,15 @@ import org.eclipse.hawkbit.simulator.AbstractSimulatedDevice.Protocol;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.data.Validator;
import com.vaadin.data.util.ObjectProperty; import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.validator.NullValidator; import com.vaadin.data.validator.NullValidator;
import com.vaadin.data.validator.RangeValidator; import com.vaadin.data.validator.RangeValidator;
import com.vaadin.data.validator.RegexpValidator; import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.server.FontAwesome; import com.vaadin.server.FontAwesome;
import com.vaadin.server.Resource;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout; import com.vaadin.ui.FormLayout;
import com.vaadin.ui.OptionGroup; import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextField; import com.vaadin.ui.TextField;
@@ -44,15 +43,13 @@ public class GenerateDialog extends Window {
private final FormLayout formLayout = new FormLayout(); private final FormLayout formLayout = new FormLayout();
private final TextField tf1; private final TextField namePrefixTextField;
private final TextField tf2; private final TextField amountTextField;
private final TextField tf3; private final TextField tenantTextField;
private final TextField tf4; private final TextField pollDelayTextField;
private final TextField tf5; private final TextField pollUrlTextField;
private final TextField tf6; private final TextField gatewayTokenTextField;
private final OptionGroup protocolGroup; private final OptionGroup protocolGroup;
private final Button buttonOk; private final Button buttonOk;
/** /**
@@ -68,55 +65,44 @@ public class GenerateDialog extends Window {
formLayout.setSpacing(true); formLayout.setSpacing(true);
formLayout.setMargin(true); formLayout.setMargin(true);
tf1 = new TextField("name prefix", "dmfSimulated"); namePrefixTextField = createRequiredTextfield("name prefix", "dmfSimulated", FontAwesome.INFO, true,
tf1.setIcon(FontAwesome.INFO); new NullValidator("Must be given", false));
tf1.setRequired(true);
tf1.addValidator(new NullValidator("Must be given", false));
tf2 = new TextField("amount", new ObjectProperty<Integer>(10)); amountTextField = createRequiredTextfield("amount", new ObjectProperty<Integer>(10), FontAwesome.GEAR, true,
tf2.setIcon(FontAwesome.GEAR); new RangeValidator<Integer>("Must be between 1 and 30000", Integer.class, 1, 30000));
tf2.setRequired(true);
tf2.addValidator(new RangeValidator<Integer>("Must be between 1 and 30000", Integer.class, 1, 30000));
tf3 = new TextField("tenant", "default"); tenantTextField = createRequiredTextfield("tenant", "default", FontAwesome.USER, true,
tf3.setIcon(FontAwesome.USER); new NullValidator("Must be given", false));
tf3.setRequired(true);
tf3.addValidator(new NullValidator("Must be given", false));
tf4 = new TextField("poll delay (sec)", new ObjectProperty<Integer>(10)); pollDelayTextField = createRequiredTextfield("poll delay (sec)", new ObjectProperty<Integer>(10),
tf4.setIcon(FontAwesome.CLOCK_O); FontAwesome.CLOCK_O, true,
tf4.setRequired(true); new RangeValidator<Integer>("Must be between 1 and 60", Integer.class, 1, 60));
tf4.setVisible(false); pollDelayTextField.setVisible(false);
tf4.addValidator(new RangeValidator<Integer>("Must be between 1 and 60", Integer.class, 1, 60));
tf5 = new TextField("base poll URL endpoint", "http://localhost:8080"); pollUrlTextField = createRequiredTextfield("base poll URL endpoint", "http://localhost:8080",
tf5.setColumns(50); FontAwesome.FLAG_O, true, new RegexpValidator(
tf5.setIcon(FontAwesome.FLAG_O); "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", "is not an URL"));
tf5.setRequired(true); pollUrlTextField.setColumns(50);
tf5.setVisible(false); pollUrlTextField.setVisible(false);
tf5.addValidator(new RegexpValidator("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]",
"is not an URL"));
tf6 = new TextField("gateway token", ""); gatewayTokenTextField = createRequiredTextfield("gateway token", "", FontAwesome.FLAG_O, true, null);
tf6.setColumns(50); gatewayTokenTextField.setColumns(50);
tf6.setIcon(FontAwesome.FLAG_O); gatewayTokenTextField.setVisible(false);
tf6.setRequired(true);
tf6.setVisible(false);
protocolGroup = createProtocolGroup(); protocolGroup = createProtocolGroup();
buttonOk = createOkButton(callback); buttonOk = createOkButton(callback);
tf1.addValueChangeListener(event -> checkValid(tf1, tf2, tf3, tf4, buttonOk)); namePrefixTextField.addValueChangeListener(event -> checkValid());
tf2.addValueChangeListener(event -> checkValid(tf1, tf2, tf3, tf4, buttonOk)); amountTextField.addValueChangeListener(event -> checkValid());
tf3.addValueChangeListener(event -> checkValid(tf1, tf2, tf3, tf4, buttonOk)); tenantTextField.addValueChangeListener(event -> checkValid());
formLayout.addComponent(tf1); formLayout.addComponent(namePrefixTextField);
formLayout.addComponent(tf2); formLayout.addComponent(amountTextField);
formLayout.addComponent(tf3); formLayout.addComponent(tenantTextField);
formLayout.addComponent(protocolGroup); formLayout.addComponent(protocolGroup);
formLayout.addComponent(tf4); formLayout.addComponent(pollDelayTextField);
formLayout.addComponent(tf5); formLayout.addComponent(pollUrlTextField);
formLayout.addComponent(tf6); formLayout.addComponent(gatewayTokenTextField);
formLayout.addComponent(buttonOk); formLayout.addComponent(buttonOk);
setCaption("Simulate Devices"); setCaption("Simulate Devices");
@@ -125,9 +111,9 @@ public class GenerateDialog extends Window {
center(); center();
} }
private void checkValid(final TextField tf1, final TextField tf2, final TextField tf3, final TextField tf4, private void checkValid() {
final Button buttonOk) { if (namePrefixTextField.isValid() && amountTextField.isValid() && tenantTextField.isValid()
if (tf1.isValid() && tf2.isValid() && tf3.isValid() && tf4.isValid()) { && pollDelayTextField.isValid()) {
buttonOk.setEnabled(true); buttonOk.setEnabled(true);
} else { } else {
buttonOk.setEnabled(false); buttonOk.setEnabled(false);
@@ -209,21 +195,10 @@ public class GenerateDialog extends Window {
protocolGroup.setItemCaption(Protocol.DDI_HTTP, "Direct Device Interface (HTTP poll)"); protocolGroup.setItemCaption(Protocol.DDI_HTTP, "Direct Device Interface (HTTP poll)");
protocolGroup.setNullSelectionAllowed(false); protocolGroup.setNullSelectionAllowed(false);
protocolGroup.select(Protocol.DMF_AMQP); protocolGroup.select(Protocol.DMF_AMQP);
protocolGroup.addValueChangeListener(new ValueChangeListener() { protocolGroup.addValueChangeListener(event -> {
private static final long serialVersionUID = 1L; pollDelayTextField.setVisible(!pollDelayTextField.isVisible());
pollUrlTextField.setVisible(!pollUrlTextField.isVisible());
@Override gatewayTokenTextField.setVisible(!gatewayTokenTextField.isVisible());
public void valueChange(final ValueChangeEvent event) {
if (event.getProperty().getValue().equals(Protocol.DDI_HTTP)) {
tf4.setVisible(true);
tf5.setVisible(true);
tf6.setVisible(true);
} else {
tf4.setVisible(false);
tf5.setVisible(false);
tf6.setVisible(false);
}
}
}); });
return protocolGroup; return protocolGroup;
} }
@@ -233,25 +208,41 @@ public class GenerateDialog extends Window {
final Button buttonOk = new Button("generate"); final Button buttonOk = new Button("generate");
buttonOk.setImmediate(true); buttonOk.setImmediate(true);
buttonOk.setIcon(FontAwesome.GEARS); buttonOk.setIcon(FontAwesome.GEARS);
buttonOk.addClickListener(new ClickListener() { buttonOk.addClickListener(event -> {
private static final long serialVersionUID = 1L; try {
callback.okButton(namePrefixTextField.getValue(), tenantTextField.getValue(),
@Override Integer.valueOf(amountTextField.getValue().replace(".", "").replace(",", "")),
public void buttonClick(final ClickEvent event) { Integer.valueOf(pollDelayTextField.getValue().replace(".", "")),
try { new URL(pollUrlTextField.getValue()), gatewayTokenTextField.getValue(),
callback.okButton(tf1.getValue(), tf3.getValue(), (Protocol) protocolGroup.getValue());
Integer.valueOf(tf2.getValue().replace(".", "").replace(",", "")), } catch (final NumberFormatException e) {
Integer.valueOf(tf4.getValue().replace(".", "")), new URL(tf5.getValue()), tf6.getValue(), LOGGER.info(e.getMessage(), e);
(Protocol) protocolGroup.getValue()); } catch (final MalformedURLException e) {
} catch (final NumberFormatException e) { LOGGER.info(e.getMessage(), e);
LOGGER.info(e.getMessage(), e);
} catch (final MalformedURLException e) {
LOGGER.info(e.getMessage(), e);
}
GenerateDialog.this.close();
} }
GenerateDialog.this.close();
}); });
return buttonOk; return buttonOk;
} }
private TextField createRequiredTextfield(final String caption, final String value, final Resource icon,
final boolean required, final Validator validator) {
final TextField textField = new TextField(caption, value);
textField.setIcon(icon);
textField.setRequired(required);
textField.addValidator(validator);
return textField;
}
private TextField createRequiredTextfield(final String caption, final Property dataSource, final Resource icon,
final boolean required, final Validator validator) {
final TextField textField = new TextField(caption, dataSource);
textField.setIcon(icon);
textField.setRequired(required);
textField.addValidator(validator);
return textField;
}
} }

View File

@@ -121,8 +121,8 @@ public class SimulatorView extends VerticalLayout implements View {
grid.getColumn("swversion").setHeaderCaption("SW Version"); grid.getColumn("swversion").setHeaderCaption("SW Version");
grid.getColumn("responseStatus").setHeaderCaption("Response Update Status"); grid.getColumn("responseStatus").setHeaderCaption("Response Update Status");
grid.getColumn("progress").setRenderer(new ProgressBarRenderer()); grid.getColumn("progress").setRenderer(new ProgressBarRenderer());
grid.getColumn("protocol").setConverter(getProtocolConverter()); grid.getColumn("protocol").setConverter(createProtocolConverter());
grid.getColumn("status").setRenderer(new HtmlRenderer(), getStatusConverter()); grid.getColumn("status").setRenderer(new HtmlRenderer(), createStatusConverter());
grid.removeColumn("tenant"); grid.removeColumn("tenant");
// grid combobox // grid combobox
@@ -266,7 +266,7 @@ public class SimulatorView extends VerticalLayout implements View {
})); }));
} }
private Converter<String, Protocol> getProtocolConverter() { private Converter<String, Protocol> createProtocolConverter() {
return new Converter<String, Protocol>() { return new Converter<String, Protocol>() {
@@ -304,7 +304,7 @@ public class SimulatorView extends VerticalLayout implements View {
} }
private Converter<String, Status> getStatusConverter() { private Converter<String, Status> createStatusConverter() {
return new Converter<String, Status>() { return new Converter<String, Status>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -317,32 +317,26 @@ public class SimulatorView extends VerticalLayout implements View {
@Override @Override
public String convertToPresentation(final Status value, final Class<? extends String> targetType, public String convertToPresentation(final Status value, final Class<? extends String> targetType,
final Locale locale) { final Locale locale) {
String style = null;
switch (value) { switch (value) {
case UNKNWON: case UNKNWON:
style = "<span class=\"v-icon grayicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY return "<span class=\"v-icon grayicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
+ ";\"color\":\"gray\";\">&#x" + ";\"color\":\"gray\";\">&#x"
+ Integer.toHexString(FontAwesome.QUESTION_CIRCLE.getCodepoint()) + ";</span>"; + Integer.toHexString(FontAwesome.QUESTION_CIRCLE.getCodepoint()) + ";</span>";
break;
case PEDNING: case PEDNING:
style = "<span class=\"v-icon yellowicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY return "<span class=\"v-icon yellowicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
+ ";\"color\":\"yellow\";\">&#x" + Integer.toHexString(FontAwesome.REFRESH.getCodepoint()) + ";\"color\":\"yellow\";\">&#x" + Integer.toHexString(FontAwesome.REFRESH.getCodepoint())
+ ";</span>"; + ";</span>";
break;
case FINISH: case FINISH:
style = "<span class=\"v-icon greenicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY return "<span class=\"v-icon greenicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
+ ";\"color\":\"green\";\">&#x" + ";\"color\":\"green\";\">&#x"
+ Integer.toHexString(FontAwesome.CHECK_CIRCLE.getCodepoint()) + ";</span>"; + Integer.toHexString(FontAwesome.CHECK_CIRCLE.getCodepoint()) + ";</span>";
break;
case ERROR: case ERROR:
style = "<span class=\"v-icon redicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY return "<span class=\"v-icon redicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
+ ";\"color\":\"red\";\">&#x" + ";\"color\":\"red\";\">&#x"
+ Integer.toHexString(FontAwesome.EXCLAMATION_CIRCLE.getCodepoint()) + ";</span>"; + Integer.toHexString(FontAwesome.EXCLAMATION_CIRCLE.getCodepoint()) + ";</span>";
break;
default: default:
throw new IllegalStateException("unknown value"); throw new IllegalStateException("unknown value");
} }
return style;
} }
@Override @Override

View File

@@ -389,19 +389,19 @@ public class DeploymentManagement {
softwareModules)); softwareModules));
} }
private Action createTargetAction(final Map<String, TargetWithActionType> targetsWithActionMap, final Target t, private Action createTargetAction(final Map<String, TargetWithActionType> targetsWithActionMap, final Target target,
final DistributionSet set, final Rollout rollout, final RolloutGroup rolloutGroup) { final DistributionSet set, final Rollout rollout, final RolloutGroup rolloutGroup) {
final Action tAction = new Action(); final Action actionForTarget = new Action();
final TargetWithActionType targetWithActionType = targetsWithActionMap.get(t.getControllerId()); final TargetWithActionType targetWithActionType = targetsWithActionMap.get(target.getControllerId());
tAction.setActionType(targetWithActionType.getActionType()); actionForTarget.setActionType(targetWithActionType.getActionType());
tAction.setForcedTime(targetWithActionType.getForceTime()); actionForTarget.setForcedTime(targetWithActionType.getForceTime());
tAction.setActive(true); actionForTarget.setActive(true);
tAction.setStatus(Status.RUNNING); actionForTarget.setStatus(Status.RUNNING);
tAction.setTarget(t); actionForTarget.setTarget(target);
tAction.setDistributionSet(set); actionForTarget.setDistributionSet(set);
tAction.setRollout(rollout); actionForTarget.setRollout(rollout);
tAction.setRolloutGroup(rolloutGroup); actionForTarget.setRolloutGroup(rolloutGroup);
return tAction; return actionForTarget;
} }
/** /**