/** * {@inheritDoc} */ @Override public Control createControl(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); GridLayout gridLayout = new GridLayout(2, false); gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; composite.setLayout(gridLayout); spinner = new Spinner(composite, SWT.BORDER | SWT.RIGHT); spinner.setValues((int) (property.getValue().floatValue() * 100), 0, 100, 0, 1, 5); spinner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); spinner.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { int selection = spinner.getSelection(); Float value = Float.valueOf(selection / 100f); sendPropertyUpdateEvent(value); } }); Label percentage = new Label(composite, SWT.NONE); percentage.setText("%"); percentage.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); return composite; }
@Test public void floatPropertyUpdateXmlSaveLoad() throws PropertyValidationException, JAXBException { float value = new Random().nextInt(100); if (1 == value) { // make sure we don't update with same value value++; } PercentageProperty floatProperty = new PercentageProperty("name", "description", "logical-name", Float.valueOf(1), false, false); AbstractPropertyUpdate<Float> floatPropertyUpdate = floatProperty.createAndValidatePropertyUpdate(Float.valueOf(value)); JAXBContext context = JAXBContext.newInstance(PercentagePropertyUpdate.class); Marshaller marshaller = context.createMarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller(); marshaller.marshal(floatPropertyUpdate, f); Object object = unmarshaller.unmarshal(f); assertThat(object, is(instanceOf(PercentagePropertyUpdate.class))); PercentagePropertyUpdate unmarsalled = (PercentagePropertyUpdate) object; assertThat(floatProperty.canUpdate(unmarsalled), is(true)); floatProperty.setValue(unmarsalled.getUpdateValue()); assertThat(floatProperty.getValue().floatValue(), is(value)); }