/** * This method adds listeners for the {@link SceneLifecycle}. */ static void addSceneLifecycleHooks(ViewModel viewModel, ObservableBooleanValue viewInSceneProperty) { if(viewModel != null) { if(viewModel instanceof SceneLifecycle) { SceneLifecycle lifecycleViewModel = (SceneLifecycle) viewModel; PreventGarbageCollectionStore.getInstance().put(viewInSceneProperty); viewInSceneProperty.addListener((observable, oldValue, newValue) -> { if(newValue) { lifecycleViewModel.onViewAdded(); } else { lifecycleViewModel.onViewRemoved(); PreventGarbageCollectionStore.getInstance().remove(viewInSceneProperty); } }); } } }
@Override @FXThread protected @NotNull ObservableBooleanValue buildAdditionalDisableCondition() { final VirtualResourceTree<C> resourceTree = getResourceTree(); final MultipleSelectionModel<TreeItem<VirtualResourceElement<?>>> selectionModel = resourceTree.getSelectionModel(); final ReadOnlyObjectProperty<TreeItem<VirtualResourceElement<?>>> selectedItemProperty = selectionModel.selectedItemProperty(); final Class<C> type = getObjectsType(); final BooleanBinding typeCondition = new BooleanBinding() { @Override protected boolean computeValue() { final TreeItem<VirtualResourceElement<?>> treeItem = selectedItemProperty.get(); return treeItem == null || !type.isInstance(treeItem.getValue().getObject()); } @Override public Boolean getValue() { return computeValue(); } }; return Bindings.or(selectedItemProperty.isNull(), typeCondition); }
/** * Generate the table layout for table selectors. * The method should be overridden when the table layout needs to be customised. * @param context the context for preparing the controller. * @return the table layout. */ protected BODTOTable<T> getTable(ViewContextBase context) { BODTOTable<T> table = new BODTOTable<>(); configureCommands(table.getCommands()); table.getCommands().end(); context.autoInjectBean(table); UserPermissions userPermissions = getUserPermissions(); Iterable<String> columns = getTableColumns().stream() .filter(s -> Optional.ofNullable(userPermissions.isVisible(s)).map(ObservableBooleanValue::get).orElse(true)) ::iterator; TableColumnBinding<BODTO<T>> tcb = new TableColumnBinding<>(BeanUtils.getBeanInfo(toClass), columns, "id"::equals); tcb.setConverterFactory(this::getConverter); Bindings.bindContent(table.getTable().getColumns(), tcb); table.getCommands() .cru() .end(); refresh(table, context); table.addQueryListener(o -> refresh(table, context)); table.addFlowLifecycle(new FlowLifeCycle.OnRefresh((c,f) -> refresh(table, context))); return table; }
/** * Generate the table layout for table selectors. * The method should be overridden when the table layout needs to be customised. * @param context the context for preparing the controller. * @return the table layout. */ protected CrudTable<T> getTable(ViewContextBase context) { CrudTable<T> table = new CrudTable<>(); configureCommands(table.getCommands()); table.getCommands().end(); UserPermissions userPermissions = getUserPermissions(); Iterable<String> columns = getTableColumns().stream() .filter(s -> Optional.ofNullable(userPermissions.isVisible(s)).map(ObservableBooleanValue::get).orElse(true)) ::iterator; TableColumnBinding<T> tcb = new TableColumnBinding<>(getBeanInfo(), columns, "id"::equals); tcb.setConverterFactory(this::getConverter); Bindings.bindContent(table.getTable().getColumns(), tcb); table.getCommands() .crud(isCreateAllowed(), true, true, true, isDeleteAllowed()) .end(); context.autoInjectBean(table); Runnable refreshData = () -> refresh(table, context); refreshData.run(); table.addQueryListener(o -> refreshData.run()); return table; }
@SuppressWarnings("unchecked") @Override public void bind(EditorHelper e, String id, BasicEntityEditor node) { ObservableValue value = e.getValueProperty(id); Class type = e.getValueClass(id); if (BasicEntity.class.isAssignableFrom(type)) { node.configure(getContext(), type); } ObservableBooleanValue editable = e.getEditableProperty(id, value); ObservableBooleanValue visible = e.getVisibleProperty(id, value); StringConverter converter = e.getConverter(id, value); if (converter != null) node.setConverter(converter); bindBidirectionalValue(node, node.valueProperty(), value); if (editable != null) node.disableProperty().bind(BooleanExpression.booleanExpression(editable).not()); if (visible != null) node.visibleProperty().bind(visible); e.onBind(id, value, node); }
/** * Reconstruct the scene graph * */ private void rebuildSceneGraph( final long t, final ObservableBooleanValue isDebug, final ObservableList<Node> graph ) { graph.clear(); terrain.iterateTiles(currentAngle).forEachRemaining(tile -> { final Point2D l = terrain.correctedIsoCoord(tile.pos, currentAngle); tile.rebuildSceneGraph(isDebug, currentAngle); tile.subGraph.setTranslateX(l.getX()); tile.subGraph.setTranslateY(l.getY()); graph.add(tile.subGraph); }); for (final Sprite s : allSprites) s.invalidate(); }
/** * Returns a new observable string which contains either the contents of ifTrue, or ifFalse, depending on the condition * @param condition * @param ifTrue * @param ifFalse * @return */ public static ObservableStringValue decision(ObservableBooleanValue condition, ObservableStringValue ifTrue, ObservableStringValue ifFalse) { StringProperty ret = new SimpleStringProperty(); condition.addListener((obs, ov, nv) -> { ret.set(nv ? ifTrue.get() : ifFalse.get()); }); ifTrue.addListener((obs, ov, nv) -> { if (condition.get()) { ret.set(nv); } }); ifFalse.addListener((obs, ov, nv) -> { if (!condition.get()) { ret.set(nv); } }); ret.set(condition.get() ? ifTrue.get() : ifFalse.get()); return ret; }
@Override public void handle(MouseEvent e) { adjustingTooltip.set(true); Node chartNode = (Node) e.getSource(); tooltip.show(chartNode, e.getScreenX(), e.getScreenY()); setLabelPosition(e); ObservableBooleanValue stillHovering = chartNode.hoverProperty().or(adjustingTooltip); stillHovering.addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean nowHovering) { if (!nowHovering) { stillHovering.removeListener(this); tooltip.hide(); } } }); T chartData = (T) chartNode.getUserData(); String txt = textProvider.apply(chartData); tooltip.setText(txt); adjustingTooltip.set(false); }
public static ObservableBooleanValue notEmpty(ObservableValue<String> source) { return Bindings.createBooleanBinding(() -> { final String s = source.getValue(); return s != null && !s.trim().isEmpty(); }, source); }
private FXMLLoader createFxmlLoader(String resource, ResourceBundle resourceBundle, View<?> codeBehind, Object root, ViewModel viewModel, ContextImpl context, ObservableBooleanValue viewInSceneProperty) throws IOException { // Load FXML file final URL location = FxmlViewLoader.class.getResource(resource); if (location == null) { throw new IOException("Error loading FXML - can't load from given resourcepath: " + resource); } final FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setRoot(root); fxmlLoader.setResources(resourceBundle); fxmlLoader.setLocation(location); // when the user provides a viewModel but no codeBehind, we need to use // the custom controller factory. // in all other cases the default factory can be used. if (viewModel != null && codeBehind == null) { fxmlLoader .setControllerFactory(new ControllerFactoryForCustomViewModel(viewModel, resourceBundle, context, viewInSceneProperty)); } else { fxmlLoader.setControllerFactory(new DefaultControllerFactory(resourceBundle, context, viewInSceneProperty)); } // When the user provides a codeBehind instance we take care of the // injection of the viewModel to this // controller here. if (codeBehind != null) { fxmlLoader.setController(codeBehind); if (viewModel == null) { handleInjection(codeBehind, resourceBundle, context, viewInSceneProperty); } else { handleInjection(codeBehind, resourceBundle, viewModel, context, viewInSceneProperty); } } return fxmlLoader; }
private static void handleInjection(View<?> codeBehind, ResourceBundle resourceBundle, ContextImpl context, ObservableBooleanValue viewInSceneProperty) { ResourceBundleInjector.injectResourceBundle(codeBehind, resourceBundle); Consumer<ViewModel> newVmConsumer = viewModel -> { ResourceBundleInjector.injectResourceBundle(viewModel, resourceBundle); ViewLoaderReflectionUtils.createAndInjectScopes(viewModel, context); ViewLoaderReflectionUtils.initializeViewModel(viewModel); ViewLoaderReflectionUtils.addSceneLifecycleHooks(viewModel, viewInSceneProperty); }; ViewLoaderReflectionUtils.createAndInjectViewModel(codeBehind, newVmConsumer); ViewLoaderReflectionUtils.injectContext(codeBehind, context); }
private static void handleInjection(View<?> codeBehind, ResourceBundle resourceBundle, ViewModel viewModel, ContextImpl context, ObservableBooleanValue viewInSceneProperty) { ResourceBundleInjector.injectResourceBundle(codeBehind, resourceBundle); if (viewModel != null) { ResourceBundleInjector.injectResourceBundle(viewModel, resourceBundle); ViewLoaderReflectionUtils.createAndInjectScopes(viewModel, context); ViewLoaderReflectionUtils.injectViewModel(codeBehind, viewModel); ViewLoaderReflectionUtils.injectContext(codeBehind, context); ViewLoaderReflectionUtils.addSceneLifecycleHooks(viewModel, viewInSceneProperty); } }
public ControllerFactoryForCustomViewModel(ViewModel customViewModel, ResourceBundle resourceBundle, ContextImpl context, ObservableBooleanValue viewInSceneProperty) { this.customViewModel = customViewModel; this.resourceBundle = resourceBundle; this.context = context; this.viewInSceneProperty = viewInSceneProperty; }
/** * A more general version of {@link Bindings#when(ObservableBooleanValue)} * that can accept general boolean properties as conditions. * * @param condition the condition to bind to * * @see Bindings#when(ObservableBooleanValue) */ public static When when(Property<Boolean> condition) { if (condition instanceof ObservableBooleanValue) { return Bindings.when((ObservableBooleanValue) condition); } SimpleBooleanProperty realCondition = new SimpleBooleanProperty(); realCondition.bind(condition); return Bindings.when(realCondition); }
private static ObservableBooleanValue allTrue(BooleanProperty... properties) { ObservableBooleanValue accu = new SimpleBooleanProperty(true); for (int i = 0; i < properties.length; i++) { accu = properties[i].and(accu); } return accu; }
@Override @FXThread protected @NotNull ObservableBooleanValue buildAdditionalDisableCondition() { final ResourceTree resourceTree = getResourceTree(); final MultipleSelectionModel<TreeItem<ResourceElement>> selectionModel = resourceTree.getSelectionModel(); final ReadOnlyObjectProperty<TreeItem<ResourceElement>> selectedItemProperty = selectionModel.selectedItemProperty(); return selectedItemProperty.isNull(); }
@Override @FXThread protected @NotNull ObservableBooleanValue buildAdditionalDisableCondition() { final ComboBox<String> comboBox = getTextureParamNameComboBox(); final SingleSelectionModel<String> selectionModel = comboBox.getSelectionModel(); final ReadOnlyObjectProperty<String> itemProperty = selectionModel.selectedItemProperty(); final ObservableBooleanValue parent = super.buildAdditionalDisableCondition(); return Bindings.and(parent, itemProperty.isNull().or(itemProperty.isEqualTo(""))); }
public void apply(ObservableBooleanValue expanded, ObservableBooleanValue animate, Region region) { Rectangle clip = new Rectangle(); clip.widthProperty().bind(region.widthProperty()); clip.heightProperty().bind(region.heightProperty()); region.setClip(clip); region.setMinHeight(0); region.setMaxHeight(expanded.get() ? Region.USE_PREF_SIZE : 0); region.visibleProperty().bind(region.heightProperty().greaterThan(0)); expanded.addListener((v, o, n) -> onExpandedChanged(n, animate.get(), region)); }
public void apply(ObservableBooleanValue trigger, Node node) { this.trigger = trigger; this.node = node; configureTransition(node); node.getProperties().put(ANIMATION_TRIGGER_KEY, trigger); // Prevents trigger from being garbage collected. trigger.addListener(triggerChangeListener); node.setOpacity(trigger.get() ? to : from); }
@Override public ObservableBooleanValue isEditable(String colName) { if ("created".equals(colName)) return ObservableConstant.FALSE; if ("modified".equals(colName)) return ObservableConstant.FALSE; if (isLocked.getAsBoolean()) return ObservableConstant.FALSE; return permissions.isEditable(colName); }
public ObservableBooleanValue getEditableProperty(String id, ObservableValue<?> property) { PropertyDescriptor pds = getPropertyDescriptor(id); if (pds.getWriteMethod() == null) { return ObservableConstant.FALSE; } else if (pds.getReadMethod().isAnnotationPresent(Id.class)) { return ObservableConstant.FALSE; } ObservableBooleanValue up = editor.getUserPermissions().isEditable(id); return up; }
private static <T> ObservableList<T> filterInternal(ObservableList<T> items, Function<T, ObservableBooleanValue> conditionExtractor, final Predicate<T> predicate) { final ObservableList<T> filteredItems = FXCollections.observableArrayList(); final InvalidationListener listener = observable -> { final List<T> completed = items.stream() .filter(predicate) .collect(Collectors.toList()); filteredItems.setAll(completed); }; items.addListener((ListChangeListener<T>) c -> { c.next(); listener.invalidated(null); if (c.wasAdded()) { c.getAddedSubList() .forEach(item -> conditionExtractor.apply(item).addListener(listener)); } if (c.wasRemoved()) { c.getRemoved() .forEach(item -> conditionExtractor.apply(item).removeListener(listener)); } }); return filteredItems; }
/** * Creates a boolean property that is bound to another boolean value * of the active editor. */ private BooleanProperty createActiveBooleanProperty(Function<FileEditor, ObservableBooleanValue> func) { BooleanProperty b = new SimpleBooleanProperty(); FileEditor fileEditor = fileEditorTabPane.getActiveFileEditor(); if (fileEditor != null) b.bind(func.apply(fileEditor)); fileEditorTabPane.activeFileEditorProperty().addListener((observable, oldFileEditor, newFileEditor) -> { b.unbind(); if (newFileEditor != null) b.bind(func.apply(newFileEditor)); else b.set(false); }); return b; }
/** * Creates a boolean property that is bound to another boolean value * of the active editor's SmartEdit. */ private BooleanProperty createActiveEditBooleanProperty(Function<SmartEdit, ObservableBooleanValue> func) { BooleanProperty b = new SimpleBooleanProperty() { @Override public void set(boolean newValue) { // invoked when the user invokes an action // do not try to change SmartEdit properties because this // would throw a "bound value cannot be set" exception } }; ChangeListener<? super FileEditor> listener = (observable, oldFileEditor, newFileEditor) -> { b.unbind(); if (newFileEditor != null) { if (newFileEditor.getEditor() != null) b.bind(func.apply(newFileEditor.getEditor().getSmartEdit())); else { newFileEditor.editorProperty().addListener((ob, o, n) -> { b.bind(func.apply(n.getSmartEdit())); }); } } else b.set(false); }; FileEditor fileEditor = fileEditorTabPane.getActiveFileEditor(); listener.changed(null, null, fileEditor); fileEditorTabPane.activeFileEditorProperty().addListener(listener); return b; }
public Action(String text, String accelerator, GlyphIcons icon, EventHandler<ActionEvent> action, ObservableBooleanValue disable, BooleanProperty selected) { this.text = text; this.accelerator = (accelerator != null) ? KeyCombination.valueOf(accelerator) : null; this.icon = icon; this.action = action; this.disable = disable; this.selected = selected; }
AddRemoveStringItem( final DOC column, final String label, final StringProperty value, final String tooltip, final ObservableBooleanValue enableThis) { super(label, tooltip, NO_DECORATOR); final String currentValue = value.get(); if (currentValue == null) { this.strings = FXCollections.observableArrayList(); } else { this.strings = FXCollections.observableArrayList( Stream.of(currentValue.split(",")) .filter(s -> !s.isEmpty()) .toArray(String[]::new) ); } this.column = requireNonNull(column); this.enabled = enableThis; this.cache = new SimpleStringProperty(); this.strings.addListener((ListChangeListener.Change<? extends String> c) -> { @SuppressWarnings("unchecked") final List<String> list = (List<String>) c.getList(); value.setValue(getFormatedString(list)); }); }
@Test public void testObservableBooleanValue(){ ObservableBooleanValue actual = new SimpleBooleanProperty(true); assertThat(actual).isTrue(); assertThat(actual).hasSameValue(actual); }
@Test public void should_fail_if_actual_is_true(){ try{ ObservableBooleanValue actual = new SimpleBooleanProperty(true); new ObservableBooleanValueAssertions(actual).isFalse(); fail("Should throw an AssertionError"); }catch (AssertionError error){ assertThat(error).hasMessageContaining("to be false but it wasn't"); } }
@Test public void should_fail_if_actual_is_false(){ try{ ObservableBooleanValue actual = new SimpleBooleanProperty(false); new ObservableBooleanValueAssertions(actual).isTrue(); fail("Should throw an AssertionError"); }catch (AssertionError error){ assertThat(error).hasMessageContaining("to be true but it wasn't"); } }
/** * @deprecated Use {@link Val#suspendable(javafx.beans.value.ObservableValue)}. */ @Deprecated public static BooleanBinding wrap(ObservableBooleanValue source) { return new BooleanBinding() { { bind(source); } @Override protected boolean computeValue() { return source.get(); } }; }
public static <T> ObservableBooleanValue fromPredicate(ObservableValue<T> source, Predicate<T> predicate) { return Bindings.createBooleanBinding(() -> predicate.test(source.getValue()), source); }
public static ObservableBooleanValue matches(ObservableValue<String> source, Pattern pattern) { return Bindings.createBooleanBinding(() -> { final String s = source.getValue(); return s != null && pattern.matcher(s).matches(); }, source); }
public DefaultControllerFactory(ResourceBundle resourceBundle, ContextImpl context, ObservableBooleanValue viewInSceneProperty) { this.resourceBundle = resourceBundle; this.context = context; this.viewInSceneProperty = viewInSceneProperty; }
public void bindVisibilityTo(ObservableBooleanValue value) { visibleProperty().bind(value); }