List of usage examples for java.util Optional ifPresent
public void ifPresent(Consumer<? super T> action)
From source file:ubicrypt.ui.ctrl.HomeController.java
@PostConstruct public void init() { gProgress = new GeneralProgress(inProgress, inProgressMessage); treeView.setCellFactory(treeView1 -> new TreeCellFactory(treeView1, fileUntracker, appEvents, gProgress)); addProvider.setOnMouseClicked(event -> ctx.browse("selectProvider")); addFile.setOnMouseClicked(event -> { if (!localConfig.getProviders().stream().findAny().isPresent()) { ctx.browse("selectProvider"); return; }//from w w w .j a va2 s .c o m fileAdder.accept(emptyPath); }); settings.setOnMouseClicked(event -> ctx.browse("settings")); filesRoot = new TreeItem<>(new RootFilesItem(event -> fileAdder.accept(emptyPath))); TreeItem<ITreeItem> root = new TreeItem<>(); treeView.setRoot(root); root.getChildren().add(filesRoot); treeView.setShowRoot(false); localConfig.getLocalFiles().stream().filter(Utils.ignoredFiles) .forEach(localFile -> addFiles(localFile.getPath().iterator(), basePath, filesRoot, localFile)); //providers providersRoot = new TreeItem<>(new RootProvidersItem()); root.getChildren().add(providersRoot); localConfig.getProviders().stream().forEach(providerAdder); //provider status events providerEvent.subscribe(pevent -> { switch (pevent.getEvent()) { case added: log.info("new provider added:{}", pevent.getHook().getProvider()); final Optional<TreeItem<ITreeItem>> optItem = providersRoot.getChildren().stream() .filter(item -> ((ProviderItem) item.getValue()).getProvider() .equals(pevent.getHook().getProvider())) .findFirst(); if (!optItem.isPresent()) { providerAdder.accept(pevent.getHook().getProvider()); } pevent.getHook().getStatusEvents().subscribe(event -> { Function<String, String> classLabel; log.info("provider status {}:{}", event, pevent.getHook().getProvider()); switch (event) { case error: classLabel = code -> format("tree-provider-%s-error", code); break; default: //TODO:labels for other statuses classLabel = code -> format("tree-provider-%s", code); } optItem.ifPresent(item -> { final ProviderItem providerItem = (ProviderItem) item.getValue(); final Node graphics = providerItem.getGraphics(); graphics.getStyleClass().clear(); providerDescriptors.stream() .filter(pd -> pd.getType().equals(providerItem.getProvider().getClass())) .map(ProviderDescriptor::getCode).findFirst() .ifPresent(code -> graphics.getStyleClass().add(classLabel.apply(code))); }); }); break; case removed: //TODO: remove provider break; default: log.warn("unmanaged event:{}", pevent.getEvent()); } }); //remote file events fileEvents.filter(fileEvent -> fileEvent.getLocation() == FileEvent.Location.remote) .subscribe(fileEvent -> { log.debug("file remote event:{}", fileEvent); //update file icon final UbiFile<UbiFile> file = fileEvent.getFile(); Observable.create(fileInSync.call(file)).subscribe(res -> { searchFile(filesRoot, file).ifPresent(treeView -> { final Node graphics = treeView.getValue().getGraphics(); graphics.getStyleClass().clear(); graphics.getStyleClass().add(format("tree-file-saved-%s", res)); }); }); }); //local file events fileEvents.filter(fileEvent -> fileEvent.getLocation() == FileEvent.Location.local && fileEvent.getType() == FileEvent.Type.created).subscribe(fileEvent -> { log.debug("file local event:{}", fileEvent); localConfig.getLocalFiles().stream().filter(fileEvent.getFile()::equals).findFirst().ifPresent( fe -> addFiles(fileEvent.getFile().getPath().iterator(), basePath, filesRoot, fe)); searchFile(filesRoot, fileEvent.getFile()).ifPresent(treeView -> { final Node graphics = treeView.getValue().getGraphics(); graphics.getStyleClass().clear(); graphics.getStyleClass().add(format("tree-file-saved-%s", true)); }); }); //file progress monitor progressEvents.subscribe(progress -> { Platform.runLater(() -> { if (progress.isCompleted()) { log.debug("progress completed"); if (!filesInProgress.remove(progress)) { log.warn("progress not tracked. progress file:{}, element:{}", progress.getProvenience().getFile()); } Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), ae -> { progressFile.setText(""); progressProvider.setText(""); progressBar.setProgress(0D); })); timeline.play(); } else { filesInProgress.add(progress); } if (filesInProgress.isEmpty()) { // footer.setVisible(false); return; } footer.setVisible(true); filesInProgress.stream().findFirst().ifPresent(pr -> { progressFile.setText(StringUtils .abbreviate(pr.getProvenience().getFile().getPath().getFileName().toString(), 30)); progressProvider.setText(StringUtils.abbreviate(pr.getTarget().toString(), 30)); progressBar.setProgress((double) progress.getChunk() / pr.getProvenience().getFile().getSize()); }); }); }); //sync-done events appEvents.subscribe(ClassMatcher.newMatcher().on(SyncBeginEvent.class, event -> { log.info("sync begin received"); Platform.runLater(() -> { gProgress.startProgress("Synchronizing providers"); addFile.setDisable(true); addProvider.setDisable(true); }); }).on(SynchDoneEvent.class, event -> { log.debug("sync done"); refreshItems(filesRoot); Platform.runLater(() -> { gProgress.stopProgress(); addFile.setDisable(false); addProvider.setDisable(false); }); })); }
From source file:ubicrypt.ui.StackNavigator.java
public <R> Parent loadFrom(final Optional<R> data) { log.debug("fxml:{}", levels.peek()); final FXMLLoader loader = new FXMLLoader( StackNavigator.class.getResource(format("/fxml/%s.fxml", levels.peek())), bundle); loader.setControllerFactory(controllerFactory); try {// w w w . j a v a 2s.c o m Parent parent; if (root != null) { root.getChildren().setAll((Node) loader.load()); parent = (Parent) root.getChildren().get(0); } else { parent = loader.load(); } Object controller = loader.getController(); stream(getAllFields(controller.getClass())).filter(field -> field.getType() == StackNavigator.class) .forEach(field -> { try { writeField(field, controller, this, true); } catch (IllegalAccessException e) { log.error("error setting field:{} in:{}", field, controller); Throwables.propagate(e); } log.debug("{} inject stack navigator", controller.getClass().getSimpleName()); }); if (Consumer.class.isAssignableFrom(controller.getClass())) { data.ifPresent(((Consumer<R>) controller)::accept); } return parent; } catch (final IOException e) { Throwables.propagate(e); } return null; }