JFaceRx { public static @SwtThread RxBox<Boolean> toggle(IAction action) { Preconditions.checkArgument(SwtMisc.flagIsSet(IAction.AS_CHECK_BOX, action.getStyle()) || SwtMisc.flagIsSet(IAction.AS_RADIO_BUTTON, action.getStyle())); RxBox<Boolean> box = RxBox.of(action.isChecked()); action.addPropertyChangeListener(e -> { if ("checked".equals(e.getProperty())) { box.set((Boolean) e.getNewValue()); } }); Rx.subscribe(box, isChecked -> { action.setChecked(isChecked); }); return box; }  static @SwtThread RxBox<Boolean> toggle(IAction action);  }
@Test public void testToggle() { InteractiveTest.testCoat("When you check and uncheck the action, the state of the selected text should change.", 20, SWT.DEFAULT, root -> { Layouts.setGrid(root); IAction action = Actions.builder() .setText("Action") .setStyle(Style.CHECK) .build(); RxBox<Boolean> selection = JFaceRx.toggle(action); { Group cmp = new Group(root, SWT.SHADOW_ETCHED_IN); cmp.setText("Under test"); Layouts.setGridData(cmp).grabHorizontal(); Layouts.setFill(cmp); ToolBarManager manager = new ToolBarManager(); manager.createControl(cmp); manager.add(action); manager.update(true); } { Group cmp = new Group(root, SWT.NONE); cmp.setText("Read"); Layouts.setGridData(cmp).grabHorizontal(); Layouts.setFill(cmp); Label selectedTxt = new Label(cmp, SWT.BORDER); Rx.subscribe(selection, s -> selectedTxt.setText(Boolean.toString(s))); } { Group cmp = new Group(root, SWT.NONE); cmp.setText("Write"); Layouts.setGridData(cmp).grabHorizontal(); Layouts.setFill(cmp); Button setFalse = new Button(cmp, SWT.PUSH); setFalse.setText("set false"); setFalse.addListener(SWT.Selection, e -> selection.set(false)); Button setTrue = new Button(cmp, SWT.PUSH); setTrue.setText("set true"); setTrue.addListener(SWT.Selection, e -> selection.set(true)); } }); }