List of usage examples for javafx.collections WeakListChangeListener WeakListChangeListener
public WeakListChangeListener(@NamedArg("listener") ListChangeListener<E> listener)
From source file:de.perdian.apps.tagtiger.fx.handlers.batchupdate.UpdateFileNamesFromTagsActionEventHandler.java
@Override protected BatchUpdateDialog createDialog() { ObservableList<UpdateFileNamesFromTagsItem> items = FXCollections.observableArrayList( this.getOtherFiles().stream().map(UpdateFileNamesFromTagsItem::new).collect(Collectors.toList())); StringProperty patternFieldProperty = new SimpleStringProperty(); List<String> patternItems = Arrays.asList("${track} ${title}"); ComboBox<String> patternBox = new ComboBox<>(FXCollections.observableArrayList(patternItems)); patternBox.setEditable(true);/*from w w w. ja va2 s . c o m*/ patternBox.setMaxWidth(Double.MAX_VALUE); Bindings.bindBidirectional(patternFieldProperty, patternBox.editorProperty().get().textProperty()); HBox.setHgrow(patternBox, Priority.ALWAYS); Button executeButton = new Button(this.getLocalization().executeRename(), new ImageView(new Image(UpdateFileNamesFromTagsActionEventHandler.class.getClassLoader() .getResourceAsStream("icons/16/save.png")))); executeButton.setDisable(true); patternFieldProperty .addListener((o, oldValue, newValue) -> executeButton.setDisable(newValue.length() <= 0)); HBox patternFieldPane = new HBox(10, patternBox, executeButton); patternFieldPane.setPadding(new Insets(5, 5, 5, 5)); TitledPane patternFieldTitlePane = new TitledPane(this.getLocalization().fileNamePattern(), patternFieldPane); patternFieldTitlePane.setCollapsible(false); TableView<?> newFileNamesPane = this.createNewFileNamesPane(items); newFileNamesPane.setPrefHeight(400); VBox.setVgrow(newFileNamesPane, Priority.ALWAYS); VBox actionPane = new VBox(10, patternFieldTitlePane, newFileNamesPane); // Create the dialog and finish BatchUpdateDialog dialog = new BatchUpdateDialog(); dialog.setDialogPrefWidth(800); dialog.setDialogTitle(this.getLocalization().updateFileNames()); dialog.setActionPane(actionPane); dialog.setLegendPane(this.createLegendPane()); // Add listeners this.getOtherFiles().addListener( new WeakListChangeListener<>((Change<? extends TaggableFile> change) -> items.setAll(change .getList().stream().map(UpdateFileNamesFromTagsItem::new).collect(Collectors.toList())))); patternFieldProperty.addListener((o, oldValue, newValue) -> this.computeNewFileNames(items, newValue)); executeButton.setOnAction(event -> { this.updateNewFileNames(items); ((Stage) executeButton.getScene().getWindow()).close(); }); return dialog; }
From source file:gov.va.isaac.gui.ConceptNode.java
/** * descriptionReader is optional/*from w ww . ja v a 2s. c o m*/ */ public ConceptNode(ConceptVersionBI initialConcept, boolean flagAsInvalidWhenBlank, ObservableList<SimpleDisplayConcept> dropDownOptions, Function<ConceptVersionBI, String> descriptionReader) { c_ = initialConcept; //We can't simply use the ObservableList from the CommonlyUsedConcepts, because it infinite loops - there doesn't seem to be a way //to change the items in the drop down without changing the selection. So, we have this hack instead. listChangeListener_ = new ListChangeListener<SimpleDisplayConcept>() { @Override public void onChanged(Change<? extends SimpleDisplayConcept> c) { //TODO I still have an infinite loop here. Find and fix. logger.debug("updating concept dropdown"); disableChangeListener_ = true; SimpleDisplayConcept temp = cb_.getValue(); cb_.setItems(FXCollections.observableArrayList(dropDownOptions_)); cb_.setValue(temp); cb_.getSelectionModel().select(temp); disableChangeListener_ = false; } }; descriptionReader_ = (descriptionReader == null ? (conceptVersion) -> { return conceptVersion == null ? "" : OTFUtility.getDescription(conceptVersion); } : descriptionReader); dropDownOptions_ = dropDownOptions == null ? AppContext.getService(CommonlyUsedConcepts.class).getObservableConcepts() : dropDownOptions; dropDownOptions_.addListener(new WeakListChangeListener<SimpleDisplayConcept>(listChangeListener_)); conceptBinding_ = new ObjectBinding<ConceptVersionBI>() { @Override protected ConceptVersionBI computeValue() { return c_; } }; flagAsInvalidWhenBlank_ = flagAsInvalidWhenBlank; cb_ = new ComboBox<>(); cb_.setConverter(new StringConverter<SimpleDisplayConcept>() { @Override public String toString(SimpleDisplayConcept object) { return object == null ? "" : object.getDescription(); } @Override public SimpleDisplayConcept fromString(String string) { return new SimpleDisplayConcept(string, 0); } }); cb_.setValue(new SimpleDisplayConcept("", 0)); cb_.setEditable(true); cb_.setMaxWidth(Double.MAX_VALUE); cb_.setPrefWidth(ComboBox.USE_COMPUTED_SIZE); cb_.setMinWidth(200.0); cb_.setPromptText("Type, drop or select a concept"); cb_.setItems(FXCollections.observableArrayList(dropDownOptions_)); cb_.setVisibleRowCount(11); cm_ = new ContextMenu(); MenuItem copyText = new MenuItem("Copy Description"); copyText.setGraphic(Images.COPY.createImageView()); copyText.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { CustomClipboard.set(cb_.getEditor().getText()); } }); cm_.getItems().add(copyText); CommonMenusNIdProvider nidProvider = new CommonMenusNIdProvider() { @Override public Set<Integer> getNIds() { Set<Integer> nids = new HashSet<>(); if (c_ != null) { nids.add(c_.getNid()); } return nids; } }; CommonMenuBuilderI menuBuilder = CommonMenus.CommonMenuBuilder.newInstance(); menuBuilder.setInvisibleWhenFalse(isValid); CommonMenus.addCommonMenus(cm_, menuBuilder, nidProvider); cb_.getEditor().setContextMenu(cm_); updateGUI(); new LookAheadConceptPopup(cb_); if (cb_.getValue().getNid() == 0) { if (flagAsInvalidWhenBlank_) { isValid.setInvalid("Concept Required"); } } else { isValid.setValid(); } cb_.valueProperty().addListener(new ChangeListener<SimpleDisplayConcept>() { @Override public void changed(ObservableValue<? extends SimpleDisplayConcept> observable, SimpleDisplayConcept oldValue, SimpleDisplayConcept newValue) { if (newValue == null) { logger.debug("Combo Value Changed - null entry"); } else { logger.debug("Combo Value Changed: {} {}", newValue.getDescription(), newValue.getNid()); } if (disableChangeListener_) { logger.debug("change listener disabled"); return; } if (newValue == null) { //This can happen if someone calls clearSelection() - it passes in a null. cb_.setValue(new SimpleDisplayConcept("", 0)); return; } else { if (newValue.shouldIgnoreChange()) { logger.debug("One time change ignore"); return; } //Whenever the focus leaves the combo box editor, a new combo box is generated. But, the new box will have 0 for an id. detect and ignore if (oldValue != null && oldValue.getDescription().equals(newValue.getDescription()) && newValue.getNid() == 0) { logger.debug("Not a real change, ignore"); newValue.setNid(oldValue.getNid()); return; } lookup(); } } }); AppContext.getService(DragRegistry.class).setupDragAndDrop(cb_, new SingleConceptIdProvider() { @Override public String getConceptId() { return cb_.getValue().getNid() + ""; } }, true); pi_ = new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS); pi_.visibleProperty().bind(isLookupInProgress_); pi_.setPrefHeight(16.0); pi_.setPrefWidth(16.0); pi_.setMaxWidth(16.0); pi_.setMaxHeight(16.0); lookupFailImage_ = Images.EXCLAMATION.createImageView(); lookupFailImage_.visibleProperty().bind(isValid.not().and(isLookupInProgress_.not())); Tooltip t = new Tooltip(); t.textProperty().bind(isValid.getReasonWhyInvalid()); Tooltip.install(lookupFailImage_, t); StackPane sp = new StackPane(); sp.setMaxWidth(Double.MAX_VALUE); sp.getChildren().add(cb_); sp.getChildren().add(lookupFailImage_); sp.getChildren().add(pi_); StackPane.setAlignment(cb_, Pos.CENTER_LEFT); StackPane.setAlignment(lookupFailImage_, Pos.CENTER_RIGHT); StackPane.setMargin(lookupFailImage_, new Insets(0.0, 30.0, 0.0, 0.0)); StackPane.setAlignment(pi_, Pos.CENTER_RIGHT); StackPane.setMargin(pi_, new Insets(0.0, 30.0, 0.0, 0.0)); hbox_ = new HBox(); hbox_.setSpacing(5.0); hbox_.setAlignment(Pos.CENTER_LEFT); hbox_.getChildren().add(sp); HBox.setHgrow(sp, Priority.SOMETIMES); }